前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >用 Spring AI 调用 OpenAI 对话接口

用 Spring AI 调用 OpenAI 对话接口

作者头像
AlphaHinex
发布2024-12-03 21:16:32
发布2024-12-03 21:16:32
26300
代码可运行
举报
文章被收录于专栏:周拱壹卒
运行总次数:0
代码可运行

环境准备

JDK

使用Spring AI[1] 需要JDK 17[2] 及以上版本。

代码语言:javascript
代码运行次数:0
复制
$ java -version
openjdk version "17.0.2" 2022-01-18
OpenJDK Runtime Environment (build 17.0.2+8-86)
OpenJDK 64-Bit Server VM (build 17.0.2+8-86, mixed mode, sharing)

start.spring.io

从 https://start.spring.io/ 下载一个包含 Spring Web 依赖的 Maven 工程:

解压,并使用其中自带的 Maven Wrapper 进行构建:

代码语言:javascript
代码运行次数:0
复制
$ unzip demo.zip
$ cd demo
$ ./mvnw clean package
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  5.569 s
[INFO] Finished at: 2024-11-30T13:58:27+08:00
[INFO] ------------------------------------------------------------------------

智谱 AI 开放平台

登录到智谱AI开放平台API Keys 页面[3] 获取最新版生成的用户 API Key,用于调用其提供的兼容 OpenAI 对话接口的免费模型GLM-4-Flash

代码语言:javascript
代码运行次数:0
复制
$ curl --location 'https://open.bigmodel.cn/api/paas/v4/chat/completions' \
--header 'Authorization: Bearer <你的apikey>' \
--header 'Content-Type: application/json' \
--data '{
    "model": "glm-4-flash",
    "messages": [
        {
            "role": "user",
            "content": "你好"
        }
    ]
}'
{"choices":[{"finish_reason":"stop","index":0,"message":{"content":"你好👋!很高兴见到你,有什么可以帮助你的吗?","role":"assistant"}}],"created":1732946586,"id":"202411301403051925a900b08f4e23","model":"glm-4-flash","request_id":"202411301403051925a900b08f4e23","usage":{"completion_tokens":16,"prompt_tokens":6,"total_tokens":22}}

添加依赖

pom.xml 中添加 Spring AI 的相关配置及依赖[4]

repositories

代码语言:javascript
代码运行次数:0
复制
<repositories>
  <repository>
    <id>spring-milestones</id>
    <name>Spring Milestones</name>
    <url>https://repo.spring.io/milestone</url>
    <snapshots>
      <enabled>false</enabled>
    </snapshots>
  </repository>
  <repository>
    <id>spring-snapshots</id>
    <name>Spring Snapshots</name>
    <url>https://repo.spring.io/snapshot</url>
    <releases>
      <enabled>false</enabled>
    </releases>
  </repository>
</repositories>

dependencyManagement

代码语言:javascript
代码运行次数:0
复制
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.ai</groupId>
      <artifactId>spring-ai-bom</artifactId>
      <version>1.0.0-SNAPSHOT</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

dependency

代码语言:javascript
代码运行次数:0
复制
<dependency>
  <groupId>org.springframework.ai</groupId>
  <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>

使用 ChatClient 与 OpenAI 兼容模型接口对话

仅对接一个大模型时

可直接通过配置项注册并使用 ChatClient。

application.properties 中添加 Spring AI OpenAI 的相关配置:

代码语言:javascript
代码运行次数:0
复制
spring.ai.openai.base-url=https://open.bigmodel.cn/api/paas
spring.ai.openai.chat.completions-path=/v4/chat/completions
spring.ai.openai.api-key=<你的apikey>
spring.ai.openai.chat.options.model=glm-4-flash

创建一个配置类:

代码语言:javascript
代码运行次数:0
复制
@Configuration
public class Config {

    @Bean
    ChatClient chatClient(ChatClient.Builder builder) {
        return builder.build();
    }

}

在工程中自带的DemoApplicationTests 单元测试中,验证对话效果:

代码语言:javascript
代码运行次数:0
复制
@Autowired
ChatClient chatClient;

@Test
void autoConfig() {
    String userMsg = "who r u";
    System.out.println(chatClient.prompt().user(userMsg).call().content());
}

执行结果如下:

代码语言:javascript
代码运行次数:0
复制
I am an AI assistant named ChatGLM, which is developed based on the language model jointly trained by Tsinghua University KEG Lab and Zhipu AI Company in 2024. My job is to provide appropriate answers and support to users' questions and requests.

需要对接多个大模型时

可定义一个工场类创建多个 ChatClient 实例:

代码语言:javascript
代码运行次数:0
复制
public class ChatClientFactory {

    public static ChatClient createOpenAiChatClient(String baseUrl, String apiKey, String model, String completionsPath) {
        if (StringUtils.isBlank(completionsPath)) {
            completionsPath = "/v1/chat/completions";
        }
        OpenAiApi openAiApi = new OpenAiApi(baseUrl, apiKey, completionsPath,
            "/v1/embeddings", RestClient.builder(), WebClient.builder(), RetryUtils.DEFAULT_RESPONSE_ERROR_HANDLER);
        OpenAiChatModel openAiChatModel = new OpenAiChatModel(openAiApi, OpenAiChatOptions.builder().withModel(model).build());
        return ChatClient.create(openAiChatModel);
    }

}
代码语言:javascript
代码运行次数:0
复制
@Test
void multiClients() {
    ChatClient llm1 = ChatClientFactory.createOpenAiChatClient("https://open.bigmodel.cn/api/paas", "xxxx", "glm-4-flash", "/v4/chat/completions");
    ChatClient llm2 = ChatClientFactory.createOpenAiChatClient("https://open.bigmodel.cn/api/paas", "xxxx", "glm-4-flash", "/v4/chat/completions");

    String userMsg = "你是谁?";
    System.out.println(llm1.prompt().user(userMsg).call().content());
    System.out.println(llm2.prompt().user(userMsg).call().content());
}

示例代码

demo.zip[5]

参考资料

[1]

Spring AI:https://github.com/spring-projects/spring-ai

[2]

JDK 17:https://github.com/spring-projects/spring-ai/blob/main/pom.xml#L171-L172

[3]

API Keys 页面:https://bigmodel.cn/usercenter/apikeys

[4]

相关配置及依赖:https://docs.spring.io/spring-ai/reference/getting-started.html

[5]

demo.zip:https://alphahinex.github.io/contents/spring-ai-chat-model/demo.zip

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-11-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 周拱壹卒 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 环境准备
    • JDK
    • start.spring.io
    • 智谱 AI 开放平台
  • 添加依赖
  • 使用 ChatClient 与 OpenAI 兼容模型接口对话
    • 仅对接一个大模型时
    • 需要对接多个大模型时
  • 示例代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档