modify this unity script so it does not need a api key using UnityEngine; using TMPro;…
- User
modify this unity script so it does not need a api key using UnityEngine; using TMPro; using UnityEngine.UI; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.Threading.Tasks; using UnityEngine.Networking; using Newtonsoft.Json; using System;
public class AdvancedAIScript : MonoBehaviour { [Header("UI Elements")] public TMP_InputField userInputField; public TMP_Text aiResponseText; public Button submitButton; public ScrollRect scrollRect; // To auto-scroll to the latest response
[Header("AI Settings")] public int MemoryLimit = 20; // Increased to store more exchanges public int TopicMemoryLimit = 5; // Memory for current topic private Queue<string> memory = new Queue<string>(); // General memory for context private Queue<string> topicMemory = new Queue<string>(); // Memory for topic tracking // DeepInfra API Settings [Header("DeepInfra API Settings")] [Tooltip("Your DeepInfra API Key")] public string deepInfraApiKey; [Tooltip("The model to use (e.g., gpt-4, gpt-3.5-turbo)")] public string deepInfraModel = "gpt-4"; // API Endpoint private const string API_URL = "https://api.deepinfra.com/v1/openai/chat/completions"; private void Start() { submitButton.onClick.AddListener(OnSubmit); } private void OnSubmit() { string userInput = userInputField.text.Trim(); if (string.IsNullOrEmpty(userInput)) return; AddToMemory($"User: {userInput}"); userInputField.text = string.Empty; // Process user input asynchronously ProcessUserInputAsync(userInput); } private void AddToMemory(string text) { if (memory.Count >= MemoryLimit) memory.Dequeue(); // Remove oldest memory memory.Enqueue(text); } private void AddToTopicMemory(string topic) { if (topicMemory.Count >= TopicMemoryLimit) topicMemory.Dequeue(); // Remove oldest topic topicMemory.Enqueue(topic); } private async void ProcessUserInputAsync(string input) { // Construct the conversation history string conversation = ConstructConversation(); // Send the conversation to DeepInfra's API and get the response string aiResponse = await GetAIResponse(conversation); if (string.IsNullOrEmpty(aiResponse)) { aiResponse = "I'm sorry, I couldn't process that."; } // Add AI response to memory AddToMemory($"AI: {aiResponse}"); // Display the response aiResponseText.text += $"AI: {aiResponse}\n"; ScrollToBottom(); } private string ConstructConversation() { // Join the memory queue into a list of messages List<OpenAIMessage> messages = new List<OpenAIMessage> { new OpenAIMessage { role = "system", content = "You are a helpful assistant." } }; foreach (var line in memory) { if (line.StartsWith("User: ")) { messages.Add(new OpenAIMessage { role = "user", content = line.Substring(6) }); } else if (line.StartsWith("AI: ")) { messages.Add(new OpenAIMessage { role = "assistant", content = line.Substring(4) }); } } // Serialize messages to JSON var payload = new OpenAIRequest { model = deepInfraModel, messages = messages }; return JsonConvert.SerializeObject(payload); } private async Task<string> GetAIResponse(string jsonPayload) { using (UnityWebRequest request = new UnityWebRequest(API_URL, "POST")) { byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(jsonPayload); request.uploadHandler = new UploadHandlerRaw(bodyRaw); request.downloadHandler = new DownloadHandlerBuffer(); request.SetRequestHeader("Content-Type", "application/json"); request.SetRequestHeader("Authorization", $"Bearer {deepInfraApiKey}"); // Send the request and wait for a response var operation = request.SendWebRequest(); - Assistant