using OpenAI_API;
using OpenAI_API.Chat;
using OpenAI_API.Completions;
using System;
using System.Threading.Tasks;

public class ChatGPT
{
    private string openAIApiKey;
    private Conversation chat;
    public ChatGPT (string apiKey)
    {
        this.openAIApiKey = apiKey;
    }
    public async Task createConversation ()
    {
        APIAuthentication aPIAuthentication = new APIAuthentication(openAIApiKey);
        OpenAIAPI openAIAPI = new OpenAIAPI(aPIAuthentication);
        try
        {
            string prompt = "\"You are a teacher who helps children understand if things are animals or not.  If the user tells you an animal, you say \\\"yes\\\".  If the user tells you something that is not an animal, you say \\\"no\\\".  You only ever respond with \\\"yes\\\" or \\\"no\\\".  You do not say anything else.\"";
            string model = "text-davinci-003";
            int maxTokens = 50;
            var completionRequest = new CompletionRequest
            {
                Prompt = prompt,
                Model = model,
                MaxTokens = maxTokens
            };
            var completionResults = await openAIAPI.Completions.CreateCompletionAsync(completionRequest);
            var generateText = completionResults.Completions[0].Text;
            this.chat = openAIAPI.Chat.CreateConversation();
            chat.AppendUserInput("Is this an animal? Cat");
            chat.AppendExampleChatbotOutput("Yes");
            chat.AppendUserInput("Is this an animal? House");
            chat.AppendExampleChatbotOutput("No");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error: {ex.Message}");
        }
    }
    public async Task<string> askGPT(string question)
    {
        try
        {
            chat.AppendUserInput(question);
            string response = await chat.GetResponseFromChatbotAsync();
            return response;
        }
        catch(Exception ex)
        {
            Console.WriteLine($"Error: {ex.Message}");
            return null;
        }
    }
}


