信誉好的做pc端网站,做ppt常用的网站,网站icp备案 年检,cms系统wordpress在Unity接入人工智能中#xff0c;本篇实现了接入百度智能云ai进行npc智能对话#xff0c;通过http方式#xff0c;并非插件#xff0c;适合于所有支持Http链接的Unity版本。对于Chartgpt可以参考本篇内容的实现过程。
1-4节讲解测试#xff0c;第5节讲解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编写脚本
public Text OutPutText;
public Button btn;
public InputField inputField;
private string GetToken_Url;
private string SendHttp_Url https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie-speed-128k;
private string grant_type client_credentials;
private string client_id 你的API Key:;
private string client_secret 你的Secret Key:;
private string access_token;
// Start is called before the first frame update
void Start()
{GetToken_Url https://aip.baidubce.com/oauth/2.0/token?grant_type grant_type client_id client_id client_secret client_secret;StartCoroutine(GetToken(GetToken_Url));btn.onClick.AddListener(() {StartSend();});
}
void Update()
{if (Input.GetKeyDown(KeyCode.Return)){StartSend();}
}
IEnumerator GetToken(string GetToken_Url)
{UnityWebRequest request new UnityWebRequest(GetToken_Url, POST);request.downloadHandler new DownloadHandlerBuffer();request.SetRequestHeader(Content-Type, application/json); yield return request.SendWebRequest();if (request.result ! UnityWebRequest.Result.ConnectionError request.result ! UnityWebRequest.Result.ProtocolError){string pattern \access_token\:\(.*?)\;Match match Regex.Match(request.downloadHandler.text, pattern);access_token match.Groups[1].Value;SendHttp_Url ?access_token access_token;}else{Debug.LogError(request.error);}request.Dispose();
}
IEnumerator SendHttp(string SendHttp_Url)
{UnityWebRequest sendrequest new UnityWebRequest(SendHttp_Url,POST);string Sessagejson { \ messages \ : [ { \ role \ : \ user \ , \ content \ : \ inputField.text \ } ] };byte[] SendJson new UTF8Encoding().GetBytes(Sessagejson);sendrequest.uploadHandler new UploadHandlerRaw(SendJson);sendrequest.downloadHandler new DownloadHandlerBuffer();sendrequest.SetRequestHeader(Content-Type, application/json);yield return sendrequest.SendWebRequest();if (sendrequest.result ! UnityWebRequest.Result.ConnectionError sendrequest.result ! UnityWebRequest.Result.ProtocolError){string pattern2 \result\:\(.*?)\;Match match2 Regex.Match(sendrequest.downloadHandler.text, pattern2);OutPutText.text match2.Groups[1].Value;}else{Debug.LogError(sendrequest.error);}sendrequest.Dispose();
}
private void StartSend()
{OutPutText.text 正在回复中,请稍等...;StartCoroutine(SendHttp(SendHttp_Url));
}
5.3.1GetToken_Url是获取access_token的链接
5.3.2SendHttp_Url是发送消息的链接
5.3.3grant_type与client_id与client_secret时链接的一部分其中client_id是你保存的API Keyclient_secret是你保存的Secret Key
5.3.4access_token是获取到的
5.3.5在Start中获取access_token并给发送按钮添加事件
5.3.6Sessagejson是发送的json文件
如果对于Unity发送Http不了解的可以查看我下一篇文章Unity发送Http-CSDN博客 结尾有任何错误请指出补充请评论看到会第一时间回复谢谢。