Created
February 11, 2024 12:08
-
-
Save asabla/6789388a0cedd59a6089fcaa645ec2b9 to your computer and use it in GitHub Desktop.
Semantic Kernel - Using a local (OpenAI API compatible) model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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