-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
257 additions
and
24 deletions.
There are no files selected for viewing
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
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
7 changes: 7 additions & 0 deletions
7
API/ASSISTENTE.Infrastructure.Embeddings/Contracts/EmbeddingType.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace ASSISTENTE.Infrastructure.Embeddings.Contracts; | ||
|
||
public enum EmbeddingType | ||
{ | ||
OpenAi, | ||
Ollama | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using ASSISTENTE.Infrastructure.Embeddings.Contracts; | ||
using ASSISTENTE.Infrastructure.LLM.OpenAi.Errors; | ||
using CSharpFunctionalExtensions; | ||
using OllamaSharp; | ||
using SharpToken; | ||
|
||
namespace ASSISTENTE.Infrastructure.Embeddings; | ||
|
||
internal class OllamaClient(OllamaApiClient client) : IEmbeddingClient | ||
Check warning on line 9 in API/ASSISTENTE.Infrastructure.Embeddings/OllamaClient.cs
|
||
{ | ||
private const string EmbeddingModel = "<MODEL>"; | ||
private const int MaxTokens = 8192; | ||
|
||
public async Task<Result<EmbeddingDto>> GetAsync(EmbeddingText text) | ||
Check warning on line 14 in API/ASSISTENTE.Infrastructure.Embeddings/OllamaClient.cs
|
||
{ | ||
return Result.Failure<EmbeddingDto>(ClientErrors.EmptyEmbeddings.Build()); | ||
} | ||
|
||
private static Result TextCanBeProcessable(EmbeddingText text) | ||
{ | ||
// Tokenizer is a simple library that can be used to count the number of tokens in a string. | ||
// https://github.com/Microsoft/Tokenizer -> Alternative to 'SharpToken' | ||
|
||
var encoding = GptEncoding.GetEncodingForModel(EmbeddingModel); | ||
var tokens = encoding.CountTokens(text.Text); | ||
|
||
if (tokens > MaxTokens) | ||
{ | ||
Result.Failure<EmbeddingDto>(ClientErrors.TooManyTokens.Build()); | ||
} | ||
|
||
return Result.Success(); | ||
} | ||
} |
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
21 changes: 21 additions & 0 deletions
21
API/ASSISTENTE.Infrastructure.LLM.Ollama/ASSISTENTE.Infrastructure.LLM.Ollama.csproj
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net9.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="OllamaSharp" Version="4.0.11" /> | ||
<PackageReference Include="SOFTURE.Results" Version="0.1.1" /> | ||
<PackageReference Include="SOFTURE.Settings" Version="0.1.1" /> | ||
<PackageReference Include="SOFTURE.Common.HealthCheck" Version="0.1.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<InternalsVisibleTo Include="ASSISTENTE.Infrastructure.Embeddings"/> | ||
<InternalsVisibleTo Include="ASSISTENTE.Infrastructure.LLM"/> | ||
</ItemGroup> | ||
|
||
</Project> |
25 changes: 25 additions & 0 deletions
25
API/ASSISTENTE.Infrastructure.LLM.Ollama/DependencyInjection.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using ASSISTENTE.Infrastructure.LLM.Ollama.Settings; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using OllamaSharp; | ||
using SOFTURE.Settings.Extensions; | ||
|
||
namespace ASSISTENTE.Infrastructure.LLM.Ollama | ||
{ | ||
internal static class DependencyInjection | ||
{ | ||
public static IServiceCollection AddOllama<TSettings>(this IServiceCollection services) | ||
where TSettings : IOllamaSettings | ||
{ | ||
var settings = services.GetSettings<TSettings, OllamaSettings>(x => x.Ollama); | ||
|
||
services.AddScoped<OllamaApiClient>(_ => | ||
new OllamaApiClient(new Uri(settings.Url)) | ||
{ | ||
SelectedModel = settings.SelectedModel | ||
} | ||
); | ||
|
||
return services; | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
API/ASSISTENTE.Infrastructure.LLM.Ollama/Errors/ClientErrors.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using SOFTURE.Results; | ||
|
||
namespace ASSISTENTE.Infrastructure.LLM.Ollama.Errors; | ||
|
||
internal static class ClientErrors | ||
{ | ||
public static readonly Error EmptyAnswer = | ||
new("ClientErrors.EmptyAnswer", "Empty answer from Ollama service"); | ||
} |
6 changes: 6 additions & 0 deletions
6
API/ASSISTENTE.Infrastructure.LLM.Ollama/Settings/IOllamaSettings.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace ASSISTENTE.Infrastructure.LLM.Ollama.Settings; | ||
|
||
public interface IOllamaSettings | ||
{ | ||
OllamaSettings Ollama { get; init; } | ||
} |
7 changes: 7 additions & 0 deletions
7
API/ASSISTENTE.Infrastructure.LLM.Ollama/Settings/OllamaSettings.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace ASSISTENTE.Infrastructure.LLM.Ollama.Settings; | ||
|
||
public sealed class OllamaSettings | ||
{ | ||
public required string Url { get; init; } | ||
public required string SelectedModel { get; init; } | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace ASSISTENTE.Infrastructure.LLM.Contracts; | ||
|
||
public enum LlmType | ||
{ | ||
OpenAi, | ||
Ollama | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using ASSISTENTE.Infrastructure.LLM.Contracts; | ||
using ASSISTENTE.Infrastructure.LLM.Ollama.Errors; | ||
using ASSISTENTE.Infrastructure.LLM.Ollama.Settings; | ||
using CSharpFunctionalExtensions; | ||
using Microsoft.Extensions.Options; | ||
using OllamaSharp; | ||
using OllamaSharp.Models; | ||
|
||
namespace ASSISTENTE.Infrastructure.LLM; | ||
|
||
internal sealed class OllamaClient(OllamaApiClient client, IOptions<OllamaSettings> options) : ILlmClient | ||
{ | ||
private readonly LlmClient _llmClient = LlmClient.Create("ollama"); | ||
|
||
public async Task<Result<Answer>> GenerateAnswer(Prompt prompt) | ||
{ | ||
var request = new GenerateRequest | ||
{ | ||
Stream = false, | ||
System = "You are programmer assistant, please answer correctly as you can.", | ||
Prompt = prompt.Value, | ||
Model = options.Value.SelectedModel | ||
}; | ||
|
||
string? answer = null; | ||
string? model = null; | ||
|
||
await foreach (var answerToken in client.GenerateAsync(request)) | ||
{ | ||
answer += answerToken?.Response; | ||
model = answerToken?.Model; | ||
} | ||
|
||
if (answer is null) | ||
return Result.Failure<Answer>(ClientErrors.EmptyAnswer.Build()); | ||
|
||
return Audit.Create( | ||
model: model, | ||
promptTokens: 0, | ||
completionTokens: 0 | ||
) | ||
.Bind(audit => Answer.Create(answer, prompt.Value, _llmClient, audit)); | ||
} | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace ASSISTENTE.Infrastructure.Enums; | ||
|
||
public enum PrivacyMode | ||
{ | ||
Cloud, | ||
Local | ||
} |
Oops, something went wrong.