Skip to content

Commit

Permalink
Update to Display Copilot and create note example
Browse files Browse the repository at this point in the history
  • Loading branch information
QilongTang committed Mar 6, 2024
1 parent 2932bec commit e8a3dd7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Windows.Controls;
using Dynamo.ViewModels;
using Dynamo.Wpf.Extensions;

namespace DynamoAssistant
Expand Down Expand Up @@ -47,8 +48,8 @@ public override void Loaded(ViewLoadedParams p)
// Set the owner of the window to the Dynamo window.
Owner = p.DynamoWindow
};

assistantMenuItem = new MenuItem { Header = "Show Dynamo Assistant", IsCheckable = true };
viewModel.dynamoViewModel = p.DynamoWindow.DataContext as DynamoViewModel;
assistantMenuItem = new MenuItem { Header = "Open Copilot", IsCheckable = true };
assistantMenuItem.Checked += (sender, args) => p.AddToExtensionsSideBar(this, window);
assistantMenuItem.Unchecked += (sender, args) => p.CloseExtensioninInSideBar(this);
p.AddExtensionMenuItem(assistantMenuItem);
Expand Down Expand Up @@ -78,7 +79,7 @@ public override string Name
{
get
{
return "Dynamo Assistant";
return "Copilot";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DynamoVisualProgramming.DynamoCoreNodes" Version="3.1.0-beta4081" />
<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" />
Expand Down
52 changes: 37 additions & 15 deletions src/DynamoAssistantViewExtension/DynamoAssistantWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
using Dynamo.Core;
using System;
using System.Collections.ObjectModel;
using System.Windows.Input;
using Dynamo.Core;
using Dynamo.Extensions;
using Dynamo.Models;
using Dynamo.UI.Commands;
using Dynamo.ViewModels;
using OpenAI_API;
using OpenAI_API.Chat;
using OpenAI_API.Models;
using System;
using System.Collections.ObjectModel;
using System.Windows.Input;

namespace DynamoAssistant
{
public class DynamoAssistantWindowViewModel : NotificationObject, IDisposable
{
private string userInput;
private readonly ReadyParams readyParams;
internal DynamoViewModel dynamoViewModel;

// Chat GPT related fields
private readonly OpenAIAPI chatGPTClient;
private readonly Conversation conversation;
private static readonly string apikey = "Your API Key";

/// <summary>
///
/// User input to the Copilot
/// </summary>
public string UserInput
{
Expand All @@ -36,6 +41,11 @@ public string UserInput
}
}

/// <summary>
/// Dynamo Model getter
/// </summary>
internal DynamoModel dynamoModel => dynamoViewModel.Model;

public ObservableCollection<string> Messages { get; set; } = new ObservableCollection<string>();

public DynamoAssistantWindowViewModel(ReadyParams p)
Expand All @@ -46,23 +56,38 @@ public DynamoAssistantWindowViewModel(ReadyParams p)
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;
conversation.Model = Model.GPT4_Turbo;
// Adjust this value for more or less "creativity" in the response
conversation.RequestParameters.Temperature = 0.2;
// Display a welcome message
Messages.Add("Assistant:\nWelcome to Dynamo world and ask me anything to get started!");
Messages.Add("Copilot:\nWelcome to Dynamo world and ask me anything to get started!\n");
}

internal async void SendMessage(string msg)
{
// Send the user's input to the ChatGPT API and receive a response
conversation?.AppendUserInput(msg);
string response = await conversation.GetResponseFromChatbotAsync();
// Display user message first
Messages.Add("You:\n" + msg);
Messages.Add("You:\n" + msg + "\n");
string response = await conversation.GetResponseFromChatbotAsync();
// Display the chatbot's response
Messages.Add("Assistant:\n" + response);
Messages.Add("Copilot:\n" + response + "\n");
//var pythonNode = new PythonNodeModels.PythonNode();
//dynamoModel.ExecuteCommand(new DynamoModel.CreateNodeCommand(pythonNode, 0, 0, false, false));

// create a Dynamo note example
CreateNote("A1BE9F01-55C4-495E-B24C-099D018A29CE", "This is a sample Note.", 0, 0, true);
}

internal void CreateNote(string nodeId, string noteText, double x, double y, bool defaultPosition)
{
dynamoModel.ExecuteCommand(new DynamoModel.CreateNoteCommand(nodeId, noteText, x, y, defaultPosition));
Messages.Add("Copilot:\nYour note has been created!\n");
}

/// <summary>
/// Dispose function
/// </summary>
public void Dispose()
{
// Do nothing
Expand All @@ -74,10 +99,7 @@ public ICommand EnterCommand
{
get
{
if (enterCommand == null)
{
enterCommand = new DelegateCommand(Enter);
}
enterCommand ??= new DelegateCommand(Enter);

return enterCommand;
}
Expand Down

0 comments on commit e8a3dd7

Please sign in to comment.