Code optimization

This commit is contained in:
Zoli Somogyi 2024-03-28 21:30:14 +01:00
parent 127c3edd44
commit 91e5a3f543
1 changed files with 9 additions and 28 deletions

View File

@ -74,36 +74,10 @@ namespace LLamaSharp.KernelMemory
/// </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 IKernelMemoryBuilder WithLLamaSharpDefaults(this IKernelMemoryBuilder builder, LLamaSharpConfig config)
{
var parameters = new ModelParams(config.ModelPath)
{
ContextSize = config?.ContextSize ?? 2048,
Seed = config?.Seed ?? 0,
GpuLayerCount = config?.GpuLayerCount ?? 20,
EmbeddingMode = true,
MainGpu = config?.MainGpu ?? 0,
SplitMode = config?.SplitMode ?? GPUSplitMode.None
};
var weights = LLamaWeights.LoadFromFile(parameters);
var context = weights.CreateContext(parameters);
var executor = new StatelessExecutor(weights, parameters);
var embedder = new LLamaEmbedder(weights, parameters);
builder.WithLLamaSharpTextEmbeddingGeneration(new LLamaSharpTextEmbeddingGenerator(embedder));
builder.WithLLamaSharpTextGeneration(new LlamaSharpTextGenerator(weights, context, executor, config?.DefaultInferenceParams));
return builder;
}
/// <summary>
/// Adds LLamaSharpTextEmbeddingGeneration and LLamaSharpTextGeneration to the KernelMemoryBuilder. Using an already loaded model.
/// </summary>
/// <param name="builder">The KernelMemoryBuilder instance.</param>
/// <param name="weights"></param>
/// <param name="context"></param>
/// <param name="config">The LLamaSharpConfig instance.</param>
/// <param name="context"></param>
/// <returns>The KernelMemoryBuilder instance with LLamaSharpTextEmbeddingGeneration and LLamaSharpTextGeneration added.</returns>
public static IKernelMemoryBuilder WithLLamaSharpDefaults(this IKernelMemoryBuilder builder, LLamaWeights weights, LLamaContext context, LLamaSharpConfig config)
public static IKernelMemoryBuilder WithLLamaSharpDefaults(this IKernelMemoryBuilder builder, LLamaSharpConfig config, LLamaWeights? weights=null, LLamaContext? context=null)
{
var parameters = new ModelParams(config.ModelPath)
{
@ -114,6 +88,13 @@ namespace LLamaSharp.KernelMemory
MainGpu = config?.MainGpu ?? 0,
SplitMode = config?.SplitMode ?? GPUSplitMode.None,
};
if (weights == null)
{
weights = LLamaWeights.LoadFromFile(parameters);
context = weights.CreateContext(parameters);
}
var executor = new StatelessExecutor(weights, parameters);
var embedder = new LLamaEmbedder(weights, parameters);
builder.WithLLamaSharpTextEmbeddingGeneration(new LLamaSharpTextEmbeddingGenerator(embedder));