赞
踩
参考文章
ERNIE-Bot-turbo - 千帆大模型平台 | 百度智能云文档 (baidu.com)
错误码 - 千帆大模型平台 | 百度智能云文档 (baidu.com)
- private readonly string apiKey = "";
- private readonly string secretKey = "";
- private readonly string tokenUrl = "https://aip.baidubce.com/oauth/2.0/token";
- void Start()
- {
- StartCoroutine(GetAccessToken());
- }
- IEnumerator GetAccessToken()
- {
- WWWForm form = new WWWForm();
- form.AddField("grant_type", "client_credentials");
- form.AddField("client_id", apiKey);
- form.AddField("client_secret", secretKey);
-
- Debug.LogError(1);
- using (UnityWebRequest www = UnityWebRequest.Post(tokenUrl, form))
- {
- yield return www.SendWebRequest();
-
- Debug.LogError(2);
- if (www.result != UnityWebRequest.Result.Success)
- {
- Debug.LogError(www.error);
- }
- else
- {
- string accessToken = JsonUtility.FromJson<AccessTokenResponse>(www.downloadHandler.text).access_token;
- StartCoroutine(SendRequest(accessToken));
- }
- }
- }
-
- IEnumerator SendRequest(string accessToken)
- {
- string url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/eb-instant?access_token=" + accessToken;
- var body = @"{""messages"":[{""role"":""user"",""content"":""你好""}]}";
- Debug.LogError(body);
- using (UnityWebRequest www = new UnityWebRequest(url, "POST"))
- {
- byte[] bodyRaw = Encoding.UTF8.GetBytes(body);
- www.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
- www.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
- www.SetRequestHeader("Content-Type", "application/json");
-
- yield return www.SendWebRequest();
-
- if (www.result != UnityWebRequest.Result.Success)
- {
- Debug.LogError(www.error);
- }
- else
- {
- Debug.LogError("Response headers: " + www.GetResponseHeaders());
- Debug.LogError("Response: " + www.downloadHandler.text);
- }
- }
- }
- [System.Serializable]
- private class AccessTokenResponse
- {
- public string access_token;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。