-
Notifications
You must be signed in to change notification settings - Fork 20
Feat: Ollama integration #118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Madzionator
wants to merge
6
commits into
main
Choose a base branch
from
feature/ollama-integration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
384758f
Ollama text chat & infra page
Madzionator d3d45a3
Ollama doesn't support image generation
Madzionator 041738e
Ollama doesn't support MCP
Madzionator bc422f8
Remove unused and redundant package references
Madzionator 3ed5cb0
versioning
Madzionator d35b376
Support local Ollama
Madzionator File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,18 @@ | ||
| using Examples.Utils; | ||
| using MaIN.Core.Hub; | ||
|
|
||
| namespace Examples.Chat; | ||
|
|
||
| public class ChatExampleOllama : IExample | ||
| { | ||
| public async Task Start() | ||
| { | ||
| OllamaExample.Setup(); // We need to set Ollama backend type and optionally provide Ollama API key | ||
| Console.WriteLine("(Ollama) ChatExample is running!"); | ||
|
|
||
| await AIHub.Chat() | ||
| .WithModel("gemma3:4b") | ||
| .WithMessage("Write a short poem about the color green.") | ||
| .CompleteAsync(interactive: true); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,16 @@ | ||
| using MaIN.Core; | ||
| using MaIN.Domain.Configuration; | ||
|
|
||
| namespace Examples.Utils; | ||
|
|
||
| public class OllamaExample | ||
| { | ||
| public static void Setup() | ||
| { | ||
| MaINBootstrapper.Initialize(configureSettings: (options) => | ||
| { | ||
| options.BackendType = BackendType.Ollama; | ||
| //options.OllamaKey = "<YOUR_OLLAMA_KEY>"; // set only if you want to use Ollama cloud | ||
| }); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,3 @@ | ||
| # 0.8.1 release | ||
|
|
||
| - Add Ollama integration |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
4 changes: 0 additions & 4 deletions
4
src/MaIN.Services/Services/ImageGenServices/ImageGenService.cs
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,77 @@ | ||
| using System.Text; | ||
| using MaIN.Domain.Configuration; | ||
| using MaIN.Domain.Entities; | ||
| using MaIN.Services.Constants; | ||
| using MaIN.Services.Services.Abstract; | ||
| using MaIN.Services.Services.LLMService.Memory; | ||
| using MaIN.Services.Services.Models; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace MaIN.Services.Services.LLMService; | ||
|
|
||
| public sealed class OllamaService( | ||
| MaINSettings settings, | ||
| INotificationService notificationService, | ||
| IHttpClientFactory httpClientFactory, | ||
| IMemoryFactory memoryFactory, | ||
| IMemoryService memoryService, | ||
| ILogger<OpenAiService>? logger = null) | ||
| : OpenAiCompatibleService(notificationService, httpClientFactory, memoryFactory, memoryService, logger) | ||
| { | ||
| private readonly MaINSettings _settings = settings ?? throw new ArgumentNullException(nameof(settings)); | ||
|
|
||
| private bool HasApiKey => !string.IsNullOrEmpty(_settings.OllamaKey) || !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("OLLAMA_API_KEY")); | ||
|
|
||
| protected override string HttpClientName => HasApiKey ? ServiceConstants.HttpClients.OllamaClient : ServiceConstants.HttpClients.OllamaLocalClient; | ||
| protected override string ChatCompletionsUrl => HasApiKey ? ServiceConstants.ApiUrls.OllamaOpenAiChatCompletions : ServiceConstants.ApiUrls.OllamaLocalOpenAiChatCompletions; | ||
| protected override string ModelsUrl => HasApiKey ? ServiceConstants.ApiUrls.OllamaModels : ServiceConstants.ApiUrls.OllamaLocalModels; | ||
|
|
||
| protected override string GetApiKey() | ||
| { | ||
| return _settings.OllamaKey ?? Environment.GetEnvironmentVariable("OLLAMA_API_KEY") ?? string.Empty; | ||
| } | ||
|
|
||
| protected override void ValidateApiKey() | ||
| { | ||
| // No validation required - local Ollama doesn't need an API key | ||
| // Cloud Ollama will fail at runtime if the key is missing | ||
| } | ||
|
|
||
| public override async Task<ChatResult?> AskMemory( | ||
| Chat chat, | ||
| ChatMemoryOptions memoryOptions, | ||
| ChatRequestOptions requestOptions, | ||
| CancellationToken cancellationToken = default) | ||
| { | ||
| var lastMsg = chat.Messages.Last(); | ||
| var filePaths = await DocumentProcessor.ConvertToFilesContent(memoryOptions); | ||
| var message = new Message() | ||
| { | ||
| Role = ServiceConstants.Roles.User, | ||
| Content = ComposeMessage(lastMsg, filePaths), | ||
| Type = MessageType.CloudLLM | ||
| }; | ||
|
|
||
| chat.Messages.Last().Content = message.Content; | ||
| chat.Messages.Last().Files = []; | ||
| var result = await Send(chat, new ChatRequestOptions(), cancellationToken); | ||
| chat.Messages.Last().Content = lastMsg.Content; | ||
| return result; | ||
| } | ||
|
|
||
| private string ComposeMessage(Message lastMsg, string[] filePaths) | ||
| { | ||
| var stringBuilder = new StringBuilder(); | ||
| stringBuilder.AppendLine($"== FILES IN MEMORY"); | ||
| foreach (var path in filePaths) | ||
| { | ||
| var doc = DocumentProcessor.ProcessDocument(path); | ||
| stringBuilder.Append(doc); | ||
| stringBuilder.AppendLine(); | ||
| } | ||
| stringBuilder.AppendLine($"== END OF FILES"); | ||
| stringBuilder.AppendLine(); | ||
| stringBuilder.Append(lastMsg.Content); | ||
| return stringBuilder.ToString(); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.