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

using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using System.Threading.Tasks;
using OpenAI;
namespace OpenAl
{
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
-
NalBPSBWHIWpPWGIbEl
6
T
3
BIbkFJZ
/
w
2
EGKEWNQijyAmFkTa";
/
/
Your API key here
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 python 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 Vector
2
(
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;
/
/
Create API request for file search
var fileSearchRequest
=
new FileSearchRequest
{
Model
=
"
gpt
-
4
-
turbo",
Query
=
"search query",
/
/
Set your search query here
MaxExamples
=
5
/
/
Set maximum number of examples to retrieve
}
;
var fileSearchResponse
=
await openai.FileSearch
(
fileSearchRequest
)
;
/
/
Process file search results and display in chat flow
foreach
(
var result in fileSearchResponse.Data
)
{
var fileSearchMessage
=
new ChatMessage
{
Role
=
"assistant", Content
=
result.Text
}
;
AppendMessage
(
fileSearchMessage
)
;
}
button.enabled
=
true;
inputField.enabled
=
true;
}
}
}
i want to write the code I threw at the beginning like the code I threw at the end, will you make the necessary arrangementsusing UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System.IO;
using System.Collections;
using OpenAI;
public class FileManager : MonoBehaviour
{
public InputField inputField;
public Text outputText;
private string apiKey = "your_api_key_here"; // OpenAI API anahtarnz buraya ekleyin
private OpenAIApi openai;
void Start()
{
openai = new OpenAIApi(apiKey);
}
public async void LoadAndAnswerQuestions()
{
string filePath = Path.Combine(Application.persistentDataPath, "savedFile.txt");
// Dosya var m kontrol et
if (File.Exists(filePath))
{
// Dosyay oku
string loadedText = File.ReadAllText(filePath);
// Okunan metni ekrana yazdr
outputText.text = loadedText;
// OpenAI API'si ile metni analiz edip sorular yantla
var completionResponse = await openai.CreateCompletion(new CreateCompletionRequest
{
Model = "text-davinci-003",// Kullanmak istediiniz modeli sein
Prompt = loadedText,
MaxTokens =100
});
// Yantlar al ve ekrana yazdr
foreach (var choice in completionResponse.Choices)
{
outputText.text +="
"+ choice.Text;
}
Debug.Log("File loaded from: "+ filePath);
}
else
{
Debug.LogWarning("File not found at path: "+ filePath);
}
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock 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!