54 lines
1.7 KiB
C#
54 lines
1.7 KiB
C#
using ChatBot.Services.Interfaces;
|
|
using OllamaSharp;
|
|
using OllamaSharp.Models;
|
|
using OllamaSharp.Models.Chat;
|
|
|
|
namespace ChatBot.Services
|
|
{
|
|
/// <summary>
|
|
/// Adapter for OllamaSharp client to implement IOllamaClient interface
|
|
/// </summary>
|
|
public class OllamaClientAdapter : IOllamaClient
|
|
{
|
|
private readonly OllamaApiClient _client;
|
|
|
|
public OllamaClientAdapter(string url)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(url))
|
|
throw new ArgumentException("URL cannot be empty", nameof(url));
|
|
|
|
// Configure HttpClient to ignore SSL certificate issues
|
|
var httpClientHandler = new HttpClientHandler
|
|
{
|
|
#pragma warning disable S4830 // Enable server certificate validation on this SSL/TLS connection
|
|
ServerCertificateCustomValidationCallback = (
|
|
message,
|
|
cert,
|
|
chain,
|
|
sslPolicyErrors
|
|
) => true
|
|
#pragma warning restore S4830 // Enable server certificate validation on this SSL/TLS connection
|
|
};
|
|
|
|
var httpClient = new HttpClient(httpClientHandler) { BaseAddress = new Uri(url) };
|
|
_client = new OllamaApiClient(httpClient, url);
|
|
}
|
|
|
|
public string SelectedModel
|
|
{
|
|
get => _client.SelectedModel;
|
|
set => _client.SelectedModel = value;
|
|
}
|
|
|
|
public IAsyncEnumerable<ChatResponseStream?> ChatAsync(ChatRequest request)
|
|
{
|
|
return _client.ChatAsync(request);
|
|
}
|
|
|
|
public Task<IEnumerable<Model>> ListLocalModelsAsync()
|
|
{
|
|
return _client.ListLocalModelsAsync();
|
|
}
|
|
}
|
|
}
|