Merge pull request #226 from xbotter/kernel-memory
feat: adapts to SK Kernel Memory.
This commit is contained in:
commit
c933a71b82
Binary file not shown.
|
@ -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>
|
||||
|
@ -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>
|
||||
|
|
|
@ -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,6 +23,7 @@
|
|||
Console.WriteLine("13: Semantic Kernel Memory.");
|
||||
Console.WriteLine("14: Coding Assistant.");
|
||||
Console.WriteLine("15: Batch Decoding.");
|
||||
Console.WriteLine("16: SK Kernel Memory.");
|
||||
|
||||
while (true)
|
||||
{
|
||||
|
@ -37,31 +38,31 @@
|
|||
{
|
||||
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();
|
||||
}
|
||||
|
@ -85,7 +86,7 @@
|
|||
{
|
||||
await SemanticKernelMemory.Run();
|
||||
}
|
||||
else if(choice == 14)
|
||||
else if (choice == 14)
|
||||
{
|
||||
await CodingAssistant.Run();
|
||||
}
|
||||
|
@ -93,6 +94,10 @@
|
|||
{
|
||||
await BatchedDecoding.Run();
|
||||
}
|
||||
else if (choice == 16)
|
||||
{
|
||||
await KernelMemory.Run();
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Cannot parse your choice. Please select again.");
|
||||
|
@ -103,5 +108,5 @@
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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>
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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; }
|
||||
}
|
||||
}
|
|
@ -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,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
|
@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LLama.Web", "LLama.Web\LLam
|
|||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LLamaSharp.SemanticKernel", "LLama.SemanticKernel\LLamaSharp.SemanticKernel.csproj", "{D98F93E3-B344-4F9D-86BB-FDBF6768B587}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LLamaSharp.KernelMemory", "LLama.KernelMemory\LLamaSharp.KernelMemory.csproj", "{E5589AE7-B86F-4343-A1CC-8E5D34596E52}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
@ -97,6 +99,18 @@ Global
|
|||
{D98F93E3-B344-4F9D-86BB-FDBF6768B587}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{D98F93E3-B344-4F9D-86BB-FDBF6768B587}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{D98F93E3-B344-4F9D-86BB-FDBF6768B587}.Release|x64.Build.0 = Release|Any CPU
|
||||
{E5589AE7-B86F-4343-A1CC-8E5D34596E52}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{E5589AE7-B86F-4343-A1CC-8E5D34596E52}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{E5589AE7-B86F-4343-A1CC-8E5D34596E52}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{E5589AE7-B86F-4343-A1CC-8E5D34596E52}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{E5589AE7-B86F-4343-A1CC-8E5D34596E52}.GPU|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{E5589AE7-B86F-4343-A1CC-8E5D34596E52}.GPU|Any CPU.Build.0 = Debug|Any CPU
|
||||
{E5589AE7-B86F-4343-A1CC-8E5D34596E52}.GPU|x64.ActiveCfg = Debug|Any CPU
|
||||
{E5589AE7-B86F-4343-A1CC-8E5D34596E52}.GPU|x64.Build.0 = Debug|Any CPU
|
||||
{E5589AE7-B86F-4343-A1CC-8E5D34596E52}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{E5589AE7-B86F-4343-A1CC-8E5D34596E52}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{E5589AE7-B86F-4343-A1CC-8E5D34596E52}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{E5589AE7-B86F-4343-A1CC-8E5D34596E52}.Release|x64.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
|
Loading…
Reference in New Issue