Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
There are a few prerequisites to getting started with integrating LLMs into your application:
- LLM API Key - To generate messages using an LLM, you will need to have an API Key for the LLM you are using.
NuGet Package - Install the Microsoft Teams SDK:
dotnet add package Microsoft.Teams.AI
- In your application, you should include your keys in a secure way. You should include your keys securely using
appsettings.jsonor environment variables
- In your application, you should include your keys in a secure way. We recommend putting it in an .env file at the root level of your project
my-app/
|── appPackage/ # Teams app package files
├── src/
│ └── main.py # Main application code
|── .env # Environment variables
my-app/
|── appPackage/ # Teams app package files
├── src/
│ └── index.ts # Main application code
|── .env # Environment variables
Azure OpenAI
You will need to deploy a model in Azure OpenAI. View the resource creation guide for more information on how to do this.
Once you have deployed a model, configure your application using appsettings.json or appsettings.Development.json:
appsettings.Development.json
{
"AzureOpenAIKey": "your-azure-openai-api-key",
"AzureOpenAIModel": "your-azure-openai-model-deployment-name",
"AzureOpenAIEndpoint": "https://your-resource.openai.azure.com/"
}
Using configuration in your code:
var azureOpenAIModel = configuration["AzureOpenAIModel"] ??
throw new InvalidOperationException("AzureOpenAIModel not configured");
var azureOpenAIEndpoint = configuration["AzureOpenAIEndpoint"] ??
throw new InvalidOperationException("AzureOpenAIEndpoint not configured");
var azureOpenAIKey = configuration["AzureOpenAIKey"] ??
throw new InvalidOperationException("AzureOpenAIKey not configured");
var azureOpenAI = new AzureOpenAIClient(
new Uri(azureOpenAIEndpoint),
new ApiKeyCredential(azureOpenAIKey)
);
var aiModel = new OpenAIChatModel(azureOpenAIModel, azureOpenAI);
Tip
Use appsettings.Development.json for local development and keep it in .gitignore. For production, use environment variables or Azure Key Vault.
Once you have deployed a model, include the following key/values in your .env file:
AZURE_OPENAI_API_KEY=your-azure-openai-api-key
AZURE_OPENAI_MODEL=your-azure-openai-model-deployment-name
AZURE_OPENAI_ENDPOINT=your-azure-openai-endpoint
AZURE_OPENAI_API_VERSION=your-azure-openai-api-version
Once you have deployed a model, include the following key/values in your .env file:
AZURE_OPENAI_API_KEY=your-azure-openai-api-key
AZURE_OPENAI_MODEL_DEPLOYMENT_NAME=your-azure-openai-model
AZURE_OPENAI_ENDPOINT=your-azure-openai-endpoint
AZURE_OPENAI_API_VERSION=your-azure-openai-api-version
Note
The Azure OpenAI SDK handles API versioning automatically. You don't need to specify an API version manually.
Note
The AZURE_OPENAI_API_VERSION is different from the model version. This is a common point of confusion. Look for the API Version here
OpenAI
You will need to create an OpenAI account and get an API key. View the OpenAI Quickstart Guide for how to do this.
Once you have your API key, configure your application:
appsettings.Development.json
{
"OpenAIKey": "sk-your-openai-api-key",
"OpenAIModel": "gpt-4o"
}
Using configuration in your code:
var openAIKey = configuration["OpenAIKey"] ??
throw new InvalidOperationException("OpenAIKey not configured");
var openAIModel = configuration["OpenAIModel"] ?? "gpt-4o";
var aiModel = new OpenAIChatModel(openAIModel, openAIKey);
Tip
Use appsettings.Development.json for local development and keep it in .gitignore. For production, use environment variables or Azure Key Vault.
Once you have your API key, include the following key/values in your .env file:
OPENAI_API_KEY=sk-your-openai-api-key
OPENAI_MODEL=gpt-4 # Optional: defaults to gpt-4o if not specified
Once you have your API key, include the following key/values in your .env file:
OPENAI_API_KEY=sk-your-openai-api-key
Note
Automatic Environment Variable Loading: The AI models automatically read these environment variables when initialized. You can also pass these values explicitly as constructor parameters if needed for advanced configurations.
# Automatic (recommended)
model = OpenAICompletionsAIModel(model="your-model-name")
# Explicit (for advanced use cases)
model = OpenAICompletionsAIModel(
key="your-api-key",
model="your-model-name",
azure_endpoint="your-endpoint", # Azure only
api_version="your-api-version" # Azure only
)