Skip to content
This repository has been archived by the owner on Sep 25, 2024. It is now read-only.

Multiple Updates - regions, versions, serverless #92

Merged
merged 1 commit into from
Mar 1, 2024
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
3 changes: 1 addition & 2 deletions Services/ChatService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Cosmos.Chat.GPT.Constants;
using Cosmos.Chat.GPT.Models;
using Microsoft.AspNetCore.SignalR.Protocol;
using SharpToken;

namespace Cosmos.Chat.GPT.Services;
Expand Down Expand Up @@ -107,7 +106,7 @@ public async Task DeleteChatSessionAsync(string? sessionId)
}

/// <summary>
/// Get a completion from _openAiService
/// Get a completion from Azure OpenAi Service
/// </summary>
public async Task<string> GetChatCompletionAsync(string? sessionId, string promptText)
{
Expand Down
23 changes: 12 additions & 11 deletions Services/OpenAiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,31 @@ public OpenAiService(string endpoint, string key, string modelName)
/// Sends a prompt to the deployed OpenAI LLM model and returns the response.
/// </summary>
/// <param name="sessionId">Chat session identifier for the current conversation.</param>
/// <param name="userPrompt">Prompt message to send to the deployment.</param>
/// <param name="userPrompt">Prompt message and chat history to send to the model.</param>
/// <returns>Response from the OpenAI model along with tokens for the prompt and response.</returns>
public async Task<(string completionText, int completionTokens)> GetChatCompletionAsync(string sessionId, string userPrompt)
{

ChatMessage systemMessage = new(ChatRole.System, _systemPrompt);
ChatMessage userMessage = new(ChatRole.User, userPrompt);
ChatRequestSystemMessage systemMessage = new(_systemPrompt);
ChatRequestUserMessage userMessage = new(userPrompt);

ChatCompletionsOptions options = new()
{

DeploymentName = _modelName,
Messages =
{
systemMessage,
userMessage
},
User = sessionId,
MaxTokens = 2000,
Temperature = 0.3f,
NucleusSamplingFactor = 0.5f,
MaxTokens = 1000,
Temperature = 0.2f,
NucleusSamplingFactor = 0.7f,
FrequencyPenalty = 0,
PresencePenalty = 0
};

Response<ChatCompletions> completionsResponse = await _client.GetChatCompletionsAsync(_modelName, options);
Response<ChatCompletions> completionsResponse = await _client.GetChatCompletionsAsync(options);

ChatCompletions completions = completionsResponse.Value;

Expand All @@ -94,11 +94,12 @@ public OpenAiService(string endpoint, string key, string modelName)
public async Task<string> SummarizeAsync(string sessionId, string conversationText)
{

ChatMessage systemMessage = new(ChatRole.System, _summarizePrompt);
ChatMessage userMessage = new(ChatRole.User, conversationText);
ChatRequestSystemMessage systemMessage = new(_summarizePrompt);
ChatRequestUserMessage userMessage = new(conversationText);

ChatCompletionsOptions options = new()
{
DeploymentName = _modelName,
Messages = {
systemMessage,
userMessage
Expand All @@ -111,7 +112,7 @@ public async Task<string> SummarizeAsync(string sessionId, string conversationTe
PresencePenalty = 0
};

Response<ChatCompletions> completionsResponse = await _client.GetChatCompletionsAsync(_modelName, options);
Response<ChatCompletions> completionsResponse = await _client.GetChatCompletionsAsync(options);

ChatCompletions completions = completionsResponse.Value;

Expand Down
64 changes: 41 additions & 23 deletions azuredeploy-no-aoai.bicep
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
@description('Location where all resources will be deployed. This value defaults to the **East US** region.')
@description('Location where all resources are deployed. Is limited by the regions with Azure OpenAI availability. Defaults to **East US** region.')
@allowed([
'Australia East'
'Brazil South'
'Canada Central'
'Canada East'
'East US'
'East US 2'
'France Central'
'Germany West Central'
'Japan East'
'Korea Central'
'North Central US'
'Norway East'
'Poland Central'
'South Africa North'
'South Central US'
'South India'
'Sweden Central'
'Switzerland North'
'UAE North'
'UK South'
'West Europe'
'West US'
'West US 3'
])
param location string = 'East US'

Expand All @@ -21,9 +34,6 @@ The name defaults to a unique string generated from the resource group identifie
''')
param name string = uniqueString(resourceGroup().id)

@description('Boolean indicating whether Azure Cosmos DB free tier should be used for the account. This defaults to **true**.')
param cosmosDbEnableFreeTier bool = true

@description('Specifies the SKU for the Azure App Service plan. Defaults to **F1** Free Tier')
@allowed([
'F1'
Expand All @@ -37,28 +47,37 @@ param openAiAccountName string = ''

@description('Specifies the key for Azure OpenAI account.')
@secure()
param openAiKey string = ''
param openAiAccountKey string = ''

@description('Specifies the deployed model name for your Azure OpenAI account completions API.')
param openAiModelName string = ''
@description('Specifies the deployed model name for your Azure OpenAI account GPT model.')
param openAiGptModelName string = ''

@description('Git repository URL for the chat application. This defaults to the [`azure-samples/cosmosdb-chatgpt`](https://github.com/azure-samples/cosmosdb-chatgpt) repository.')
param appGitRepository string = 'https://github.com/azure-samples/cosmosdb-chatgpt.git'

@description('Git repository branch for the chat application. This defaults to the [**main** branch of the `azure-samples/cosmosdb-chatgpt`](https://github.com/azure-samples/cosmosdb-chatgpt/tree/main) repository.')
param appGetRepositoryBranch string = 'main'

var openAiEndpoint = 'https://${openAiAccountName}.openai.azure.com'
var openAiSettings = {
name: openAiAccountName
endpoint: 'https://${openAiAccountName}.openai.azure.com'
key: openAiAccountKey
sku: 'S0'
maxConversationTokens: '2000'
model: {
deployment: {
name: openAiGptModelName
}
}
}

var cosmosDbSettings = {
name: '${name}-cosmos-nosql'
enableFreeTier: cosmosDbEnableFreeTier
database: {
name: 'chatdatabase'
}
container: {
name: 'chatcontainer'
throughput: 400
}
}

Expand All @@ -76,7 +95,7 @@ var appServiceSettings = {
sku: appServiceSku
}

resource cosmosDbAccount 'Microsoft.DocumentDB/databaseAccounts@2022-08-15' = {
resource cosmosDbAccount 'Microsoft.DocumentDB/databaseAccounts@2023-04-15' = {
name: cosmosDbSettings.name
location: location
kind: 'GlobalDocumentDB'
Expand All @@ -85,7 +104,7 @@ resource cosmosDbAccount 'Microsoft.DocumentDB/databaseAccounts@2022-08-15' = {
defaultConsistencyLevel: 'Session'
}
databaseAccountOfferType: 'Standard'
enableFreeTier: cosmosDbSettings.enableFreeTier
capabilities: [ { name: 'EnableServerless' } ]
locations: [
{
failoverPriority: 0
Expand All @@ -96,7 +115,7 @@ resource cosmosDbAccount 'Microsoft.DocumentDB/databaseAccounts@2022-08-15' = {
}
}

resource cosmosDbDatabase 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases@2022-08-15' = {
resource cosmosDbDatabase 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases@2023-04-15' = {
parent: cosmosDbAccount
name: cosmosDbSettings.database.name
properties: {
Expand All @@ -106,7 +125,7 @@ resource cosmosDbDatabase 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases@20
}
}

resource cosmosDbContainer 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers@2022-08-15' = {
resource cosmosDbContainer 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers@2023-04-15' = {
parent: cosmosDbDatabase
name: cosmosDbSettings.container.name
properties: {
Expand Down Expand Up @@ -138,20 +157,19 @@ resource cosmosDbContainer 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/c
}
}
options: {
throughput: cosmosDbSettings.container.throughput
}
}
}

resource appServicePlan 'Microsoft.Web/serverfarms@2022-03-01' = {
resource appServicePlan 'Microsoft.Web/serverfarms@2022-09-01' = {
name: appServiceSettings.plan.name
location: location
sku: {
name: appServiceSettings.sku
}
}

resource appServiceWeb 'Microsoft.Web/sites@2022-03-01' = {
resource appServiceWeb 'Microsoft.Web/sites@2022-09-01' = {
name: appServiceSettings.web.name
location: location
properties: {
Expand All @@ -160,7 +178,7 @@ resource appServiceWeb 'Microsoft.Web/sites@2022-03-01' = {
}
}

resource appServiceWebSettings 'Microsoft.Web/sites/config@2022-03-01' = {
resource appServiceWebSettings 'Microsoft.Web/sites/config@2022-09-01' = {
parent: appServiceWeb
name: 'appsettings'
kind: 'string'
Expand All @@ -169,14 +187,14 @@ resource appServiceWebSettings 'Microsoft.Web/sites/config@2022-03-01' = {
COSMOSDB__KEY: cosmosDbAccount.listKeys().primaryMasterKey
COSMOSDB__DATABASE: cosmosDbDatabase.name
COSMOSDB__CONTAINER: cosmosDbContainer.name
OPENAI__ENDPOINT: openAiEndpoint
OPENAI__KEY: openAiKey
OPENAI__MODELNAME: openAiModelName
OPENAI__MAXCONVERSATIONTOKENS: '2000'
OPENAI__ENDPOINT: openAiSettings.endpoint
OPENAI__KEY: openAiSettings.key
OPENAI__MODELNAME: openAiSettings.model.deployment.name
OPENAI__MAXCONVERSATIONTOKENS: openAiSettings.maxConversationTokens
}
}

resource appServiceWebDeployment 'Microsoft.Web/sites/sourcecontrols@2021-03-01' = {
resource appServiceWebDeployment 'Microsoft.Web/sites/sourcecontrols@2022-09-01' = {
parent: appServiceWeb
name: 'web'
properties: {
Expand Down
Loading