1+ using System . ComponentModel ;
2+ using System . Diagnostics . CodeAnalysis ;
3+ using Microsoft . Extensions . AI ;
4+ using Microsoft . Extensions . VectorData ;
5+ using Microsoft . SemanticKernel ;
6+ using Microsoft . SemanticKernel . Connectors . InMemory ;
7+ using Microsoft . SemanticKernel . Data ;
8+
9+ namespace Elsa . Agents . Plugins ;
10+
11+ public class DocumentQueryPlugin
12+ {
13+ [ Experimental ( "SKEXP0001" ) ]
14+ [ KernelFunction ( "query_document" ) ]
15+ [ Description ( "Queries a document using vector search." ) ]
16+ public async Task < string [ ] > QueryDocumentAsync (
17+ Kernel kernel ,
18+ [ Description ( "The document to query." ) ] string documentText ,
19+ [ Description ( "The query to perform on the document." ) ] string query ,
20+ CancellationToken cancellationToken = default )
21+ {
22+ var embeddingService = kernel . GetRequiredService < IEmbeddingGenerator < string , Embedding < float > > > ( ) ;
23+ var vectorStore = new InMemoryVectorStore ( new ( ) { EmbeddingGenerator = embeddingService } ) ;
24+ var collection = vectorStore . GetCollection < string , Document > ( "documents" ) ;
25+ var document = new Document
26+ {
27+ Key = Guid . NewGuid ( ) . ToString ( ) ,
28+ Text = documentText ,
29+ TextEmbedding = await embeddingService . GenerateAsync ( documentText , cancellationToken : cancellationToken )
30+ } ;
31+ await collection . EnsureCollectionExistsAsync ( cancellationToken ) ;
32+ await collection . UpsertAsync ( document , cancellationToken ) ;
33+
34+ var vectorSearch = new VectorStoreTextSearch < Document > ( collection ) ;
35+ var results = await vectorSearch . SearchAsync ( query , cancellationToken : cancellationToken ) ;
36+ var results2 = await results . Results . ToArrayAsync ( cancellationToken : cancellationToken ) ;
37+
38+ return results2 ;
39+ }
40+ }
41+
42+ public class DocumentQueryPluginProvider : PluginProvider
43+ {
44+ public override IEnumerable < PluginDescriptor > GetPlugins ( )
45+ {
46+ yield return PluginDescriptor . From < DocumentQueryPlugin > ( ) ;
47+ }
48+ }
49+
50+ public class Document
51+ {
52+ [ VectorStoreKey ] [ TextSearchResultName ] public string Key { get ; set ; } = Guid . NewGuid ( ) . ToString ( ) ;
53+ [ VectorStoreData ] [ TextSearchResultValue ] public string Text { get ; set ; } = null ! ;
54+ [ VectorStoreVector ( 1536 ) ] public Embedding < float > TextEmbedding { get ; set ; } = null ! ;
55+ }
0 commit comments