-
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: LLamaSharp embeddings and test (#47)
* updated LLamaSharp to 0.7, updated examples * LLamaSharp embeddings and test
- Loading branch information
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
src/libs/Providers/LangChain.Providers.LLamaSharp/LLamaSharpEmbeddings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using LangChain.Abstractions.Embeddings.Base; | ||
using LLama.Common; | ||
using LLama; | ||
|
||
namespace LangChain.Providers.LLamaSharp; | ||
|
||
public class LLamaSharpEmbeddings:IEmbeddings | ||
{ | ||
protected readonly LLamaSharpConfiguration _configuration; | ||
protected readonly LLamaWeights _model; | ||
protected readonly ModelParams _parameters; | ||
private readonly LLamaEmbedder _embedder; | ||
|
||
public LLamaSharpEmbeddings(LLamaSharpConfiguration configuration) | ||
{ | ||
_parameters = new ModelParams(configuration.PathToModelFile) | ||
{ | ||
ContextSize = (uint)configuration.ContextSize, | ||
Seed = (uint)configuration.Seed, | ||
|
||
}; | ||
_model = LLamaWeights.LoadFromFile(_parameters); | ||
_configuration = configuration; | ||
_embedder = new LLamaEmbedder(_model, _parameters); | ||
} | ||
|
||
public Task<float[][]> EmbedDocumentsAsync(string[] texts, CancellationToken cancellationToken = default) | ||
{ | ||
float[][] result = new float[texts.Length][]; | ||
for (int i = 0; i < texts.Length; i++) | ||
{ | ||
result[i] = _embedder.GetEmbeddings(texts[i]); | ||
} | ||
return Task.FromResult(result); | ||
} | ||
|
||
public Task<float[]> EmbedQueryAsync(string text, CancellationToken cancellationToken = default) | ||
{ | ||
return Task.FromResult(_embedder.GetEmbeddings(text)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters