Skip to content

Commit

Permalink
Update to enable clickable Copilot commands
Browse files Browse the repository at this point in the history
  • Loading branch information
QilongTang committed Mar 8, 2024
1 parent ed23adf commit 65d37ea
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 38 deletions.
36 changes: 19 additions & 17 deletions src/DynamoAssistantViewExtension/DynamoAssistantWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
VerticalAlignment="Stretch"
Grid.Row="0">
<Grid.RowDefinitions>
<RowDefinition Height="4*"/>
<RowDefinition Height="6*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
Expand All @@ -35,20 +35,20 @@
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>

<ListView
ItemsSource="{Binding Messages}"
FontFamily="Arial"
Padding="10"
FontWeight="Medium"
FontSize="15"
Background="#2d2d2d"
Foreground="White"
Grid.Row="0"
Grid.Column="0"
Width="Auto"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.CanContentScroll="True">
ItemsSource="{Binding Messages}"
FontFamily="Arial"
Padding="10"
FontWeight="Medium"
FontSize="15"
Background="#2d2d2d"
Foreground="White"
Grid.Row="0"
Grid.Column="0"
Width="Auto"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.CanContentScroll="True">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=.}" TextWrapping="Wrap"></TextBlock>
Expand All @@ -61,7 +61,8 @@
Height="50"
Background="#2d2d2d"
Foreground="White"
Grid.Row="1"/>
Grid.Row="1"
Click="DescribeGraphButton_Click"/>
<Button
Content="Generate annotations for me"
Width="400"
Expand All @@ -82,7 +83,8 @@
Height="50"
Background="#2d2d2d"
Foreground="White"
Grid.Row="4"/>
Grid.Row="4"
Click="OptimizeGraphButton_Click"/>
<Button
Content="Help me create a graph"
Width="400"
Expand Down Expand Up @@ -136,7 +138,7 @@
<Button
Content=""
FontSize="25"
Click="Button_Click"
Click="SendButton_Click"
Width="50"
Height="50"
Grid.Column="1"
Expand Down
12 changes: 11 additions & 1 deletion src/DynamoAssistantViewExtension/DynamoAssistantWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,20 @@ public DynamoAssistantWindow()
InitializeComponent();
}

private void Button_Click(object sender, RoutedEventArgs e)
private void SendButton_Click(object sender, RoutedEventArgs e)
{
ViewModel.SendMessage(UserInput.Text);
ViewModel.UserInput = string.Empty;
}

private void DescribeGraphButton_Click(object sender, RoutedEventArgs e)
{
ViewModel.DescribeGraph();
}

private void OptimizeGraphButton_Click(object sender, RoutedEventArgs e)
{
ViewModel.OptimizeGraph();
}
}
}
62 changes: 42 additions & 20 deletions src/DynamoAssistantViewExtension/DynamoAssistantWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ public class DynamoAssistantWindowViewModel : NotificationObject, IDisposable
private readonly Conversation conversation;
private static readonly string apikey = "Your API Key";

// Chat GPT pre instruction fields
// A set of instructions to prepare GPT to describe Dynamo graph better
private const string DescribePreInstruction = "Given a JSON file representing a Dynamo for Revit project, perform a comprehensive analysis focusing on the graph's node structure. Your tasks include:\r\n\r\nReview Node Connections: Ensure each node is connected correctly according to Dynamo's expected data types and functionalities. Identify any instances where inputs may be receiving incorrect data types or where outputs are not utilized efficiently.\r\n\r\nData Type Validation: For each node input and output, validate that the data types are compatible with their intended functions. Highlight mismatches, such as a string data type connected to a numeric input without appropriate conversion.";

// A set of instructions to prepare GPT to optimize Dynamo graph better
private const string OptimizePreInstruction = "Given a JSON file representing a Dynamo for Revit project, perform a comprehensive analysis focusing on the graph's node structure. Your tasks include:\r\n\r\nIdentify Unnecessary Nodes: Detect nodes that do not contribute to the final output or create redundant processes within the graph. This includes nodes with default values that never change or intermediary nodes that could be bypassed without altering the graph's outcome.\r\n\r\nOptimization Recommendations: Based on your analysis, recommend specific changes to the node structure. This might involve reordering nodes for logical flow, changing node types for efficiency, or altering connections to ensure data type compatibility.\r\n\r\nUpdate JSON Structure: Apply the optimization recommendations to the JSON file. Directly modify the \"Nodes\" and \"Connectors\" sections to reflect the optimized graph layout. Ensure that all other elements of the JSON file, such as \"Uuid\", \"Description\", \"ElementResolver\", and metadata, remain unchanged to preserve the file's integrity and additional context.\r\n\r\nOutput an Optimized JSON: Provide a revised JSON file, focusing exclusively on an updated node structure that reflects your analysis and optimizations. This file should retain all original details except for the modifications to nodes and their connections to address identified issues and enhance efficiency.";
/// <summary>
/// User input to the Copilot
/// </summary>
Expand Down Expand Up @@ -64,7 +70,7 @@ public DynamoAssistantWindowViewModel(ReadyParams p)
conversation = chatGPTClient.Chat.CreateConversation();
conversation.Model = Model.GPT4_Turbo;
// Adjust this value for more or less "creativity" in the response
conversation.RequestParameters.Temperature = 0.2;
conversation.RequestParameters.Temperature = 0.1;
// Display a welcome message
Messages.Add("Copilot:\nWelcome to Dynamo world and ask me anything to get started!\n");
}
Expand All @@ -73,44 +79,60 @@ internal async void SendMessage(string msg)
{
if (string.IsNullOrEmpty(msg)) return;

// Set Dynamo file location
string filePath = readyParams.CurrentWorkspaceModel.FileName;

// A set of instructions to prepare GBT to analyze Dynamo nodes better
string preInfo = "Given a JSON file representing a Dynamo for Revit project, perform a comprehensive analysis focusing on the graph's node structure. Your tasks include:\r\n\r\nReview Node Connections: Ensure each node is connected correctly according to Dynamo's expected data types and functionalities. Identify any instances where inputs may be receiving incorrect data types or where outputs are not utilized efficiently.\r\n\r\nData Type Validation: For each node input and output, validate that the data types are compatible with their intended functions. Highlight mismatches, such as a string data type connected to a numeric input without appropriate conversion.\r\n\r\nIdentify Unnecessary Nodes: Detect nodes that do not contribute to the final output or create redundant processes within the graph. This includes nodes with default values that never change or intermediary nodes that could be bypassed without altering the graph's outcome.\r\n\r\nOptimization Recommendations: Based on your analysis, recommend specific changes to the node structure. This might involve reordering nodes for logical flow, changing node types for efficiency, or altering connections to ensure data type compatibility.\r\n\r\nUpdate JSON Structure: Apply the optimization recommendations to the JSON file. Directly modify the \"Nodes\" and \"Connectors\" sections to reflect the optimized graph layout. Ensure that all other elements of the JSON file, such as \"Uuid\", \"Description\", \"ElementResolver\", and metadata, remain unchanged to preserve the file's integrity and additional context.\r\n\r\nOutput an Optimized JSON: Provide a revised JSON file, focusing exclusively on an updated node structure that reflects your analysis and optimizations. This file should retain all original details except for the modifications to nodes and their connections to address identified issues and enhance efficiency.";

//Read the file
string jsonData = PickFile(filePath);

IsWaitingForInput = false;

// Display user message first
Messages.Add("You:\n" + msg + "\n");

msg = msg + "This is my Dynamo project JSON structure." + jsonData;
// Send the user's input to the ChatGPT API and receive a response
conversation?.AppendUserInput(preInfo + msg);
conversation?.AppendUserInput(msg);
string response = await conversation.GetResponseFromChatbotAsync();
// Display the chatbot's response
Messages.Add("Copilot:\n" + response + "\n");
File.WriteAllText(filePath, response);

var responseToLower = response.ToLower();
if (responseToLower.Contains("python script") || responseToLower.Contains("python node"))
{
CreatePythonNode(response);
}

// create a Dynamo note example
// CreateNote((new Guid()).ToString(), "This is a sample Note.", 0, 0, true);
IsWaitingForInput = true;
}
internal string PickFile(string filePath)

internal async void DescribeGraph()
{
// Set Dynamo file location
string filePath = readyParams.CurrentWorkspaceModel.FileName;

//Read the file
string jsonData = File.ReadAllText(filePath);

var msg = "This is my Dynamo project JSON structure." + jsonData;

// Send the user's input to the ChatGPT API and receive a response
conversation?.AppendUserInput(DescribePreInstruction + msg);
string response = await conversation.GetResponseFromChatbotAsync();
// Display the chatbot's graph description
Messages.Add("Copilot:\n" + response + "\n");
}

internal async void OptimizeGraph()
{
// Read the file into a string
var jsonData = File.ReadAllText(filePath);
return jsonData;
// Set Dynamo file location
string filePath = readyParams.CurrentWorkspaceModel.FileName;

//Read the file
string jsonData = File.ReadAllText(filePath);

var msg = "This is my Dynamo project JSON structure." + jsonData;

// Send the user's input to the ChatGPT API and receive a response
conversation?.AppendUserInput(OptimizePreInstruction + msg);
string response = await conversation.GetResponseFromChatbotAsync();
File.WriteAllText(filePath, response);
// Display the chatbot's response
Messages.Add("Copilot:\n" + response + "\n");
}

/// <summary>
/// Create a python node in Dynamo, use latest Nuget package for this
/// </summary>
Expand Down

0 comments on commit 65d37ea

Please sign in to comment.