Skip to content

Commit

Permalink
refactor: ♻️ enhance configuration for reading model base-address fro…
Browse files Browse the repository at this point in the history
…m env and command options
  • Loading branch information
mehdihadeli committed Nov 21, 2024
1 parent 6a0bcef commit 9e15799
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 29 deletions.
14 changes: 9 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
- ✅ Showing token usage count and calculated priced based on each model `input token` and `output token` price.
- ✅ Customize models information with creating a customized models information through `ModelsInformationOptions` section in the `aiassist-config.json` and a format like [predefined models information](./src/Clients/LLMs/models_information_list.json).
- ✅ Customize models options through `ModelsOptions` section in the `aiassist-config.json` and a format like [predefined models options](./src/Clients/LLMs/models_options.json).
- ✅ Terminal main commands like code, chat, explain and support some internal commands like clear, add-file, clear-history, token, ...
- ✅ Terminal main commands like `aiassist code`, `aiassist chat`, `aiassist explain` and support some internal commands like `:clear`, `:add-file`, `:clear-history`, `:token`, ...

## Get Started

Expand Down Expand Up @@ -65,31 +65,35 @@ $env:EMBEDDINGS_MODEL_API_KEY=your-embedding-api-key-here
aiassist code --chat-api-key your-chat-api-key-here --embeddings-api-key your-embedding-api-key-here
```

- If you are using AI models that need `ApiVersion` and `DeploymentId` like Azure AI Service models, you can set them by environment variable or command options.
- Set `ApiVersion` and `DeploymentId` through `environment variable`:
- If you are using AI models that need `ApiVersion`, `DeploymentId` and `BaseAddress` like Azure AI Service models, you can set them by environment variable or command options.
- Set `ApiVersion`, `DeploymentId` and `BaseAddress` through`environment variable`:

Linux terminal:

```bash
export CHAT_BASE_ADDRESS=your-chat-base-address-here
export CHAT_API_VERSION=your-chat-api-version-here
export CHAT_DEPLOYMENT_ID=your-chat-deployment-id-here
export EMBEDDINGS_BASE_ADDRESS=your-embedding-base-address-here
export EMBEDDINGS_API_VERSION=your-embedding-api-version-here
export EMBEDDINGS_DEPLOYMENT_ID=your-embedding-deployment-id-here
```

Windows Powershell Terminal:

```powershell
$env:CHAT_BASE_ADDRESS=your-chat-base-address-here
$env:CHAT_API_VERSION=your-chat-api-version-here
$env:CHAT_DEPLOYMENT_ID=your-chat-deployment-id-here
$env:EMBEDDINGS_BASE_ADDRESS=your-embedding-base-address-here
$env:EMBEDDINGS_API_VERSION=your-embedding-api-version-here
$env:EMBEDDINGS_DEPLOYMENT_ID=your-embedding-deployment-id-here
```

- Or set `ApiVersion` and `DeploymentId` through `command option`:
- Or set `ApiVersion`, `DeploymentId` and `BaseAddress` through `command option`:

```bash
aiassist code --chat-api-version your-chat-api-version-here --chat-deployment-id your-chat-deployment-id-here --embeddings-api-version your-embeddings-api-version-here --embeddings-deployment-id your-embeddings-deployment-id-here
aiassist code --chat-base-address your-chat-base-address-here --chat-api-version your-chat-api-version-here --chat-deployment-id your-chat-deployment-id-here --embeddings-base-address your-embeddings-base-address-here --embeddings-api-version your-embeddings-api-version-here --embeddings-deployment-id your-embeddings-deployment-id-here
```

- Now run the ai assistant with `aiassist` command.
Expand Down
6 changes: 0 additions & 6 deletions src/AIAssistant/AIAssistantConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,4 @@ public class Configurations
{
public const string ThemeName = "ThemeName";
}

public class Environments
{
public const string EmbeddingsModelApiKey = "EMBEDDINGS_MODEL_API_KEY";
public const string ChatModelApiKey = "CHAT_MODEL_API_KEY";
}
}
22 changes: 22 additions & 0 deletions src/AIAssistant/Commands/CodeAssistCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,21 @@ public sealed class Settings : CommandSettings
[Description("[grey] the chat model deployment-id.[/].")]
public string? ChatDeploymentId { get; set; }

[CommandOption("--chat-base-address <base-address>")]
[Description("[grey] the chat model base-address.[/].")]
public string? ChatBaseAddress { get; set; }

[CommandOption("--embeddings-api-version <version>")]
[Description("[grey] the embeddings model api version.[/].")]
public string? EmbeddingsApiVersion { get; set; }

[CommandOption("--embeddings-deployment-id <deployment-id>")]
[Description("[grey] the embeddings model deployment-id.[/].")]
public string? EmbeddingsDeploymentId { get; set; }

[CommandOption("--embeddings-base-address <base-address>")]
[Description("[grey] the embeddings model base-address.[/].")]
public string? EmbeddingsBaseAddress { get; set; }
}

public override async Task<int> ExecuteAsync(CommandContext context, Settings settings)
Expand Down Expand Up @@ -183,6 +191,13 @@ private void SetupOptions(Settings settings)
chatModel.ModelOption.DeploymentId = settings.ChatDeploymentId.Trim();
}

if (!string.IsNullOrEmpty(settings.ChatBaseAddress))
{
ArgumentException.ThrowIfNullOrEmpty(_llmOptions.ChatModel);
var chatModel = cacheModels.GetModel(_llmOptions.ChatModel);
chatModel.ModelOption.BaseAddress = settings.ChatBaseAddress.Trim();
}

if (!string.IsNullOrEmpty(settings.EmbeddingsModelApiKey))
{
ArgumentException.ThrowIfNullOrEmpty(_llmOptions.EmbeddingsModel);
Expand All @@ -204,6 +219,13 @@ private void SetupOptions(Settings settings)
embeddingModel.ModelOption.DeploymentId = settings.EmbeddingsDeploymentId.Trim();
}

if (!string.IsNullOrEmpty(settings.EmbeddingsBaseAddress))
{
ArgumentException.ThrowIfNullOrEmpty(_llmOptions.EmbeddingsModel);
var embeddingModel = cacheModels.GetModel(_llmOptions.EmbeddingsModel);
embeddingModel.ModelOption.BaseAddress = settings.EmbeddingsBaseAddress.Trim();
}

_appOptions.ContextWorkingDirectory = !string.IsNullOrEmpty(settings.ContextWorkingDirectory)
? Path.Combine(Directory.GetCurrentDirectory(), settings.ContextWorkingDirectory)
: Directory.GetCurrentDirectory(); // set to current working directory
Expand Down
74 changes: 56 additions & 18 deletions src/AIAssistant/Extensions/DependencyInjectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,35 +291,54 @@ private static void AddClientDependencies(HostApplicationBuilder builder)
client.Timeout = TimeSpan.FromSeconds(policyOptions.TimeoutSeconds);

var chatApiKey =
Environment.GetEnvironmentVariable(AIAssistantConstants.Environments.ChatModelApiKey)
Environment.GetEnvironmentVariable(ClientsConstants.Environments.ChatModelApiKey)
?? chatModel.ModelOption.ApiKey;

switch (chatModel.ModelInformation.AIProvider)
{
case AIProvider.Openai:
{
// https://platform.openai.com/docs/api-reference
ArgumentException.ThrowIfNullOrEmpty(chatApiKey);
client.BaseAddress = new Uri(
chatModel.ModelOption.BaseAddress?.Trim() ?? "https://api.openai.com"
);

var baseAddress =
Environment.GetEnvironmentVariable(ClientsConstants.Environments.ChatBaseAddress)
?? chatModel.ModelOption.BaseAddress
?? "https://api.openai.com";

client.BaseAddress = new Uri(baseAddress.Trim());
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
"Bearer",
chatApiKey
);
break;
}
case AIProvider.Azure:
{
// https://learn.microsoft.com/en-us/azure/ai-services/openai/reference
ArgumentException.ThrowIfNullOrEmpty(chatApiKey);
ArgumentException.ThrowIfNullOrEmpty(chatModel.ModelOption.BaseAddress);
client.BaseAddress = new Uri(chatModel.ModelOption.BaseAddress.Trim());

var baseAddress =
Environment.GetEnvironmentVariable(ClientsConstants.Environments.ChatBaseAddress)
?? chatModel.ModelOption.BaseAddress;
ArgumentException.ThrowIfNullOrEmpty(baseAddress);

client.BaseAddress = new Uri(baseAddress.Trim());
client.DefaultRequestHeaders.Add("api-key", chatApiKey);
break;
}

case AIProvider.Ollama:
{
var baseAddress =
Environment.GetEnvironmentVariable(ClientsConstants.Environments.ChatBaseAddress)
?? chatModel.ModelOption.BaseAddress
?? "http://localhost:11434";

// https://github.com/ollama/ollama/blob/main/docs/api.md
client.BaseAddress = new Uri(
chatModel.ModelOption.BaseAddress?.Trim() ?? "http://localhost:11434"
);
client.BaseAddress = new Uri(baseAddress.Trim());
break;
}
// case AIProvider.Anthropic:
// // https://docs.anthropic.com/en/api/messages
// ArgumentException.ThrowIfNullOrEmpty(model.ModelOption.ApiVersion);
Expand All @@ -345,35 +364,54 @@ private static void AddClientDependencies(HostApplicationBuilder builder)
client.Timeout = TimeSpan.FromSeconds(policyOptions.TimeoutSeconds);

var embeddingsApiKey =
Environment.GetEnvironmentVariable(AIAssistantConstants.Environments.EmbeddingsModelApiKey)
Environment.GetEnvironmentVariable(ClientsConstants.Environments.EmbeddingsModelApiKey)
?? embeddingModel.ModelOption.ApiKey;

switch (embeddingModel.ModelInformation.AIProvider)
{
case AIProvider.Openai:
{
// https://platform.openai.com/docs/api-reference
ArgumentException.ThrowIfNullOrEmpty(embeddingsApiKey);
client.BaseAddress = new Uri(
embeddingModel.ModelOption.BaseAddress?.Trim() ?? "https://api.openai.com"
);

var baseAddress =
Environment.GetEnvironmentVariable(ClientsConstants.Environments.EmbeddingsBaseAddress)
?? embeddingModel.ModelOption.BaseAddress
?? "https://api.openai.com";

client.BaseAddress = new Uri(baseAddress.Trim());
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
"Bearer",
embeddingsApiKey
);
break;
}
case AIProvider.Azure:
{
// https://learn.microsoft.com/en-us/azure/ai-services/openai/reference
ArgumentException.ThrowIfNullOrEmpty(embeddingsApiKey);
ArgumentException.ThrowIfNullOrEmpty(embeddingModel.ModelOption.BaseAddress);
client.BaseAddress = new Uri(embeddingModel.ModelOption.BaseAddress.Trim());

var baseAddress =
Environment.GetEnvironmentVariable(ClientsConstants.Environments.EmbeddingsBaseAddress)
?? embeddingModel.ModelOption.BaseAddress;
ArgumentException.ThrowIfNullOrEmpty(baseAddress);

client.BaseAddress = new Uri(baseAddress.Trim());
client.DefaultRequestHeaders.Add("api-key", embeddingsApiKey);
break;
}

case AIProvider.Ollama:
{
var baseAddress =
Environment.GetEnvironmentVariable(ClientsConstants.Environments.EmbeddingsBaseAddress)
?? embeddingModel.ModelOption.BaseAddress
?? "http://localhost:11434";

// https://github.com/ollama/ollama/blob/main/docs/api.md
client.BaseAddress = new Uri(
embeddingModel.ModelOption.BaseAddress?.Trim() ?? "http://localhost:11434"
);
client.BaseAddress = new Uri(baseAddress.Trim());
break;
}
}
}
);
Expand Down
4 changes: 4 additions & 0 deletions src/Clients/ClientsConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ public static class ClientsConstants
{
public class Environments
{
public const string ChatModelApiKey = "CHAT_MODEL_API_KEY";
public const string ChatBaseAddress = "CHAT_BASE_ADDRESS";
public const string ChatApiVersion = "CHAT_API_VERSION";
public const string ChatDeploymentId = "CHAT_DEPLOYMENT_ID";
public const string EmbeddingsModelApiKey = "EMBEDDINGS_MODEL_API_KEY";
public const string EmbeddingsBaseAddress = "EMBEDDINGS_BASE_ADDRESS";
public const string EmbeddingsApiVersion = "EMBEDDINGS_API_VERSION";
public const string EmbeddingsDeploymentId = "EMBEDDINGS_DEPLOYMENT_ID";
}
Expand Down

0 comments on commit 9e15799

Please sign in to comment.