Skip to content

Commit

Permalink
Umeshma/install test (#155)
Browse files Browse the repository at this point in the history
In this PR
* Added a sample app to allow testing of nuget packages
* Added a  CodeOwners file
* Refactored IVocabCollection and IVocab into their own files
* Updated readmes for examples
  • Loading branch information
umeshma committed Sep 25, 2023
1 parent f2642e8 commit 048cb41
Show file tree
Hide file tree
Showing 14 changed files with 196 additions and 44 deletions.
2 changes: 2 additions & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# @microsoft/TypeChat.Net-PR owns any files in this repo
@microsoft/TypeChat.Net-PR
15 changes: 8 additions & 7 deletions examples/CoffeeShop/ReadMe.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
# CoffeeShop
Demonstrates **JsonTranslator** with a [complex schema](CoffeeShopSchema.cs) for a coffee shop. It shows how GPT 3.5 and 4.0 can effectively be used to translate a broad range of user intent into strongly typed objects that can then be processed with conventional software.
CoffeeShop demonstrates how to use **JsonTranslator** to create a natural language interface to take orders for a coffee shop. This requires a [complex schema](CoffeeShopSchema.cs). CoffeeShop shows how GPT 3.5 and 4.0 can effectively be used to translate a broad range of user intent into strongly typed objects that can then be processed with conventional software.

CoffeeShop emulates natural language ordering at a Coffee Shop serving:
- Coffee
- Espresso
- Lattes
- Baked Good
* Coffee
* Espresso
* Lattes
* Baked Good

A range of syrups and other familiar options are also offered.

Also demonstrates how to use the ***[JsonVocab]*** attribute to:
CoffeeShop also demonstrates how to use the ***JsonVocab*** attribute to:
* Specify string vocabularies like:
```[JsonVocab(whole milk | two percent milk | nonfat milk | soy milk | almond milk | oat milk)]```
* Automatic validation and repair of strings using these vocabularies
* Vocabularies differ from enums because they can also be loaded on demand and/or be customized for each request (based on the request's context)
* Automatic validation and repair of strings using these vocabularies.

## Target models
Works with gpt-35-turbo and gpt-4.
Expand Down
9 changes: 4 additions & 5 deletions examples/CoffeeShop2/Readme.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# CoffeeShop2

CoffeeShop2 implements the same features as [CoffeeShop](../CoffeeShop/Readme.md), but with dynamic vocabularies - how to use IVocabCollection and IVocab.
- Vocab Strings are loaded from a file and bound dynamically.
- Your vocabularies could be loaded from a database or any other source. This lets you alter vocabularies WITHOUT recompiling your code.
- Each request could use a different translator and associated vocabulary (with associated schema generated on the fly).
-- e.g. Premium Coffee Shop users could be offered more complex Syrups or Toppings. Vegan users would only see non-dairy milks. Etc.
CoffeeShop2 implements the same features as [CoffeeShop](../CoffeeShop/Readme.md). But all vocabularies are dynamically loaded. CoffeeShop2 demonstrates how to use [IVocab](../../src/typechat/Schema/IVocab.cs) and [IVocabCollection](../../src/typechat/Schema/IVocabCollection.cs).
* Vocab Strings are loaded from a file and bound dynamically. The same vocabularies could also be loaded from a database or any other source. This lets you alter vocabularies without recompiling your code.
* Each request could use a different translator and associated vocabulary.
* E.g. Premium Coffee Shop users could be offered more complex Syrups or Toppings. Vegan users would only see non-dairy milks. Etc.

## Target models
Works with gpt-35-turbo and gpt-4.
Expand Down
4 changes: 2 additions & 2 deletions examples/HealthData/Readme.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Health Data Agent
This example requires GPT-4.

Demonstrates a ***strongly typed*** Chat. Here, you work with an Agent to interactively enter medications or conditions.
Demonstrates a ***strongly typed*** Chat: a natural language interface for entering health information. You work with a Health Data Agent to interactively enter your medications or conditions.

Shows how to use **Agents (Chat) with history** to ***interactively*** acquire information needed for one or more Types from the user: **form filling**.
The Health Data Agent shows how strongly typed **Agents with history** could interact with a user to collect information needed for one or more data types ("form filling").

## Target models
For best and consistent results, use **gpt-4**.
Expand Down
4 changes: 2 additions & 2 deletions examples/Math/Readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Math

The Math example shows how to use **TypeChat.Program** for program generation based on an API schema. This example translates calculations into simple programs given an [`API`](MathAPI.cs) type that can perform the four basic mathematical operations.
The Math example shows how to use **TypeChat.Program** for program generation based on an API schema. This example translates calculations into simple programs given an [`API`](MathAPI.cs) type that can perform the basic mathematical operations.

## Target models
Works with gpt-35-turbo and gpt-4.
Expand All @@ -9,7 +9,7 @@ Works with gpt-35-turbo and gpt-4.

Example prompts can be found in [`input.txt`](input.txt).

For example, we could use natural language to describe mathematical operations, and TypeChat.Program will generate a json program for the math API defined in the schema. It will then execute the program using a simple interpreter.
For example, we could use natural language to describe mathematical operations, and TypeChat.Program will generate a json program for the math API defined in the schema. It will then execute the program.

**Input**:

Expand Down
4 changes: 3 additions & 1 deletion examples/Plugins/Readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Plugins

Demonstrates how to to generate programs (plans) that target Semantic Kernel and Open API plugins
Demonstrates how to generate programs (plans) that target Semantic Kernel and Open API plugins. The Plugins example also shows how to:
* Use a Program Interpreter to invoke plugins
* Produce Schema from metadata that originates from outside of the .NET type system. Sources could include plugin manifests, Open API specs and other sources.

For best and consistent results, use **GPT-4**.

Expand Down
21 changes: 21 additions & 0 deletions src/typechat/Schema/IVocab.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) Microsoft. All rights reserved.

namespace Microsoft.TypeChat.Schema;

/// <summary>
/// A Vocabulary is a string table whose values need not be hardcoded in code
/// Example: lists of product names
/// This means that each time a schema is generated, it can be different.
///
/// These string tables can be emitted in Typescript schema, e.g. like this:
/// type FourSeasons: 'Winter' | 'Spring' | 'Summer' | 'Fall'
///
/// The string tables can also be automatically validated to ensure the model sends back know strings.
/// Any error can be sent to the model for correction
///
/// </summary>
public interface IVocab : IEnumerable<VocabEntry>
{
bool Contains(VocabEntry entry);
bool Contains(VocabEntry entry, StringComparison comparison);
}
13 changes: 13 additions & 0 deletions src/typechat/Schema/IVocabCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) Microsoft. All rights reserved.

namespace Microsoft.TypeChat.Schema;

/// <summary>
/// A collection of named vocabularies.
/// Vocabulary collections can be dynamic and load vocabularies at runtime from data bases, files and so on
/// </summary>
public interface IVocabCollection : IEnumerable<NamedVocab>
{
NamedVocab? Get(string name);
}

18 changes: 0 additions & 18 deletions src/typechat/Schema/Vocab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,6 @@

namespace Microsoft.TypeChat.Schema;

/// <summary>
/// A Vocabulary is a string table whose values need not be hardcoded in code
/// Example: lists of product names
/// This means that each time a schema is generated, it can be different.
///
/// These string tables can be emitted in Typescript schema, e.g. like this:
/// type FourSeasons: 'Winter' | 'Spring' | 'Summer' | 'Fall'
///
/// The string tables can also be automatically validated to ensure the model sends back know strings.
/// Any error can be sent to the model for correction
///
/// </summary>
public interface IVocab : IEnumerable<VocabEntry>
{
bool Contains(VocabEntry entry);
bool Contains(VocabEntry entry, StringComparison comparison);
}

/// <summary>
/// An in-memory vocabulary.
/// </summary>
Expand Down
9 changes: 0 additions & 9 deletions src/typechat/Schema/VocabCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@

namespace Microsoft.TypeChat.Schema;

/// <summary>
/// A collection of named vocabularies.
/// Vocabulary collections can be dynamic and load vocabularies at runtime from data bases, files and so on
/// </summary>
public interface IVocabCollection : IEnumerable<NamedVocab>
{
NamedVocab? Get(string name);
}

/// <summary>
/// An in-memory vocabulary collection
/// </summary>
Expand Down
17 changes: 17 additions & 0 deletions tests/EmojiApp/Emoji.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.TypeChat" Version="0.1.230922.3-preview" />
<PackageReference Include="Microsoft.TypeChat.Dialog" Version="0.1.230922.3-preview" />
<PackageReference Include="Microsoft.TypeChat.Program" Version="0.1.230922.3-preview" />
<PackageReference Include="Microsoft.TypeChat.SemanticKernel" Version="0.1.230922.3-preview" />
</ItemGroup>

</Project>
25 changes: 25 additions & 0 deletions tests/EmojiApp/Emoji.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.7.34031.279
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Emoji", "Emoji.csproj", "{E05F478D-9736-4252-BEE1-8060E027C5A6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E05F478D-9736-4252-BEE1-8060E027C5A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E05F478D-9736-4252-BEE1-8060E027C5A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E05F478D-9736-4252-BEE1-8060E027C5A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E05F478D-9736-4252-BEE1-8060E027C5A6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {1709003B-ED8F-466F-9B59-E7D4FE42247A}
EndGlobalSection
EndGlobal
95 changes: 95 additions & 0 deletions tests/EmojiApp/EmojiProgram.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright (c) Microsoft. All rights reserved.

//------------------------------------------------------
//
// A simple app that translates user requests into emojis
// using all 3 options in TypeChat.
// This application is used to test Package installs
//
//------------------------------------------------------

using Microsoft.TypeChat;
using Microsoft.TypeChat.Dialog;

namespace Emoji;

public class EmojiResponse
{
public string EmojiTranslation { get; set; } = string.Empty;
}

public interface IEmojiService
{
Task<string> ToEmoji(string userText);
}

internal class EmojiProgram : IEmojiService
{
LanguageModel _model;
JsonTranslator<EmojiResponse> _translator;
public EmojiProgram()
{
_model = new LanguageModel(OpenAIConfig.FromEnvironment());
_translator = new JsonTranslator<EmojiResponse>(_model);
}

public async Task<string> ToEmoji(string line)
{
EmojiResponse response = await _translator.TranslateAsync(line);
return response.EmojiTranslation;
}

public async Task TestTranslate()
{
string? line;
Console.Write(">");

while ((line = Console.ReadLine()) != null)
{
string result = await ToEmoji(line);
Console.WriteLine(result);
Console.Write(">");
}
}

public async Task TestProgram()
{
string? line;
Console.Write(">");
Console.OutputEncoding = System.Text.Encoding.UTF8;

var api = new Api<IEmojiService>(this);
ProgramTranslator programGen = new ProgramTranslator<IEmojiService>(_model, api);
while ((line = Console.ReadLine()) != null)
{
using Program program = await programGen.TranslateAsync(line);
string result = await program.RunAsync(api);
Console.WriteLine(result);
Console.Write(">");
}
}

public async Task TestAgent()
{
Agent<EmojiResponse> agent = new AgentWithHistory<EmojiResponse>(_model);
agent.Instructions.Append("Respond to me in emojis");
string? line;
Console.Write(">");
while ((line = Console.ReadLine()) != null)
{
var response = await agent.GetResponseAsync(line);
Console.WriteLine(response.EmojiTranslation);
Console.Write(">");
}
}

public static async Task<int> Main(string[] args)
{
Console.OutputEncoding = System.Text.Encoding.UTF8;
EmojiProgram p = new EmojiProgram();
await p.TestTranslate();
//await p.TestProgram();
//await p.TestAgent();
return 0;
}
}
4 changes: 4 additions & 0 deletions tests/EmojiApp/addPackages.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dotnet add package Microsoft.TypeChat --prerelease
dotnet add package Microsoft.TypeChat.SemanticKernel --prerelease
dotnet add package Microsoft.TypeChat.Program --prerelease
dotnet add package Microsoft.TypeChat.Dialog --prerelease

0 comments on commit 048cb41

Please sign in to comment.