spring-ai
https://github.com/chatanywhere/GPT_API_free
文章目录
- pom.xml
- OpenAiApp
- AiController
- application.yml
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>spring-boot-dependencies</artifactId>
<groupId>org.springframework.boot</groupId>
<version>3.2.4</version>
</parent>
<groupId>org.example</groupId>
<artifactId>demo-openai</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
<version>1.0.0-M6</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.10</version>
</dependency>
</dependencies>
</project>
OpenAiApp
@SpringBootApplication
public class OpenAiApp {
public static void main(String[] args) {
SpringApplication.run(OpenAiApp.class, args);
}
}
AiController
package com.zzhua.controller;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
/*
转发Host1: https://api.chatanywhere.tech (国内中转)
转发Host2: https://api.chatanywhere.org (国外使用)
*/
@Controller
public class AiController {
@Autowired
private ChatClient.Builder builder;
@RequestMapping("test01")
@ResponseBody
public String test01(@RequestParam("prompt") String prompt) {
ChatClient chatClient = builder.build();
String response = chatClient.prompt(prompt).call().content();
return response;
}
@RequestMapping(value = "test02", produces = "text/event-stream")
@ResponseBody
public Flux<String> test02(@RequestParam("prompt") String prompt, HttpServletResponse response) {
response.setCharacterEncoding("utf-8");
ChatClient chatClient = builder.build();
Flux<String> flux = chatClient.prompt(prompt).stream().content();
// flux.subscribe(System.out::println);
return flux;
}
}
application.yml
spring:
ai:
openai:
# base-url: https://api.chatanywhere.tech
# base-url: https://api.chatanywhere.org
base-url: https://api.deepseek.com
api-key: sk-xxx
chat:
options:
# model: gpt-3.5-turbo
model: deepseek-reasoner
# model: deepseek-chat