springboot接入springai-openAi代理和智谱ai调用示例
Spring Boot 接入 SpringAI 和 智谱 AI 的代理调用示例需要使用对应的 SDK 或 API 客户端。以下是一个简化的示例,展示如何在 Spring Boot 应用中集成调用代理:
- 添加依赖(以 Maven 为例):
<!-- SpringAI OpenAI 代理 -->
<dependency>
<groupId>com.springai</groupId>
<artifactId>springai-openai-proxy-client</artifactId>
<version>最新版本</version>
</dependency>
<!-- 智谱 AI 依赖 -->
<dependency>
<groupId>com.zhihui</groupId>
<artifactId>zhihuai-ai-client</artifactId>
<version>最新版本</version>
</dependency>
- 配置 Bean:
@Configuration
public class AiConfiguration {
@Bean
public SpringAiOpenAiProxyClient springAiOpenAiProxyClient() {
return new SpringAiOpenAiProxyClient("http://openai.proxy.url", "api-key");
}
@Bean
public ZhiHuiAiClient zhiHuiAiClient() {
return new ZhiHuiAiClient("http://zhihuiai.service.url", "api-key");
}
}
- 使用代理客户端:
@Service
public class AiService {
private final SpringAiOpenAiProxyClient springAiOpenAiProxyClient;
private final ZhiHuiAiClient zhiHuiAiClient;
public AiService(SpringAiOpenAiProxyClient springAiOpenAiProxyClient, ZhiHuiAiClient zhiHuiAiClient) {
this.springAiOpenAiProxyClient = springAiOpenAiProxyClient;
this.zhiHuiAiClient = zhiHuiAiClient;
}
public String callSpringAiOpenAi(String message) {
return springAiOpenAiProxyClient.sendMessage(message);
}
public String callZhiHuiAi(String message) {
return zhiHuiAiClient.sendMessage(message);
}
}
- 在 Controller 中使用 AI 服务:
@RestController
public class AiController {
private final AiService aiService;
public AiController(AiService aiService) {
this.aiService = aiService;
}
@GetMapping("/springai/message")
public String sendMessageToSpringAi(@RequestParam String message) {
return aiService.callSpringAiOpenAi(message);
}
@GetMapping("/zhihui/message")
public String sendMessageToZhiHuiAi(@RequestParam String message) {
return aiService.callZhiHuiAi(message);
}
}
以上代码提供了一个简化的示例,展示了如何在 Spring Boot 应用中集成和使用两个 AI 服务的代理。在实际应用中,你需要替换相应的依赖版本、服务 URL 和 API 密钥,并且实现具体的业务逻辑。
评论已关闭