unity上でストリームのchatgpt apiを叩きたい

実現したいこと

unity上でストリームのchatgpt apiを叩きたい

前提

unityでChatGPT APIを用いてNPCと対話できるゲームを作りたいのですが、streamでAPIを叩くにはどうすればよろしいでしょうか。
一応、ChatGPTに聞いたソースコードでやってみた所、非streamであればできました。しかし、streamにしてほしいとお願いしてみた所できませんでした。

発生している問題・エラーメッセージ

該当のソースコード

非streamのソースコード
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using TMPro;

public class GPTManager3 : MonoBehaviour
{
#region 必要なクラスの定義など
[System.Serializable]
public class MessageModel
{
public string role;
public string content;
}
[System.Serializable]
public class CompletionRequestModel
{
public string model;
public List<MessageModel> messages;
}

[System.Serializable] public class ChatGPTRecieveModel { public string id; public string @object; public int created; public Choice[] choices; public Usage usage; [System.Serializable] public class Choice { public int index; public MessageModel message; public string finish_reason; } [System.Serializable] public class Usage { public int prompt_tokens; public int completion_tokens; public int total_tokens; } } #endregion public TMP_InputField inputField; public TMP_Text text; private MessageModel assistantModel = new() { role = "system", content = "こんにちは" }; private readonly string apiKey = "sk-○○"; private List<MessageModel> communicationHistory = new(); private void Start() { // inputField への参照を取得する inputField = GetComponent<TMP_InputField>(); text = GameObject.Find("Text").GetComponent<TMP_Text>(); // OnEndEdit イベントにメソッドを登録する inputField.onEndEdit.AddListener(OnEndEdit); } private void OnEndEdit(string userMessage) { communicationHistory.Add(assistantModel);//role:systemの内容をリストに追加 // string userMessage = inputField.text; MessageSubmit(userMessage); } private void Communication(string newMessage, Action<MessageModel> result) { Debug.Log(newMessage); communicationHistory.Add(new MessageModel()//ユーザーのメッセージをリストに追加 { role = "user", content = newMessage }); var apiUrl = "https://api.openai.com/v1/chat/completions"; var jsonOptions = JsonUtility.ToJson( new CompletionRequestModel() { model = "gpt-3.5-turbo", messages = communicationHistory//これまでのリストの内容をmessagesに格納 }, true); var headers = new Dictionary<string, string> { {"Authorization", "Bearer " + apiKey}, {"Content-type", "application/json"}, }; var request = new UnityWebRequest(apiUrl, "POST") { uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(jsonOptions)), downloadHandler = new DownloadHandlerBuffer() }; foreach (var header in headers) { request.SetRequestHeader(header.Key, header.Value); } var operation = request.SendWebRequest(); operation.completed += _ => { if (operation.webRequest.result == UnityWebRequest.Result.ConnectionError || operation.webRequest.result == UnityWebRequest.Result.ProtocolError) { Debug.LogError(operation.webRequest.error); throw new Exception(); } else { var responseString = operation.webRequest.downloadHandler.text; var responseObject = JsonUtility.FromJson<ChatGPTRecieveModel>(responseString); communicationHistory.Add(responseObject.choices[0].message); //Debug.Log(responseObject.choices[0].message.content); text.text = responseObject.choices[0].message.content; //text.text=responseText; } request.Dispose(); }; } public void MessageSubmit(string sendMessage) { Communication(sendMessage, (result) => { //Debug.Log(result.content); text.text = result.content; }); }

}

試したこと

chatgptに聞く
ネットで検索

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

コメントを投稿

0 コメント