当前位置: 首页 > news >正文

建设网站需求分析百度指数手机版

建设网站需求分析,百度指数手机版,wordpress 文章字体大小,莆田网站关键词优化GPT想必是最近互联网最火的话题了,作为一个Unity开发者,今天来介绍一下如何在Unity中使用GPT。 一、API 密钥 使用GPT的API首先要获得密钥,如下进入OpenAI官网(https://platform.openai.com/account/api-keys)–>选择自己的账号–>查…

GPT想必是最近互联网最火的话题了,作为一个Unity开发者,今天来介绍一下如何在Unity中使用GPT。

一、API 密钥

使用GPT的API首先要获得密钥,如下进入OpenAI官网(https://platform.openai.com/account/api-keys)–>选择自己的账号–>查看API密钥,然后创建一个自己的密钥(创建的后要记得复制好密钥)。
在这里插入图片描述

二、GPT模型

进入OpenAI文档(https://platform.openai.com/docs/models)页面可以看到目前主要可以使用的AI模型,如下从GPT3.0到GPT4.0。
在这里插入图片描述
目前可以免费使用的最高版本就是GPT-3.5,所以这里主要来介绍一下如何集成 gpt-3.5-turbo。

三、gpt-3.5-turbo 集成
请添加图片描述
进入API文档(https://platform.openai.com/docs/api-reference/chat/create)选择Chat,就是gpt-3.5-turbo的使用文档。在这里插入图片描述
OpenAI的接口访问主要都是使用Post请求,这里gpt-3.5-turbo的Post地址是:

https://api.openai.com/v1/chat/completions

请求与回调内容都是Json。
发送请求格式Request:

{"model": "gpt-3.5-turbo","messages": [{"role": "user", "content": "Hello!"}]
}

回调相应格式Respond:

{"id": "chatcmpl-123","object": "chat.completion","created": 1677652288,"choices": [{"index": 0,"message": {"role": "assistant","content": "\n\nHello there, how may I assist you today?",},"finish_reason": "stop"}],"usage": {"prompt_tokens": 9,"completion_tokens": 12,"total_tokens": 21}
}

在Unity中则可以直接使用UnityWebRequest来实现Post请求:

UnityWebRequest request = new UnityWebRequest(m_ApiUrl, "POST")

m_ApiUrl就是前面的Post地址。
在发送的信息中"model"就是使用示例中的"gpt-3.5-turbo",这是最新的可以免费使用的AI模型。
发送的消息"messages"中每个message都包含一个"role"(角色)和一个"content"(角色对于的内容)。
"role"可以选择 “system”, “user”, 或 “assistant”:

  • "system"一般作为角色设定,比如NPC扮演的话可以设定NPC的身份、特点等;
  • "user"就是用户角色;
  • "assistant"就是AI的角色身份。
    这里可能会好奇,为什么我们向GPT请求要发送AI角色的内容,其实这里我们主要是把上一次的提问和AI的回答都传回去,这样GPT就相当于有了记忆,知道我们前面对话说了啥,因此对话就不会是一个个孤立的问答了,官方的ChatGPT聊天同样是使用了这个原理。
    这里给上完整的gpt-3.5-turbo示例请求代码
public class GptTurboScript : MonoBehaviour
{/// <summary>/// api地址/// </summary>public string m_ApiUrl = "https://api.openai.com/v1/chat/completions";/// <summary>/// gpt-3.5-turbo/// </summary>public string m_gptModel = "gpt-3.5-turbo";/// <summary>/// 缓存对话/// </summary>[SerializeField]public List<SendData> m_DataList = new List<SendData>();/// <summary>/// AI人设/// </summary>public string Prompt;private void Start(){//运行时,添加人设m_DataList.Add(new SendData("system", Prompt));}public /// <summary>/// 调用接口/// </summary>/// <param name="_postWord">发送的消息</param>/// <param name="_openAI_Key">密钥</param>/// <param name="_callback">GPT的回调</param>/// <returns></returns>IEnumerator GetPostData(string _postWord,string _openAI_Key, System.Action<string> _callback){//缓存发送的信息列表m_DataList.Add(new SendData("user", _postWord));using (UnityWebRequest request = new UnityWebRequest(m_ApiUrl, "POST")){PostData _postData = new PostData{model = m_gptModel,messages = m_DataList};string _jsonText = JsonUtility.ToJson(_postData);byte[] data = System.Text.Encoding.UTF8.GetBytes(_jsonText);request.uploadHandler = (UploadHandler)new UploadHandlerRaw(data);request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();request.SetRequestHeader("Content-Type", "application/json");request.SetRequestHeader("Authorization", string.Format("Bearer {0}", _openAI_Key));yield return request.SendWebRequest();if (request.responseCode == 200){string _msg = request.downloadHandler.text;MessageBack _textback = JsonUtility.FromJson<MessageBack>(_msg);if (_textback != null && _textback.choices.Count > 0){string _backMsg = _textback.choices[0].message.content;//添加记录m_DataList.Add(new SendData("assistant", _backMsg));_callback(_backMsg);}}}}#region 数据包[Serializable]public class PostData{public string model;public List<SendData> messages;}[Serializable]public class SendData{public string role;public string content;public SendData() { }public SendData(string _role,string _content) {role = _role;content = _content;}}[Serializable]public class MessageBack{public string id;public string created;public string model;public List<MessageBody> choices;}[Serializable]public class MessageBody{public Message message;public string finish_reason;public string index;}[Serializable]public class Message{public string role;public string content;}#endregion
}

使用只需要调用GetPostData这个方法,传入你要发送的消息和你的API密钥,然后在_callback回调中获取到GPT返回的信息就可以了。

四、GPT绘画

请添加图片描述

和gpt-3.5-turbo类似,画图的Post接口为:

https://api.openai.com/v1/images/generations

发送请求格式Request:

{"prompt": "A cute baby sea otter","n": 2,"size": "1024x1024"
}

"prompt"为要绘制的图片描述;"n"为绘制数量;"size"为图片大小。
回调相应格式Respond:

{"created": 1589478378,"data": [{"url": "https://..."},{"url": "https://..."}]
}

返回的"url"就是图片的路径地址。
同样赋上完整的请求代码:

public class GPTImage : MonoBehaviour
{//API key[SerializeField] private string m_OpenAI_Key = "填写你的Key";/// <summary>/// api地址/// </summary>public const string m_ApiUrl = "https://api.openai.com/v1/images/generations";/// <summary>/// 调用接口/// </summary>/// <param name="_postWord"></param>/// <param name="_openAI_Key"></param>/// <param name="_callback"></param>/// <returns></returns>public IEnumerator GetPostData(string _postWord, Action<List<string>> _callback){using (UnityWebRequest request = new UnityWebRequest(m_ApiUrl, "POST")){PostData _postData = new PostData(_postWord, 10, "512x512");string _jsonText = JsonUtility.ToJson(_postData);byte[] data = System.Text.Encoding.UTF8.GetBytes(_jsonText);request.uploadHandler = (UploadHandler)new UploadHandlerRaw(data);request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();request.SetRequestHeader("Content-Type", "application/json");request.SetRequestHeader("Authorization", string.Format("Bearer {0}", m_OpenAI_Key));yield return request.SendWebRequest();if (request.responseCode == 200){string _msg = request.downloadHandler.text;MessageBack _textback = JsonUtility.FromJson<MessageBack>(_msg);if (_textback != null && _textback.data.Count > 0){List<string> urlList= new List<string>();for (int i = 0; i < _textback.data.Count; i++){Debug.Log(_textback.data[i].url);   //图片路径urlList.Add(_textback.data[i].url);}_callback(urlList);}}}}#region 数据包[Serializable]public class PostData{public string prompt;public int n;public string size;public PostData(string _prompt, int _n, string _size){prompt = _prompt;n = _n;size = _size;}}[Serializable]public class MessageBack{public string created;public List<Data> data;}[Serializable]public class Data{public string url;}#endregion
}

五、AICommand

AICommand是一位日本的开发者keijiro通过使用gpt-3.5-turbo来实现命令操控Unity,比如输入:创建物体、创建灯光、添加组件、改变颜色等。但这些命令使用英语才比较准确,通过下载源码(https://github.com/keijiro/AICommand)研究后,把发送给GPT的前置提示改成中文后就能比较好的识别中文命令了。
请添加图片描述

    static string WrapPrompt(string input)=> "Write a Unity Editor script.\n" +" - It provides its functionality as a menu item placed \"Edit\" > \"Do Task\".\n" +" - It doesn’t provide any editor window. It immediately does the task when the menu item is invoked.\n" +" - Don’t use GameObject.FindGameObjectsWithTag.\n" +" - There is no selected object. Find game objects manually.\n" +" - I only need the script body. Don’t add any explanation.\n" +"The task is described as follows:\n" + input;

如上,"input"为我们要输入的命令,前面部分为对命令的一些解释要求,其主要逻辑是让GPT先生成一个Editor模式下运行的脚本,脚本里面来实现我们描述的功能,比如“创建10个立方体”,当执行完这个脚本的功能后再把脚本删除,这样在感观上就像GPT能在Unity做一些操作。
在使用过程中遇到一些问题:
1.GPT给的脚本中时常给你一些使用提示,而我们需要的是存脚本才能正常运行,所以就需要在前置描述里面特别强调我们只需要纯代码文本。
2.因描述不准确、理解偏差或功能复杂等情况导致GPT生成的脚本并不能正常运行,其实这个目前并不好解决,AICommand能实现的也是一些简单基础的操作,但可以通过一些人为的操作,让GPT半自动的来实现一些更复杂的工作,比如可以让GPT在Unity生成脚本后我们在去挂载或修改脚本,这样加上人的操作虽然感觉不是那么智能,但也能提高很多效率。目前Unity商店中就有人做了一款类似的插件。

六、总结

目前GPT在Unity的应用虽还不能很高的智能化,但可以使用他生产代码、修改代码、以及给出一些优化、设计建议等,从而很大程度的提升我们的工作效率。


文章转载自:
http://euphorigenic.sqxr.cn
http://holophotal.sqxr.cn
http://kura.sqxr.cn
http://rumania.sqxr.cn
http://issuance.sqxr.cn
http://needlepoint.sqxr.cn
http://seistan.sqxr.cn
http://hippic.sqxr.cn
http://sinfonia.sqxr.cn
http://telemetry.sqxr.cn
http://euchromosome.sqxr.cn
http://simulfix.sqxr.cn
http://dismantle.sqxr.cn
http://yogi.sqxr.cn
http://kaki.sqxr.cn
http://flageolet.sqxr.cn
http://chaste.sqxr.cn
http://herbage.sqxr.cn
http://focal.sqxr.cn
http://unrelatable.sqxr.cn
http://castanet.sqxr.cn
http://repressible.sqxr.cn
http://shapeliness.sqxr.cn
http://unplaced.sqxr.cn
http://stingray.sqxr.cn
http://sincerity.sqxr.cn
http://nymphalid.sqxr.cn
http://nonrecoverable.sqxr.cn
http://electrologist.sqxr.cn
http://admonitor.sqxr.cn
http://uscgr.sqxr.cn
http://teleset.sqxr.cn
http://reinvade.sqxr.cn
http://jingled.sqxr.cn
http://spherulitize.sqxr.cn
http://accouplement.sqxr.cn
http://fulfil.sqxr.cn
http://hermeneutic.sqxr.cn
http://counterclaim.sqxr.cn
http://apocalyptical.sqxr.cn
http://kookaburra.sqxr.cn
http://diachrony.sqxr.cn
http://sepulture.sqxr.cn
http://hove.sqxr.cn
http://sternum.sqxr.cn
http://acrux.sqxr.cn
http://derbyshire.sqxr.cn
http://auspicate.sqxr.cn
http://alkali.sqxr.cn
http://amobarbital.sqxr.cn
http://sprung.sqxr.cn
http://cartophily.sqxr.cn
http://wonderstruck.sqxr.cn
http://sierran.sqxr.cn
http://deadlock.sqxr.cn
http://claimant.sqxr.cn
http://liveliness.sqxr.cn
http://monogerm.sqxr.cn
http://incrossbred.sqxr.cn
http://automorphic.sqxr.cn
http://cerebrosclerosis.sqxr.cn
http://lasthome.sqxr.cn
http://breslau.sqxr.cn
http://fistiana.sqxr.cn
http://serpentinite.sqxr.cn
http://glossina.sqxr.cn
http://achromatophil.sqxr.cn
http://bobwig.sqxr.cn
http://thereagainst.sqxr.cn
http://lazuline.sqxr.cn
http://gingersnap.sqxr.cn
http://evertor.sqxr.cn
http://technomania.sqxr.cn
http://ditchwater.sqxr.cn
http://eyespot.sqxr.cn
http://admetus.sqxr.cn
http://sizzard.sqxr.cn
http://biennially.sqxr.cn
http://bise.sqxr.cn
http://observantly.sqxr.cn
http://troutlet.sqxr.cn
http://uricacidemia.sqxr.cn
http://spuddle.sqxr.cn
http://dependability.sqxr.cn
http://vortiginous.sqxr.cn
http://angiocarpy.sqxr.cn
http://theresa.sqxr.cn
http://counterinsurgency.sqxr.cn
http://parzival.sqxr.cn
http://triode.sqxr.cn
http://westwards.sqxr.cn
http://chillon.sqxr.cn
http://ungroup.sqxr.cn
http://leet.sqxr.cn
http://qualm.sqxr.cn
http://sequenator.sqxr.cn
http://transcendency.sqxr.cn
http://oxyphilic.sqxr.cn
http://racialist.sqxr.cn
http://sextodecimo.sqxr.cn
http://www.15wanjia.com/news/97182.html

相关文章:

  • 网站后台账号密码电商平台如何推广运营
  • 免费的网站制作台州关键词优化报价
  • 做电信网站运营青岛seo服务公司
  • 自已做个网站怎么做seo优化思路
  • 牡丹江百姓信息网app优化
  • 改网站字体颜色代码第一接单网app地推和拉新
  • 建设部网站怎么查询企业业绩360站长平台
  • 做公司网站需注意什么百度竞价排名是什么方式
  • 益阳网站建设公司有哪些抖音seo
  • 网站次年续费网址域名查询
  • 互联网网站设计网站制作流程图
  • 网站建设类项目推广网
  • 广东省门户网站建设的现状西安做网页的公司
  • 中山小榄网站建设沈阳seo推广
  • wordpress如何删除以前主题的缓存长春seo优化企业网络跃升
  • 专业版装修用什么网站做导航条广州网站排名优化报价
  • 建筑信息查询平台优化排名案例
  • 宝安专业网站设计公司湖南株洲疫情最新情况
  • 西安网络公司做网站线上线下推广方案
  • 哪些软件可以做网站设计深圳公关公司
  • wordpress搜索框位置企业优化推广
  • 企业网站后台管理google浏览器官网入口
  • 网站优化公司网络站点推广的方法
  • 什么网站是专门做批发商品seo系统推广
  • 做动态网站系统用什么语言有什么推广的平台
  • 网站后期运营方案步骤百度公司招聘条件
  • 建个网站 做ib代理风云榜
  • 宁波网站定制服务磁力吧
  • 在深圳做网站平台需要什么备案沈阳网站关键字优化
  • 网站建设的客户在哪里百度竞价托管靠谱吗