以下是使用Spring AI请求ZhiPu AI的相关代码示例:
1. 自动配置方式(使用Spring Boot Starter)
- 添加依赖
- 在Maven的
pom.xml
文件中添加:
<dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-zhipuai-spring-boot-starter</artifactId> </dependency>
- 在Gradle的
build.gradle
文件中添加:
dependencies { implementation 'org.springframework.ai:spring-ai-zhipuai-spring-boot-starter' }
- 在Maven的
- 配置属性
- 在
application.properties
文件中配置(示例):
spring.ai.zhipuai.api-key=YOUR_API_KEY spring.ai.zhipuai.chat.options.model=glm-4-air spring.ai.zhipuai.chat.options.temperature=0.7
- 在
- 使用示例(在控制器中)
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import java.util.Map; @RestController public class ChatController { private final ZhiPuAiChatModel chatModel; @Autowired public ChatController(ZhiPuAiChatModel chatModel) { this.chatModel = chatModel; } @GetMapping("/ai/generate") public Map generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) { return Map.of("generation", chatModel.call(message)); } @GetMapping("/ai/generateStream") public Flux<ChatResponse> generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) { var prompt = new Prompt(new UserMessage(message)); return chatModel.stream(prompt); } }
2. 手动配置方式
- 添加依赖
- 在Maven的
pom.xml
文件中添加:
<dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-zhipuai</artifactId> </dependency>
- 在Gradle的
build.gradle
文件中添加:
dependencies { implementation 'org.springframework.ai:spring-ai-zhipuai' }
- 在Maven的
- 创建和使用
ZhiPuAiChatModel
import org.springframework.ai.zhipuai.ZhiPuAiApi; import org.springframework.ai.zhipuai.ZhiPuAiChatModel; import org.springframework.ai.zhipuai.ZhiPuAiChatOptions; import org.springframework.ai.zhipuai.chat.Prompt; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import java.util.List; import java.util.Map; public class ManualConfigurationExample { public static void main(String[] args) { var zhiPuAiApi = new ZhiPuAiApi(System.getenv("ZHIPU_AI_API_KEY")); var chatModel = new ZhiPuAiChatModel(zhiPuAiApi, ZhiPuAiChatOptions.builder() .withModel(ZhiPuAiApi.ChatModel.GLM_3_Turbo.getValue()) .withTemperature(0.4) .withMaxTokens(200) .build()); ChatResponse response = chatModel.call( new Prompt("Generate the names of 5 famous pirates.")); // Or with streaming responses Flux<ChatResponse> streamResponse = chatModel.stream( new Prompt("Generate the names of 5 famous pirates.")); } }
3. 直接使用ZhiPuAiApi
客户端
import org.springframework.ai.zhipuai.ZhiPuAiApi;
import org.springframework.http.ResponseEntity;
import reactor.core.publisher.Flux;
import java.util.List;
public class ZhiPuAiApiExample {
public static void main(String[] args) {
ZhiPuAiApi zhiPuAiApi = new ZhiPuAiApi(System.getenv("ZHIPU_AI_API_KEY"));
ChatCompletionMessage chatCompletionMessage = new ChatCompletionMessage("Hello world", Role.USER);
// Sync request
ResponseEntity<ChatCompletion> response = zhiPuAiApi.chatCompletionEntity(
new ChatCompletionRequest(List.of(chatCompletionMessage), ZhiPuAiApi.ChatModel.GLM_3_Turbo.getValue(), 0.7, false));
// Streaming request
Flux<ChatCompletionChunk> streamResponse = zhiPuAiApi.chatCompletionStream(
new ChatCompletionRequest(List.of(chatCompletionMessage), ZhiPuAiApi.ChatModel.GLM_3_Turbo.getValue(), 0.7, true));
}
}