幂等性
commit
3940f84ad9
|
@ -0,0 +1,38 @@
|
||||||
|
target/
|
||||||
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
|
!**/src/main/**/target/
|
||||||
|
!**/src/test/**/target/
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea/modules.xml
|
||||||
|
.idea/jarRepositories.xml
|
||||||
|
.idea/compiler.xml
|
||||||
|
.idea/libraries/
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
### Eclipse ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
build/
|
||||||
|
!**/src/main/**/build/
|
||||||
|
!**/src/test/**/build/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
||||||
|
|
||||||
|
### Mac OS ###
|
||||||
|
.DS_Store
|
|
@ -0,0 +1,8 @@
|
||||||
|
# 默认忽略的文件
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# 基于编辑器的 HTTP 客户端请求
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
|
@ -0,0 +1,7 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Encoding">
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,14 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||||
|
<component name="MavenProjectsManager">
|
||||||
|
<option name="originalFiles">
|
||||||
|
<list>
|
||||||
|
<option value="$PROJECT_DIR$/pom.xml" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,112 @@
|
||||||
|
<?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>
|
||||||
|
|
||||||
|
<groupId>org.example</groupId>
|
||||||
|
<artifactId>dome2</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>17</maven.compiler.source>
|
||||||
|
<maven.compiler.target>17</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>2.7.15</version>
|
||||||
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
|
</parent>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-api</artifactId>
|
||||||
|
<version>5.10.0-M1</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
<version>2.7.8</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.hutool</groupId>
|
||||||
|
<artifactId>hutool-all</artifactId>
|
||||||
|
<version>5.8.18</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
<version>2.7.8</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>fastjson</artifactId>
|
||||||
|
<version>1.2.67</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.java-websocket</groupId>
|
||||||
|
<artifactId>Java-WebSocket</artifactId>
|
||||||
|
<version>1.3.8</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.squareup.okhttp3</groupId>
|
||||||
|
<artifactId>okhttp</artifactId>
|
||||||
|
<version>4.9.3</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>1.18.24</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-log4j12</artifactId>
|
||||||
|
<version>1.7.30</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
|
<version>5.1.32</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mybatis.spring.boot</groupId>
|
||||||
|
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||||
|
<version>2.3.1</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-aop</artifactId>
|
||||||
|
<version>2.7.2</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.json</groupId>
|
||||||
|
<artifactId>json</artifactId>
|
||||||
|
<version>20210307</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>commons-codec</groupId>
|
||||||
|
<artifactId>commons-codec</artifactId>
|
||||||
|
<version>1.15</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.amazonaws</groupId>
|
||||||
|
<artifactId>aws-java-sdk-s3</artifactId>
|
||||||
|
<version>1.12.68</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.amazonaws</groupId>
|
||||||
|
<artifactId>aws-java-sdk-core</artifactId>
|
||||||
|
<version>1.12.68</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,75 @@
|
||||||
|
package com.durant;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author DongZl
|
||||||
|
* @description: 响应信息主体
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class Result<T> implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
/**
|
||||||
|
* 成功
|
||||||
|
*/
|
||||||
|
public static final int SUCCESS = 200;
|
||||||
|
/**
|
||||||
|
* 失败
|
||||||
|
*/
|
||||||
|
public static final int FAIL = 500;
|
||||||
|
/**
|
||||||
|
* 返回状态码
|
||||||
|
*/
|
||||||
|
private int code;
|
||||||
|
/**
|
||||||
|
* 响应信息
|
||||||
|
*/
|
||||||
|
private String msg;
|
||||||
|
/**
|
||||||
|
* 响应数据
|
||||||
|
*/
|
||||||
|
private T data;
|
||||||
|
|
||||||
|
public static <T> Result<T> success() {
|
||||||
|
return restResult(null, SUCCESS, "操作成功");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> Result<T> success(T data) {
|
||||||
|
return restResult(data, SUCCESS, "操作成功");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> Result<T> success(T data, String msg) {
|
||||||
|
return restResult(data, SUCCESS, msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> Result<T> error() {
|
||||||
|
return restResult(null, FAIL, "操作失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> Result<T> error(String msg) {
|
||||||
|
return restResult(null, FAIL, msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> Result<T> error(T data) {
|
||||||
|
return restResult(data, FAIL, "操作失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> Result<T> error(T data, String msg) {
|
||||||
|
return restResult(data, FAIL, msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> Result<T> error(int code, String msg) {
|
||||||
|
return restResult(null, code, msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static <T> Result<T> restResult(T data, int code, String msg) {
|
||||||
|
Result<T> apiResult = new Result<>();
|
||||||
|
apiResult.setCode(code);
|
||||||
|
apiResult.setData(data);
|
||||||
|
apiResult.setMsg(msg);
|
||||||
|
return apiResult;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
package com.durant;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class XfXhApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(XfXhApplication.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
package com.durant.annotion;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 冯凯
|
||||||
|
* @version 1.0
|
||||||
|
* @description: TODO
|
||||||
|
* @date 2023/10/3 22:02
|
||||||
|
*/
|
||||||
|
@Target(ElementType.METHOD)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
public @interface Independent {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,39 @@
|
||||||
|
package com.durant.aspect;
|
||||||
|
|
||||||
|
import com.durant.annotion.Independent;
|
||||||
|
import org.aspectj.lang.ProceedingJoinPoint;
|
||||||
|
import org.aspectj.lang.annotation.Around;
|
||||||
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
@Aspect
|
||||||
|
@Component
|
||||||
|
public class IndependentAspect {
|
||||||
|
private Object result=null;
|
||||||
|
private Map<String,Object> resultMap=new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
@Around("@annotation(independent)")
|
||||||
|
public Object independent(ProceedingJoinPoint joinPoint, Independent independent) throws Throwable {
|
||||||
|
|
||||||
|
String id = generateId(joinPoint.getArgs());
|
||||||
|
if (resultMap.containsKey(id)){
|
||||||
|
System.out.println("不要重复提交请求");
|
||||||
|
return resultMap.get(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
result= joinPoint.proceed();
|
||||||
|
resultMap.put(id,result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String generateId(Object[] args){
|
||||||
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
|
for (Object arg : args) {
|
||||||
|
stringBuilder.append(arg.hashCode());
|
||||||
|
}
|
||||||
|
return stringBuilder.toString();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,150 @@
|
||||||
|
package com.durant.component;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.durant.config.XfXhConfig;
|
||||||
|
import com.durant.dto.MsgDTO;
|
||||||
|
import com.durant.dto.request.RequestDTO;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import okhttp3.*;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import javax.crypto.Mac;
|
||||||
|
import javax.crypto.spec.SecretKeySpec;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
public class XfXhStreamClient {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 狐狸半面添
|
||||||
|
* @create 2023-09-15 1:10
|
||||||
|
*/
|
||||||
|
@Resource
|
||||||
|
private XfXhConfig xfXhConfig;
|
||||||
|
|
||||||
|
@Value("${xfxh.QPS}")
|
||||||
|
private int connectionTokenCount;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取令牌
|
||||||
|
*/
|
||||||
|
public static int GET_TOKEN_STATUS = 0;
|
||||||
|
/**
|
||||||
|
* 归还令牌
|
||||||
|
*/
|
||||||
|
public static int BACK_TOKEN_STATUS = 1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 操作令牌
|
||||||
|
*
|
||||||
|
* @param status 0-获取令牌 1-归还令牌
|
||||||
|
* @return 是否操作成功
|
||||||
|
*/
|
||||||
|
public synchronized boolean operateToken(int status) {
|
||||||
|
if (status == GET_TOKEN_STATUS) {
|
||||||
|
// 获取令牌
|
||||||
|
if (connectionTokenCount != 0) {
|
||||||
|
// 说明还有令牌,将令牌数减一
|
||||||
|
connectionTokenCount -= 1;
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 放回令牌
|
||||||
|
connectionTokenCount += 1;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送消息
|
||||||
|
*
|
||||||
|
* @param uid 每个用户的id,用于区分不同用户
|
||||||
|
* @param msgList 发送给大模型的消息,可以包含上下文内容
|
||||||
|
* @return 获取websocket连接,以便于我们在获取完整大模型回复后手动关闭连接
|
||||||
|
*/
|
||||||
|
public WebSocket sendMsg(String uid, List<MsgDTO> msgList, WebSocketListener listener) {
|
||||||
|
// 获取鉴权url
|
||||||
|
String authUrl = this.getAuthUrl();
|
||||||
|
// 鉴权方法生成失败,直接返回 null
|
||||||
|
if (authUrl == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
|
||||||
|
// 将 https/http 连接替换为 ws/wss 连接
|
||||||
|
String url = authUrl.replace("http://", "ws://").replace("https://", "wss://");
|
||||||
|
Request request = new Request.Builder().url(url).build();
|
||||||
|
// 建立 wss 连接
|
||||||
|
WebSocket webSocket = okHttpClient.newWebSocket(request, listener);
|
||||||
|
// 组装请求参数
|
||||||
|
RequestDTO requestDTO = getRequestParam(uid, msgList);
|
||||||
|
// 发送请求
|
||||||
|
webSocket.send(JSONObject.toJSONString(requestDTO));
|
||||||
|
return webSocket;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成鉴权方法,具体实现不用关心,这是讯飞官方定义的鉴权方式
|
||||||
|
*
|
||||||
|
* @return 鉴权访问大模型的路径
|
||||||
|
*/
|
||||||
|
public String getAuthUrl() {
|
||||||
|
try {
|
||||||
|
URL url = new URL(xfXhConfig.getHostUrl());
|
||||||
|
// 时间
|
||||||
|
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
|
||||||
|
format.setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||||
|
String date = format.format(new Date());
|
||||||
|
// 拼接
|
||||||
|
String preStr = "host: " + url.getHost() + "\n" +
|
||||||
|
"date: " + date + "\n" +
|
||||||
|
"GET " + url.getPath() + " HTTP/1.1";
|
||||||
|
// SHA256加密
|
||||||
|
Mac mac = Mac.getInstance("hmacsha256");
|
||||||
|
SecretKeySpec spec = new SecretKeySpec(xfXhConfig.getApiSecret().getBytes(StandardCharsets.UTF_8), "hmacsha256");
|
||||||
|
mac.init(spec);
|
||||||
|
|
||||||
|
byte[] hexDigits = mac.doFinal(preStr.getBytes(StandardCharsets.UTF_8));
|
||||||
|
// Base64加密
|
||||||
|
String sha = Base64.getEncoder().encodeToString(hexDigits);
|
||||||
|
// 拼接
|
||||||
|
String authorizationOrigin = String.format("api_key=\"%s\", algorithm=\"%s\", headers=\"%s\", signature=\"%s\"", xfXhConfig.getApiKey(), "hmac-sha256", "host date request-line", sha);
|
||||||
|
// 拼接地址
|
||||||
|
HttpUrl httpUrl = Objects.requireNonNull(HttpUrl.parse("https://" + url.getHost() + url.getPath())).newBuilder().
|
||||||
|
addQueryParameter("authorization", Base64.getEncoder().encodeToString(authorizationOrigin.getBytes(StandardCharsets.UTF_8))).
|
||||||
|
addQueryParameter("date", date).
|
||||||
|
addQueryParameter("host", url.getHost()).
|
||||||
|
build();
|
||||||
|
|
||||||
|
return httpUrl.toString();
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("鉴权方法中发生错误:" + e.getMessage());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取请求参数
|
||||||
|
*
|
||||||
|
* @param uid 每个用户的id,用于区分不同用户
|
||||||
|
* @param msgList 发送给大模型的消息,可以包含上下文内容
|
||||||
|
* @return 请求DTO,该 DTO 转 json 字符串后生成的格式参考 resources/demo-json/request.json
|
||||||
|
*/
|
||||||
|
public RequestDTO getRequestParam(String uid, List<MsgDTO> msgList) {
|
||||||
|
RequestDTO requestDTO = new RequestDTO();
|
||||||
|
requestDTO.setHeader(new RequestDTO.HeaderDTO(xfXhConfig.getAppId(), uid));
|
||||||
|
requestDTO.setParameter(new RequestDTO.ParameterDTO(new RequestDTO.ParameterDTO.ChatDTO(xfXhConfig.getDomain(), xfXhConfig.getTemperature(), xfXhConfig.getMaxTokens())));
|
||||||
|
requestDTO.setPayload(new RequestDTO.PayloadDTO(new RequestDTO.PayloadDTO.MessageDTO(msgList)));
|
||||||
|
return requestDTO;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
package com.durant.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class CorsConfig implements WebMvcConfigurer {
|
||||||
|
|
||||||
|
public void addCorsMappings(CorsRegistry registry) {
|
||||||
|
registry.addMapping("/test/sendQuestion")
|
||||||
|
.allowedOrigins("http://localhost:9528")
|
||||||
|
.allowedMethods("GET", "POST")
|
||||||
|
.allowCredentials(true)
|
||||||
|
.maxAge(3600);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,48 @@
|
||||||
|
package com.durant.config;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@ConfigurationProperties(prefix = "xfxh")
|
||||||
|
@Data
|
||||||
|
public class XfXhConfig {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 服务引擎使用 讯飞星火认知大模型V2.0,如果使用 V1.5 需要将 hostUrl 修改为 https://spark-api.xf-yun.com/v1.1/chat
|
||||||
|
*/
|
||||||
|
private String hostUrl;
|
||||||
|
/**
|
||||||
|
* 发送请求时指定的访问领域,如果是 V1.5版本 设置为 general,如果是 V2版本 设置为 generalv2
|
||||||
|
*/
|
||||||
|
private String domain;
|
||||||
|
/**
|
||||||
|
* 核采样阈值。用于决定结果随机性,取值越高随机性越强即相同的问题得到的不同答案的可能性越高。取值 [0,1]
|
||||||
|
*/
|
||||||
|
private Float temperature;
|
||||||
|
/**
|
||||||
|
* 模型回答的tokens的最大长度,V1.5取值为[1,4096],V2.0取值为[1,8192]。
|
||||||
|
*/
|
||||||
|
private Integer maxTokens;
|
||||||
|
/**
|
||||||
|
* 大模型回复问题的最大响应时长,单位 s
|
||||||
|
*/
|
||||||
|
private Integer maxResponseTime;
|
||||||
|
/**
|
||||||
|
* 用于权限验证,从服务接口认证信息中获取
|
||||||
|
*/
|
||||||
|
private String appId;
|
||||||
|
/**
|
||||||
|
* 用于权限验证,从服务接口认证信息中获取
|
||||||
|
*/
|
||||||
|
private String apiKey;
|
||||||
|
/**
|
||||||
|
* 用于权限验证,从服务接口认证信息中获取
|
||||||
|
*/
|
||||||
|
private String apiSecret;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,37 @@
|
||||||
|
package com.durant.controller;
|
||||||
|
|
||||||
|
import com.durant.Result;
|
||||||
|
import com.durant.annotion.Independent;
|
||||||
|
import com.durant.dto.request.LoginReq;
|
||||||
|
import com.durant.service.LoginService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@CrossOrigin(origins = "http://localhost:9528")
|
||||||
|
public class LoginController {
|
||||||
|
@Autowired
|
||||||
|
private LoginService loginService;
|
||||||
|
|
||||||
|
@Independent
|
||||||
|
@PostMapping("/user/login")
|
||||||
|
|
||||||
|
public Result<LoginReq> userLogin(@RequestBody LoginReq loginReq){
|
||||||
|
Result<LoginReq> loginReqResult = loginService.userLogin(loginReq);
|
||||||
|
if (loginReqResult.getData()!=null){
|
||||||
|
return Result.success(loginReqResult.getData());
|
||||||
|
}
|
||||||
|
|
||||||
|
return Result.error();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/user/info")
|
||||||
|
|
||||||
|
public Result<LoginReq> userInfo(){
|
||||||
|
LoginReq loginReq = new LoginReq();
|
||||||
|
loginReq.setUsername("fengkai");
|
||||||
|
loginReq.setPassword("123456");
|
||||||
|
return Result.success(loginReq);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,90 @@
|
||||||
|
package com.durant.controller;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.durant.component.XfXhStreamClient;
|
||||||
|
import com.durant.config.XfXhConfig;
|
||||||
|
import com.durant.dto.MsgDTO;
|
||||||
|
import com.durant.listener.XfXhWebSocketListener;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import okhttp3.WebSocket;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/test")
|
||||||
|
@Slf4j
|
||||||
|
public class TestController {
|
||||||
|
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private XfXhStreamClient xfXhStreamClient;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private XfXhConfig xfXhConfig;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送问题
|
||||||
|
*
|
||||||
|
* @param question 问题
|
||||||
|
* @return 星火大模型的回答
|
||||||
|
*/
|
||||||
|
@GetMapping("/sendQuestion")
|
||||||
|
public String sendQuestion(@RequestParam("question") String question) {
|
||||||
|
// 如果是无效字符串,则不对大模型进行请求
|
||||||
|
if (StrUtil.isBlank(question)) {
|
||||||
|
return "无效问题,请重新输入";
|
||||||
|
}
|
||||||
|
// 获取连接令牌
|
||||||
|
if (!xfXhStreamClient.operateToken(XfXhStreamClient.GET_TOKEN_STATUS)) {
|
||||||
|
return "当前大模型连接数过多,请稍后再试";
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建消息对象
|
||||||
|
MsgDTO msgDTO = MsgDTO.createUserMsg(question);
|
||||||
|
// 创建监听器
|
||||||
|
XfXhWebSocketListener listener = new XfXhWebSocketListener();
|
||||||
|
// 发送问题给大模型,生成 websocket 连接
|
||||||
|
|
||||||
|
WebSocket webSocket = xfXhStreamClient.sendMsg(UUID.randomUUID().toString().substring(0, 10), Collections.singletonList(msgDTO), listener);
|
||||||
|
if (webSocket == null) {
|
||||||
|
// 归还令牌
|
||||||
|
xfXhStreamClient.operateToken(XfXhStreamClient.BACK_TOKEN_STATUS);
|
||||||
|
return "系统内部错误,请联系管理员";
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
int count = 0;
|
||||||
|
// 为了避免死循环,设置循环次数来定义超时时长
|
||||||
|
int maxCount = xfXhConfig.getMaxResponseTime() * 5;
|
||||||
|
while (count <= maxCount) {
|
||||||
|
Thread.sleep(200);
|
||||||
|
if (listener.isWsCloseFlag()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
if (count > maxCount) {
|
||||||
|
return "大模型响应超时,请联系管理员";
|
||||||
|
}
|
||||||
|
// 响应大模型的答案
|
||||||
|
System.out.println(listener.getAnswer().toString());
|
||||||
|
return listener.getAnswer().toString();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
log.error("错误:" + e.getMessage());
|
||||||
|
return "系统内部错误,请联系管理员";
|
||||||
|
} finally {
|
||||||
|
// 关闭 websocket 连接
|
||||||
|
webSocket.close(1000, "");
|
||||||
|
// 归还令牌
|
||||||
|
xfXhStreamClient.operateToken(XfXhStreamClient.BACK_TOKEN_STATUS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,37 @@
|
||||||
|
package com.durant.dto;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Builder
|
||||||
|
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||||
|
public class MsgDTO {
|
||||||
|
|
||||||
|
private String role;
|
||||||
|
/**
|
||||||
|
* 消息内容
|
||||||
|
*/
|
||||||
|
private String content;
|
||||||
|
/**
|
||||||
|
* 响应结果字段:结果序号,取值为[0,10]; 当前为保留字段,开发者可忽略
|
||||||
|
*/
|
||||||
|
private Integer index;
|
||||||
|
|
||||||
|
public static final String ROLE_USER = "user";
|
||||||
|
public static final String ROLE_ASSISTANT = "assistant";
|
||||||
|
|
||||||
|
public static MsgDTO createUserMsg(String content) {
|
||||||
|
return new MsgDTO(ROLE_USER, content, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static MsgDTO createAssistantMsg(String content) {
|
||||||
|
return new MsgDTO(ROLE_ASSISTANT, content, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.durant.dto.request;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class LoginReq {
|
||||||
|
|
||||||
|
private String username;
|
||||||
|
|
||||||
|
private String password;
|
||||||
|
}
|
|
@ -0,0 +1,90 @@
|
||||||
|
package com.durant.dto.request;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.annotation.JSONField;
|
||||||
|
import com.durant.dto.MsgDTO;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 请求参数
|
||||||
|
* 对应生成的 JSON 结构参考 resources/demo-json/request.json
|
||||||
|
*
|
||||||
|
* @author 狐狸半面添
|
||||||
|
* @create 2023-09-15 0:42
|
||||||
|
*/
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
public class RequestDTO {
|
||||||
|
|
||||||
|
@JsonProperty("header")
|
||||||
|
private HeaderDTO header;
|
||||||
|
@JsonProperty("parameter")
|
||||||
|
private ParameterDTO parameter;
|
||||||
|
@JsonProperty("payload")
|
||||||
|
private PayloadDTO payload;
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
public static class HeaderDTO {
|
||||||
|
/**
|
||||||
|
* 应用appid,从开放平台控制台创建的应用中获取
|
||||||
|
*/
|
||||||
|
@JSONField(name = "app_id")
|
||||||
|
private String appId;
|
||||||
|
/**
|
||||||
|
* 每个用户的id,用于区分不同用户,最大长度32
|
||||||
|
*/
|
||||||
|
@JSONField(name = "uid")
|
||||||
|
private String uid;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
public static class ParameterDTO {
|
||||||
|
private ChatDTO chat;
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
public static class ChatDTO {
|
||||||
|
/**
|
||||||
|
* 指定访问的领域,general指向V1.5版本 generalv2指向V2版本。注意:不同的取值对应的url也不一样!
|
||||||
|
*/
|
||||||
|
@JsonProperty("domain")
|
||||||
|
private String domain;
|
||||||
|
/**
|
||||||
|
* 核采样阈值。用于决定结果随机性,取值越高随机性越强即相同的问题得到的不同答案的可能性越高
|
||||||
|
*/
|
||||||
|
@JsonProperty("temperature")
|
||||||
|
private Float temperature;
|
||||||
|
/**
|
||||||
|
* 模型回答的tokens的最大长度
|
||||||
|
*/
|
||||||
|
@JSONField(name = "max_tokens")
|
||||||
|
private Integer maxTokens;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
public static class PayloadDTO {
|
||||||
|
@JsonProperty("message")
|
||||||
|
private MessageDTO message;
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
public static class MessageDTO {
|
||||||
|
@JsonProperty("text")
|
||||||
|
private List<MsgDTO> text;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,123 @@
|
||||||
|
package com.durant.dto.response;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 冯凯
|
||||||
|
* @version 1.0
|
||||||
|
* @description: TODO
|
||||||
|
* @date 2023/9/29 13:57
|
||||||
|
*/
|
||||||
|
|
||||||
|
import com.durant.dto.MsgDTO;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回参数
|
||||||
|
* 对应生成的 JSON 结构参考 resources/demo-json/response.json
|
||||||
|
*
|
||||||
|
* @author 狐狸半面添
|
||||||
|
* @create 2023-09-15 0:42
|
||||||
|
*/
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
public class ResponseDTO {
|
||||||
|
|
||||||
|
@JsonProperty("header")
|
||||||
|
private HeaderDTO header;
|
||||||
|
@JsonProperty("payload")
|
||||||
|
private PayloadDTO payload;
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
public static class HeaderDTO {
|
||||||
|
/**
|
||||||
|
* 错误码,0表示正常,非0表示出错
|
||||||
|
*/
|
||||||
|
@JsonProperty("code")
|
||||||
|
private Integer code;
|
||||||
|
/**
|
||||||
|
* 会话是否成功的描述信息
|
||||||
|
*/
|
||||||
|
@JsonProperty("message")
|
||||||
|
private String message;
|
||||||
|
/**
|
||||||
|
* 会话的唯一id,用于讯飞技术人员查询服务端会话日志使用,出现调用错误时建议留存该字段
|
||||||
|
*/
|
||||||
|
@JsonProperty("sid")
|
||||||
|
private String sid;
|
||||||
|
/**
|
||||||
|
* 会话状态,取值为[0,1,2];0代表首次结果;1代表中间结果;2代表最后一个结果
|
||||||
|
*/
|
||||||
|
@JsonProperty("status")
|
||||||
|
private Integer status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
public static class PayloadDTO {
|
||||||
|
@JsonProperty("choices")
|
||||||
|
private ChoicesDTO choices;
|
||||||
|
/**
|
||||||
|
* 在最后一次结果返回
|
||||||
|
*/
|
||||||
|
@JsonProperty("usage")
|
||||||
|
private UsageDTO usage;
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
public static class ChoicesDTO {
|
||||||
|
/**
|
||||||
|
* 文本响应状态,取值为[0,1,2]; 0代表首个文本结果;1代表中间文本结果;2代表最后一个文本结果
|
||||||
|
*/
|
||||||
|
@JsonProperty("status")
|
||||||
|
private Integer status;
|
||||||
|
/**
|
||||||
|
* 返回的数据序号,取值为[0,9999999]
|
||||||
|
*/
|
||||||
|
@JsonProperty("seq")
|
||||||
|
private Integer seq;
|
||||||
|
/**
|
||||||
|
* 响应文本
|
||||||
|
*/
|
||||||
|
@JsonProperty("text")
|
||||||
|
private List<MsgDTO> text;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
public static class UsageDTO {
|
||||||
|
@JsonProperty("text")
|
||||||
|
private TextDTO text;
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
public static class TextDTO {
|
||||||
|
/**
|
||||||
|
* 保留字段,可忽略
|
||||||
|
*/
|
||||||
|
@JsonProperty("question_tokens")
|
||||||
|
private Integer questionTokens;
|
||||||
|
/**
|
||||||
|
* 包含历史问题的总tokens大小
|
||||||
|
*/
|
||||||
|
@JsonProperty("prompt_tokens")
|
||||||
|
private Integer promptTokens;
|
||||||
|
/**
|
||||||
|
* 回答的tokens大小
|
||||||
|
*/
|
||||||
|
@JsonProperty("completion_tokens")
|
||||||
|
private Integer completionTokens;
|
||||||
|
/**
|
||||||
|
* prompt_tokens和completion_tokens的和,也是本次交互计费的tokens大小
|
||||||
|
*/
|
||||||
|
@JsonProperty("total_tokens")
|
||||||
|
private Integer totalTokens;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,67 @@
|
||||||
|
package com.durant.listener;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.durant.dto.MsgDTO;
|
||||||
|
import com.durant.dto.response.ResponseDTO;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import okhttp3.Response;
|
||||||
|
import okhttp3.WebSocket;
|
||||||
|
import okhttp3.WebSocketListener;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public class XfXhWebSocketListener extends WebSocketListener {
|
||||||
|
private StringBuilder answer = new StringBuilder();
|
||||||
|
|
||||||
|
private boolean wsCloseFlag = false;
|
||||||
|
|
||||||
|
public StringBuilder getAnswer() {
|
||||||
|
return answer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isWsCloseFlag() {
|
||||||
|
return wsCloseFlag;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onOpen(@NotNull WebSocket webSocket, @NotNull Response response) {
|
||||||
|
super.onOpen(webSocket, response);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMessage(@NotNull WebSocket webSocket, @NotNull String text) {
|
||||||
|
super.onMessage(webSocket, text);
|
||||||
|
// 将大模型回复的 JSON 文本转为 ResponseDTO 对象
|
||||||
|
ResponseDTO responseData = JSONObject.parseObject(text, ResponseDTO.class);
|
||||||
|
// 如果响应数据中的 header 的 code 值不为 0,则表示响应错误
|
||||||
|
if (responseData.getHeader().getCode() != 0) {
|
||||||
|
// 日志记录
|
||||||
|
log.error("发生错误,错误码为:" + responseData.getHeader().getCode() + "; " + "信息:" + responseData.getHeader().getMessage());
|
||||||
|
// 设置回答
|
||||||
|
this.answer = new StringBuilder("大模型响应错误,请稍后再试");
|
||||||
|
// 关闭连接标识
|
||||||
|
wsCloseFlag = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 将回答进行拼接
|
||||||
|
for (MsgDTO msgDTO : responseData.getPayload().getChoices().getText()) {
|
||||||
|
this.answer.append(msgDTO.getContent());
|
||||||
|
}
|
||||||
|
// 对最后一个文本结果进行处理
|
||||||
|
if (2 == responseData.getHeader().getStatus()) {
|
||||||
|
wsCloseFlag = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFailure(@NotNull WebSocket webSocket, @NotNull Throwable t, @Nullable Response response) {
|
||||||
|
super.onFailure(webSocket, t, response);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onClosed(@NotNull WebSocket webSocket, int code, @NotNull String reason) {
|
||||||
|
super.onClosed(webSocket, code, reason);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
package com.durant.mapper;
|
||||||
|
|
||||||
|
import com.durant.dto.request.LoginReq;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface LoginMapper {
|
||||||
|
LoginReq userLogin(LoginReq loginReq);
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
package com.durant.service;
|
||||||
|
|
||||||
|
import com.durant.Result;
|
||||||
|
import com.durant.dto.request.LoginReq;
|
||||||
|
|
||||||
|
public interface LoginService {
|
||||||
|
Result<LoginReq> userLogin(LoginReq loginReq);
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.durant.service;
|
||||||
|
|
||||||
|
import com.durant.Result;
|
||||||
|
import com.durant.dto.request.LoginReq;
|
||||||
|
import com.durant.mapper.LoginMapper;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class LoginServiceImpl implements LoginService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private LoginMapper loginMapper;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Result<LoginReq> userLogin(LoginReq loginReq) {
|
||||||
|
LoginReq loginReq1=loginMapper.userLogin(loginReq);
|
||||||
|
|
||||||
|
return Result.success(loginReq1);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue