Microsoft.Extensions.AI Neo4j GraphRAG Demo

run query
CREATE (n:Test {name: 'Hello Neo4j'}) RETURN n;
CREATE VECTOR INDEX chunkEmbeddings IF NOT EXISTS
FOR (n:Chunk) ON (n.embedding)
OPTIONS { indexConfig: {
  `vector.dimensions`: 1024,
  `vector.similarity_function`: 'cosine'
}}

     static string model = "text-embedding-v4";
 static async Task Main(string[] args)
 {
     OpenAIClientOptions opt = new OpenAIClientOptions();
     
     opt.Endpoint = new Uri("https://maas.aliyuncs.com/compatible-mode/v1");
     // Create the embedding generator.
     OpenAIClient azureClient = new OpenAIClient(new ApiKeyCredential(key), opt);
     IEmbeddingGenerator<string, Embedding<float>> generator = azureClient
             .GetEmbeddingClient(model: model)
             .AsIEmbeddingGenerator();
     var neo4jSettings = new Neo4jSettings();
     
     // Create Neo4j driver
     await using Neo4j.Driver.IDriver driver = GraphDatabase.Driver(
         "bolt://10.0.0.1:7687", AuthTokens.Basic("neo4j", pwd));
     // Create the Neo4j context provider
     await using var provider = new Neo4jContextProvider(driver, new Neo4jContextProviderOptions
     {
         IndexName = "chunkEmbeddings",
         IndexType = IndexType.Vector,
         EmbeddingGenerator = generator,
         TopK = 5,
         RetrievalQuery = "MATCH (n:Test) RETURN n ",
     });

     // Create an agent with the provider
     AIAgent agent = azureClient
         .GetChatClient("qwen3.6-flash")
         .AsIChatClient()
         .AsBuilder()
         .UseAIContextProviders(provider)
         .BuildAIAgent(new ChatClientAgentOptions
         {
             ChatOptions = new ChatOptions
             {
                 Instructions = "You are a financial analyst assistant.",
             },
         });

     var session = await agent.CreateSessionAsync();
     Console.WriteLine(await agent.RunAsync("What risks does Acme Corp face?", session));
 }
Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐