modify this unity c# script so it has the same api key as the html i made.…
- User
modify this unity c# script so it has the same api key as the html i made. AI LLM Chat - WebSim body { font-family: Arial, sans-serif; margin: 0; padding: 20px; background-color: #f0f0f0; } .container { max-width: 800px; margin: 0 auto; background-color: #ffffff; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); padding: 20px; } h1 { color: #333; text-align: center; } .model-select { width: 100%; padding: 10px; margin-bottom: 20px; border: 1px solid #ddd; border-radius: 4px; } .chat-container { height: 400px; overflow-y: auto; border: 1px solid #ddd; padding: 10px; margin-bottom: 20px; background-color: #f9f9f9; } .message { margin-bottom: 10px; padding: 8px; border-radius: 4px; } .user-message { background-color: #e1f5fe; text-align: right; } .ai-message { background-color: #f0f4c3; } .input-container { display: flex; } #user-input { flex-grow: 1; padding: 10px; border: 1px solid #ddd; border-radius: 4px 0 0 4px; } #send-button { padding: 10px 20px; background-color: #4CAF50; color: white; border: none; border-radius: 0 4px 4px 0; cursor: pointer; } #send-button:hover { background-color: #45a049; }
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