Skip to content
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

Adapts to SK Kernel Memory #226

Merged
merged 11 commits into from
Nov 4, 2023
Binary file added LLama.Examples/Assets/sample-SK-Readme.pdf
Binary file not shown.
4 changes: 4 additions & 0 deletions LLama.Examples/LLama.Examples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\LLama.KernelMemory\LLamaSharp.KernelMemory.csproj" />
<ProjectReference Include="..\LLama.SemanticKernel\LLamaSharp.SemanticKernel.csproj" />
<ProjectReference Include="..\LLama\LLamaSharp.csproj" />
</ItemGroup>
Expand Down Expand Up @@ -62,6 +63,9 @@
<None Update="Assets\reason-act.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Assets\sample-SK-Readme.pdf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
47 changes: 47 additions & 0 deletions LLama.Examples/NewVersion/KernelMemory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LLamaSharp.KernelMemory;
using Microsoft.KernelMemory;
using Microsoft.KernelMemory.Handlers;

namespace LLama.Examples.NewVersion
{
public class KernelMemory
{
public static async Task Run()
{
Console.WriteLine("Example from: https://github.com/microsoft/kernel-memory/blob/main/examples/101-using-core-nuget/Program.cs");
Console.Write("Please input your model path: ");
var modelPath = Console.ReadLine();
var memory = new KernelMemoryBuilder()
.WithLLamaSharpDefaults(new LLamaSharpConfig(modelPath))
.With(new TextPartitioningOptions
{
MaxTokensPerParagraph = 300,
MaxTokensPerLine = 100,
OverlappingTokens = 30
})
.BuildServerlessClient();

await memory.ImportDocumentAsync(@"./Assets/sample-SK-Readme.pdf", steps: Constants.PipelineWithoutSummary);

var question = "What's Semantic Kernel?";

Console.WriteLine($"\n\nQuestion: {question}");

var answer = await memory.AskAsync(question);

Console.WriteLine($"\nAnswer: {answer.Result}");

Console.WriteLine("\n\n Sources:\n");

foreach (var x in answer.RelevantSources)
{
Console.WriteLine($" - {x.SourceName} - {x.Link} [{x.Partitions.First().LastUpdate:D}]");
}
}
}
}
23 changes: 14 additions & 9 deletions LLama.Examples/NewVersion/TestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public static async Task Run()
Console.WriteLine("13: Semantic Kernel Memory.");
Console.WriteLine("14: Coding Assistant.");
Console.WriteLine("15: Batch Decoding.");
Console.WriteLine("16: SK Kernel Memory.");

while (true)
{
Expand All @@ -37,31 +38,31 @@ public static async Task Run()
{
await ChatSessionStripRoleName.Run();
}
else if(choice == 2)
else if (choice == 2)
{
await InteractiveModeExecute.Run();
}
else if(choice == 3)
else if (choice == 3)
{
await InstructModeExecute.Run();
}
else if(choice == 4)
else if (choice == 4)
{
await StatelessModeExecute.Run();
}
else if(choice == 5)
else if (choice == 5)
{
await SaveAndLoadSession.Run();
}
else if(choice == 6)
else if (choice == 6)
{
await LoadAndSaveState.Run();
}
else if(choice == 7)
else if (choice == 7)
{
GetEmbeddings.Run();
}
else if(choice == 8)
else if (choice == 8)
{
QuantizeModel.Run();
}
Expand All @@ -85,14 +86,18 @@ public static async Task Run()
{
await SemanticKernelMemory.Run();
}
else if(choice == 14)
else if (choice == 14)
{
await CodingAssistant.Run();
}
else if (choice == 15)
{
await BatchedDecoding.Run();
}
else if (choice == 16)
{
await KernelMemory.Run();
}
else
{
Console.WriteLine("Cannot parse your choice. Please select again.");
Expand All @@ -103,5 +108,5 @@ public static async Task Run()
}
}


}
52 changes: 52 additions & 0 deletions LLama.KernelMemory/BuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Microsoft.KernelMemory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LLamaSharp.KernelMemory
{
/// <summary>
/// Provides extension methods for the KernelMemoryBuilder class.
/// </summary>
public static class BuilderExtensions
{
/// <summary>
/// Adds LLamaSharpTextEmbeddingGeneration to the KernelMemoryBuilder.
/// </summary>
/// <param name="builder">The KernelMemoryBuilder instance.</param>
/// <param name="config">The LLamaSharpConfig instance.</param>
/// <returns>The KernelMemoryBuilder instance with LLamaSharpTextEmbeddingGeneration added.</returns>
public static KernelMemoryBuilder WithLLamaSharpTextEmbeddingGeneration(this KernelMemoryBuilder builder, LLamaSharpConfig config)
{
builder.WithCustomEmbeddingGeneration(new LLamaSharpTextEmbeddingGeneration(config));
return builder;
}

/// <summary>
/// Adds LLamaSharpTextGeneration to the KernelMemoryBuilder.
/// </summary>
/// <param name="builder">The KernelMemoryBuilder instance.</param>
/// <param name="config">The LLamaSharpConfig instance.</param>
/// <returns>The KernelMemoryBuilder instance with LLamaSharpTextGeneration added.</returns>
public static KernelMemoryBuilder WithLLamaSharpTextGeneration(this KernelMemoryBuilder builder, LLamaSharpConfig config)
{
builder.WithCustomTextGeneration(new LlamaSharpTextGeneration(config));
return builder;
}

/// <summary>
/// Adds LLamaSharpTextEmbeddingGeneration and LLamaSharpTextGeneration to the KernelMemoryBuilder.
/// </summary>
/// <param name="builder">The KernelMemoryBuilder instance.</param>
/// <param name="config">The LLamaSharpConfig instance.</param>
/// <returns>The KernelMemoryBuilder instance with LLamaSharpTextEmbeddingGeneration and LLamaSharpTextGeneration added.</returns>
public static KernelMemoryBuilder WithLLamaSharpDefaults(this KernelMemoryBuilder builder, LLamaSharpConfig config)
{
builder.WithLLamaSharpTextEmbeddingGeneration(config);
builder.WithLLamaSharpTextGeneration(config);
return builder;
}
}
}
17 changes: 17 additions & 0 deletions LLama.KernelMemory/LLamaSharp.KernelMemory.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.KernelMemory.Core" Version="0.5.231030.1-preview" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\LLama\LLamaSharp.csproj" />
</ItemGroup>

</Project>
54 changes: 54 additions & 0 deletions LLama.KernelMemory/LLamaSharpTextEmbeddingGeneration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using LLama;
using LLama.Common;
using Microsoft.SemanticKernel.AI.Embeddings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LLamaSharp.KernelMemory
{
/// <summary>
/// Provides text embedding generation for LLamaSharp.
/// </summary>
public class LLamaSharpTextEmbeddingGeneration : ITextEmbeddingGeneration, IDisposable
{
private readonly LLamaSharpConfig _config;
private readonly LLamaEmbedder _embedder;
private readonly LLamaWeights _weights;

/// <summary>
/// Initializes a new instance of the <see cref="LLamaSharpTextEmbeddingGeneration"/> class.
/// </summary>
/// <param name="config">The configuration for LLamaSharp.</param>
public LLamaSharpTextEmbeddingGeneration(LLamaSharpConfig config)
{
this._config = config;
var @params = new ModelParams(_config.ModelPath);
_weights = LLamaWeights.LoadFromFile(@params);
_embedder = new LLamaEmbedder(_weights, @params);
}

/// <inheritdoc/>
public void Dispose()
{
_embedder.Dispose();
_weights.Dispose();
}

/// <inheritdoc/>
public Task<IList<ReadOnlyMemory<float>>> GenerateEmbeddingsAsync(IList<string> data, CancellationToken cancellationToken = default)
{
IList<ReadOnlyMemory<float>> results = new List<ReadOnlyMemory<float>>();

foreach (var d in data)
{
var embeddings = _embedder.GetEmbeddings(d);
results.Add(new ReadOnlyMemory<float>(embeddings));
}

return Task.FromResult(results);
}
}
}
43 changes: 43 additions & 0 deletions LLama.KernelMemory/LlamaSharpConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LLamaSharp.KernelMemory
{
/// <summary>
/// Represents the configuration for LLamaSharp.
/// </summary>
public class LLamaSharpConfig
{
/// <summary>
/// Initializes a new instance of the <see cref="LLamaSharpConfig"/> class.
/// </summary>
/// <param name="modelPath">The path to the model file.</param>
public LLamaSharpConfig(string modelPath)
{
ModelPath = modelPath;
}

/// <summary>
/// Gets or sets the path to the model file.
/// </summary>
public string ModelPath { get; set; }

/// <summary>
/// Gets or sets the size of the context.
/// </summary>
public uint? ContextSize { get; set; }

/// <summary>
/// Gets or sets the seed value.
/// </summary>
public uint? Seed { get; set; }

/// <summary>
/// Gets or sets the number of GPU layers.
/// </summary>
public int? GpuLayerCount { get; set; }
}
}
66 changes: 66 additions & 0 deletions LLama.KernelMemory/LlamaSharpTextGeneration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using LLama;
using LLama.Common;
using Microsoft.KernelMemory.AI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LLamaSharp.KernelMemory
{
/// <summary>
/// Provides text generation for LLamaSharp.
/// </summary>
public class LlamaSharpTextGeneration : ITextGeneration, IDisposable
{
private readonly LLamaSharpConfig _config;
private readonly LLamaWeights _weights;
private readonly StatelessExecutor _executor;
private readonly LLamaContext _context;

/// <summary>
/// Initializes a new instance of the <see cref="LlamaSharpTextGeneration"/> class.
/// </summary>
/// <param name="config">The configuration for LLamaSharp.</param>
public LlamaSharpTextGeneration(LLamaSharpConfig config)
{
this._config = config;
var parameters = new ModelParams(config.ModelPath)
{
ContextSize = config?.ContextSize ?? 2048,
Seed = config?.Seed ?? 0,
GpuLayerCount = config?.GpuLayerCount ?? 20
};
_weights = LLamaWeights.LoadFromFile(parameters);
_context = _weights.CreateContext(parameters);
_executor = new StatelessExecutor(_weights, parameters);
}

/// <inheritdoc/>
public void Dispose()
{
_context.Dispose();
_weights.Dispose();
}

/// <inheritdoc/>
public IAsyncEnumerable<string> GenerateTextAsync(string prompt, TextGenerationOptions options, CancellationToken cancellationToken = default)
{
return _executor.InferAsync(prompt, OptionsToParams(options), cancellationToken: cancellationToken);
}

private static InferenceParams OptionsToParams(TextGenerationOptions options)
{
return new InferenceParams()
{
AntiPrompts = options.StopSequences.ToList().AsReadOnly(),
Temperature = (float)options.Temperature,
MaxTokens = options.MaxTokens ?? 1024,
FrequencyPenalty = (float)options.FrequencyPenalty,
PresencePenalty = (float)options.PresencePenalty,
TopP = (float)options.TopP,
};
}
}
}
Loading
Loading