Skip to content

Commit

Permalink
bug: Fix OpenAiCompatible's use and configuration of base address and…
Browse files Browse the repository at this point in the history
… api key (#92)
  • Loading branch information
kaspermarstal authored Jan 23, 2025
1 parent 01e04eb commit 8989ed3
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 12 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ These use cases are starting points. Experiment with different instructions to f
## Models
Cellm supports both hosted and local models. These are configured via appsettings files. In general, you should leave `appsettings.json` alone and add your own configuration to `appsettings.Local.json` only. Any settings in this file will override the default settings in `appsettings.json`.
You can use `appsettings.Local.OpenAiCompatible.json` as a starting point for configuring any model provider that is compatible with OpenAI's API. Just rename it to `appsettings.Local.json` and edit the values. The following sections shows you how to configure `appsettings.Local.json` for commonly used hosted and local models.
### Hosted LLMs
Cellm supports hosted models from Anthropic, Google, OpenAI, and any OpenAI-compatible provider. To use e.g. Claude 3.5 Sonnet from Anthropic:
Expand Down Expand Up @@ -221,7 +225,7 @@ Llamafile is a stand-alone executable that is very easy to setup. To get started
3. Rename `appsettings.Llamafile.json` to `appsettings.Local.json`.
4. Build and install Cellm.
### Dockerized Ollama and vLLM
#### Dockerized Ollama and vLLM
Both Ollama and vLLM are packaged up with docker compose files in the `docker/` folder. Ollama is designed for easy of use and vLLM is designed to run many requests in parallel which is particularly useful for this Add-In. Open WebUI in included in both Ollama and vLLM docker compose files so you can test the local model outside of Cellm. It is available at `http://localhost:3000`.
Expand Down
4 changes: 2 additions & 2 deletions src/Cellm/Models/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Cellm.Models;

public class Client(ISender sender)
internal class Client(ISender sender)
{
public async Task<Prompt> Send(Prompt prompt, Provider? provider, Uri? baseAddress, CancellationToken cancellationToken)
{
Expand All @@ -23,7 +23,7 @@ public async Task<Prompt> Send(Prompt prompt, Provider? provider, Uri? baseAddre
Provider.Llamafile => await sender.Send(new LlamafileRequest(prompt), cancellationToken),
Provider.Ollama => await sender.Send(new OllamaRequest(prompt), cancellationToken),
Provider.OpenAi => await sender.Send(new OpenAiRequest(prompt), cancellationToken),
Provider.OpenAiCompatible => await sender.Send(new OpenAiCompatibleRequest(prompt, baseAddress ?? throw new NullReferenceException($"{nameof(Provider.OpenAiCompatible)} requires BaseAddress")), cancellationToken),
Provider.OpenAiCompatible => await sender.Send(new OpenAiCompatibleRequest(prompt, baseAddress), cancellationToken),
_ => throw new NotSupportedException($"Provider {provider} is not supported")
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ namespace Cellm.Models.Providers.OpenAiCompatible;

internal record OpenAiCompatibleRequest(
Prompt Prompt,
Uri BaseAddress,
Uri? BaseAddress,
string? ApiKey = null) : IModelRequest<OpenAiCompatibleResponse>;
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
using Cellm.Models.Prompts;
using Microsoft.Extensions.Options;

namespace Cellm.Models.Providers.OpenAiCompatible;

internal class OpenAiCompatibleRequestHandler(
OpenAiCompatibleChatClientFactory openAiCompatibleChatClientFactory)
OpenAiCompatibleChatClientFactory openAiCompatibleChatClientFactory,
IOptions<OpenAiCompatibleConfiguration> openAiCompatibleConfiguration)
: IModelRequestHandler<OpenAiCompatibleRequest, OpenAiCompatibleResponse>
{

public async Task<OpenAiCompatibleResponse> Handle(OpenAiCompatibleRequest request, CancellationToken cancellationToken)
{
var api_key = string.IsNullOrEmpty(request.ApiKey) ? "API_KEY" : request.ApiKey;

var chatClient = openAiCompatibleChatClientFactory.Create(
request.BaseAddress,
request.BaseAddress ?? openAiCompatibleConfiguration.Value.BaseAddress,
request.Prompt.Options.ModelId ?? string.Empty,
request.ApiKey ?? "API_KEY");
api_key);

var chatCompletion = await chatClient.CompleteAsync(request.Prompt.Messages, request.Prompt.Options, cancellationToken);

Expand Down
10 changes: 10 additions & 0 deletions src/Cellm/appsettings.Local.OpenAiCompatible.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"OpenAiCompatibleConfiguration": {
"BaseAddress": "YOUR_API_ENDPOINT",
"DefaultModel": "YOUR_DEFAULT_MODEL",
"ApiKey": "YOUR_API_KEY_OPTIONAL"
},
"ProviderConfiguration": {
"DefaultProvider": "OpenAiCompatible"
}
}
19 changes: 14 additions & 5 deletions src/Cellm/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,28 @@
"AnthropicConfiguration": {
"BaseAddress": "https://api.anthropic.com",
"DefaultModel": "claude-3-5-sonnet-latest",
"Version": "2023-06-01"
"Version": "2023-06-01",
"ApiKey": "YOUR_ANTHROPIC_API_KEY"
},
"LlamafileConfiguration": {
"BaseAddress": "http://127.0.0.1/v1",
"DefaultModel": "gemma-2-2b"
"BaseAddress": "http://127.0.0.1:8080/v1",
"DefaultModel": "gemma-2-2b",
"ApiKey": "YOUR_LLAMAFILE_API_KEY_OPTIONAL"
},
"OllamaConfiguration": {
"BaseAddress": "http://127.0.0.1:11434",
"DefaultModel": "gemma2:2b"
"DefaultModel": "gemma2:2b",
"ApiKey": "YOUR_OLLAMA_API_KEY_OPTIONAL"
},
"OpenAiConfiguration": {
"BaseAddress": "https://api.openai.com/v1",
"DefaultModel": "gpt-4o-mini"
"DefaultModel": "gpt-4o-mini",
"ApiKey": "YOUR_OPENAI_API_KEY"
},
"OpenAiCompatibleConfiguration": {
"BaseAddress": "https://api.openai.com/v1",
"DefaultModel": "gpt-4o-mini",
"ApiKey": "YOUR_API_KEY"
},
"CellmConfiguration": {
"Debug": false
Expand Down

0 comments on commit 8989ed3

Please sign in to comment.