unity3d 如何在Unity中实现ChatGPT?

vddsk6oq  于 2023-04-07  发布在  其他
关注(0)|答案(1)|浏览(299)

我目前正在尝试使用C#将ChatGPT的功能实现到我的Unity项目中。
我有JSON类来 Package 我的请求和展开它,我成功地实现了它,所以每当我发送请求时,我都会得到响应。问题是我得到的响应是完全随机的。例如,我会问它“什么是动词?”它会给予我一个回答,告诉我一个成功的播客的因素。不知道我的配置是不是错了,或者到底是怎么回事,所以我会在下面贴出这些类。
请求类:

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并尽我所能配置了它,但如果我错过了什么。

kb5ga3dv

kb5ga3dv1#

你需要提供一个提示,告诉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倍,但尚未公开,并且在最初发布时将更加昂贵。

相关问题