diff --git a/dotnet/agent-framework-dotnet.slnx b/dotnet/agent-framework-dotnet.slnx index 0cedcfbdde..9c5f1a81f3 100644 --- a/dotnet/agent-framework-dotnet.slnx +++ b/dotnet/agent-framework-dotnet.slnx @@ -101,6 +101,7 @@ + diff --git a/dotnet/samples/GettingStarted/AgentWithRAG/AgentWithRAG_Step04_FoundryServiceRAG/AgentWithRAG_Step04_FoundryServiceRAG.csproj b/dotnet/samples/GettingStarted/AgentWithRAG/AgentWithRAG_Step04_FoundryServiceRAG/AgentWithRAG_Step04_FoundryServiceRAG.csproj new file mode 100644 index 0000000000..aefb46524f --- /dev/null +++ b/dotnet/samples/GettingStarted/AgentWithRAG/AgentWithRAG_Step04_FoundryServiceRAG/AgentWithRAG_Step04_FoundryServiceRAG.csproj @@ -0,0 +1,26 @@ + + + + Exe + net9.0 + + enable + enable + + + + + + + + + + + + + + Always + + + + diff --git a/dotnet/samples/GettingStarted/AgentWithRAG/AgentWithRAG_Step04_FoundryServiceRAG/Program.cs b/dotnet/samples/GettingStarted/AgentWithRAG/AgentWithRAG_Step04_FoundryServiceRAG/Program.cs new file mode 100644 index 0000000000..0989394185 --- /dev/null +++ b/dotnet/samples/GettingStarted/AgentWithRAG/AgentWithRAG_Step04_FoundryServiceRAG/Program.cs @@ -0,0 +1,60 @@ +// Copyright (c) Microsoft. All rights reserved. + +// This sample shows how to use the built in RAG capabilities that the Foundry service provides when using AI Agents provided by Foundry. + +using System.ClientModel; +using Azure.AI.Projects; +using Azure.Identity; +using Microsoft.Agents.AI; +using Microsoft.Extensions.AI; +using OpenAI; +using OpenAI.Files; +using OpenAI.VectorStores; + +var endpoint = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_ENDPOINT") ?? throw new InvalidOperationException("AZURE_FOUNDRY_PROJECT_ENDPOINT is not set."); +var deploymentName = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_DEPLOYMENT_NAME") ?? "gpt-4o-mini"; + +// Create an AI Project client and get an OpenAI client that works with the foundry service. +AIProjectClient aiProjectClient = new( + new Uri(endpoint), + new AzureCliCredential()); +OpenAIClient openAIClient = aiProjectClient.GetProjectOpenAIClient(); + +// Upload the file that contains the data to be used for RAG to the Foundry service. +OpenAIFileClient fileClient = openAIClient.GetOpenAIFileClient(); +ClientResult uploadResult = await fileClient.UploadFileAsync( + filePath: "contoso-outdoors-knowledge-base.md", + purpose: FileUploadPurpose.Assistants); + +// Create a vector store in the Foundry service using the uploaded file. +VectorStoreClient vectorStoreClient = openAIClient.GetVectorStoreClient(); +ClientResult vectorStoreCreate = await vectorStoreClient.CreateVectorStoreAsync(options: new VectorStoreCreationOptions() +{ + Name = "contoso-outdoors-knowledge-base", + FileIds = { uploadResult.Value.Id } +}); + +var fileSearchTool = new HostedFileSearchTool() { Inputs = [new HostedVectorStoreContent(vectorStoreCreate.Value.Id)] }; + +AIAgent agent = await aiProjectClient + .CreateAIAgentAsync( + model: deploymentName, + name: "AskContoso", + instructions: "You are a helpful support specialist for Contoso Outdoors. Answer questions using the provided context and cite the source document when available.", + tools: [fileSearchTool]); + +AgentThread thread = agent.GetNewThread(); + +Console.WriteLine(">> Asking about returns\n"); +Console.WriteLine(await agent.RunAsync("Hi! I need help understanding the return policy.", thread)); + +Console.WriteLine("\n>> Asking about shipping\n"); +Console.WriteLine(await agent.RunAsync("How long does standard shipping usually take?", thread)); + +Console.WriteLine("\n>> Asking about product care\n"); +Console.WriteLine(await agent.RunAsync("What is the best way to maintain the TrailRunner tent fabric?", thread)); + +// Cleanup +await fileClient.DeleteFileAsync(uploadResult.Value.Id); +await vectorStoreClient.DeleteVectorStoreAsync(vectorStoreCreate.Value.Id); +await aiProjectClient.Agents.DeleteAgentAsync(agent.Name); diff --git a/dotnet/samples/GettingStarted/AgentWithRAG/AgentWithRAG_Step04_FoundryServiceRAG/contoso-outdoors-knowledge-base.md b/dotnet/samples/GettingStarted/AgentWithRAG/AgentWithRAG_Step04_FoundryServiceRAG/contoso-outdoors-knowledge-base.md new file mode 100644 index 0000000000..901e45b4dd --- /dev/null +++ b/dotnet/samples/GettingStarted/AgentWithRAG/AgentWithRAG_Step04_FoundryServiceRAG/contoso-outdoors-knowledge-base.md @@ -0,0 +1,19 @@ +# Contoso Outdoors Knowledge Base + +## Contoso Outdoors Return Policy + +Customers may return any item within 30 days of delivery. Items should be unused and include original packaging. Refunds are issued to the original payment method within 5 business days of inspection. + +## Contoso Outdoors Shipping Guide + +Standard shipping is free on orders over $50 and typically arrives in 3-5 business days within the continental United States. Expedited options are available at checkout. + +## Product Information + +### TrailRunner Tent + +The TrailRunner Tent is a lightweight, 2-person tent designed for easy setup and durability. It features waterproof materials, ventilation windows, and a compact carry bag. + +#### Care Instructions + +Clean the tent fabric with lukewarm water and a non-detergent soap. Allow it to air dry completely before storage and avoid prolonged UV exposure to extend the lifespan of the waterproof coating. \ No newline at end of file diff --git a/dotnet/samples/GettingStarted/AgentWithRAG/README.md b/dotnet/samples/GettingStarted/AgentWithRAG/README.md index bf2a8f9b11..d606ac767c 100644 --- a/dotnet/samples/GettingStarted/AgentWithRAG/README.md +++ b/dotnet/samples/GettingStarted/AgentWithRAG/README.md @@ -7,3 +7,4 @@ These samples show how to create an agent with the Agent Framework that uses Ret |[Basic Text RAG](./AgentWithRAG_Step01_BasicTextRAG/)|This sample demonstrates how to create and run a basic agent with simple text Retrieval Augmented Generation (RAG).| |[RAG with Vector Store and custom schema](./AgentWithRAG_Step02_CustomVectorStoreRAG/)|This sample demonstrates how to create and run an agent that uses Retrieval Augmented Generation (RAG) with a vector store. It also uses a custom schema for the documents stored in the vector store.| |[RAG with custom RAG data source](./AgentWithRAG_Step03_CustomRAGDataSource/)|This sample demonstrates how to create and run an agent that uses Retrieval Augmented Generation (RAG) with a custom RAG data source.| +|[RAG with Foundry VectorStore service](./AgentWithRAG_Step04_FoundryServiceRAG/)|This sample demonstrates how to create and run an agent that uses Retrieval Augmented Generation (RAG) with the Foundry VectorStore service.|