当前位置:   article > 正文

【Unity】AI实战应用——Unity接入GPT和对游戏开发实际应用的展望_unity接入chatgpt

unity接入chatgpt

GPT for unity插件地址: GitHub - sunsvip/ChatGPTForUnity: ChatGPT for unity

用法:

  1. 打开Unity PackageManager界面.
  2. Add package from git URL
  3. 粘贴插件地址添加 https://github.com/sunsvip/ChatGPTForUnity.git

————————————————————————————————————

几个资本大佬花钱让一群程序员研发出了AI,砸了程序员的大锅。尤其是ChatGPT 4.0发布后,认识的同事朋友们都在恐慌AI的发展,甚至有不少人开始抗拒。我的观点是:人工智能是大势所趋,势不可挡,那就驾驭它吧!

我司已经在商业项目中实际使用了AI, 包括Stable Diffusion及其扩展插件,当然也有爆火的GPT。

Midjourney + Stable Diffusion + ControlNET + Lora以及Photoshop Stable Diffusion 等插件的结合使用已经强大到基本取代初中级原画师;

GPT 4.0势头已经咄咄逼人,据说下一代会进化到运行时处理功能,比如对磁盘的读写等。用户让“创建一个cs代码”,它就能在电脑硬盘里创建一个代码文件。个人觉得任重道远,功能越强责任越大,如果没有有效解决安全问题之前,这一步很难到来。

GPT已经能全面应用到游戏开发的各个环节了,策划的剧情设定、技术的代码编写优化、测试的测试用例等。我从去年12月开始使用,完全把它当成了一个搜索引擎,自从有了它几乎没使用多少次谷歌百度。4.0的到来彻底激发了我对ChatGPT实际应用的思考。

首先第一步肯定是先把GPT接入Unity,先建立起通讯,以此为基础各种应用功能才能百花齐放。

工具效果预览:

将回答里的代码保存成文件

 GPT接口实现:

用UnityWebRequest实现一个与ChatGPT通讯的类,之后的各种功能基于此类。

一,获取API Key

GPT提供了开放接口,仅需一个个人版的API Key即可, API Key获取入口:https://platform.openai.com/account/api-keys

 二,使用UnityWebRequest发送Post请求和接收GPT返回信息

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

上行数据结构如下:

  1. {
  2. "messages": [
  3. {
  4. "role": "user",
  5. "content": "你是机器人吗"//要发送的问题
  6. }
  7. ],
  8. "model": "gpt-3.5-turbo",//AI数据模型
  9. "temperature": 0.7 //默认是1, 数值越大结果随机性越高
  10. }

 需要注意的是,messages是个数组,如果想连续对话就需要把发送历史塞到这个messages数组,gpt根据你的发送历史分析上下文给出处理结果。gpt消耗token不是根据发送请求次数计算,聊天历史越多单次发送请求消耗的token越多,所以及时清除历史(新建话题)可以节省token消耗。

下行数据结构如下:

  1. {
  2. "id": "chatcmpl-xxxxxxxxxxxxxxxxxx",
  3. "object": "chat.completion",
  4. "created": 1678987654,
  5. "model": "gpt-3.5-turbo-0301",
  6. "usage": {
  7. "prompt_tokens": 14,
  8. "completion_tokens": 23,
  9. "total_tokens": 37
  10. },
  11. "choices": [
  12. {
  13. "message": {
  14. "role": "assistant",
  15. "content": "\n\n是的,我是一个AI语言模型,也可以被称为机器人。" //得到的回复
  16. },
  17. "finish_reason": "stop",
  18. "index": 0
  19. }
  20. ]
  21. }

使用UnityWebRequest发送Post请求:

  1. private IEnumerator Request(string input, Action<bool, string> onComplete, Action<float> onProgressUpdate)
  2. {
  3. var msg = new Message()
  4. {
  5. role = UserId,
  6. content = input,
  7. };
  8. requestData.AppendChat(msg);
  9. messageHistory.Add(msg);
  10. using (webRequest = new UnityWebRequest(ChatgptUrl, "POST"))
  11. {
  12. var jsonDt = UtilityBuiltin.Json.ToJson(requestData);
  13. Debug.Log(jsonDt);
  14. byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonDt);
  15. webRequest.uploadHandler = new UploadHandlerRaw(bodyRaw);
  16. webRequest.downloadHandler = new DownloadHandlerBuffer();
  17. webRequest.SetRequestHeader("Content-Type", "application/json");
  18. webRequest.SetRequestHeader("Authorization", $"Bearer {this.ApiKey}");
  19. //webRequest.certificateHandler = new ChatGPTWebRequestCert();
  20. var req = webRequest.SendWebRequest();
  21. while (!webRequest.isDone)
  22. {
  23. onProgressUpdate?.Invoke((webRequest.downloadProgress + webRequest.uploadProgress) / 2f);
  24. yield return null;
  25. }
  26. if (webRequest.result != UnityWebRequest.Result.Success)
  27. {
  28. Debug.LogError($"---------ChatGPT请求失败:{webRequest.error}---------");
  29. onComplete?.Invoke(false, string.Empty);
  30. }
  31. else
  32. {
  33. var json = webRequest.downloadHandler.text;
  34. Debug.Log(json);
  35. try
  36. {
  37. ChatCompletion result = UtilityBuiltin.Json.ToObject<ChatCompletion>(json);
  38. int lastChoiceIdx = result.choices.Count - 1;
  39. var replyMsg = result.choices[lastChoiceIdx].message;
  40. replyMsg.content = replyMsg.content.Trim();
  41. messageHistory.Add(replyMsg);
  42. onComplete?.Invoke(true, replyMsg.content);
  43. }
  44. catch (System.Exception e)
  45. {
  46. Debug.LogError($"---------ChatGPT返回数据解析失败:{e.Message}---------");
  47. onComplete?.Invoke(false, e.Message);
  48. }
  49. }
  50. webRequest.Dispose();
  51. webRequest = null;
  52. }
  53. }

 以上就是向ChatGPT发送请求并接受回复的核心代码,非常简单。然而不出意外的话会出现请求报错:Cert verify failed: UNITYTLS_X509VERIFY_FLAG_CN_MISMATCH, https证书验证失败。

所以还需要自定义验证类,直接跳过验证返回true:

  1. class ChatGPTWebRequestCert : UnityEngine.Networking.CertificateHandler
  2. {
  3. protected override bool ValidateCertificate(byte[] certificateData)
  4. {
  5. //return base.ValidateCertificate(certificateData);
  6. return true;
  7. }
  8. }

然后为UnityWebRequest势力指定验证Handler:

webRequest.certificateHandler = new ChatGPTWebRequestCert();

再次运行就能正常接收数据了。

完整代码:

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Unity.EditorCoroutines.Editor;
  7. using UnityEditor;
  8. using UnityEngine;
  9. using UnityEngine.Networking;
  10. namespace UnityGameFramework.Editor.AIAssistant
  11. {
  12. public class ChatGPT
  13. {
  14. const string ChatgptUrl = "https://api.openai.com/v1/chat/completions";
  15. const string DefaultAPIKey = "替换自己的ChatGPT API Key";
  16. const string DefaultModel = "gpt-3.5-turbo";
  17. const float DefaultTemperature = 0;
  18. const string DefaultUserId = "user";
  19. string ApiKey;
  20. string UserId;
  21. List<Message> messageHistory;
  22. public List<Message> MessageHistory => messageHistory;
  23. ChatGPTRequestData requestData;
  24. UnityWebRequest webRequest;
  25. public float ChatGPTRandomness { get => requestData.temperature; set { requestData.temperature = Mathf.Clamp(value, 0, 2); } }
  26. public bool IsRequesting => webRequest != null && !webRequest.isDone;
  27. public float RequestProgress => IsRequesting ? (webRequest.uploadProgress + webRequest.downloadProgress) / 2f : 0f;
  28. public ChatGPT(string apiKey = DefaultAPIKey, string userId = DefaultUserId, string model = DefaultModel, float temperature = DefaultTemperature)
  29. {
  30. this.ApiKey = apiKey;
  31. this.UserId = string.IsNullOrWhiteSpace(userId) ? DefaultUserId : userId;
  32. messageHistory = new List<Message>();
  33. requestData = new ChatGPTRequestData(model, temperature);
  34. }
  35. /// <summary>
  36. /// 接着上次的话题
  37. /// </summary>
  38. public void RestoreChatHistory()
  39. {
  40. var chatHistoryJson = EditorPrefs.GetString("ChatGPT.Settings.ChatHistory", string.Empty);
  41. var requestDataJson = EditorPrefs.GetString("ChatGPT.Settings.RequestData", string.Empty);
  42. if (!string.IsNullOrEmpty(chatHistoryJson))
  43. {
  44. var jsonObj = UtilityBuiltin.Json.ToObject<ChatGPTRequestData>(requestDataJson);
  45. if (jsonObj != null)
  46. {
  47. requestData.messages = jsonObj.messages;
  48. }
  49. }
  50. if (!string.IsNullOrEmpty(requestDataJson))
  51. {
  52. var jsonObj = UtilityBuiltin.Json.ToObject<List<Message>>(chatHistoryJson);
  53. if (jsonObj != null)
  54. {
  55. messageHistory = jsonObj;
  56. }
  57. }
  58. }
  59. public void SaveChatHistory()
  60. {
  61. var chatHistoryJson = UtilityBuiltin.Json.ToJson(messageHistory);
  62. var requestDataJson = UtilityBuiltin.Json.ToJson(requestData);
  63. EditorPrefs.SetString("ChatGPT.Settings.ChatHistory", chatHistoryJson);
  64. EditorPrefs.SetString("ChatGPT.Settings.RequestData", requestDataJson);
  65. }
  66. public void Send(string message, Action<bool, string> onComplete = null, Action<float> onProgressUpdate = null)
  67. {
  68. EditorCoroutineUtility.StartCoroutine(Request(message, onComplete, onProgressUpdate), this);
  69. }
  70. public async Task<string> SendAsync(string message)
  71. {
  72. bool isCompleted = false;
  73. string result = string.Empty;
  74. Action<bool, string> onComplete = (success, str) =>
  75. {
  76. isCompleted = true;
  77. if (success) result = str;
  78. };
  79. EditorCoroutineUtility.StartCoroutine(Request(message, onComplete, null), this);
  80. while (!isCompleted)
  81. {
  82. await Task.Delay(10);
  83. }
  84. return result;
  85. }
  86. private IEnumerator Request(string input, Action<bool, string> onComplete, Action<float> onProgressUpdate)
  87. {
  88. var msg = new Message()
  89. {
  90. role = UserId,
  91. content = input,
  92. };
  93. requestData.AppendChat(msg);
  94. messageHistory.Add(msg);
  95. using (webRequest = new UnityWebRequest(ChatgptUrl, "POST"))
  96. {
  97. var jsonDt = UtilityBuiltin.Json.ToJson(requestData);
  98. Debug.Log(jsonDt);
  99. byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonDt);
  100. webRequest.uploadHandler = new UploadHandlerRaw(bodyRaw);
  101. webRequest.downloadHandler = new DownloadHandlerBuffer();
  102. webRequest.SetRequestHeader("Content-Type", "application/json");
  103. webRequest.SetRequestHeader("Authorization", $"Bearer {this.ApiKey}");
  104. webRequest.certificateHandler = new ChatGPTWebRequestCert();
  105. var req = webRequest.SendWebRequest();
  106. while (!webRequest.isDone)
  107. {
  108. onProgressUpdate?.Invoke((webRequest.downloadProgress + webRequest.uploadProgress) / 2f);
  109. yield return null;
  110. }
  111. if (webRequest.result != UnityWebRequest.Result.Success)
  112. {
  113. Debug.LogError($"---------ChatGPT请求失败:{webRequest.error}---------");
  114. onComplete?.Invoke(false, string.Empty);
  115. }
  116. else
  117. {
  118. var json = webRequest.downloadHandler.text;
  119. Debug.Log(json);
  120. try
  121. {
  122. ChatCompletion result = UtilityBuiltin.Json.ToObject<ChatCompletion>(json);
  123. int lastChoiceIdx = result.choices.Count - 1;
  124. var replyMsg = result.choices[lastChoiceIdx].message;
  125. replyMsg.content = replyMsg.content.Trim();
  126. messageHistory.Add(replyMsg);
  127. onComplete?.Invoke(true, replyMsg.content);
  128. }
  129. catch (System.Exception e)
  130. {
  131. Debug.LogError($"---------ChatGPT返回数据解析失败:{e.Message}---------");
  132. onComplete?.Invoke(false, e.Message);
  133. }
  134. }
  135. webRequest.Dispose();
  136. webRequest = null;
  137. }
  138. }
  139. public void NewChat()
  140. {
  141. requestData.ClearChat();
  142. messageHistory.Clear();
  143. }
  144. public bool IsSelfMessage(Message msg)
  145. {
  146. return this.UserId.CompareTo(msg.role) == 0;
  147. }
  148. }
  149. class ChatGPTRequestData
  150. {
  151. public List<Message> messages;
  152. public string model;
  153. public float temperature;
  154. public ChatGPTRequestData(string model, float temper)
  155. {
  156. this.model = model;
  157. this.temperature = temper;
  158. this.messages = new List<Message>();
  159. }
  160. /// <summary>
  161. /// 同一话题追加会话内容
  162. /// </summary>
  163. /// <param name="chatMsg"></param>
  164. /// <returns></returns>
  165. public ChatGPTRequestData AppendChat(Message msg)
  166. {
  167. this.messages.Add(msg);
  168. return this;
  169. }
  170. /// <summary>
  171. /// 清除聊天历史(结束一个话题), 相当于新建一个聊天话题
  172. /// </summary>
  173. public void ClearChat()
  174. {
  175. this.messages.Clear();
  176. }
  177. }
  178. class ChatGPTWebRequestCert : UnityEngine.Networking.CertificateHandler
  179. {
  180. protected override bool ValidateCertificate(byte[] certificateData)
  181. {
  182. //return base.ValidateCertificate(certificateData);
  183. return true;
  184. }
  185. }
  186. class Usage
  187. {
  188. public int prompt_tokens;
  189. public int completion_tokens;
  190. public int total_tokens;
  191. }
  192. public class Message
  193. {
  194. public string role;
  195. public string content;
  196. }
  197. class Choice
  198. {
  199. public Message message;
  200. public string finish_reason;
  201. public int index;
  202. }
  203. class ChatCompletion
  204. {
  205. public string id;
  206. public string @object;
  207. public int created;
  208. public string model;
  209. public Usage usage;
  210. public List<Choice> choices;
  211. }
  212. }

使用方法一, 同步获取结果:

  1. var ai = new ChatGPT();
  2. var str = await ai.SendAsync("你好");
  3. Debug.Log(str);

 使用方法二, 异步获取结果:

new ChatGPT().Send("你好", (success, message) => { if (success) Debug.Log(message); }, requestProgress => { Debug.Log($"Request progress:{requestProgress}"); });

三,基于上面的类实现一个AI聊天窗口

为什么要写个聊天窗口:

1. https://chat.openai.com/chat 网页版登陆锁IP,并且会验证时区,如果用美国的节点时区时间对不上拒绝登录。

2. 虽然登录成功后不科学也能用,但是同一话题很快就会超时无法应答,刷新界面或新建话题才能正常使用,总之非常鸡肋。

而通过开放接口就没有这些问题,API请求会更加爽快。

聊天窗口功能设计:

1. 需要一个滚动列表展示双方对话记录,对话文本内容支持选择复制。

2. 问题输入框和发送按钮、新建话题(清除话题对话)

3. 对话历史存档。

代码实现,比较简单就不解释了,直接上源码:

  1. using System;
  2. using UnityEditor;
  3. using UnityEngine;
  4. namespace UnityGameFramework.Editor.AIAssistant
  5. {
  6. [EditorToolMenu("AI助手/ChatGPT", 5)]
  7. public class ChatGPTWindow : EditorToolBase
  8. {
  9. public override string ToolName => "ChatGPT";
  10. Vector2 scrollPos = Vector2.zero;
  11. ChatGPT ai;
  12. private bool settingFoldout = false;
  13. string message;
  14. const string aiRoleName = "AI";
  15. private float chatBoxWidthRatio = 0.85f;
  16. private float iconSizeRatio = 0.6f;
  17. private float chatBoxPadding = 20;
  18. private float chatBoxEdgePadding = 10;
  19. GUIStyle myChatStyle;
  20. GUIStyle aiChatStyle;
  21. GUIStyle aiIconStyle;
  22. GUIStyle myIconStyle;
  23. GUIStyle txtAreaStyle;
  24. GUIContent chatContent;
  25. bool isEditorInitialized = false;
  26. private float scrollViewHeight;
  27. private void OnEnable()
  28. {
  29. EditorApplication.update += OnEditorUpdate;
  30. ai = new ChatGPT(AppBuildSettings.Instance.ChatGPTKey);
  31. ai.ChatGPTRandomness = AppBuildSettings.Instance.ChatGPTRandomness;
  32. chatContent = new GUIContent();
  33. ai.RestoreChatHistory();
  34. }
  35. private void OnEditorUpdate()
  36. {
  37. if (EditorApplication.isCompiling || EditorApplication.isUpdating)
  38. {
  39. return;
  40. }
  41. try
  42. {
  43. InitGUIStyles();
  44. isEditorInitialized = true;
  45. EditorApplication.update -= OnEditorUpdate;
  46. }
  47. catch (Exception)
  48. {
  49. }
  50. }
  51. private void InitGUIStyles()
  52. {
  53. aiChatStyle = new GUIStyle(EditorStyles.selectionRect);
  54. aiChatStyle.wordWrap = true;
  55. aiChatStyle.normal.textColor = Color.white;
  56. aiChatStyle.fontSize = 18;
  57. aiChatStyle.alignment = TextAnchor.MiddleLeft;
  58. myChatStyle = new GUIStyle(EditorStyles.helpBox);
  59. myChatStyle.wordWrap = true;
  60. myChatStyle.normal.textColor = Color.white;
  61. myChatStyle.fontSize = 18;
  62. myChatStyle.alignment = TextAnchor.MiddleLeft;
  63. txtAreaStyle = new GUIStyle(EditorStyles.textArea);
  64. txtAreaStyle.fontSize = 18;
  65. aiIconStyle = new GUIStyle();
  66. aiIconStyle.wordWrap = true;
  67. aiIconStyle.alignment = TextAnchor.MiddleCenter;
  68. aiIconStyle.fontSize = 18;
  69. aiIconStyle.fontStyle = FontStyle.Bold;
  70. aiIconStyle.normal.textColor = Color.black;
  71. aiIconStyle.normal.background = EditorGUIUtility.FindTexture("sv_icon_dot5_pix16_gizmo");
  72. myIconStyle = new GUIStyle(aiIconStyle);
  73. myIconStyle.normal.background = EditorGUIUtility.FindTexture("sv_icon_dot2_pix16_gizmo");
  74. }
  75. private void OnDisable()
  76. {
  77. ai.SaveChatHistory();
  78. }
  79. private void OnGUI()
  80. {
  81. if (!isEditorInitialized) return;
  82. EditorGUILayout.BeginVertical();
  83. {
  84. scrollPos = EditorGUILayout.BeginScrollView(scrollPos);
  85. {
  86. scrollViewHeight = 0;
  87. foreach (var msg in ai.MessageHistory)
  88. {
  89. var msgRect = EditorGUILayout.BeginVertical();
  90. {
  91. EditorGUILayout.BeginHorizontal();
  92. {
  93. bool isMyMsg = ai.IsSelfMessage(msg);
  94. var labelStyle = isMyMsg ? myChatStyle : aiChatStyle;
  95. chatContent.text = msg.content;
  96. float chatBoxWidth = this.position.width * chatBoxWidthRatio;
  97. float iconSize = (this.position.width - chatBoxWidth) * iconSizeRatio;
  98. float chatBoxHeight = Mathf.Max(iconSize, chatBoxEdgePadding + labelStyle.CalcHeight(chatContent, chatBoxWidth - chatBoxEdgePadding));
  99. if (isMyMsg) { GUILayout.FlexibleSpace(); }
  100. else
  101. {
  102. EditorGUILayout.LabelField(aiRoleName, aiIconStyle, GUILayout.Width(iconSize), GUILayout.Height(iconSize));
  103. }
  104. EditorGUILayout.SelectableLabel(msg.content, labelStyle, GUILayout.Width(chatBoxWidth), GUILayout.Height(chatBoxHeight));
  105. if (!isMyMsg) { GUILayout.FlexibleSpace(); }
  106. else
  107. {
  108. EditorGUILayout.LabelField(msg.role, myIconStyle, GUILayout.Width(iconSize), GUILayout.Height(iconSize));
  109. }
  110. EditorGUILayout.EndHorizontal();
  111. }
  112. EditorGUILayout.EndVertical();
  113. }
  114. EditorGUILayout.Space(chatBoxPadding);
  115. scrollViewHeight += msgRect.height;
  116. }
  117. EditorGUILayout.EndScrollView();
  118. }
  119. if (ai.IsRequesting)
  120. {
  121. var barWidth = position.width * 0.8f;
  122. var pBarRect = new Rect((position.width - barWidth) * 0.5f, (position.height - 30f) * 0.5f, barWidth, 30f);
  123. EditorGUI.ProgressBar(pBarRect, ai.RequestProgress, $"请求进度:{ai.RequestProgress:P2}");
  124. }
  125. GUILayout.FlexibleSpace();
  126. if (settingFoldout = EditorGUILayout.Foldout(settingFoldout, "展开设置项:"))
  127. {
  128. EditorGUILayout.BeginVertical("box");
  129. {
  130. EditorGUILayout.BeginHorizontal();
  131. {
  132. EditorGUILayout.LabelField("ChatGPT API Key:", GUILayout.Width(170));
  133. AppBuildSettings.Instance.ChatGPTKey = EditorGUILayout.TextField(AppBuildSettings.Instance.ChatGPTKey);
  134. EditorGUILayout.EndHorizontal();
  135. }
  136. EditorGUILayout.BeginHorizontal();
  137. {
  138. EditorGUILayout.LabelField("结果随机性:", GUILayout.Width(170));
  139. ai.ChatGPTRandomness = AppBuildSettings.Instance.ChatGPTRandomness = EditorGUILayout.Slider(AppBuildSettings.Instance.ChatGPTRandomness, 0, 2);
  140. EditorGUILayout.EndHorizontal();
  141. }
  142. EditorGUILayout.EndVertical();
  143. }
  144. }
  145. //EditorGUILayout.LabelField(scrollPos.ToString());
  146. EditorGUILayout.BeginHorizontal();
  147. {
  148. message = EditorGUILayout.TextArea(message, txtAreaStyle, GUILayout.MinHeight(80));
  149. EditorGUI.BeginDisabledGroup(ai.IsRequesting);
  150. {
  151. if (GUILayout.Button("发送消息", GUILayout.MaxWidth(120), GUILayout.Height(80)))
  152. {
  153. if (!string.IsNullOrWhiteSpace(message))
  154. {
  155. ai.Send(message, OnChatGPTMessage);
  156. }
  157. }
  158. if (GUILayout.Button("新话题", GUILayout.MaxWidth(80), GUILayout.Height(80)))
  159. {
  160. ai.NewChat();
  161. }
  162. EditorGUI.EndDisabledGroup();
  163. }
  164. EditorGUILayout.EndHorizontal();
  165. }
  166. EditorGUILayout.EndVertical();
  167. }
  168. }
  169. private void OnChatGPTMessage(bool arg1, string arg2)
  170. {
  171. scrollPos.y = scrollViewHeight;
  172. if (arg1)
  173. {
  174. message = string.Empty;
  175. }
  176. Repaint();
  177. }
  178. }
  179. }

添加代码后,Toolbar的Tools工具栏会自动识别这个工具菜单,点击即可打开AI对话窗口:

 AI的应用展望:

下一步就是为GPT赋予双手,添加各种指令Handler。比如生成json文件、生成语言国际化Excel文件、修改优化代码、生成代码文件、生成Shader、一键拼UI等等。

分为三个模块:

1. 描述文本(发送给GPT)

2. 动态追加描述内容:有时需要追加一些文本数据,比如让GPT优化一段代码,需要动态追加把代码补充到描述文本。

3. 结果解析。得到目标结果,使用Handler解析GPT返回结果,达成某种功能。GPT返回的代码或json等数据都会用标签包住,通过解析标签就可以生成各种类型的文件。

最终就可以不用写程序,只写问题的描述文本,确保能从GPT得到满意答案就可以实现某项功能。想要新增新的工具,扩展新的功能只需要在文件中添加修改问题描述文本即可。

比如语言国际化,我通过问题描述让GPT从工程代码中扫描所有语言本地化函数GF.Localization.GetText()传入的国际化文本,并且把文本翻译成中文,把结果以key,value键值对保存输出json文件。最后我得到了GPT返回的诸如{"Hello":”你好“}的所有国际化文本和翻译结果。

然后我只需要解析GPT返回结果,生成语言国际化文件到工程中就完成了AI自动处理语言国际化的问题。

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

闽ICP备14008679号