我目前正在尝试使用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);
您正在初始化消息的新列表作为每个请求的一部分,它只包含您想要发送到聊天模型的消息。你应该用"系统"来构思信息。角色解释你想让AI完成什么样的对话,例如
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
支持的令牌数量是gpt-3.5-turbo
的8倍,但尚未公开可用,并且在最初发布时将会昂贵得多。https://platform.openai.com/docs/models/gpt-4