microsoft / typechat.net

MIT License
184 stars 26 forks source link
ai artificial-intelligence llm openai types

TypeChat.NET

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.

Assemblies

TypeChat.NET currently consists of the following assemblies:

Microsoft.TypeChat

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:

Microsoft.TypeChat.Program

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);

Microsoft.TypeChat.SemanticKernel

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.

Getting Started

Prerequisite: OpenAI

Building

Nuget Packages

Examples

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.

Hello World

The Sentiment example is TypeChat's Hello World and a minimal introduction to JsonTranslator.

JsonTranslator

The following examples demonstrate how to use JsonTranslator, Schemas and Vocabularies:

Hierarchical schemas

Json Programs

Interactive agents

Api Key and Configuration

To use TypeChat.net or run the examples, you need either:

Configure Api Key or Azure Identity for examples

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
  }
}

OpenAIConfig

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 :

var model = new LanguageModel(config);


## Using Semantic Kernel
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(modelName), modelName);



## Using your own client
TypeChat accesses language models using the [ILanguageModel](src/typechat/ILanguageModel.cs) interface. [LanguageModel](src/typechat/LanguageModel.cs) 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](./src/typechat/IJsonTranslatorPrompts.cs) that work best with your model.

# Code of Conduct

This project has adopted the
[Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
For more information see the
[Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/)
or contact [opencode@microsoft.com](mailto:opencode@microsoft.com)
with any additional questions or comments.

# License

Copyright (c) Microsoft Corporation. All rights reserved.

Licensed under the [MIT](LICENSE) license.

# Trademarks

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](https://www.microsoft.com/en-us/legal/intellectualproperty/trademarks/usage/general).
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.