Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Brings TypeChat to .NET with .NET idiom introduced as appropriate.
* Schema Exporters from .NET Types to schema expressed using Typescript. Schema export includes support for:
* Dynamic export at runtime. This is needed for scenarios where the schema must include dynamic lists, such as relevant product names or lists of players in a team.
* Vocabularies: easy unions of string tables, like in Typescript, along with support for dynamic loading. See examples: CoffeeShop and CoffeeShop2.
* Classifiers used to route requests to child or hierarchical schemas and handlers

## TypeChat.Program ##
TypeChat.Program translates natural language requests into simple programs (***Plans***), represented as JSON. These programs are then type checked, compiled and run with type safety.
Expand All @@ -54,6 +55,7 @@ TypeChat.Program includes:

The library contains classes for:
* LLM bindings for TypeChat using the Semantic Kernel. All TypeChat examples use the Semantic Kernel to call LLMs
* Embeddings, VectorizedLists
* **Program synthesis with Plugins**: Automatically turns registered plugins into a PluginAPI that programs synthesized by the LLM can call. [Plugins Example](examples/Plugins/Program.cs)

# TypeChat.Dialog
Expand All @@ -64,8 +66,6 @@ TypeChat.Dialog is an early version of framework desiged for strongly typed inte

# TypeChat.App
Contains classes used by Typechat examples. These classes may be generally useful for apps built on top of Typechat. Helper classes include:
* Text Classification
* Input routing to sub-apps and schemas
* Program synthesis of programs that call Plugins
* Console Apps

Expand All @@ -80,7 +80,7 @@ Contains classes used by Typechat examples. These classes may be generally usefu
* Command Line
* Launch a command prompt
* Go to the root directory of the project
* dotnet Typechat.sln
* dotnet build Typechat.sln


## Nuget Packages
Expand Down
12 changes: 9 additions & 3 deletions examples/HealthData/HealthSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public class Condition
}

[Comment("Use for health data that match nothing else. E.g. immunization, blood prssure etc")]
public class OtherData
public class OtherHealthData
{
[JsonPropertyName("text")]
public string Text { get; set; }
Expand All @@ -85,17 +85,23 @@ public class HealthData
{
public Medication[]? Medication { get; set; }
public Condition[]? Condition { get; set; }
public OtherData[]? Other { get; set; }
public OtherHealthData[]? Other { get; set; }
}

public class HealthDataResponse
{
[Comment("Return this if JSON has ALL required information. Else ask questions")]
public HealthData? Data { get; set; }

[Comment("Use this to ask questions, respond to user, return errors or text you did not understand")]
[Comment("Use this to ask questions and give pertient responses")]
public string? Message { get; set; }

[Comment("Use this parts of the user request not translateed, off topic, etc")]
public string? NotTranslated { get; set; }

[JsonIgnore]
public bool HasMessage => (!string.IsNullOrEmpty(Message));

[JsonIgnore]
public bool HasNotTranslated => (!string.IsNullOrEmpty(NotTranslated));
}
4 changes: 4 additions & 0 deletions examples/HealthData/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ void PrintResponse(HealthDataResponse response)
{
Console.WriteLine($"📝: {response.Message}");
}
if (response.HasNotTranslated)
{
Console.WriteLine($"🤔: I did not understand\n {response.NotTranslated}");
}
}

public static async Task<int> Main(string[] args)
Expand Down
25 changes: 13 additions & 12 deletions examples/MultiSchema/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft. All rights reserved.

using Microsoft.TypeChat;
using Microsoft.TypeChat.Classification;

using Sentiment;
using CoffeeShop;
Expand All @@ -19,18 +20,18 @@ namespace MultiSchema;
public class MultiSchemaApp : ConsoleApp
{
ILanguageModel _model;
InputRouter _childApps;
TextRequestRouter<IInputHandler> _childApps;

public MultiSchemaApp()
{
_model = new LanguageModel(Config.LoadOpenAI());
_childApps = new InputRouter(_model);
_childApps = new TextRequestRouter<IInputHandler>(_model);
InitApps();
}

public override async Task ProcessInputAsync(string input, CancellationToken cancelToken)
{
var match = await _childApps.RouteAsync(input, cancelToken);
var match = await _childApps.ClassifyRequestAsync(input, cancelToken);
var processor = match.Value;
if (processor != null)
{
Expand All @@ -50,20 +51,20 @@ void InitApps()
{
// While this is hardcoded here, you can also do this dynamically
// targets dynamically
_childApps.Add("CoffeeShop", new CoffeeShopApp(), "Order Coffee Drinks (Italian names included) and Baked Goods");
_childApps.Add("Calendar", new CalendarApp(), "Actions related to calendars, appointments, meetings, schedules");
_childApps.Add("Restaurant", new RestaurantApp(), "Order pizza, beer and salads");
_childApps.Add("Math", new MathApp(), "Calculations using the four basic math operations");
_childApps.Add("Sentiment", new SentimentApp(), "Statements with sentiments, emotions, feelings, impressions about places, things, the surroundings");
_childApps.Add("Plugins", new PluginApp(), "Command shell operations: list and search for files, machine information");
_childApps.Add("HealthData", new HealthDataAgent(), "Health information: enter your medications, conditions");
_childApps.Add("No Match", null, "None of the others matched");
_childApps.Add(new CoffeeShopApp(), "CoffeeShop", "Order Coffee Drinks (Italian names included) and Baked Goods");
_childApps.Add(new CalendarApp(), "Calendar", "Actions related to calendars, appointments, meetings, schedules");
_childApps.Add(new RestaurantApp(), "Restaurant", "Order pizza, beer and salads");
_childApps.Add(new MathApp(), "Math", "Calculations using the four basic math operations");
_childApps.Add(new SentimentApp(), "Sentiment", "Statements with sentiments, emotions, feelings, impressions about places, things, the surroundings");
_childApps.Add(new PluginApp(), "Plugins", "Command shell operations: list and search for files, machine information");
_childApps.Add(new HealthDataAgent(), "HealthData", "Health information: enter your medications, conditions");
_childApps.Add(null, "No Match", "None of the others matched");
}

void PrintApps()
{
int i = 0;
foreach (var app in _childApps.Handlers)
foreach (var app in _childApps.Routes)
{
if (app.Value != null)
{
Expand Down
43 changes: 43 additions & 0 deletions src/typechat.app/HierarchicalJsonTranslator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) Microsoft. All rights reserved.

using Microsoft.TypeChat.Classification;

namespace Microsoft.TypeChat;

/// <summary>
/// Some scenarios have schemas that can be partioned into sub-schemas or translators.
/// Language models have token limits, which means we must do a first pass and route requests to the appropriate target
/// It may also be necessary to bind to these translators dynamically
///
/// A DynamicJsonTranslator is a simple aggregate translator that routes a request to child JsonTranslators.
/// </summary>
public class HierarchicalJsonTranslator : IJsonTranslator
{
ITextRequestRouter<IJsonTranslator> _requestRouter;

/// <summary>
/// Create a new JsonTranslator that routes requests to child translators
/// </summary>
/// <param name="router"></param>
public HierarchicalJsonTranslator(ITextRequestRouter<IJsonTranslator> router)
{
ArgumentNullException.ThrowIfNull(router, nameof(router));
_requestRouter = router;
}

/// <summary>
/// The router being used by this translator
/// </summary>
public ITextRequestRouter<IJsonTranslator> Router => _requestRouter;

public async Task<object> TranslateToObjectAsync(string request, CancellationToken cancelToken)
{
// First, select the translator that is best suited to translate this request
IJsonTranslator? translator = await _requestRouter.RouteRequestAsync(request, cancelToken);
if (translator == null)
{
throw new TypeChatException(TypeChatException.ErrorCode.NoTranslator, request);
}
return await translator.TranslateToObjectAsync(request, cancelToken);
}
}
73 changes: 0 additions & 73 deletions src/typechat.app/InputRouter.cs

This file was deleted.

4 changes: 0 additions & 4 deletions src/typechat.app/Readme.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# Typechat.App
Classes and extension methods used by Typechat examples. These classes may be generally useful for apps built on top of Typechat.

* TextClassifier: Simple but powerful text classifier

* InputRouter: Route user input to the semantically closest application or select the most appropriate schema

* PluginProgramTranslator: Sample program translator that translates user requests into programs for APIs defined by Microsoft Semantic Kernel Plugins

Expand Down
Loading