2 Star 3 Fork 0

广州阿尔菲软件有限公司 / openai-java-sdk

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

⚠️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

已被 OpenAI 弃用

Maven导入

   <dependency>
    <groupId>cn.ai-connect</groupId>
    <artifactId>openai-java-sdk</artifactId>
    <version>1.0.0</version>       
   </dependency>

使用

流式返回(打字机效果)

        final List<ChatMessage> 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使用

        final List<ChatMessage> messagesByCallback = new ArrayList<>();
        final ChatMessage userMessage = new ChatMessage(ChatMessageRole.USER.value(), "What is the weather like in Boston?");
        messagesByCallback.add(userMessage);


        final List<CallbackFunction> 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 客户端,如下所示:

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 完全免费,支持联网搜索

添加作者微信获得帮助

MIT License Copyright (c) 2023 Alfy Software Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

基于OpenAI 的 rest api 开发的Java 库, 目前支持 GPT-3、ChatGPT 和 GPT-4。 展开 收起
Java
MIT
取消

发行版 (1)

全部

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/alfy-software/openai-java-sdk.git
git@gitee.com:alfy-software/openai-java-sdk.git
alfy-software
openai-java-sdk
openai-java-sdk
master

搜索帮助