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

updated LLamaSharp to 0.7, updated examples #46

Merged
merged 1 commit into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LangChain.Sources.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"src\\libs\\LangChain.Core\\LangChain.Core.csproj",
"src\\libs\\Providers\\LangChain.Providers.LLamaSharp\\LangChain.Providers.LLamaSharp.csproj",
"src\\libs\\Providers\\LangChain.Providers.HuggingFace\\LangChain.Providers.HuggingFace.csproj",
"src\\tests\\LangChain.IntegrationTests.LLamaSharp\\LangChain.IntegrationTests.LLamaSharp.csproj",
"src\\tests\\LangChain.Providers.LLamaSharp.IntegrationTests\\LangChain.Providers.LLamaSharp.IntegrationTests.csproj",
"src\\tests\\LangChain.UnitTest\\LangChain.UnitTest.csproj"
]
}
Expand Down
4 changes: 2 additions & 2 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<PackageVersion Include="H.Resources.Generator" Version="1.5.1" />
<PackageVersion Include="HuggingFace" Version="0.2.4" />
<PackageVersion Include="LeonardoAi" Version="0.1.0" />
<PackageVersion Include="LLamaSharp" Version="0.3.0" />
<PackageVersion Include="LLamaSharp.Backend.Cpu" Version="0.3.0" />
<PackageVersion Include="LLamaSharp" Version="0.7.0" />
<PackageVersion Include="LLamaSharp.Backend.Cpu" Version="0.7.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.7.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageVersion Include="Microsoft.SemanticKernel.Connectors.AI.OpenAI" Version="0.15.230531.5-preview" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,22 @@ public class LLamaSharpConfiguration
/// Path to *.bin file
/// </summary>
public string PathToModelFile { get; set; }

/// <summary>
/// Model mode.
/// Chat - for conversation completion
/// Instruction - for instruction execution
/// </summary>
public ELLamaSharpModelMode Mode { get; set; } = ELLamaSharpModelMode.Chat;


/// <summary>
/// Context size
/// How much tokens model will remember.
/// Usually 2048 for llama
/// </summary>
public int ContextSize { get; set; } = 512;
public int ContextSize { get; set; } = 1024;

/// <summary>
/// Temperature
/// The level of model's creativity
/// </summary>
public float Temperature { get; set; } = 0.7f;

public int Seed { get; set; }

public int MaxTokens { get; set; } = 600;

}
145 changes: 0 additions & 145 deletions src/libs/Providers/LangChain.Providers.LLamaSharp/LLamaSharpModel.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using LLama.Common;
using LLama;

namespace LangChain.Providers.LLamaSharp;

public abstract class LLamaSharpModelBase:IChatModel
{
public string Id { get; }
public Usage TotalUsage { get; protected set; }
public int ContextLength =>_configuration.ContextSize;

protected readonly LLamaSharpConfiguration _configuration;
protected readonly LLamaWeights _model;
protected readonly ModelParams _parameters;

protected LLamaSharpModelBase(LLamaSharpConfiguration configuration)
{
_parameters = new ModelParams(configuration.PathToModelFile)
{
ContextSize = (uint)configuration.ContextSize,
Seed = (uint)configuration.Seed,

};
_model = LLamaWeights.LoadFromFile(_parameters);
_configuration = configuration;
Id = Path.GetFileNameWithoutExtension(configuration.PathToModelFile);
}

public abstract Task<ChatResponse>
GenerateAsync(ChatRequest request, CancellationToken cancellationToken = default);

protected string ConvertRole(MessageRole role)
{
return role switch
{
MessageRole.Human => "Human: ",
MessageRole.Ai => "Assistant: ",
MessageRole.System => "",
_ => throw new NotSupportedException($"the role {role} is not supported")
};
}
protected string ConvertMessage(Message message)
{
return $"{ConvertRole(message.Role)}{message.Content}";
}
protected string ToPrompt(IEnumerable<Message> messages)
{
return string.Join("\n", messages.Select(ConvertMessage).ToArray());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System.Diagnostics;
using LLama;
using System.Reflection;
using LLama.Common;

namespace LangChain.Providers.LLamaSharp
{
public class LLamaSharpModelChat : LLamaSharpModelBase
{
public LLamaSharpModelChat(LLamaSharpConfiguration configuration) : base(configuration)
{
}


string SanitizeOutput(string res)
{
return res.Replace("Human:", "").Replace("Assistant:", "").Trim();
}

public override async Task<ChatResponse> GenerateAsync(ChatRequest request,
CancellationToken cancellationToken = default)
{
var prompt = ToPrompt(request.Messages);

var watch = Stopwatch.StartNew();


var context = _model.CreateContext(_parameters);
var ex = new InteractiveExecutor(context);
ChatSession session = new ChatSession(ex);
var inferenceParams = new InferenceParams()
{
Temperature = _configuration.Temperature,
AntiPrompts = new List<string> { "Human:" },
MaxTokens = _configuration.MaxTokens,

};


var buf = "";
await foreach (var text in session.ChatAsync(prompt,
inferenceParams, cancellationToken))
{
buf += text;
}

buf = SanitizeOutput(buf);
var result = request.Messages.ToList();
result.Add(buf.AsAiMessage());

watch.Stop();

// Unsupported
var usage = Usage.Empty with
{
Time = watch.Elapsed,
};
TotalUsage += usage;

return new ChatResponse(
Messages: result,
Usage: usage);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using LLama.Common;
using LLama;
using System.Diagnostics;

namespace LangChain.Providers.LLamaSharp;

public class LLamaSharpModelInstruction:LLamaSharpModelBase
{
public LLamaSharpModelInstruction(LLamaSharpConfiguration configuration) : base(configuration)
{
}
string SanitizeOutput(string res)
{
return res.Replace("\n>", "").Trim();
}
public override async Task<ChatResponse> GenerateAsync(ChatRequest request, CancellationToken cancellationToken = default)
{
var prompt = ToPrompt(request.Messages)+"\n";

var watch = Stopwatch.StartNew();


var context = _model.CreateContext(_parameters);
var ex = new InstructExecutor(context);

var inferenceParams = new InferenceParams()
{
Temperature = _configuration.Temperature,
AntiPrompts = new List<string> { ">" },
MaxTokens = _configuration.MaxTokens,

};


var buf = "";
await foreach (var text in ex.InferAsync(prompt,
inferenceParams, cancellationToken))
{
buf += text;
}

buf=SanitizeOutput(buf);
var result = request.Messages.ToList();
result.Add(buf.AsAiMessage());

watch.Stop();

// Unsupported
var usage = Usage.Empty with
{
Time = watch.Elapsed,
};
TotalUsage += usage;

return new ChatResponse(
Messages: result,
Usage: usage);
}
}
Loading
Loading