-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a8aedd1
commit 5db1e09
Showing
10 changed files
with
441 additions
and
1 deletion.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
...sion/Sample View Extension/extra/DynamoAssistantViewExtension_ViewExtensionDefinition.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<ViewExtensionDefinition> | ||
<AssemblyPath>..\bin\DynamoAssistantViewExtension.dll</AssemblyPath> | ||
<TypeName>DynamoAssistant.DynamoAssistantViewExtension</TypeName> | ||
</ViewExtensionDefinition> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<dynamic>(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."; | ||
} | ||
|
||
} | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
src/DynamoAssistantViewExtension/DynamoAssistantViewExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using System.Windows.Controls; | ||
using Dynamo.Wpf.Extensions; | ||
|
||
namespace DynamoAssistant | ||
{ | ||
/// <summary> | ||
/// 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. | ||
/// </summary> | ||
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"; | ||
} | ||
} | ||
|
||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/DynamoAssistantViewExtension/DynamoAssistantViewExtension.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<ImportGroup Label="PropertySheets"> | ||
<Import Project="$(SolutionDir)Config\CS.props" /> | ||
</ImportGroup> | ||
<PropertyGroup> | ||
<RootNamespace>DynamoAssistant</RootNamespace> | ||
<AssemblyName>DynamoAssistantViewExtension</AssemblyName> | ||
<!--Windows and WPF APIs are needed to support dynamo UI extensions--> | ||
<TargetFramework>net8.0-windows</TargetFramework> | ||
<UseWPF>true</UseWPF> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="DynamoVisualProgramming.DynamoServices" Version="3.1.0-beta4081" /> | ||
<PackageReference Include="DynamoVisualProgramming.WpfUILibrary" Version="3.1.0-beta4081" /> | ||
<PackageReference Include="DynamoVisualProgramming.ZeroTouchLibrary" Version="3.1.0-beta4081" /> | ||
<PackageReference Include="RestSharp" Version="108.0.1" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Update="DynamoAssistantViewExtension_ViewExtensionDefinition.xml"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
<Target Name="AfterBuildOps" AfterTargets="Build"> | ||
<ItemGroup> | ||
<PackageDll Include="$(ProjectDir)bin\$(ConfigurationName)\DynamoAssistantViewExtension.dll" /> | ||
<PackageXml Include="$(ProjectDir)bin\$(ConfigurationName)\DynamoAssistantViewExtension_ViewExtensionDefinition.xml" /> | ||
</ItemGroup> | ||
<Copy SourceFiles="@(PackageDll)" DestinationFolder="$(ProjectDir)..\..\dynamo_viewExtension\Sample View Extension\bin" /> | ||
<Copy SourceFiles="@(PackageXml)" DestinationFolder="$(ProjectDir)..\..\dynamo_viewExtension\Sample View Extension\extra" /> | ||
</Target> | ||
</Project> |
4 changes: 4 additions & 0 deletions
4
src/DynamoAssistantViewExtension/DynamoAssistantViewExtension_ViewExtensionDefinition.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<ViewExtensionDefinition> | ||
<AssemblyPath>..\bin\DynamoAssistantViewExtension.dll</AssemblyPath> | ||
<TypeName>DynamoAssistant.DynamoAssistantViewExtension</TypeName> | ||
</ViewExtensionDefinition> |
84 changes: 84 additions & 0 deletions
84
src/DynamoAssistantViewExtension/DynamoAssistantWindow.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<Window x:Class="DynamoAssistant.DynamoAssistantWindow" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:local="clr-namespace:DynamoAssistant" | ||
d:DataContext="{d:DesignInstance Type=local:DynamoAssistantWindowViewModel}" | ||
mc:Ignorable="d" | ||
d:DesignHeight="300" | ||
d:DesignWidth="300" | ||
Width="500" | ||
Height="100"> | ||
<Grid Name="MainGrid" | ||
HorizontalAlignment="Stretch" | ||
VerticalAlignment="Stretch"> | ||
|
||
<Grid.RowDefinitions> | ||
<RowDefinition Height="5*"/> | ||
<RowDefinition Height="*"/> | ||
</Grid.RowDefinitions> | ||
|
||
<ListView | ||
ItemsSource="{Binding Messages}" | ||
FontFamily="Arial" | ||
Padding="10" | ||
FontWeight="Medium" | ||
FontSize="15" | ||
Background="#2d2d2d" | ||
Foreground="White" | ||
Grid.Row="0" | ||
Width="Auto" | ||
ScrollViewer.HorizontalScrollBarVisibility="Disabled" | ||
ScrollViewer.CanContentScroll="True"> | ||
<ListView.ItemTemplate> | ||
<DataTemplate> | ||
<TextBlock Text="{Binding Path=.}" TextWrapping="Wrap"></TextBlock> | ||
</DataTemplate> | ||
</ListView.ItemTemplate> | ||
</ListView> | ||
|
||
<Grid Name="TextInput" | ||
HorizontalAlignment="Stretch" | ||
VerticalAlignment="Stretch" | ||
Grid.Row="1"> | ||
<Grid.ColumnDefinitions> | ||
<ColumnDefinition Width="6*"/> | ||
<ColumnDefinition Width="*"/> | ||
</Grid.ColumnDefinitions> | ||
|
||
<ScrollViewer | ||
HorizontalScrollBarVisibility="Disabled" | ||
VerticalScrollBarVisibility="Auto" | ||
Grid.Column="0"> | ||
<TextBox | ||
Name="UserInput" | ||
Text="{Binding UserInput, UpdateSourceTrigger=PropertyChanged}" | ||
FontFamily="Arial" | ||
Padding="10" | ||
FontWeight="Medium" | ||
FontSize="15" | ||
Background="#2d2d2d" | ||
Foreground="White" | ||
AcceptsReturn="True" | ||
TextWrapping="Wrap" | ||
Height="50"> | ||
<TextBox.InputBindings> | ||
<KeyBinding Key="Return" | ||
Command="{Binding EnterCommand}" | ||
CommandParameter="{Binding ElementName=UserInput, Path=Text}"/> | ||
</TextBox.InputBindings> | ||
</TextBox> | ||
</ScrollViewer> | ||
|
||
<Button | ||
Content="Send" | ||
Command="{Binding SendMessagesCommand}" | ||
Click="Button_Click" | ||
Height="50" | ||
Grid.Column="1"/> | ||
</Grid> | ||
|
||
|
||
</Grid> | ||
</Window> |
22 changes: 22 additions & 0 deletions
22
src/DynamoAssistantViewExtension/DynamoAssistantWindow.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System.Windows; | ||
|
||
namespace DynamoAssistant | ||
{ | ||
/// <summary> | ||
/// Interaction logic for SampleWindow.xaml | ||
/// </summary> | ||
public partial class DynamoAssistantWindow : Window | ||
{ | ||
internal DynamoAssistantWindowViewModel ViewModel => MainGrid.DataContext as DynamoAssistantWindowViewModel; | ||
public DynamoAssistantWindow() | ||
{ | ||
InitializeComponent(); | ||
} | ||
|
||
private void Button_Click(object sender, RoutedEventArgs e) | ||
{ | ||
ViewModel.SendMessage(UserInput.Text); | ||
ViewModel.UserInput = string.Empty; | ||
} | ||
} | ||
} |
Oops, something went wrong.