108 lines
3.6 KiB
C#
108 lines
3.6 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Text;
|
||
using UnityEngine;
|
||
using UnityEngine.Networking;
|
||
using LitJson;
|
||
|
||
public class ChatAli : LLM
|
||
{
|
||
/// <summary>
|
||
/// 配置脚本
|
||
/// </summary>
|
||
[SerializeField] private AliSettings m_Settings;
|
||
///// <summary>
|
||
///// 用于连续对话
|
||
///// </summary>
|
||
[SerializeField] private string session_id;
|
||
private void Awake()
|
||
{
|
||
OnInit();
|
||
}
|
||
/// <summary>
|
||
/// 初始化
|
||
/// </summary>
|
||
private void OnInit()
|
||
{
|
||
m_Settings = this.GetComponent<AliSettings>();
|
||
}
|
||
/// <summary>
|
||
/// 发送消息
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public override void PostMsg(string _msg, Action<string> _callback)
|
||
{
|
||
base.PostMsg(_msg, _callback);
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 发送数据
|
||
/// </summary>
|
||
/// <param name="_postWord"></param>
|
||
/// <param name="_callback"></param>
|
||
/// <returns></returns>
|
||
public override IEnumerator Request(string _postWord, System.Action<string> _callback)
|
||
{
|
||
stopwatch.Restart();
|
||
|
||
//若没有配置环境变量,可用百炼API Key将下行替换为:apiKey="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
|
||
string apiKey = m_Settings.m_API_key;
|
||
string appId = m_Settings.m_APP_ID; // 替换为实际的应用ID
|
||
|
||
string url = $"https://dashscope.aliyuncs.com/api/v1/apps/{appId}/completion";
|
||
string _jsonData = null;
|
||
if (string.IsNullOrEmpty(session_id))
|
||
{
|
||
StringBuilder str = new StringBuilder();
|
||
str.Append("{");
|
||
str.Append("\"input\":");
|
||
str.Append("{\"prompt\":\"" + _postWord + "\"},");
|
||
str.Append("\"parameters\": { },");
|
||
str.Append("\"debug\": { }");
|
||
str.Append("}");
|
||
_jsonData = str.ToString();
|
||
}
|
||
else
|
||
{
|
||
StringBuilder str = new StringBuilder();
|
||
str.Append("{");
|
||
str.Append("\"input\":");
|
||
str.Append("{\"prompt\":\"" + _postWord + "\",");
|
||
str.Append("\"session_id\":\"" + session_id + "\"},");
|
||
str.Append("\"parameters\": { },");
|
||
str.Append("\"debug\": { }");
|
||
str.Append("}");
|
||
_jsonData = str.ToString();
|
||
}
|
||
Debug.Log(_postWord+ session_id);
|
||
using (UnityWebRequest request = new UnityWebRequest(url, "POST"))
|
||
{
|
||
request.timeout = 15;
|
||
request.SetRequestHeader("Authorization", apiKey);
|
||
request.SetRequestHeader("Content-Type", "application/json");
|
||
byte[] data = System.Text.Encoding.UTF8.GetBytes(_jsonData);
|
||
request.uploadHandler = (UploadHandler)new UploadHandlerRaw(data);
|
||
request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
|
||
yield return request.SendWebRequest();
|
||
|
||
if (request.responseCode == 200)
|
||
{
|
||
string _msg = request.downloadHandler.text;
|
||
JsonData responseBody = JsonMapper.ToObject(_msg);
|
||
Debug.Log(responseBody["output"]["session_id"].ToString() + responseBody["output"]["text"].ToString());
|
||
session_id = responseBody["output"]["session_id"].ToString();
|
||
//回调
|
||
_callback(responseBody["output"]["text"].ToString());
|
||
}
|
||
else
|
||
{
|
||
Debug.Log(request.downloadHandler.text + "[error]");
|
||
_callback("[error]");
|
||
}
|
||
}
|
||
stopwatch.Stop();
|
||
Debug.Log("chat阿里-耗时:" + stopwatch.Elapsed.TotalSeconds);
|
||
}
|
||
}
|