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
45 changes: 36 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,10 @@ A utility library with classes used by TypeChat examples. These classes may be g

To see TypeChat in action, explore the [TypeChat example projects](./examples). The list below describes which examples will best introduc which concept. Some examples or scenarios may work best with gpt-4.

* Hello World: The Sentiment example is TypeChat's Hello World. It is a minimal introduction to JsonTranslator.

* JsonTranslator and Schemas:
* Sentiment: the simplest example that demonstrates JsonTranslator and other core features

* CoffeeShop: Natural language ordering at a coffee shop
* Calendar: Transform language into calendar actions
* Restaurant: Order at a pizza restaurant
Expand All @@ -130,12 +132,15 @@ To see TypeChat in action, explore the [TypeChat example projects](./examples).
* TypeChat.Dialog:
* Healthdata

### Api Key
To run the examples, you will need an **API key** for an Open AI service. Azure Open AI and the Open AI service are both supported.
Each example includes an **input.txt** with sample input. Pass the input file as an argument to run the example in **batch mode**.

* Go to the ***examples*** folder in the solution
* Make a copy of the appSettings.json file. Name it **appSettings.Development.json**. Ensure it is in the same folder as the appSettings.json
* appSettings.Development.json is a local development only override of the settings in appSettings.json
## Api Key and Configuration
To use TypeChat.net or run the examples, you will need an **API key** for an Open AI service. Azure Open AI and the Open AI service are both supported.

### Configure Api Key for examples
* Go to the **[examples](./examples)** folder in the solution
* Make a copy of the [appSettings.json](./examples/appSettings.json) file and name it **appSettings.Development.json**. Ensure it is in the same folder as appSettings.json
* appSettings.Development.json is a local development only override of the settings in appSettings.json and is **never** checked in.
* Add your Api Key to **appSettings.Development.json**.

A typical appSettings.Development.json will look like this:
Expand All @@ -160,9 +165,31 @@ A typical appSettings.Development.json will look like this:
}
}
```
### Inputs
- Each example includes an **input.txt** with sample input.
- Pass the input file as an argument to run the example in **batch mode**.

## OpenAIConfig
TypeChat accesses language models using the [LanguageModel](./src/typechat.sk/LanguageModel.cs) class. The OpenAIConfig class provides settings used to initialize it.

You can initialize OpenAIConfig via your application's configuration system (see examples for how) or via environment variables.

See [OpenAIConfig.cs](./src/typechat.sk/OpenAIConfig.cs) for a list full list of :
* Configurable properties
* Supported environment variables.
```
// Your configuration
OpenAIConfig config = Config.LoadOpenAI();
// Or from config
config = OpenAIConfig.FromEnvironment();

var model = new LanguageModel(config);
```

### Using an Semantick Kernel directly
You can also initialize LanguageModel using a existing Microsoft Semantic IKernel you created using a KernelBuilder.
```
const string modelName = "gpt-35-turbo";
new LanguageModel(_kernel.GetService<IChatCompletion>(modelName), modelName);
```

# License

This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
Expand Down
5 changes: 4 additions & 1 deletion examples/Sentiment/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ public class SentimentApp : ConsoleApp

public SentimentApp()
{
_translator = new JsonTranslator<SentimentResponse>(new LanguageModel(Config.LoadOpenAI()));
OpenAIConfig config = Config.LoadOpenAI();
// Although this sample uses config files, you can also load config from environment variables
// OpenAIConfig config = OpenAIConfig.FromEnvironment();
_translator = new JsonTranslator<SentimentResponse>(new LanguageModel(config));
}

public override async Task ProcessInputAsync(string input, CancellationToken cancelToken)
Expand Down
45 changes: 0 additions & 45 deletions src/typechat.app/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,49 +57,4 @@ public Config()

public bool HasOpenAI => (_openAI != null);
public bool HasOpenAIEmbeddings => (_openAIEmbeddings != null);

/// <summary>
/// Load a file in .env format. Apply contained variables as process specific environment
/// variables
/// </summary>
/// <param name="filePath">Path to env file</param>
public static void ApplyEnvFile(string filePath)
{
if (!File.Exists(filePath))
{
return;
}

using StreamReader reader = new StreamReader(filePath);
string line = null;
while ((line = reader.ReadLine()) != null)
{
if (line.StartsWith("#"))
{
continue;
}
#if NET6_0_OR_GREATER
string[] envVars = line.Split('=', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
#else
string[] envVars = line.Split('=', (char)StringSplitOptions.RemoveEmptyEntries).Select(p => p.Trim()).ToArray();
#endif
if (envVars != null)
{
UpdateVariable(envVars);
}
}
}

static void UpdateVariable(string[] nvPairs)
{
if (nvPairs != null && nvPairs.Length > 0)
{
Environment.SetEnvironmentVariable(
nvPairs[0], // name
(nvPairs.Length > 1) ? nvPairs[1] : null, // value
EnvironmentVariableTarget.Process
);
}
}

}
24 changes: 24 additions & 0 deletions src/typechat.sk/OpenAIConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,36 @@ public class OpenAIConfig
/// </summary>
public static class VariableNames
{
/// <summary>
/// Api key to use with the Open AI service
/// </summary>
public const string OPENAI_API_KEY = "OPENAI_API_KEY";
/// <summary>
/// The Open AI endpoint. This follows semantic kernel conventions
/// https://api.openai.com/v1/chat/completions
/// </summary>
public const string OPENAI_ENDPOINT = "OPENAI_ENDPOINT";
/// <summary>
/// Optional: OpenAI organization
/// </summary>
public const string OPENAI_ORGANIZATION = "OPENAI_ORGANIZATION";
/// <summary>
/// Name of the language model to use
/// </summary>
public const string OPENAI_MODEL = "OPENAI_MODEL";
/// <summary>
/// Name of the embedding model to use
/// </summary>
public const string OPENAI_EMBEDDINGMODEL = "OPENAI_EMBEDDINGMODEL";
/// <summary>
/// Api key to use for Azure Open AI service
/// </summary>
public const string AZURE_OPENAI_API_KEY = "AZURE_OPENAI_API_KEY";
/// <summary>
/// Endpoint to use for Azure OpenAI service.
/// This follows the Semantic Kernel convention
/// https://YOUR_RESOURCE_NAME.openai.azure.com
/// </summary>
public const string AZURE_OPENAI_ENDPOINT = "AZURE_OPENAI_ENDPOINT";
}

Expand Down