Skip to content

Commit 8d5f28d

Browse files
authored
Merge pull request #606 from iceljc/features/add-embedding-dim-setting
Features/add embedding dim setting
2 parents 8a63815 + 37e87c9 commit 8d5f28d

File tree

34 files changed

+339
-103
lines changed

34 files changed

+339
-103
lines changed

src/Infrastructure/BotSharp.Abstraction/Knowledges/IKnowledgeService.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ public interface IKnowledgeService
88
Task<IEnumerable<string>> GetVectorCollections();
99
Task<IEnumerable<VectorSearchResult>> SearchVectorKnowledge(string query, string collectionName, VectorSearchOptions options);
1010
Task FeedVectorKnowledge(string collectionName, KnowledgeCreationModel model);
11-
Task<StringIdPagedItems<VectorSearchResult>> GetVectorCollectionData(string collectionName, VectorFilter filter);
11+
Task<StringIdPagedItems<VectorSearchResult>> GetPagedVectorCollectionData(string collectionName, VectorFilter filter);
1212
Task<bool> DeleteVectorCollectionData(string collectionName, string id);
13+
Task<bool> CreateVectorCollectionData(string collectionName, VectorCreateModel create);
14+
Task<bool> UpdateVectorCollectionData(string collectionName, VectorUpdateModel update);
1315
Task<GraphSearchResult> SearchGraphKnowledge(string query, GraphSearchOptions options);
1416
Task<KnowledgeSearchResult> SearchKnowledge(string query, string collectionName, VectorSearchOptions vectorOptions, GraphSearchOptions graphOptions);
1517
}

src/Infrastructure/BotSharp.Abstraction/Knowledges/Settings/KnowledgeBaseSettings.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,28 @@ namespace BotSharp.Abstraction.Knowledges.Settings;
44

55
public class KnowledgeBaseSettings
66
{
7-
public string DefaultCollection { get; set; } = KnowledgeCollectionName.BotSharp;
87
public string VectorDb { get; set; }
98
public string GraphDb { get; set; }
10-
public KnowledgeModelSetting TextEmbedding { get; set; }
9+
10+
public DefaultKnowledgeBaseSetting Default { get; set; }
11+
public List<VectorCollectionSetting> Collections { get; set; } = new();
12+
}
13+
14+
public class DefaultKnowledgeBaseSetting
15+
{
16+
public string CollectionName { get; set; } = KnowledgeCollectionName.BotSharp;
17+
public KnowledgeTextEmbeddingSetting TextEmbedding { get; set; }
18+
}
19+
20+
public class VectorCollectionSetting
21+
{
22+
public string Name { get; set; }
23+
public KnowledgeTextEmbeddingSetting TextEmbedding { get; set; }
1124
}
1225

13-
public class KnowledgeModelSetting
26+
public class KnowledgeTextEmbeddingSetting
1427
{
1528
public string Provider { get; set; }
1629
public string Model { get; set; }
30+
public int Dimension { get; set; }
1731
}

src/Infrastructure/BotSharp.Abstraction/MLTasks/ITextEmbedding.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ public interface ITextEmbedding
66
/// The Embedding provider like Microsoft Azure, OpenAI, ClaudAI
77
/// </summary>
88
string Provider { get; }
9-
int Dimension { get; set; }
109
Task<float[]> GetVectorAsync(string text);
1110
Task<List<float[]>> GetVectorsAsync(List<string> texts);
1211
void SetModelName(string model);
12+
void SetDimension(int dimension);
13+
int GetDimension();
1314
}

src/Infrastructure/BotSharp.Abstraction/MLTasks/Settings/LlmModelSetting.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ public class LlmModelSetting
5252
/// </summary>
5353
public float CompletionCost { get; set; }
5454

55+
/// <summary>
56+
/// Embedding dimension
57+
/// </summary>
58+
public int Dimension { get; set; }
59+
5560
public override string ToString()
5661
{
5762
return $"[{Type}] {Name} {Endpoint}";

src/Infrastructure/BotSharp.Abstraction/VectorStorage/IVectorDb.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ public interface IVectorDb
77
string Name { get; }
88

99
Task<IEnumerable<string>> GetCollections();
10-
Task<StringIdPagedItems<VectorCollectionData>> GetCollectionData(string collectionName, VectorFilter filter);
10+
Task<StringIdPagedItems<VectorCollectionData>> GetPagedCollectionData(string collectionName, VectorFilter filter);
11+
Task<IEnumerable<VectorCollectionData>> GetCollectionData(string collectionName, IEnumerable<Guid> ids, bool withPayload = false, bool withVector = false);
1112
Task CreateCollection(string collectionName, int dim);
1213
Task<bool> Upsert(string collectionName, Guid id, float[] vector, string text, Dictionary<string, string>? payload = null);
1314
Task<IEnumerable<VectorCollectionData>> Search(string collectionName, float[] vector, IEnumerable<string>? fields, int limit = 5, float confidence = 0.5f, bool withVector = false);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace BotSharp.Abstraction.VectorStorage.Models;
2+
3+
public class VectorCreateModel
4+
{
5+
public string Text { get; set; }
6+
public Dictionary<string, string>? Payload { get; set; }
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace BotSharp.Abstraction.VectorStorage.Models;
2+
3+
public class VectorUpdateModel : VectorCreateModel
4+
{
5+
public string Id { get; set; }
6+
}

src/Infrastructure/BotSharp.Core/Infrastructures/CompletionProvider.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,12 @@ public static ITextEmbedding GetTextEmbedding(IServiceProvider services,
111111
logger.LogError($"Can't resolve completion provider by {provider}");
112112
}
113113

114+
115+
var llmProviderService = services.GetRequiredService<ILlmProviderService>();
116+
var found = llmProviderService.GetSetting(provider, model);
117+
114118
completer.SetModelName(model);
119+
completer.SetDimension(found.Dimension);
115120
return completer;
116121
}
117122

src/Infrastructure/BotSharp.OpenAPI/Controllers/KnowledgeBaseController.cs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ public async Task<IEnumerable<VectorKnowledgeViewModel>> SearchVectorKnowledge([
3939
return results.Select(x => VectorKnowledgeViewModel.From(x)).ToList();
4040
}
4141

42-
[HttpPost("/knowledge/vector/{collection}/data")]
43-
public async Task<StringIdPagedItems<VectorKnowledgeViewModel>> GetVectorCollectionData([FromRoute] string collection, [FromBody] VectorFilter filter)
42+
[HttpPost("/knowledge/vector/{collection}/page")]
43+
public async Task<StringIdPagedItems<VectorKnowledgeViewModel>> GetPagedVectorCollectionData([FromRoute] string collection, [FromBody] VectorFilter filter)
4444
{
45-
var data = await _knowledgeService.GetVectorCollectionData(collection, filter);
45+
var data = await _knowledgeService.GetPagedVectorCollectionData(collection, filter);
4646
var items = data.Items?.Select(x => VectorKnowledgeViewModel.From(x))?
4747
.ToList() ?? new List<VectorKnowledgeViewModel>();
4848

@@ -54,6 +54,33 @@ public async Task<StringIdPagedItems<VectorKnowledgeViewModel>> GetVectorCollect
5454
};
5555
}
5656

57+
[HttpPost("/knowledge/vector/{collection}/create")]
58+
public async Task<bool> CreateVectorKnowledge([FromRoute] string collection, [FromBody] VectorKnowledgeCreateRequest request)
59+
{
60+
var create = new VectorCreateModel
61+
{
62+
Text = request.Text,
63+
Payload = request.Payload
64+
};
65+
66+
var created = await _knowledgeService.CreateVectorCollectionData(collection, create);
67+
return created;
68+
}
69+
70+
[HttpPut("/knowledge/vector/{collection}/update")]
71+
public async Task<bool> UpdateVectorKnowledge([FromRoute] string collection, [FromBody] VectorKnowledgeUpdateRequest request)
72+
{
73+
var update = new VectorUpdateModel
74+
{
75+
Id = request.Id,
76+
Text = request.Text,
77+
Payload = request.Payload
78+
};
79+
80+
var updated = await _knowledgeService.UpdateVectorCollectionData(collection, update);
81+
return updated;
82+
}
83+
5784
[HttpDelete("/knowledge/vector/{collection}/data/{id}")]
5885
public async Task<bool> DeleteVectorCollectionData([FromRoute] string collection, [FromRoute] string id)
5986
{

src/Infrastructure/BotSharp.OpenAPI/Controllers/TextEmbeddingController.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public TextEmbeddingController(
1818
_logger = logger;
1919
}
2020

21-
[HttpPost("/text-embedding/generation")]
21+
[HttpPost("/text-embedding/generate")]
2222
public async Task<List<float[]>> GenerateTextEmbeddings(EmbeddingInputModel input)
2323
{
2424
var state = _services.GetRequiredService<IConversationStateService>();
@@ -27,7 +27,10 @@ public async Task<List<float[]>> GenerateTextEmbeddings(EmbeddingInputModel inpu
2727
try
2828
{
2929
var completion = CompletionProvider.GetTextEmbedding(_services, provider: input.Provider ?? "openai", model: input.Model ?? "text-embedding-3-large");
30-
completion.Dimension = input.Dimension;
30+
if (input.Dimension.HasValue && input.Dimension.Value > 0)
31+
{
32+
completion.SetDimension(input.Dimension.Value);
33+
}
3134

3235
var embeddings = await completion.GetVectorsAsync(input.Texts?.ToList() ?? []);
3336
return embeddings;

0 commit comments

Comments
 (0)