# openai-java-sdk
**Repository Path**: alfy-software/openai-java-sdk
## Basic Information
- **Project Name**: openai-java-sdk
- **Description**: 基于OpenAI 的 rest api 开发的Java 库, 目前支持 GPT-3、ChatGPT 和 GPT-4。
- **Primary Language**: Java
- **License**: MIT
- **Default Branch**: master
- **Homepage**: https://ai-connect.cn/chat
- **GVP Project**: No
## Statistics
- **Stars**: 3
- **Forks**: 0
- **Created**: 2023-06-15
- **Last Updated**: 2024-04-25
## Categories & Tags
**Categories**: Uncategorized
**Tags**: chatGPT, openAI, AI, gpt
## README
> ⚠️OpenAI 已弃用所有基于Engine的 API.
# OpenAI-Java-Sdk
基于OpenAI 的 rest api 开发的Java 库, 目前支持 GPT-3(支持函数回调及16K上下文)、ChatGPT 和 GPT-4。
该库包含以下组件:
- `api` : GPT API 的请求/响应 POJO。
- `client` : 采用retrofit库的api模块客户端
- `service` : 创建和调用客户端的基本服务类。
以及使用该服务的示例项目。
## 当前支持API
- [Models](https://platform.openai.com/docs/api-reference/models)
- [Completions](https://platform.openai.com/docs/api-reference/completions)
- [Chat Completions](https://platform.openai.com/docs/api-reference/chat/create)
- [Edits](https://platform.openai.com/docs/api-reference/edits)
- [Embeddings](https://platform.openai.com/docs/api-reference/embeddings)
- [Files](https://platform.openai.com/docs/api-reference/files)
- [Fine-tunes](https://platform.openai.com/docs/api-reference/fine-tunes)
- [Images](https://platform.openai.com/docs/api-reference/images)
- [Moderations](https://platform.openai.com/docs/api-reference/moderations)
#### 已被 OpenAI 弃用
- [Engines](https://platform.openai.com/docs/api-reference/engines)
## Maven导入
```xml
cn.ai-connect
openai-java-sdk
1.0.0
```
## 使用
### 流式返回(打字机效果)
```java
final List messagesByStream = new ArrayList<>();
final ChatMessage systemMessage = new ChatMessage(ChatMessageRole.SYSTEM.value(), "你是一个助手.");
messagesByStream.add(systemMessage);
ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest
.builder()
.model("gpt-3.5-turbo-16k")
.messages(messagesByStream)
.n(1)
.maxTokens(50)
.logitBias(new HashMap<>())
.build();
service.streamChatCompletion(chatCompletionRequest)
.doOnError(Throwable::printStackTrace)
.blockingForEach(System.out::println);
service.shutdownExecutor();
```
### 回调方法function_call使用
```java
final List messagesByCallback = new ArrayList<>();
final ChatMessage userMessage = new ChatMessage(ChatMessageRole.USER.value(), "What is the weather like in Boston?");
messagesByCallback.add(userMessage);
final List functions = new LinkedList<>();
String parmJson = "{\"type\":\"object\",\"properties\":{\"location\":{\"type\":\"string\",\"description\":\"The city and state, e.g. San Francisco, CA\"},\"unit\":{\"type\":\"string\",\"enum\":[\"celsius\",\"fahrenheit\"]}},\"required\":[\"location\"]}";
final CallbackFunction callbackFunction = new CallbackFunction("get_current_weather", "Get the current weather in a given location", parmJson);
functions.add(callbackFunction);
ChatCompletionRequest chatCompletionRequestByCallback = ChatCompletionRequest
.builder()
.model("gpt-3.5-turbo-16k")
.functions(functions)
.messages(messagesByCallback)
.n(1)
.maxTokens(50)
.logitBias(new HashMap<>())
.build();
service.createChatCompletion(chatCompletionRequestByCallback).getChoices().forEach(System.out::println);
```
### 添加代理
要使用代理,请修改 OkHttp 客户端,如下所示:
```java
ObjectMapper mapper = defaultObjectMapper();
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port));
OkHttpClient client = defaultClient(token, timeout)
.newBuilder()
.proxy(proxy)
.build();
Retrofit retrofit = defaultRetrofit(client, mapper);
OpenAiApi api = retrofit.create(OpenAiApi.class);
OpenAiService service = new OpenAiService(api);
```
### 流式线程关闭
如果您想在流式传输响应后立即关闭进程, 请调用 `OpenAiService.shutdown()`.
对于非流式调用,则不用。
### 采用该SDK的成功案例
爱联Ai Connect 完全免费,支持联网搜索
### 添加作者微信获得帮助
