From 2932bece0af8717795594431f3df38f65b080f08 Mon Sep 17 00:00:00 2001 From: "Aaron (Qilong)" Date: Mon, 4 Mar 2024 23:30:52 -0500 Subject: [PATCH] Use OpenAI dependency instead of own RestSharp code --- .../ChatGPTClient.cs | 80 ------------------- .../DynamoAssistantViewExtension.csproj | 1 + .../DynamoAssistantWindowViewModel.cs | 19 +++-- 3 files changed, 15 insertions(+), 85 deletions(-) delete mode 100644 src/DynamoAssistantViewExtension/ChatGPTClient.cs diff --git a/src/DynamoAssistantViewExtension/ChatGPTClient.cs b/src/DynamoAssistantViewExtension/ChatGPTClient.cs deleted file mode 100644 index c4897e2..0000000 --- a/src/DynamoAssistantViewExtension/ChatGPTClient.cs +++ /dev/null @@ -1,80 +0,0 @@ -using System; -using Newtonsoft.Json; -using RestSharp; - -namespace DynamoAssistant -{ - public class ChatGPTClient - { - private readonly string _apiKey; - private readonly RestClient _client; - private string _conversationHistory = string.Empty; - - // Constructor that takes the API key as a parameter - public ChatGPTClient(string apiKey) - { - _apiKey = apiKey; - // Initialize the RestClient with the ChatGPT API endpoint - _client = new RestClient("https://api.openai.com/v1/chat/completions"); - } - - // We'll add methods here to interact with the API. - // Method to send a message to the ChatGPT API and return the response - public string SendMessage(string message) - { - // Check for empty input - if (string.IsNullOrWhiteSpace(message)) - { - return "Sorry, I didn't receive any input. Please try again!"; - } - - try - { - // Update the conversation history with the user's message - _conversationHistory += $"User: {message}\n"; - - // Create a new POST request - var request = new RestRequest("", Method.Post); - // Set the Content-Type header - // request.AddHeader("Content-Type", "application/json"); - // Set the Authorization header with the API key - request.AddHeader("Authorization", $"Bearer {_apiKey}"); - - // Create the request body with the message and other parameters - var requestBody = new - { - model = "tts-1", - prompt = message, - max_tokens = 100, - n = 1, - stop = (string)null, - temperature = 0.7, - }; - - // Add the JSON body to the request - request.AddJsonBody(JsonConvert.SerializeObject(requestBody)); - - // Execute the request and receive the response - var response = _client.Execute(request); - - // Deserialize the response JSON content - var jsonResponse = JsonConvert.DeserializeObject(response.Content ?? string.Empty); - - // Extract and return the chatbot's response text - string chatbotResponse = jsonResponse?.choices[0]?.text?.ToString()?.Trim() ?? string.Empty; - - // Update the conversation history with the chatbot's response - _conversationHistory += $"Chatbot: {chatbotResponse}\n"; - - return chatbotResponse; - } - catch (Exception ex) - { - // Handle any exceptions that may occur during the API request - Console.WriteLine($"Error: {ex.Message}"); - return "Sorry, there was an error processing your request. Please try again later."; - } - - } - } -} diff --git a/src/DynamoAssistantViewExtension/DynamoAssistantViewExtension.csproj b/src/DynamoAssistantViewExtension/DynamoAssistantViewExtension.csproj index 3626653..1e86dc1 100644 --- a/src/DynamoAssistantViewExtension/DynamoAssistantViewExtension.csproj +++ b/src/DynamoAssistantViewExtension/DynamoAssistantViewExtension.csproj @@ -13,6 +13,7 @@ + diff --git a/src/DynamoAssistantViewExtension/DynamoAssistantWindowViewModel.cs b/src/DynamoAssistantViewExtension/DynamoAssistantWindowViewModel.cs index 537c914..30054fd 100644 --- a/src/DynamoAssistantViewExtension/DynamoAssistantWindowViewModel.cs +++ b/src/DynamoAssistantViewExtension/DynamoAssistantWindowViewModel.cs @@ -1,6 +1,9 @@ using Dynamo.Core; using Dynamo.Extensions; using Dynamo.UI.Commands; +using OpenAI_API; +using OpenAI_API.Chat; +using OpenAI_API.Models; using System; using System.Collections.ObjectModel; using System.Windows.Input; @@ -11,8 +14,9 @@ public class DynamoAssistantWindowViewModel : NotificationObject, IDisposable { private string userInput; private readonly ReadyParams readyParams; - private readonly ChatGPTClient chatGPTClient; - private static readonly string apikey = "Your API"; + private readonly OpenAIAPI chatGPTClient; + private readonly Conversation conversation; + private static readonly string apikey = "Your API Key"; /// /// @@ -39,15 +43,20 @@ public DynamoAssistantWindowViewModel(ReadyParams p) readyParams = p; // Create a ChatGPTClient instance with the API key - chatGPTClient = new ChatGPTClient(apikey); + chatGPTClient = new OpenAIAPI(new APIAuthentication(apikey)); + // ChatGPT lets you start a new chat. + conversation = chatGPTClient.Chat.CreateConversation(); + conversation.Model = Model.DefaultChatModel; + conversation.RequestParameters.Temperature = 0; // Display a welcome message Messages.Add("Assistant:\nWelcome to Dynamo world and ask me anything to get started!"); } - internal void SendMessage(string msg) + internal async void SendMessage(string msg) { // Send the user's input to the ChatGPT API and receive a response - string response = chatGPTClient?.SendMessage(msg); + conversation?.AppendUserInput(msg); + string response = await conversation.GetResponseFromChatbotAsync(); // Display user message first Messages.Add("You:\n" + msg); // Display the chatbot's response