TypeChat.NET brings the ideas of TypeChat to .NET.
TypeChat.NET provides cross platform libraries that help you build natural language interfaces with language models using strong types, type validation and simple type safe programs (plans). Strong typing may help make software that uses language models more deterministic and reliable.
// Translates user intent into strongly typed Calendar Actions
var model = new LanguageModel(Config.LoadOpenAI());
var translator = new JsonTranslator<CalendarActions>(model);
// Translate natural language request
CalendarActions actions = await translator.TranslateAsync(requestText);
TypeChat.NET is in active development with frequent updates. The framework will evolve as the team explores the space and incorporates feedback. Supported scenarios are shown in the included Examples. Documentation will also continue to improve. When in doubt, please look at the code.
TypeChat.NET currently consists of the following assemblies:
Microsoft.TypeChat: Classes that translate user intent into strongly typed and validated objects.
Microsoft.TypeChat.Program: Classes to synthesize, validate and run JSON programs.
Microsoft.TypeChat.SemanticKernel: Integration with Microsoft Semantic Kernel for models, plugins, embeddings and other features.
TypeChat uses language models to translate user intent into JSON that conforms to a schema. This JSON is then validated and deserialized into a typed object. Additional constraint checking is applied as needed. Validation errors are sent back to the language model, which uses them to repair the Json it originally returned.
TypeChat provides:
[Comment("Milks currently in stock")]
public class Milks
{
[JsonVocab("whole milk | two percent milk | nonfat milk | soy milk | almond milk")]
public string Name { get; set; }
}
TypeChat.Program translates natural language requests into simple programs (Plans), represented as JSON.
JSON programs can be thought of as a DSL or Plan, expressed in JSON, with an associated grammar that is enforced. JSON programs can be type checked against the APIs they target. They can then be run using an interpreter, or compiled into .NET code.
TypeChat.Program includes:
// Translates user intent into typed Programs that call methods on a Math API
var model = new LanguageModel(Config.LoadOpenAI());
var api = new MathAPI();
var translator = new ProgramTranslator<IMathAPI>(model, api);
// Translate natural language request
Program program = await translator.TranslateAsync(requestText);
// Run the program
program.Run(api);
TypeChat.SemanticKernel uses Semantic Kernel 1.0. It includes classes for:
Prior versions of TypeChat.NET used the pre-release programming model of Semantic Kernel. You can access this deprecated version using the sk_prerelease branch.
dotnet add package Microsoft.TypeChat
dotnet add package Microsoft.TypeChat.Program
dotnet add package Microsoft.TypeChat.SemanticKernel
To see TypeChat.NET in action, explore the Example projects and TypeChat.Examples Library.
Each example includes an input.txt with sample input. Pass the input file as an argument to run the example in batch mode.
The sections below describe which examples will best introduce which concept. Some examples or scenarios may work best with gpt-4.
The Sentiment example is TypeChat's Hello World and a minimal introduction to JsonTranslator.
The following examples demonstrate how to use JsonTranslator, Schemas and Vocabularies:
To use TypeChat.net or run the examples, you need either:
A typical appSettings.Development.json will look like this:
// For Azure OpenAI service
{
"OpenAI": {
"Azure": true,
"ApiKey": "YOUR API KEY",
"Endpoint": "https://YOUR_RESOURCE_NAME.openai.azure.com",
"Model": "gpt-35-turbo" // Name of Azure deployment
}
}
// For Azure OpenAI service with Azure Identity: DefaultCredentials
{
"OpenAI": {
"Azure": true,
"ApiKey": "identity",
"Endpoint": "https://YOUR_RESOURCE_NAME.openai.azure.com",
"Model": "gpt-35-turbo" // Name of Azure deployment
}
}
// For OpenAI Service:
{
"OpenAI": {
"Azure": false,
"ApiKey": "YOUR API KEY",
"Endpoint": "https://api.openai.com/v1/chat/completions",
"Model": "gpt-3.5-turbo" // Name of OpenAI model
}
}
TypeChat examples accesses language models using the LanguageModel class. The OpenAIConfig class supplies configuration for LanguageModel. You initialize OpenAIConfig from your application's configuration, from a Json file or from environment variables.
See OpenAIConfig.cs for a list of :
// Your configuration
OpenAIConfig config = Config.LoadOpenAI();
// Or Json file
OpenAIConfig config = OpenAIConfig.LoadFromJsonFile(...);
// Or from config
config = OpenAIConfig.FromEnvironment();
var model = new LanguageModel(config);
You can also access a LanguageModel using the SemanticKernel Kernel object you created using a KernelBuilder.
const string modelName = "gpt-35-turbo";
new ChatLanguageModel(_kernel.GetService<IChatCompletionService>(modelName), modelName);
TypeChat accesses language models using the ILanguageModel interface. LanguageModel implements ILanguageModel.
You can use your own model client by implementing ILanguageModel. If you don't use one of the Open AI models listed above, you will likely also need to supply JsonTranslatorPrompts that work best with your model.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the MIT license.
This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow Microsoft's Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party's policies.