Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

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... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Marketing The New Profit Frontier

Authors: Ed Burnett

1st Edition

0964535629, 978-0964535626

More Books

Students also viewed these Databases questions

Question

4. Does cultural aptitude impact ones emotional intelligence?

Answered: 1 week ago