From 5db1e0958f4f3f6d9caa4fb42ef7625a045c0016 Mon Sep 17 00:00:00 2001 From: "Aaron (Qilong)" Date: Fri, 1 Mar 2024 15:36:11 -0500 Subject: [PATCH 01/15] Dynamo Copilot NET8 Migrated --- ...tViewExtension_ViewExtensionDefinition.xml | 4 + .../ChatGPTClient.cs | 80 +++++++++++++++++ .../DynamoAssistantViewExtension.cs | 86 +++++++++++++++++++ .../DynamoAssistantViewExtension.csproj | 31 +++++++ ...tViewExtension_ViewExtensionDefinition.xml | 4 + .../DynamoAssistantWindow.xaml | 84 ++++++++++++++++++ .../DynamoAssistantWindow.xaml.cs | 22 +++++ .../DynamoAssistantWindowViewModel.cs | 84 ++++++++++++++++++ .../Properties/AssemblyInfo.cs | 36 ++++++++ src/DynamoSamples.sln | 11 ++- 10 files changed, 441 insertions(+), 1 deletion(-) create mode 100644 dynamo_viewExtension/Sample View Extension/extra/DynamoAssistantViewExtension_ViewExtensionDefinition.xml create mode 100644 src/DynamoAssistantViewExtension/ChatGPTClient.cs create mode 100644 src/DynamoAssistantViewExtension/DynamoAssistantViewExtension.cs create mode 100644 src/DynamoAssistantViewExtension/DynamoAssistantViewExtension.csproj create mode 100644 src/DynamoAssistantViewExtension/DynamoAssistantViewExtension_ViewExtensionDefinition.xml create mode 100644 src/DynamoAssistantViewExtension/DynamoAssistantWindow.xaml create mode 100644 src/DynamoAssistantViewExtension/DynamoAssistantWindow.xaml.cs create mode 100644 src/DynamoAssistantViewExtension/DynamoAssistantWindowViewModel.cs create mode 100644 src/DynamoAssistantViewExtension/Properties/AssemblyInfo.cs diff --git a/dynamo_viewExtension/Sample View Extension/extra/DynamoAssistantViewExtension_ViewExtensionDefinition.xml b/dynamo_viewExtension/Sample View Extension/extra/DynamoAssistantViewExtension_ViewExtensionDefinition.xml new file mode 100644 index 0000000..3ec86e8 --- /dev/null +++ b/dynamo_viewExtension/Sample View Extension/extra/DynamoAssistantViewExtension_ViewExtensionDefinition.xml @@ -0,0 +1,4 @@ + + ..\bin\DynamoAssistantViewExtension.dll + DynamoAssistant.DynamoAssistantViewExtension + diff --git a/src/DynamoAssistantViewExtension/ChatGPTClient.cs b/src/DynamoAssistantViewExtension/ChatGPTClient.cs new file mode 100644 index 0000000..c4897e2 --- /dev/null +++ b/src/DynamoAssistantViewExtension/ChatGPTClient.cs @@ -0,0 +1,80 @@ +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.cs b/src/DynamoAssistantViewExtension/DynamoAssistantViewExtension.cs new file mode 100644 index 0000000..98c707b --- /dev/null +++ b/src/DynamoAssistantViewExtension/DynamoAssistantViewExtension.cs @@ -0,0 +1,86 @@ +using System.Windows.Controls; +using Dynamo.Wpf.Extensions; + +namespace DynamoAssistant +{ + /// + /// The View Extension framework for Dynamo allows you to extend + /// the Dynamo UI by registering custom MenuItems. A ViewExtension has + /// two components, an assembly containing a class that implements + /// IViewExtension or extends ViewExtensionBase, and a ViewExtensionDefinition + /// XML file used to instruct Dynamo where to find the class containing the + /// View Extension implementation. The ViewExtensionDefinition XML file must + /// be located in your [dynamo]\viewExtensions folder. + /// + /// This sample demonstrates a View Extension implementation which inherits from + /// ViewExtensionBase. It adds a user interface to Dynamo's extension sidebar + /// when its MenuItem is clicked. + /// The Window created tracks the number of nodes in the current workspace, + /// by handling the workspace's NodeAdded and NodeRemoved events. + /// + public class DynamoAssistantViewExtension : ViewExtensionBase + { + private MenuItem assistantMenuItem; + + public override void Dispose() + { + // Do nothing for now + } + + public override void Startup(ViewStartupParams p) + { + } + + public override void Loaded(ViewLoadedParams p) + { + // Save a reference to your loaded parameters. + // You'll need these later when you want to use + // the supplied workspaces + + var viewModel = new DynamoAssistantWindowViewModel(p); + var window = new DynamoAssistantWindow + { + DataContext = viewModel, + // Set the data context for the main grid in the window. + MainGrid = { DataContext = viewModel }, + + // Set the owner of the window to the Dynamo window. + Owner = p.DynamoWindow + }; + + assistantMenuItem = new MenuItem { Header = "Show Dynamo Assistant", IsCheckable = true }; + assistantMenuItem.Checked += (sender, args) => p.AddToExtensionsSideBar(this, window); + assistantMenuItem.Unchecked += (sender, args) => p.CloseExtensioninInSideBar(this); + p.AddExtensionMenuItem(assistantMenuItem); + } + + public override void Shutdown() + { + } + + public override void Closed() + { + if (assistantMenuItem != null) + { + assistantMenuItem.IsChecked = false; + } + } + + public override string UniqueId + { + get + { + return "DA05ED05-6842-4CF0-9ADC-575888A01FEC"; + } + } + + public override string Name + { + get + { + return "Dynamo Assistant"; + } + } + + } +} diff --git a/src/DynamoAssistantViewExtension/DynamoAssistantViewExtension.csproj b/src/DynamoAssistantViewExtension/DynamoAssistantViewExtension.csproj new file mode 100644 index 0000000..3626653 --- /dev/null +++ b/src/DynamoAssistantViewExtension/DynamoAssistantViewExtension.csproj @@ -0,0 +1,31 @@ + + + + + + DynamoAssistant + DynamoAssistantViewExtension + + net8.0-windows + true + + + + + + + + + + Always + + + + + + + + + + + \ No newline at end of file diff --git a/src/DynamoAssistantViewExtension/DynamoAssistantViewExtension_ViewExtensionDefinition.xml b/src/DynamoAssistantViewExtension/DynamoAssistantViewExtension_ViewExtensionDefinition.xml new file mode 100644 index 0000000..3ec86e8 --- /dev/null +++ b/src/DynamoAssistantViewExtension/DynamoAssistantViewExtension_ViewExtensionDefinition.xml @@ -0,0 +1,4 @@ + + ..\bin\DynamoAssistantViewExtension.dll + DynamoAssistant.DynamoAssistantViewExtension + diff --git a/src/DynamoAssistantViewExtension/DynamoAssistantWindow.xaml b/src/DynamoAssistantViewExtension/DynamoAssistantWindow.xaml new file mode 100644 index 0000000..17bb093 --- /dev/null +++ b/src/DynamoAssistantViewExtension/DynamoAssistantWindow.xaml @@ -0,0 +1,84 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +