Skip to content

Commit

Permalink
Add code generation and code interpretation sample code
Browse files Browse the repository at this point in the history
  • Loading branch information
kbeaugrand committed Feb 5, 2024
1 parent f9415cc commit 1eb0f60
Show file tree
Hide file tree
Showing 21 changed files with 570 additions and 103 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -398,3 +398,4 @@ FodyWeavers.xsd
*.sln.iml
/src/Assistants.Tests/testsettings.development.json
/samples/01-mathematician/appsettings.development.json
/samples/02-autogen/appsettings.development.json
9 changes: 9 additions & 0 deletions SemanticKernel.Assistants.sln
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{
.github\workflows\publish.yml = .github\workflows\publish.yml
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "02-autogen", "samples\02-autogen\02-autogen.csproj", "{F5C5DEF1-3AB7-4DAF-A29D-A46483875287}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -57,6 +59,12 @@ Global
{BBC6C36F-DC43-4FD3-9706-ECA4738F8F57}.Publish|Any CPU.Build.0 = Release|Any CPU
{BBC6C36F-DC43-4FD3-9706-ECA4738F8F57}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BBC6C36F-DC43-4FD3-9706-ECA4738F8F57}.Release|Any CPU.Build.0 = Release|Any CPU
{F5C5DEF1-3AB7-4DAF-A29D-A46483875287}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F5C5DEF1-3AB7-4DAF-A29D-A46483875287}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F5C5DEF1-3AB7-4DAF-A29D-A46483875287}.Publish|Any CPU.ActiveCfg = Debug|Any CPU
{F5C5DEF1-3AB7-4DAF-A29D-A46483875287}.Publish|Any CPU.Build.0 = Debug|Any CPU
{F5C5DEF1-3AB7-4DAF-A29D-A46483875287}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F5C5DEF1-3AB7-4DAF-A29D-A46483875287}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -66,6 +74,7 @@ Global
{03C21161-E835-4857-A81A-C1727140E920} = {96B59E8F-BF38-4918-8312-63DA3363B20B}
{BBC6C36F-DC43-4FD3-9706-ECA4738F8F57} = {803BA424-8745-4689-9C1D-72CA4384E6AC}
{B69AC62F-2CB1-4BCD-AB94-2C558AD3DB29} = {324300B5-4DBA-4DF0-957C-75458CCF93CE}
{F5C5DEF1-3AB7-4DAF-A29D-A46483875287} = {803BA424-8745-4689-9C1D-72CA4384E6AC}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3252A8D5-644E-45F0-B096-AC8C2F0A15B4}
Expand Down
21 changes: 10 additions & 11 deletions samples/01-mathematician/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.SemanticKernel;
using SemanticKernel.Assistants;
using SemanticKernel.Assistants.Extensions;
using Spectre.Console;

var configuration = new ConfigurationBuilder()
Expand Down Expand Up @@ -39,21 +40,19 @@
.AddAzureOpenAIChatCompletion(azureOpenAIDeploymentName, azureOpenAIEndpoint, azureOpenAIKey)
.Build();

financialKernel.CreatePluginFromObject(new FinancialPlugin(), "financial");
financialKernel.ImportPluginFromObject(new FinancialPlugin(), "financial");

var financialCalculator = AssistantBuilder.FromTemplate("./Assistants/FinancialCalculator.yaml")
.WithKernel(financialKernel)
.Build();

var butlerKernel = Kernel.CreateBuilder()
.AddAzureOpenAIChatCompletion(azureOpenAIDeploymentName, azureOpenAIEndpoint, azureOpenAIKey)
.Build();
.AddAzureOpenAIChatCompletion(azureOpenAIDeploymentName, azureOpenAIEndpoint, azureOpenAIKey)
.Build();

var financialCalculator = AssistantBuilder.FromTemplate("./Assistants/FinancialCalculator.yaml")
.WithKernel(financialKernel)
.Build();
butlerKernel.ImportPluginFromAssistant(financialCalculator);

assistant = AssistantBuilder.FromTemplate("./Assistants/Butler.yaml",
assistants: new IAssistant[]
{
financialCalculator
})
assistant = AssistantBuilder.FromTemplate("./Assistants/Butler.yaml")
.WithKernel(butlerKernel)
.Build();
});
Expand Down
47 changes: 47 additions & 0 deletions samples/02-autogen/02-autogen.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>_02_autogen</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Docker.DotNet" Version="3.125.15" />
<PackageReference Include="FinancialFormulas" Version="1.3.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
<PackageReference Include="Microsoft.SemanticKernel.Abstractions" Version="1.3.0" />
<PackageReference Include="Microsoft.SemanticKernel.Core" Version="1.3.0" />
<PackageReference Include="Spectre.Console" Version="0.48.0" />
<PackageReference Include="Spectre.Console.Json" Version="0.48.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Assistants\SemanticKernel.Assistants.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="appsettings.development.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Assistants\AssistantAgent.yaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Assistants\CodeInterpreter.yaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>


</Project>
27 changes: 27 additions & 0 deletions samples/02-autogen/Assistants/AssistantAgent.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: AssistantAgent
description: A helpful and general-purpose AI assistant that has strong language skills, Python skills, and Linux command line skills.
instructions: |
You are a helpful and general-purpose AI assistant that has strong Python skills.
Solve tasks using your coding and language skills.
In the following cases, suggest python code (in a python coding block) or shell script (in a sh coding block) for the CodeInterpreter to execute.
1. When you need to collect info, use the code to output the info you need, for example, browse or search the web, download/read a file, print the content of a webpage or a file, get the current date/time, check the operating system. After sufficient info is printed and the task is ready to be solved based on your language skill, you can solve the task by yourself.
2. When you need to perform some task with code, use the code to perform the task and output the result. Finish the task smartly.
3. You should always choose the most precise way of solving the task.
Solve the task step by step if you need to. If a plan is not provided, explain your plan first. Be clear which step uses code, and which step uses your language skill.
When using code, you must indicate the script type in the code block. The user cannot provide any other feedback or perform any other action beyond executing the code you suggest. The user can't modify your code. So do not suggest incomplete code which requires users to modify. Don't use a code block if it's not intended to be executed by the user.
If the result indicates there is an error, fix the error and output the code again. Suggest the full code instead of partial code or code changes. If the error can't be fixed or if the task is not solved even after the code is executed successfully, analyze the problem, revisit your assumption, collect additional info you need, and think of a different approach to try.
Don't ever assume that you can't access the WEB, using CodeInterpreter agent, you'll be able to connect to the Internet.
When you find an answer, verify the answer carefully. Include verifiable evidence in your response if possible.
Reply "TERMINATE" in the end when everything is done.
execution_settings:
planner: Stepwise
prompt_settings:
temperature: 0.0
top_p: 1
max_tokens: 1000
56 changes: 56 additions & 0 deletions samples/02-autogen/Assistants/CodeInterpreter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: CodeInterpreter
description: |
This AI agent is designed to execute python code reliably and efficiently.
It has a built-in python interpreter that allows it to read, analyze and execute any valid python script.
This agent does not store instructions between calls, so you need to provide a complete code file to execute.
instructions:
input_parameters:
- name: input
is_required: True
default_value: ""
description: |
The python code to execute.
Make sure you follow the correct Python code syntax before submitting it.
If you expect me to send you a result, you should use ``print`` method to output your expectactions.
## Example
```
x = 1
y = 2.8
z = 1j
print(type(x))
print(type(y))
print(type(z))
```
This code should be sended like this:
```
x = 1\r\ny = 2.8\r\nz = 1j\r\n\r\nprint(type(x))\r\nprint(type(y))\r\nprint(type(z))
```
Important: You should alway send a full code to execute.
- name: requirements
is_required: False
default_value: ""
description: |
The contents of the Python requirements file to be used.
These requirements will be added to the ``requirements.txt`` file in the sandbox by the CodeInterpreter.
Here you must provide all the requirements necessary for the successful execution of your code.
## Example
```
tensorflow==2.3.1
uvicorn==0.12.2
fastapi==0.63.0
```
execution_settings:
planner: Handlebars
fixed_plan: |
{{json (code-ExecutePythonCode)}}
prompt_settings:
temperature: 0.0
top_p: 1
max_tokens: 1000
19 changes: 19 additions & 0 deletions samples/02-autogen/Exceptions/CodeInterpreterException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _02_autogen.Exceptions
{
public class CodeInterpreterException : Exception
{
public CodeInterpreterException(string message, params string[] warnings)
: base(message)
{
this.Warnings = warnings;
}

public string[] Warnings { get; }
}
}
Loading

0 comments on commit 1eb0f60

Please sign in to comment.