Question: using UnityEngine; using UnityEngine.UI; using System.Collections.Generic; using System.Threading.Tasks; using OpenAI; namespace OpenAI { public class ChatGPT : MonoBehaviour { [ SerializeField ] private InputField inputField;

using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using System.Threading.Tasks;
using OpenAI;
namespace OpenAI
{
public class ChatGPT : MonoBehaviour
{
[SerializeField] private InputField inputField;
[SerializeField] private Button button;
[SerializeField] private ScrollRect scroll;
[SerializeField] private RectTransform sent;
[SerializeField] private RectTransform received;
private float height;
private OpenAIApi openai;
private string apiKey ="sk-proj-NalBPSBWHIWpPWGlbEl6T3BlbkFJZYw2EGKEWNQijyAmFkTa"; // API anahtarnz buraya yazn
private List messages = new List();
private string prompt = "you're a python teaching assistant. you have mastered the rules of python.You will answer the questions asked to you clearly and clearly according to the oython rules.";
private void Start()
{
openai = new OpenAIApi(apiKey);
button.onClick.AddListener(SendReply);
}
private void AppendMessage(ChatMessage message)
{
scroll.content.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 0);
var item = Instantiate(message.Role == "user" ? sent : received, scroll.content);
item.GetChild(0).GetChild(0).GetComponent().text = message.Content;
item.anchoredPosition = new Vector2(0,-height);
LayoutRebuilder.ForceRebuildLayoutImmediate(item);
height += item.sizeDelta.y;
scroll.content.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height);
scroll.verticalNormalizedPosition =0;
}
private async void SendReply()
{
var newMessage = new ChatMessage()
{
Role = "user",
Content = inputField.text
};
AppendMessage(newMessage);
if (messages.Count ==0) newMessage.Content = prompt +"
"+ inputField.text;
messages.Add(newMessage);
button.enabled = false;
inputField.text ="";
inputField.enabled = false;
// Dosya aramas iin API istei oluturma
var fileSearchRequest = new FileSearchRequest()
{
Model ="gpt-4-turbo",
Query = "search query", // Arama sorgusu buraya yazlmal
MaxExamples =5// En fazla karnek alnacan belirtin
};
var fileSearchResponse = await openai.FileSearch(fileSearchRequest);
// Dosya arama sonularn ileme ve sohbet akna ekleyerek gsterme
foreach (var result in fileSearchResponse.Data)
{
var fileSearchMessage = new ChatMessage()
{
Role = "assistant",
Content = result.Text
};
AppendMessage(fileSearchMessage);
}
button.enabled = true;
inputField.enabled = true;
}
}
} FileSearchRequest CS0246

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

The question appears to provide a block of C code for a Unity application designed to interact with the OpenAI API specifically mimicking a chat inter... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!