Skip to content

Commit 628930d

Browse files
committed
Some code cleanup
1 parent eec0e5d commit 628930d

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

200_CSharpSemanticMemory/Helper/ExternalEmbeddingGenerator.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,37 @@ public ExternalEmbeddingGenerator(
3030
_embeddingGeneratorConfig = embeddingGeneratorConfig;
3131

3232
_log = DefaultLogger<ExternalEmbeddingGenerator>.Instance;
33+
34+
ScanModel();
3335
}
3436

35-
private async Task GetDenseVectorSizeAsync()
37+
private void ScanModel()
3638
{
3739
var client = _httpClientFactory.CreateClient();
3840
//do a post request to the model to get the dense vector size
39-
var body = new { modelName = _embeddingGeneratorConfig.ModelName };
41+
var body = new DimensionInput(_embeddingGeneratorConfig.ModelName);
4042
var request = new HttpRequestMessage(HttpMethod.Post, $"{_embeddingGeneratorConfig.Address.TrimEnd('/')}/dimensions")
4143
{
4244
Content = new StringContent(JsonSerializer.Serialize(body), Encoding.UTF8, "application/json")
4345
};
44-
var response = await client.GetAsync($"{_embeddingGeneratorConfig.Address.TrimEnd('/')}/dimensions");
46+
47+
var response = client.Send(request);
48+
4549
//TODO: Proper error handling
4650
if (!response.IsSuccessStatusCode)
4751
{
48-
throw new Exception("Failed to get dense vector size");
52+
throw new Exception("Failed to get dimensions");
4953
}
50-
var responseStream = await response.Content.ReadAsStreamAsync();
51-
var embeddingInfo = await JsonSerializer.DeserializeAsync<EmbeddingInfo>(responseStream);
52-
VectorSize = embeddingInfo.dimension;
54+
var responseStream = response.Content.ReadAsStream();
55+
var dimensionResult = JsonSerializer.Deserialize<DimensionResult>(responseStream);
56+
this.VectorSize = dimensionResult.dimension;
57+
this.MaxTokens = dimensionResult.maxSequenceLength;
5358
}
5459

5560
/// <inheritdoc />
56-
public int MaxTokens { get; private set; } = 4096;
61+
public int MaxTokens { get; private set; } = 384;
5762

58-
public int VectorSize { get; private set; }
63+
public int VectorSize { get; private set; } = 768;
5964

6065
/// <inheritdoc />
6166
public int CountTokens(string text)
@@ -118,6 +123,8 @@ public async Task<Embedding> GenerateEmbeddingAsync(
118123
}
119124

120125
public record SentenceInput(List<string> sentences, string modelName);
126+
public record DimensionInput(string modelName);
127+
public record DimensionResult(string model, int maxSequenceLength, int dimension);
121128
public record EmbeddingModel(List<double[]> embeddings, string model);
122129

123130
private record EmbeddingInfo(string model, int dimension);

200_CSharpSemanticMemory/Samples/BookSample.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ public async Task RunSample(string bookPdf)
4848
// Create sample pipeline with 4 files
4949
Console.WriteLine("* Defining pipeline with 4 files...");
5050
var pipeline = orchestrator
51-
.PrepareNewDocumentUpload(index: "booksample", documentId: "booksample", new TagCollection { { "example", "books" } })
51+
.PrepareNewDocumentUpload(
52+
index: "booksample",
53+
documentId: "booksample",
54+
new TagCollection { { "example", "books" } })
5255
.AddUploadFile("file1", Path.GetFileName(bookPdf), bookPdf)
5356
.Then("extract")
5457
.Then("clean")

200_CSharpSemanticMemory/Samples/SBertSample.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,26 @@ public async Task RunSample(string bookPdf)
4646

4747
if (useLocal)
4848
{
49-
kernelMemoryBuilder.WithCustomTextGenerator(new LmStudioTextGeneration(httpClientFactory, new Uri("http://localhost:1234")));
49+
kernelMemoryBuilder.WithCustomTextGenerator(
50+
new LmStudioTextGeneration(
51+
httpClientFactory,
52+
new Uri("http://localhost:1234")));
5053
}
5154
else
5255
{
5356
kernelMemoryBuilder.WithAzureOpenAITextGeneration(chatConfig);
5457
}
5558

59+
var embeddingConfig = new ExternalEmbeddingGeneratorConfig()
60+
{
61+
ModelName = "msmarco-distilbert-base-v4"
62+
};
63+
5664
kernelMemoryBuilder
57-
.WithCustomEmbeddingGenerator(new ExternalEmbeddingGenerator(httpClientFactory, new ExternalEmbeddingGeneratorConfig()))
65+
.WithCustomEmbeddingGenerator(
66+
new ExternalEmbeddingGenerator(
67+
httpClientFactory,
68+
embeddingConfig))
5869
.WithSimpleFileStorage(new SimpleFileStorageConfig()
5970
{
6071
Directory = "c:\\temp\\kmsbert\\storage",

0 commit comments

Comments
 (0)