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();

#if UNITY_2020_1_OR_NEWER
            while (!operation.isDone)
                await Task.Yield();
#else
            while (!operation.isDone)
                await Task.Delay(100);
#endif

            if (request.result == UnityWebRequest.Result.ConnectionError ||
                request.result == UnityWebRequest.Result.ProtocolError)
            {
                Debug.LogError($"Error: {request.error}");
                return null;
            }
            else
            {
                // Parse the response
                try
                {
                    var response = JsonConvert.DeserializeObject<DeepInfraResponse>(request.downloadHandler.text);
                    if (response != null && response.choices != null && response.choices.Count > 0)
                    {
                        return response.choices[0].message.content.Trim();
                    }
                    else
                    {
                        Debug.LogWarning("No response received from DeepInfra.");
                        return null;
                    }
                }
                catch (Exception ex)
                {
                    Debug.LogError($"Failed to parse AI response: {ex.Message}");
                    return null;
                }
            }
        }
    }

    private void ScrollToBottom()
    {
        Canvas.ForceUpdateCanvases();
        scrollRect.verticalNormalizedPosition = 0f;
    }
}

// OpenAI API Request Models
[Serializable]
public class OpenAIRequest
{
    public string model;
    public List<OpenAIMessage> messages;
}

[Serializable]
public class OpenAIMessage
{
    public string role; // "system", "user", "assistant"
    public string content;
}

// DeepInfra API Response Models
[Serializable]
public class DeepInfraResponse
{
    public string id;
    public string @object;
    public int created;
    public string model;
    public List<Choice> choices;
    public Usage usage;
}

[Serializable]
public class Choice
{
    public int index;
    public OpenAIMessage message;
    public string finish_reason;
}

[Serializable]
public class Usage
{
    public int prompt_tokens;
    public int completion_tokens;
    public int total_tokens;
}