Browse Source

📝 Add LLamaSharp.KernelMemory project

Add the LLamaSharp.KernelMemory project to the solution. This project includes classes for text embedding generation and text generation using the LLamaSharp library.
tags/v0.8.0
xbotter 2 years ago
parent
commit
26cded73c6
No known key found for this signature in database GPG Key ID: A3F32F44E9F160E1
5 changed files with 155 additions and 0 deletions
  1. +17
    -0
      LLama.KernelMemory/LLamaSharp.KernelMemory.csproj
  2. +45
    -0
      LLama.KernelMemory/LLamaSharpTextEmbeddingGeneration.cs
  3. +21
    -0
      LLama.KernelMemory/LlamaSharpConfig.cs
  4. +58
    -0
      LLama.KernelMemory/LlamaSharpTextGeneration.cs
  5. +14
    -0
      LLamaSharp.sln

+ 17
- 0
LLama.KernelMemory/LLamaSharp.KernelMemory.csproj View File

@@ -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.SemanticMemory.Core" Version="0.4.231023.1-preview" />
</ItemGroup>

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

</Project>

+ 45
- 0
LLama.KernelMemory/LLamaSharpTextEmbeddingGeneration.cs View File

@@ -0,0 +1,45 @@
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
{
public class LLamaSharpTextEmbeddingGeneration : ITextEmbeddingGeneration, IDisposable
{
private readonly LlamaSharpConfig _config;
private readonly LLamaEmbedder _embedder;
private readonly LLamaWeights _weights;

public LLamaSharpTextEmbeddingGeneration(LlamaSharpConfig config)
{
this._config = config;
var @params = new ModelParams(_config.ModelPath);
_weights = LLamaWeights.LoadFromFile(@params);
_embedder = new LLamaEmbedder(_weights, @params);
}

public void Dispose()
{
_embedder.Dispose();
_weights.Dispose();
}

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);
}
}
}

+ 21
- 0
LLama.KernelMemory/LlamaSharpConfig.cs View File

@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LLamaSharp.KernelMemory
{
public class LlamaSharpConfig
{
public LlamaSharpConfig(string modelPath)
{
ModelPath = modelPath;
}

public string ModelPath { get; set; }
public uint? ContextSize { get; set; }
public uint? Seed { get; set; }
public int? GpuLayerCount { get; set; }
}
}

+ 58
- 0
LLama.KernelMemory/LlamaSharpTextGeneration.cs View File

@@ -0,0 +1,58 @@
using LLama;
using LLama.Common;
using Microsoft.SemanticMemory.AI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LLamaSharp.KernelMemory
{
public class LlamaSharpTextGeneration : ITextGeneration, IDisposable
{
private readonly LlamaSharpConfig _config;
private readonly LLamaWeights _weights;
private readonly InstructExecutor _executor;
private readonly LLamaContext _context;

public LlamaSharpTextGeneration(LlamaSharpConfig config)
{
this._config = config;
var parameters = new ModelParams(config.ModelPath)
{
ContextSize = config?.ContextSize ?? 1024,
Seed = config?.Seed ?? 0,
GpuLayerCount = config?.GpuLayerCount ?? 20
};
_weights = LLamaWeights.LoadFromFile(parameters);
_context = _weights.CreateContext(parameters);
_executor = new InstructExecutor(_context);

}

public void Dispose()
{
_context.Dispose();
_weights.Dispose();
}

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,
Temperature = (float)options.Temperature,
MaxTokens = options.MaxTokens ?? 1024,
FrequencyPenalty = (float)options.FrequencyPenalty,
PresencePenalty = (float)options.PresencePenalty,
TopP = (float)options.TopP,
};
}
}
}

+ 14
- 0
LLamaSharp.sln View File

@@ -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…
Cancel
Save