From 5701fb6c43890586a49a515979633c9f1d717c77 Mon Sep 17 00:00:00 2001
From: lige2333 <609653319@qq.com>
Date: Sat, 23 Nov 2024 21:56:46 +0800
Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pom.xml | 213 +++++++++---------
.../cn/lige2333/ligeai/LigeaiApplication.java | 6 +-
.../cn/lige2333/ligeai/base/GptConfig.java | 45 +---
.../chatgpt/controller/ChatGptController.java | 39 +++-
.../chatgpt/service/ChatGptService.java | 192 +---------------
.../ligeai/chatgpt/service/GptFunctions.java | 79 +------
src/main/resources/application.properties | 3 +
.../ligeai/LigeaiApplicationTests.java | 6 +-
8 files changed, 158 insertions(+), 425 deletions(-)
diff --git a/pom.xml b/pom.xml
index b15e10c..b323b86 100644
--- a/pom.xml
+++ b/pom.xml
@@ -1,113 +1,112 @@
- 4.0.0
-
- org.springframework.boot
- spring-boot-starter-parent
- 3.3.5
-
-
- cn.lige2333
- ligeai
- 0.0.1-SNAPSHOT
- ligeai
- ligeai
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 17
- 1.0.0-M3
-
-
-
- org.springframework.boot
- spring-boot-starter-web
-
-
- org.springframework.boot
- spring-boot-starter-webflux
-
-
- org.mybatis.spring.boot
- mybatis-spring-boot-starter
- 3.0.3
-
-
- org.springframework.ai
- spring-ai-openai-spring-boot-starter
-
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
+ 4.0.0
+
+ org.springframework.boot
+ spring-boot-starter-parent
+ 3.3.5
+
+
+ cn.lige2333
+ ligeai
+ 0.0.1-SNAPSHOT
+ ligeai
+ ligeai
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 17
+ 1.0.0-M3
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+ org.springframework.boot
+ spring-boot-starter-webflux
+
+
+ com.alibaba
+ fastjson
+ 1.2.58
+
+
+ cn.hutool
+ hutool-all
+ 5.8.21
+
+
+ org.springframework.ai
+ spring-ai-openai-spring-boot-starter
+
-
- org.projectlombok
- lombok
- true
-
-
- org.springframework.boot
- spring-boot-starter-test
- test
-
-
- io.projectreactor
- reactor-test
- test
-
-
- org.mybatis.spring.boot
- mybatis-spring-boot-starter-test
- 3.0.3
- test
-
-
-
-
-
- org.springframework.ai
- spring-ai-bom
- ${spring-ai.version}
- pom
- import
-
-
-
+
+ org.projectlombok
+ lombok
+ true
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+ io.projectreactor
+ reactor-test
+ test
+
+
+
+
+
+ org.springframework.ai
+ spring-ai-bom
+ ${spring-ai.version}
+ pom
+ import
+
+
+
-
-
-
- org.springframework.boot
- spring-boot-maven-plugin
-
-
-
- org.projectlombok
- lombok
-
-
-
-
-
-
-
-
- spring-milestones
- Spring Milestones
- https://repo.spring.io/milestone
-
- false
-
-
-
+
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+
+
+
+ org.projectlombok
+ lombok
+
+
+
+
+
+
+
+
+ spring-milestones
+ Spring Milestones
+ https://repo.spring.io/milestone
+
+ false
+
+
+
diff --git a/src/main/java/cn/lige2333/ligeai/LigeaiApplication.java b/src/main/java/cn/lige2333/ligeai/LigeaiApplication.java
index 98726d0..de4f9cc 100644
--- a/src/main/java/cn/lige2333/ligeai/LigeaiApplication.java
+++ b/src/main/java/cn/lige2333/ligeai/LigeaiApplication.java
@@ -6,8 +6,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class LigeaiApplication {
- public static void main(String[] args) {
- SpringApplication.run(LigeaiApplication.class, args);
- }
+ public static void main(String[] args) {
+ SpringApplication.run(LigeaiApplication.class, args);
+ }
}
diff --git a/src/main/java/cn/lige2333/ligeai/base/GptConfig.java b/src/main/java/cn/lige2333/ligeai/base/GptConfig.java
index b17c2af..1953af0 100644
--- a/src/main/java/cn/lige2333/ligeai/base/GptConfig.java
+++ b/src/main/java/cn/lige2333/ligeai/base/GptConfig.java
@@ -1,61 +1,18 @@
-package cn.lige2333.qrdouble.common.config;
+package cn.lige2333.ligeai.base;
-import com.unfbx.chatgpt.OpenAiClient;
-import com.unfbx.chatgpt.OpenAiStreamClient;
-import com.unfbx.chatgpt.interceptor.OpenAiResponseInterceptor;
-import okhttp3.OkHttpClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.embedding.EmbeddingModel;
-import org.springframework.ai.reader.TextReader;
-import org.springframework.ai.transformer.splitter.TokenTextSplitter;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
-import org.springframework.core.io.Resource;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.concurrent.TimeUnit;
@Configuration
public class GptConfig {
- @Value("${chatgpt.token}")
- private String token;
-
- @Bean
- public OkHttpClient okHttpClient() {
- return new OkHttpClient
- .Builder()
- .connectTimeout(1000, TimeUnit.SECONDS)//自定义超时时间
- .writeTimeout(3000, TimeUnit.SECONDS)//自定义超时时间
- .readTimeout(3000, TimeUnit.SECONDS)//自定义超时时间
- .build();
- }
- @Bean
- public OpenAiClient openAiClient() {
-
- return OpenAiClient.builder()
- .apiKey(Arrays.asList(token))
- .apiHost("http://gptapi.kurtwang.com/")
- .okHttpClient(okHttpClient())
- .build();
- }
-
- @Bean
- public OpenAiStreamClient openAiStreamClient() {
- return OpenAiStreamClient.builder()
- .apiHost("http://gptapi.kurtwang.com/")
- .apiKey(Arrays.asList(token))
- .okHttpClient(okHttpClient())
- .build();
- }
-
@Bean
public ChatMemory chatMemory(){
return new InMemoryChatMemory();
diff --git a/src/main/java/cn/lige2333/ligeai/chatgpt/controller/ChatGptController.java b/src/main/java/cn/lige2333/ligeai/chatgpt/controller/ChatGptController.java
index 50adcb2..bf1e344 100644
--- a/src/main/java/cn/lige2333/ligeai/chatgpt/controller/ChatGptController.java
+++ b/src/main/java/cn/lige2333/ligeai/chatgpt/controller/ChatGptController.java
@@ -1,2 +1,39 @@
-package cn.lige2333.ligeai.chatgpt.controller;public class ChatGptController {
+package cn.lige2333.ligeai.chatgpt.controller;
+
+import org.springframework.ai.chat.client.ChatClient;
+import org.springframework.ai.openai.OpenAiChatModel;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.MediaType;
+import org.springframework.web.bind.annotation.*;
+import reactor.core.publisher.Flux;
+
+
+@RestController
+@RequestMapping("/ajax")
+@CrossOrigin
+public class ChatGptController {
+ @Autowired
+ private OpenAiChatModel chatModel;
+
+ private final ChatClient chatClient;
+
+ public ChatGptController(ChatClient.Builder chatClientBuilder) {
+ this.chatClient = chatClientBuilder.build();
+ }
+
+ @GetMapping("/ai/generate")
+ public String generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
+ return this.chatClient.prompt()
+ .user(message)
+ .call()
+ .content();
+ }
+
+ @GetMapping(value = "/ai/generateStream",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
+ public Flux generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
+ return this.chatClient.prompt()
+ .user(message)
+ .stream()
+ .content();
+ }
}
diff --git a/src/main/java/cn/lige2333/ligeai/chatgpt/service/ChatGptService.java b/src/main/java/cn/lige2333/ligeai/chatgpt/service/ChatGptService.java
index 652e5b8..d8aa51f 100644
--- a/src/main/java/cn/lige2333/ligeai/chatgpt/service/ChatGptService.java
+++ b/src/main/java/cn/lige2333/ligeai/chatgpt/service/ChatGptService.java
@@ -1,31 +1,11 @@
-package cn.lige2333.qrdouble.chatgpt.service;
+package cn.lige2333.ligeai.chatgpt.service;
-import cn.hutool.core.collection.CollUtil;
-import cn.hutool.core.util.StrUtil;
-import cn.hutool.json.JSONUtil;
-import cn.lige2333.qrdouble.security.entity.SysUser;
-import com.alibaba.fastjson.JSON;
-import com.alibaba.fastjson.JSONObject;
-import com.unfbx.chatgpt.OpenAiClient;
-import com.unfbx.chatgpt.OpenAiStreamClient;
-import com.unfbx.chatgpt.entity.chat.*;
-import com.unfbx.chatgpt.entity.common.Choice;
-import com.unfbx.chatgpt.entity.images.Image;
-import com.unfbx.chatgpt.entity.images.ImageResponse;
-import com.unfbx.chatgpt.entity.images.SizeEnum;
-import com.unfbx.chatgpt.sse.ConsoleEventSourceListener;
import lombok.extern.slf4j.Slf4j;
-import org.apache.logging.log4j.util.Strings;
-import org.jetbrains.annotations.NotNull;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.PromptChatMemoryAdvisor;
-import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.model.ChatResponse;
-import org.springframework.ai.openai.api.OpenAiApi;
-import org.springframework.ai.vectorstore.SearchRequest;
-import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.CrossOrigin;
@@ -41,12 +21,6 @@ import java.util.*;
@CrossOrigin
public class ChatGptService {
- @Autowired
- private OpenAiClient openAiClient;
-
- @Autowired
- private OpenAiStreamClient openAiStreamClient;
-
private final ChatClient chatClient;
public ChatGptService(ChatClient.Builder chatClientBuilder, ChatMemory chatMemory) {
@@ -74,10 +48,7 @@ public class ChatGptService {
.build();
}
- public Flux