Ver código fonte

新增:

1、新增对雅意大模型的支持。
修改:
  1、修改智谱以及百度大模型的部分数据类型。
dev_splitText_Gongzhenhuan
10710 1 ano atrás
pai
commit
08afe27eda
3 arquivos alterados com 154 adições e 22 exclusões
  1. +10
    -11
      xueyi-modules/xueyi-nlt/src/main/java/com/xueyi/nlt/nlt/service/impl/BaiduServiceImpl.java
  2. +134
    -0
      xueyi-modules/xueyi-nlt/src/main/java/com/xueyi/nlt/nlt/service/impl/YayiServiceImpl.java
  3. +10
    -11
      xueyi-modules/xueyi-nlt/src/main/java/com/xueyi/nlt/nlt/service/impl/ZhipuServiceImpl.java

+ 10
- 11
xueyi-modules/xueyi-nlt/src/main/java/com/xueyi/nlt/nlt/service/impl/BaiduServiceImpl.java Ver arquivo

@@ -1,8 +1,7 @@
package com.xueyi.nlt.nlt.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.shaded.com.google.gson.JsonArray;
import com.alibaba.nacos.shaded.com.google.gson.JsonObject;
import com.xueyi.nlt.nlt.domain.LlmContext;
import com.xueyi.nlt.nlt.domain.LlmParam;
import com.xueyi.nlt.nlt.domain.LlmResponse;
@@ -126,22 +125,22 @@ public class BaiduServiceImpl implements ISysLlmService {
private JSONObject sendMsg(List<String> questions){
String accessTokens = getAccessToken(apiKey, secretKey);
String url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/eb-instant?access_token="+ accessTokens;
JsonArray ja = new JsonArray();
JsonObject text;
JsonObject input = new JsonObject();
JSONArray ja = new JSONArray();
JSONObject text;
JSONObject input = new JSONObject();
for(int i = 0; i < questions.size(); i++)
{
text = new JsonObject();
text = new JSONObject();
if(i%2==0){
text.addProperty("role", "user");
text.put("role", "user");
}else{
text.addProperty("role", "assistant");
text.put("role", "assistant");
}
text.addProperty("content", questions.get(i));
text.put("content", questions.get(i));
ja.add(text);
}
input.add("messages", ja);
input.addProperty("stream", false);
input.put("messages", ja);
input.put("stream", false);
JSONObject result = doPOST(url, input.toString());
System.out.println(result);
return result;


+ 134
- 0
xueyi-modules/xueyi-nlt/src/main/java/com/xueyi/nlt/nlt/service/impl/YayiServiceImpl.java Ver arquivo

@@ -0,0 +1,134 @@
package com.xueyi.nlt.nlt.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xueyi.nlt.nlt.domain.LlmContext;
import com.xueyi.nlt.nlt.domain.LlmParam;
import com.xueyi.nlt.nlt.domain.LlmResponse;
import com.xueyi.nlt.nlt.service.ISysLlmService;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSON;

public class YayiServiceImpl implements ISysLlmService {
final static HostnameVerifier DO_NOT_VERIFY = (hostname, session) -> true;

private JSONObject sendMsg(List<String> questions){
try{
Calendar cal = Calendar.getInstance();
SimpleDateFormat greenwichDate = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss 'GMT'", Locale.US);
greenwichDate.setTimeZone(TimeZone.getTimeZone("GMT"));
String Date = greenwichDate.format(cal.getTime());
String Accept = "application/json";
String ContentType = "application/json";
String stringToSign=
"POST" + "\n" + Accept + "\n" + ContentType + "\n" +
Date + "\n" +
"/238b7e0a4459ce5dc4e8b0a79bbaf719/generate";
Mac hmacSha256 = Mac.getInstance("HmacSHA256");
String appSecret = "6326e25b794b4f218c6a2ec33c53fd89";
byte[] appSecretBytes = appSecret.getBytes(Charset.forName("UTF-8"));
hmacSha256.init(new SecretKeySpec(appSecretBytes, 0, appSecretBytes.length,
"HmacSHA256"));
byte[] md5Result = hmacSha256.doFinal(stringToSign.getBytes(Charset.forName("UTF8")));
String signature = Base64.encodeBase64String(md5Result);
JSONObject header = new JSONObject();
header.put("Accept", Accept);
header.put("Content-Type", ContentType);
header.put("Date", Date);
header.put("x-tilake-app-key", "c5591958d5d349349b875aca0d8a03ac");
java.util.Date currentDate = new Date();
long currentTimeMillis = currentDate.getTime();
header.put("x-tilake-ca-timestamp", currentTimeMillis);
header.put("x-tilake-canonce", String.valueOf(Math.random()));
header.put("x-tilake-casignature", signature);
header.put("x-tilake-ca-signature-method","HmacSHA256");

String strUrl = "https://tilake.wenge.com/saas-gateway/238b7e0a4459ce5dc4e8b0a79bbaf719/generate";
URL url = new URL(strUrl);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setHostnameVerifier(DO_NOT_VERIFY);

// 设置请求方式
connection.setRequestMethod("POST");
connection.setRequestProperty("accept","*/*");
connection.setRequestProperty("Content-Type", ContentType);
connection.setRequestProperty("Date", Date);
connection.setRequestProperty("x-tilake-app-key", "c5591958d5d349349b875aca0d8a03ac");
connection.setRequestProperty("x-tilake-ca-timestamp", String.valueOf(currentTimeMillis));
connection.setRequestProperty("x-tilake-ca-nonce", String.valueOf(Math.random()));
connection.setRequestProperty("x-tilake-ca-signature", signature);
connection.setRequestProperty("x-tilake-ca-signature-method","HmacSHA256");
connection.setRequestProperty("Accept", Accept);

connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8);
JSONArray messages = new JSONArray();
JSONObject text;
for(int i = 0; i < questions.size(); i++)
{
text = new JSONObject();
if(i%2==0){
text.put("role", "user");
}else{
text.put("role", "yayi");
}
text.put("content", questions.get(i));
messages.add(text);
}
JSONObject pa = new JSONObject();
pa.put("id", "1");
pa.put("messages", messages);
out.append(pa.toString());
out.flush();
out.close();
StringBuffer respResult = new StringBuffer();
BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
String line;
while ((line = reader.readLine()) != null) {
respResult.append(line);
}
reader.close();
JSONObject result = JSON.parseObject(respResult.toString());
return result;
}catch (Exception e) {
System.out.println("Req Error:" + e.getMessage());
return null;
}
}

@Override
public LlmResponse chat(LlmContext context, LlmParam param) {
List<String> contentArr = context.getContextList().stream().map(LlmContext.Context::getContent).collect(Collectors.toList());
JSONObject result = sendMsg(contentArr);
JSONObject usage = result.getJSONObject("data").getJSONObject("usage");
JSONObject message = result.getJSONObject("data").getJSONArray("choices").getJSONObject(0).getJSONObject("message");
LlmResponse response = new LlmResponse();
response.setContent(message.getString("content"));
response.setTotalToken(usage.getInteger("total_tokens"));
return response;
}

public static void main(String[] args){
LlmContext temp = new LlmContext("\"北京今天天气如何\"");
YayiServiceImpl a = new YayiServiceImpl();
System.out.println(a.chat(temp,null).getContent());
}
}

+ 10
- 11
xueyi-modules/xueyi-nlt/src/main/java/com/xueyi/nlt/nlt/service/impl/ZhipuServiceImpl.java Ver arquivo

@@ -1,8 +1,7 @@
package com.xueyi.nlt.nlt.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.shaded.com.google.gson.JsonArray;
import com.alibaba.nacos.shaded.com.google.gson.JsonObject;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.xueyi.nlt.nlt.domain.LlmContext;
@@ -95,22 +94,22 @@ public class ZhipuServiceImpl implements ISysLlmService {

private JSONObject sendMsg(List<String> questions){
String url = String.format("https://open.bigmodel.cn/api/paas/v3/model-api/chatglm_%s/invoke", llmclass);
JsonArray ja = new JsonArray();
JsonObject text;
JsonObject input = new JsonObject();
JSONArray ja = new JSONArray();
JSONObject text;
JSONObject input = new JSONObject();
for(int i = 0; i < questions.size(); i++)
{
text = new JsonObject();
text = new JSONObject();
if(i%2==0){
text.addProperty("role", "user");
text.put("role", "user");
}else{
text.addProperty("role", "assistant");
text.put("role", "assistant");
}
text.addProperty("content", questions.get(i));
text.put("content", questions.get(i));
ja.add(text);
}
input.add("prompt", ja);
input.addProperty("return_type", "json_string");
input.put("prompt", ja);
input.put("return_type", "json_string");
JSONObject result = doPOST(url, input.toString()).getJSONObject("data");
return result;



Carregando…
Cancelar
Salvar