Skip to content

Commit

Permalink
feat(Ollama): Updated to use Ollama SDK from NuGet.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed May 12, 2024
1 parent 8b2106c commit ade17bb
Show file tree
Hide file tree
Showing 16 changed files with 53 additions and 643 deletions.
5 changes: 3 additions & 2 deletions examples/LangChain.Samples.Serve/Program.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using LangChain.Memory;
using LangChain.Providers;
using LangChain.Providers.Ollama;
using LangChain.Serve;
using LangChain.Serve.Abstractions.Repository;
using Ollama;
using static LangChain.Chains.Chain;
using Message = LangChain.Providers.Message;
using MessageRole = LangChain.Providers.MessageRole;


var builder = WebApplication.CreateBuilder();
Expand All @@ -15,7 +16,7 @@
builder.Services.AddLangChainServe();

// 2. Create a model
var model = new OllamaChatModel(new OllamaProvider(options: new OllamaOptions
var model = new OllamaChatModel(new OllamaProvider(options: new RequestOptions
{
Temperature = 0,
Stop = ["User:"],
Expand Down
46 changes: 25 additions & 21 deletions src/Meta/test/WikiTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using LangChain.Chains.StackableChains.Agents.Tools.BuiltIn;
using LangChain.Databases;
using LangChain.Databases.Sqlite;
using LangChain.Extensions;
using LangChain.Memory;
Expand All @@ -9,7 +8,9 @@
using LangChain.Providers.LLamaSharp;
using LangChain.Providers.OpenAI.Predefined;
using LangChain.DocumentLoaders;
using LangChain.Providers.Ollama;
using LangChain.Splitters.Text;
using Ollama;
using static LangChain.Chains.Chain;

namespace LangChain.IntegrationTests;
Expand All @@ -21,15 +22,16 @@ public class WikiTests
[Test]
public async Task AgentWithOllama()
{
var model = new OllamaLanguageModelInstruction("mistral:latest",
"http://localhost:11434",
options: new OllamaLanguageModelOptions
var provider = new OllamaProvider(
// "http://172.16.50.107:11434",
options: new RequestOptions
{
Temperature = 0,
}).UseConsoleForDebug();
});
var model = new OllamaChatModel(provider, id: "llama3").UseConsoleForDebug();

var chain =
Set("What is tryAGI/LangChain?")
Set("What is tryAGI/LangChain? In 5 words")
| LLM(model);

await chain.RunAsync();
Expand All @@ -38,13 +40,13 @@ public async Task AgentWithOllama()
[Test]
public async Task AgentWithOllamaReact()
{
var model = new OllamaLanguageModelInstruction("mistral:latest",
"http://localhost:11434",
options: new OllamaLanguageModelOptions()
var provider = new OllamaProvider(
options: new RequestOptions
{
Stop = new[] { "Observation", "[END]" }, // add injection word `Observation` and `[END]` to stop the model(just as additional safety feature)
Temperature = 0
}).UseConsoleForDebug();
});
var model = new OllamaChatModel(provider, id: "mistral:latest").UseConsoleForDebug();

// create a google search tool
var searchTool = new GoogleCustomSearchTool(key: "<your key>", cx: "<your cx>", resultsLimit: 1);
Expand Down Expand Up @@ -158,13 +160,13 @@ public async Task HowToUseOpenAiProviderSmaller()
[Test]
public async Task ImageGenerationWithOllamaAndStableDiffusion()
{
var olmodel = new OllamaLanguageModelInstruction("mistral:latest",
"http://localhost:11434",
options: new OllamaLanguageModelOptions()
var provider = new OllamaProvider(
options: new RequestOptions
{
Stop = new[] { "\n" },
Temperature = 0
}).UseConsoleForDebug();
});
var llm = new OllamaChatModel(provider, id: "mistral:latest").UseConsoleForDebug();

var sdmodel = new Automatic1111Model
{
Expand Down Expand Up @@ -201,7 +203,7 @@ public async Task ImageGenerationWithOllamaAndStableDiffusion()

var chain = Set("a cute girl cosplaying a cat") // describe a desired image in simple words
| Template(template, outputKey: "prompt") // insert our description into the template
| LLM(olmodel, inputKey: "prompt", outputKey: "image_prompt") // ask ollama to generate a prompt for stable diffusion
| LLM(llm, inputKey: "prompt", outputKey: "image_prompt") // ask ollama to generate a prompt for stable diffusion
| GenerateImage(sdmodel, inputKey: "image_prompt", outputKey: "image") // generate an image using stable diffusion
| SaveIntoFile("image.png", inputKey: "image"); // save the image into a file

Expand All @@ -217,11 +219,13 @@ public async Task RagWithOpenAiOllama()
var embeddingModel = new TextEmbeddingV3SmallModel(apiKey);

// prepare Ollama with mistral model
var model = new OllamaLanguageModelInstruction("mistral:latest", options: new OllamaLanguageModelOptions
{
Stop = ["\n"],
Temperature = 0.0f,
}).UseConsoleForDebug();
var provider = new OllamaProvider(
options: new RequestOptions
{
Stop = ["\n"],
Temperature = 0.0f,
});
var llm = new OllamaChatModel(provider, id: "mistral:latest").UseConsoleForDebug();

using var vectorDatabase = new SqLiteVectorDatabase("vectors.db");
var vectorCollection = await vectorDatabase.AddDocumentsFromAsync<PdfPigPdfLoader>(
Expand All @@ -247,7 +251,7 @@ public async Task RagWithOpenAiOllama()
| RetrieveDocuments(vectorCollection, embeddingModel, inputKey: "question", outputKey: "documents", amount: 5) // take 5 most similar documents
| StuffDocuments(inputKey: "documents", outputKey: "context") // combine documents together and put them into context
| Template(promptText) // replace context and question in the prompt with their values
| LLM(model); // send the result to the language model
| LLM(llm); // send the result to the language model

var result = await chain.RunAsync("text", CancellationToken.None); // get chain result

Expand Down
6 changes: 5 additions & 1 deletion src/Providers/Ollama/src/LangChain.Providers.Ollama.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFrameworks>net4.6.2;netstandard2.0;net6.0;net8.0</TargetFrameworks>
<SignAssembly>false</SignAssembly>
<NoWarn>$(NoWarn);CA1003;CA1307;CA1054;CA1711;CA1819;CA2234</NoWarn>
<NoWarn>$(NoWarn);CA1003;CA1307;CA1054;CA1711;CA1819;CA2234;CS3001;CS3003</NoWarn>
<IsAotCompatible>false</IsAotCompatible>
<IsTrimmable>false</IsTrimmable>
<EnableTrimAnalyzer>false</EnableTrimAnalyzer>
Expand All @@ -27,6 +27,10 @@
<PackageTags>$(PackageTags);Ollama;api</PackageTags>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Ollama" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Abstractions\src\LangChain.Providers.Abstractions.csproj" />
</ItemGroup>
Expand Down
18 changes: 0 additions & 18 deletions src/Providers/Ollama/src/Legacy.cs

This file was deleted.

13 changes: 7 additions & 6 deletions src/Providers/Ollama/src/OllamaChatModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using Ollama;

namespace LangChain.Providers.Ollama;

Expand Down Expand Up @@ -31,31 +32,31 @@ public override async Task<ChatResponse> GenerateAsync(
{
request = request ?? throw new ArgumentNullException(nameof(request));

var models = await Provider.Api.ListLocalModels().ConfigureAwait(false);
if (models.All(x => x.Name != Id))
var models = await Provider.Api.ListLocalModelsAsync(cancellationToken).ConfigureAwait(false);
if (models.All(x => x.Model != Id))
{
await Provider.Api.PullModel(Id).ConfigureAwait(false);
await Provider.Api.PullModelAsync(Id, cancellationToken: cancellationToken).WaitAsync().ConfigureAwait(false);
}

var prompt = ToPrompt(request.Messages);
var watch = Stopwatch.StartNew();
var response = Provider.Api.GenerateCompletion(new GenerateCompletionRequest()
var response = Provider.Api.GetCompletionAsync(new GenerateCompletionRequest
{
Prompt = prompt,
Model = Id,
Options = Provider.Options,
Stream = true,
Raw = true,
Format = UseJson ? "json" : string.Empty,
});
}, cancellationToken);

OnPromptSent(prompt);

var buf = "";
await foreach (var completion in response)
{
buf += completion.Response;
OnPartialResponseGenerated(completion.Response);
OnPartialResponseGenerated(completion.Response ?? string.Empty);
}

OnCompletedResponseGenerated(buf);
Expand Down
14 changes: 8 additions & 6 deletions src/Providers/Ollama/src/OllamaEmbeddingModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace LangChain.Providers.Ollama;
using Ollama;

namespace LangChain.Providers.Ollama;

/// <summary>
///
Expand All @@ -23,17 +25,17 @@ public async Task<EmbeddingResponse> CreateEmbeddingsAsync(
{
request = request ?? throw new ArgumentNullException(nameof(request));

var results = new List<double[]>(capacity: request.Strings.Count);
var results = new List<IList<double>>(capacity: request.Strings.Count);
foreach (var prompt in request.Strings)
{
var response = await Provider.Api.GenerateEmbeddings(new GenerateEmbeddingRequest
var response = await Provider.Api.GenerateEmbeddingsAsync(new GenerateEmbeddingRequest
{
Prompt = prompt,
Model = Id,
Options = Provider.Options,
}).ConfigureAwait(false);
}, cancellationToken).ConfigureAwait(false);

results.Add(response.Embedding);
results.Add(response.Embedding ?? []);
}

return new EmbeddingResponse
Expand All @@ -42,7 +44,7 @@ public async Task<EmbeddingResponse> CreateEmbeddingsAsync(
.Select(x => x.Select(y => (float)y).ToArray())
.ToArray(),
UsedSettings = EmbeddingSettings.Default,
Dimensions = results.FirstOrDefault()?.Length ?? 0,
Dimensions = results.FirstOrDefault()?.Count ?? 0,
};
}
}
8 changes: 5 additions & 3 deletions src/Providers/Ollama/src/OllamaProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace LangChain.Providers.Ollama;
using Ollama;

namespace LangChain.Providers.Ollama;

/// <summary>
///
Expand All @@ -7,7 +9,7 @@
/// <param name="options"></param>
public class OllamaProvider(
string url = "http://localhost:11434",
OllamaOptions? options = null)
RequestOptions? options = null)
: Provider(id: "ollama")
{
/// <summary>
Expand All @@ -18,5 +20,5 @@ public class OllamaProvider(
/// <summary>
///
/// </summary>
public OllamaOptions Options { get; } = options ?? new OllamaOptions();
public RequestOptions Options { get; } = options ?? new RequestOptions();
}

This file was deleted.

Loading

0 comments on commit ade17bb

Please sign in to comment.