0


Unity接入人工智能

在Unity接入人工智能中,本篇实现了接入百度智能云ai进行npc智能对话,通过http方式,并非插件,适合于所有支持Http链接的Unity版本。对于Chartgpt可以参考本篇内容的实现过程。

1-4节讲解测试,第5节讲解Unity中的实现,建议从头开始阅读。

一、创建应用

1.1注册百度智能云账号

按照图片顺序依次点击

1.2开始创建应用

1.2.1搜索并打开图中产品

1.2.2打开应用接入

1.2.3创建应用

这里全部选择,输入对应信息,然后创建,点击创建好的应用的详情,找到

API Key与Secret Key,后续用于获取access_token。

注意:这俩都是私密内容,避免泄露!

二、开通应用

找到计费管理并进入,点击开通付费,找到ERNIE-Speed-128K并开通,本篇使用ERNIE-Speed-128K是因为这个免费,也有其他免费的,有需要自行开通,后文以ERNIE-Speed-128K直接讲解,不再赘述。

三、获取access_token

3.1打开access_token获取文档

按照图片内容依次点击就好

3.2测试接口

重点内容已经使用红色框框出

我个人喜欢使用apipost,测试接口有很多软件,大家自行解决,这里使用apipost作为案例

在对应的地方输入对应的参数就可以了,这里用到了上面保存的API Key和Secret Key,输入完后发送等待响应。

四、发送对话

4.1测试对话接口

响应中的result就是回复的内容了

现在已经测试完毕,证明此方法可以,现在将他写入Unity中

五、将测试正确内容写入Unity

5.1创建需要的ui

Scroll View用于放文本,避免一些回复文本过长导致看不到或其他效果

在content添加组件,按照图片设置

5.2创建c#脚本,附在物体上(任意物体)

5.3编写脚本

  1. public Text OutPutText;
  2. public Button btn;
  3. public InputField inputField;
  4. private string GetToken_Url;
  5. private string SendHttp_Url= "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie-speed-128k";
  6. private string grant_type = "client_credentials";
  7. private string client_id = "你的API Key:";
  8. private string client_secret = "你的Secret Key:";
  9. private string access_token;
  10. // Start is called before the first frame update
  11. void Start()
  12. {
  13. GetToken_Url = "https://aip.baidubce.com/oauth/2.0/token?grant_type="
  14. + grant_type + "&client_id=" + client_id + "&client_secret=" + client_secret;
  15. StartCoroutine(GetToken(GetToken_Url));
  16. btn.onClick.AddListener(() =>
  17. {
  18. StartSend();
  19. });
  20. }
  21. void Update()
  22. {
  23. if (Input.GetKeyDown(KeyCode.Return))
  24. {
  25. StartSend();
  26. }
  27. }
  28. IEnumerator GetToken(string GetToken_Url)
  29. {
  30. UnityWebRequest request = new UnityWebRequest(GetToken_Url, "POST");
  31. request.downloadHandler = new DownloadHandlerBuffer();
  32. request.SetRequestHeader("Content-Type", "application/json");
  33. yield return request.SendWebRequest();
  34. if (request.result != UnityWebRequest.Result.ConnectionError && request.result != UnityWebRequest.Result.ProtocolError)
  35. {
  36. string pattern = "\"access_token\":\"(.*?)\"";
  37. Match match = Regex.Match(request.downloadHandler.text, pattern);
  38. access_token = match.Groups[1].Value;
  39. SendHttp_Url += "?access_token=" + access_token;
  40. }
  41. else
  42. {
  43. Debug.LogError(request.error);
  44. }
  45. request.Dispose();
  46. }
  47. IEnumerator SendHttp(string SendHttp_Url)
  48. {
  49. UnityWebRequest sendrequest = new UnityWebRequest(SendHttp_Url,"POST");
  50. string Sessagejson = "{" +
  51. "\"" + "messages" + "\"" + ":" + "[" +
  52. "{" +
  53. "\"" + "role" + "\"" + ":" + "\"" + "user" + "\"" + "," +
  54. "\"" + "content" + "\"" + ":" + "\"" + inputField.text + "\"" +
  55. "}" +
  56. "]" +
  57. "}";
  58. byte[] SendJson = new UTF8Encoding().GetBytes(Sessagejson);
  59. sendrequest.uploadHandler = new UploadHandlerRaw(SendJson);
  60. sendrequest.downloadHandler = new DownloadHandlerBuffer();
  61. sendrequest.SetRequestHeader("Content-Type", "application/json");
  62. yield return sendrequest.SendWebRequest();
  63. if (sendrequest.result != UnityWebRequest.Result.ConnectionError && sendrequest.result != UnityWebRequest.Result.ProtocolError)
  64. {
  65. string pattern2 = "\"result\":\"(.*?)\"";
  66. Match match2 = Regex.Match(sendrequest.downloadHandler.text, pattern2);
  67. OutPutText.text = match2.Groups[1].Value;
  68. }
  69. else
  70. {
  71. Debug.LogError(sendrequest.error);
  72. }
  73. sendrequest.Dispose();
  74. }
  75. private void StartSend()
  76. {
  77. OutPutText.text = "正在回复中,请稍等...";
  78. StartCoroutine(SendHttp(SendHttp_Url));
  79. }

5.3.1GetToken_Url是获取access_token的链接

5.3.2SendHttp_Url是发送消息的链接

5.3.3grant_type与client_id与client_secret时链接的一部分,其中client_id是你保存的API Key,client_secret是你保存的Secret Key

5.3.4access_token是获取到的

5.3.5在Start中获取access_token,并给发送按钮添加事件

5.3.6Sessagejson是发送的json文件

如果对于Unity发送Http不了解的可以查看我下一篇文章:Unity发送Http-CSDN博客

结尾:有任何错误请指出,补充请评论,看到会第一时间回复,谢谢。

标签: unity 游戏引擎 http

本文转载自: https://blog.csdn.net/cherry_f_f/article/details/142955436
版权归原作者 _Cherry| 所有, 如有侵权,请联系我们删除。

“Unity接入人工智能”的评论:

还没有评论