gpt4 book ai didi

c# - 如何在Unity中实现ChatGPT?

转载 作者:行者123 更新时间:2023-12-02 05:49:35 28 4
gpt4 key购买 nike

我目前正在尝试使用 C# 将 ChatGPT 的功能实现到我的 Unity 项目中。

我有 JSON 类来包装和解开我的请求,并且我成功地实现了它,这样每当我发送请求时我都会收到响应。问题是我得到的回应完全是随机的。例如,我会问“什么是动词?”它会给我一个回应,告诉我有助于成功播客的因素。不确定我的配置是否错误或到底发生了什么,所以我将发布下面的类。

请求类:

namespace OpenAIAPIManagement
{
[Serializable]
public class OpenAIAPIRequest
{
public string model = "gpt-3.5-turbo";
public Message[] messages;
public float temperature = 0.5f;
public int max_tokens = 50;
public float top_p = 1f;
public float presence_penalty = 0f;
public float frequency_penalty = 0f;

public OpenAIAPIRequest(string model_, Message[] messages_, float temperature_, int max_tokens_, float top_p_, float presence_penalty_, float frequency_penalty_)
{
this.model = model_;
this.messages = messages_;
this.temperature = temperature_;
this.max_tokens = max_tokens_;
this.top_p = top_p_;
this.presence_penalty = presence_penalty_;
this.frequency_penalty = frequency_penalty_;
}
}

[Serializable]
public class Message
{
public string role = "user";
public string content = "What is your purpose?";

public Message(string role_, string content_)
{
this.role = role_;
this.content = content_;
}
}
}

我发送回复的方式:

public static async Task<Message> SendMessageToChatGPT(Message[] message, float temperature, int max_tokens, float top_p, float presence_penalty, float frequency_penalty)
{
string request = OpenAIAPIManager.SerializeAPIRequest("gpt-4", message, temperature, max_tokens, top_p, presence_penalty, frequency_penalty);

HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {_apiKey}");
HttpResponseMessage response = await client.PostAsync(_apiURL, new StringContent(request, System.Text.Encoding.UTF8, "application/json"));

if (response.IsSuccessStatusCode)
{
Message responseMessage = OpenAIAPIManager.DeserializeAPIResponse(await response.Content.ReadAsStringAsync()).choices[0].message;
Debug.Log("ChatGPT: " + responseMessage.content);
return await Task.FromResult<Message>(responseMessage);
}
else
{
return await Task.FromResult<Message>(new Message("Error", "Status" + response.StatusCode));
}
}

最后将字符串从文本字段中取出:

public async void ProcessMessageFromInputField()
{
if (_userInput && !string.IsNullOrWhiteSpace(_userInput.text))
{
_chatData.Clear();
_chatData.Add(_userInput.text + _userPostfix);
PostMessageToContentPanel(_chatData[0]);
_userInput.text = "";
Message userMessage = new Message("user", _userInput.text);
Message chatAgentResponse = await OpenAIAPIManager.SendMessageToChatGPT(new Message[]{userMessage}, 0.7f, 256, 1f, 0f, 0f);
PostMessageToContentPanel(chatAgentResponse.content + _aiPostfix);
}
}

我已阅读 API 并尽我所能配置它,但如果我遗漏了某些内容。

最佳答案

您需要提供提示,告诉 AI 模型您希望它与您进行哪种对话。目前您只是一次向 ChatGPT API 发送一条消息:

Message chatAgentResponse = await OpenAIAPIManager.SendMessageToChatGPT(new Message[]{userMessage}, 0.7f, 256, 1f, 0f, 0f);

您正在初始化一个新的消息列表作为每个请求的一部分,它仅包含您想要发送到聊天模型的消息。您应该制作一条包含“系统”角色的消息,解释您希望人工智能完成哪种对话,例如

Message promptMessage = new Message("system", "You are an AI participant in a chess game. Your opponent is having a conversation with you. Respond professionally as though you are taking part in a renowned international chess tournament, and there is a significant amount of publicity surrounding this match. The whole world is watching.");
Message[] messages = {promptMessage, userMessage};
Message chatAgentResponse = await OpenAIAPIManager.SendMessageToChatGPT(messages, 0.7f, 256, 1f, 0f, 0f);

要让 AI 模型继续对话并记住已经说过的话,您需要将 API 的响应消息附加到此列表,然后附加用户的回复,并继续发送完整列表要求。请查看此处的聊天完成指南,示例 API 调用很好地演示了这一点: https://platform.openai.com/docs/guides/chat/introduction

但是,您还需要了解您选择的模型可以使用的最大标记数(基本上是单词,但不完全是)。随着对话的发展,这个数字会越来越大,最终你会用完:https://platform.openai.com/docs/api-reference/chat/create#chat/create-max_tokens 。例如,gpt-4-32k 支持的 token 数量是 gpt-3.5-turbo 的 8 倍,但尚未公开,并且在发布后价格会贵得多它最初被释放。 https://platform.openai.com/docs/models/gpt-4

关于c# - 如何在Unity中实现ChatGPT?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75906752/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com