当前位置:   article > 正文

Unity调用文心-ERNIE-Bot-turbo

Unity调用文心-ERNIE-Bot-turbo

参考文章

ERNIE-Bot-turbo - 千帆大模型平台 | 百度智能云文档 (baidu.com) 

错误码 - 千帆大模型平台 | 百度智能云文档 (baidu.com)

  1. private readonly string apiKey = "";
  2. private readonly string secretKey = "";
  3. private readonly string tokenUrl = "https://aip.baidubce.com/oauth/2.0/token";
  4. void Start()
  5. {
  6. StartCoroutine(GetAccessToken());
  7. }
  8. IEnumerator GetAccessToken()
  9. {
  10. WWWForm form = new WWWForm();
  11. form.AddField("grant_type", "client_credentials");
  12. form.AddField("client_id", apiKey);
  13. form.AddField("client_secret", secretKey);
  14. Debug.LogError(1);
  15. using (UnityWebRequest www = UnityWebRequest.Post(tokenUrl, form))
  16. {
  17. yield return www.SendWebRequest();
  18. Debug.LogError(2);
  19. if (www.result != UnityWebRequest.Result.Success)
  20. {
  21. Debug.LogError(www.error);
  22. }
  23. else
  24. {
  25. string accessToken = JsonUtility.FromJson<AccessTokenResponse>(www.downloadHandler.text).access_token;
  26. StartCoroutine(SendRequest(accessToken));
  27. }
  28. }
  29. }
  30. IEnumerator SendRequest(string accessToken)
  31. {
  32. string url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/eb-instant?access_token=" + accessToken;
  33. var body = @"{""messages"":[{""role"":""user"",""content"":""你好""}]}";
  34. Debug.LogError(body);
  35. using (UnityWebRequest www = new UnityWebRequest(url, "POST"))
  36. {
  37. byte[] bodyRaw = Encoding.UTF8.GetBytes(body);
  38. www.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  39. www.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
  40. www.SetRequestHeader("Content-Type", "application/json");
  41. yield return www.SendWebRequest();
  42. if (www.result != UnityWebRequest.Result.Success)
  43. {
  44. Debug.LogError(www.error);
  45. }
  46. else
  47. {
  48. Debug.LogError("Response headers: " + www.GetResponseHeaders());
  49. Debug.LogError("Response: " + www.downloadHandler.text);
  50. }
  51. }
  52. }
  53. [System.Serializable]
  54. private class AccessTokenResponse
  55. {
  56. public string access_token;
  57. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/211090
推荐阅读
相关标签
  

闽ICP备14008679号