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>
|
|||
|
|
/// <20><><EFBFBD>ýű<C3BD>
|
|||
|
|
/// </summary>
|
|||
|
|
[SerializeField] private AliSettings m_Settings;
|
|||
|
|
///// <summary>
|
|||
|
|
///// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ի<EFBFBD>
|
|||
|
|
///// </summary>
|
|||
|
|
[SerializeField] private string session_id;
|
|||
|
|
private void Awake()
|
|||
|
|
{
|
|||
|
|
OnInit();
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ʼ<EFBFBD><CABC>
|
|||
|
|
/// </summary>
|
|||
|
|
private void OnInit()
|
|||
|
|
{
|
|||
|
|
m_Settings = this.GetComponent<AliSettings>();
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public override void PostMsg(string _msg, Action<string> _callback)
|
|||
|
|
{
|
|||
|
|
base.PostMsg(_msg, _callback);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="_postWord"></param>
|
|||
|
|
/// <param name="_callback"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public override IEnumerator Request(string _postWord, System.Action<string> _callback)
|
|||
|
|
{
|
|||
|
|
stopwatch.Restart();
|
|||
|
|
|
|||
|
|
//<2F><>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ð<EFBFBD><C3B0><EFBFBD>API Key<65><79><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>滻Ϊ<E6BBBB><CEAA>apiKey="sk-xxx"<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD>ӽ<EFBFBD>API KeyӲ<79><D3B2><EFBFBD>뵽<EFBFBD><EBB5BD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3>Լ<EFBFBD><D4BC><EFBFBD>API Keyй¶<D0B9><C2B6><EFBFBD>ա<EFBFBD>
|
|||
|
|
string apiKey = m_Settings.m_API_key;
|
|||
|
|
string appId = m_Settings.m_APP_ID; // <20>滻Ϊʵ<CEAA>ʵ<EFBFBD>Ӧ<EFBFBD><D3A6>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();
|
|||
|
|
//<2F>ص<EFBFBD>
|
|||
|
|
_callback(responseBody["output"]["text"].ToString());
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
Debug.Log(request.downloadHandler.text + "[error]");
|
|||
|
|
_callback("[error]");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
stopwatch.Stop();
|
|||
|
|
Debug.Log("chat<61><74><EFBFBD><EFBFBD>-<2D><>ʱ<EFBFBD><CAB1>" + stopwatch.Elapsed.TotalSeconds);
|
|||
|
|
}
|
|||
|
|
}
|