|
|
@@ -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()); |
|
|
|
} |
|
|
|
} |