Skip to content

Instantly share code, notes, and snippets.

@asabla
Created February 11, 2024 12:08
Show Gist options
  • Save asabla/6789388a0cedd59a6089fcaa645ec2b9 to your computer and use it in GitHub Desktop.
Save asabla/6789388a0cedd59a6089fcaa645ec2b9 to your computer and use it in GitHub Desktop.
Semantic Kernel - Using a local (OpenAI API compatible) model
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
var kernelBuilder = Kernel.CreateBuilder()
.AddOpenAIChatCompletion(
modelId: "ollama-model-name"
apiKey: "can-be-what-ever");
kernelBuilder.Services.ConfigureHttpClientDefaults(e =>
e.ConfigurePrimaryHttpMessagehandler(() => new ExampleRedirectHandler()));
sealed class ExampleRedirectHandler : DelegatingHandler(new HttpClientHandler())
{
protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken)
{
Console.WriteLine($"[INFO] Original url {request.RequestUri}");
var newUrl = new UriBuilder(request.RequestUri!)
{
Host = "localhost"
}.Uri;
request.RequestUri = new Uri(newUrl.ToString().Replace("https://localhost", "http://localhost:11434"));
Console.WriteLine($"[INFO] New url {request.RequestUri}");
return await base.SendAsync(request, cancellationToken);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment