Skip to content

Commit ce738cc

Browse files
.NET: Add sample to show how to do RAG using Foundry's built-in service (microsoft#2324)
* Add sample to show how to do RAG using Foundry's built-in service * Update README.me * Update dotnet/samples/GettingStarted/AgentWithRAG/AgentWithRAG_Step04_FoundryServiceRAG/Program.cs Co-authored-by: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com> --------- Co-authored-by: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com>
1 parent f99dca0 commit ce738cc

File tree

5 files changed

+107
-0
lines changed

5 files changed

+107
-0
lines changed

dotnet/agent-framework-dotnet.slnx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
<Project Path="samples/GettingStarted/AgentWithRAG/AgentWithRAG_Step01_BasicTextRAG/AgentWithRAG_Step01_BasicTextRAG.csproj" />
102102
<Project Path="samples/GettingStarted/AgentWithRAG/AgentWithRAG_Step02_CustomVectorStoreRAG/AgentWithRAG_Step02_CustomVectorStoreRAG.csproj" />
103103
<Project Path="samples/GettingStarted/AgentWithRAG/AgentWithRAG_Step03_CustomRAGDataSource/AgentWithRAG_Step03_CustomRAGDataSource.csproj" />
104+
<Project Path="samples/GettingStarted/AgentWithRAG/AgentWithRAG_Step04_FoundryServiceRAG/AgentWithRAG_Step04_FoundryServiceRAG.csproj" />
104105
</Folder>
105106
<Folder Name="/Samples/GettingStarted/FoundryAgents/">
106107
<File Path="samples/GettingStarted/FoundryAgents/README.md" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
7+
<Nullable>enable</Nullable>
8+
<ImplicitUsings>enable</ImplicitUsings>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Azure.AI.Projects" />
13+
<PackageReference Include="Azure.Identity" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\..\..\..\src\Microsoft.Agents.AI.AzureAI\Microsoft.Agents.AI.AzureAI.csproj" />
18+
</ItemGroup>
19+
20+
<ItemGroup>
21+
<None Update="contoso-outdoors-knowledge-base.md">
22+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
23+
</None>
24+
</ItemGroup>
25+
26+
</Project>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
// This sample shows how to use the built in RAG capabilities that the Foundry service provides when using AI Agents provided by Foundry.
4+
5+
using System.ClientModel;
6+
using Azure.AI.Projects;
7+
using Azure.Identity;
8+
using Microsoft.Agents.AI;
9+
using Microsoft.Extensions.AI;
10+
using OpenAI;
11+
using OpenAI.Files;
12+
using OpenAI.VectorStores;
13+
14+
var endpoint = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_ENDPOINT") ?? throw new InvalidOperationException("AZURE_FOUNDRY_PROJECT_ENDPOINT is not set.");
15+
var deploymentName = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_DEPLOYMENT_NAME") ?? "gpt-4o-mini";
16+
17+
// Create an AI Project client and get an OpenAI client that works with the foundry service.
18+
AIProjectClient aiProjectClient = new(
19+
new Uri(endpoint),
20+
new AzureCliCredential());
21+
OpenAIClient openAIClient = aiProjectClient.GetProjectOpenAIClient();
22+
23+
// Upload the file that contains the data to be used for RAG to the Foundry service.
24+
OpenAIFileClient fileClient = openAIClient.GetOpenAIFileClient();
25+
ClientResult<OpenAIFile> uploadResult = await fileClient.UploadFileAsync(
26+
filePath: "contoso-outdoors-knowledge-base.md",
27+
purpose: FileUploadPurpose.Assistants);
28+
29+
// Create a vector store in the Foundry service using the uploaded file.
30+
VectorStoreClient vectorStoreClient = openAIClient.GetVectorStoreClient();
31+
ClientResult<VectorStore> vectorStoreCreate = await vectorStoreClient.CreateVectorStoreAsync(options: new VectorStoreCreationOptions()
32+
{
33+
Name = "contoso-outdoors-knowledge-base",
34+
FileIds = { uploadResult.Value.Id }
35+
});
36+
37+
var fileSearchTool = new HostedFileSearchTool() { Inputs = [new HostedVectorStoreContent(vectorStoreCreate.Value.Id)] };
38+
39+
AIAgent agent = await aiProjectClient
40+
.CreateAIAgentAsync(
41+
model: deploymentName,
42+
name: "AskContoso",
43+
instructions: "You are a helpful support specialist for Contoso Outdoors. Answer questions using the provided context and cite the source document when available.",
44+
tools: [fileSearchTool]);
45+
46+
AgentThread thread = agent.GetNewThread();
47+
48+
Console.WriteLine(">> Asking about returns\n");
49+
Console.WriteLine(await agent.RunAsync("Hi! I need help understanding the return policy.", thread));
50+
51+
Console.WriteLine("\n>> Asking about shipping\n");
52+
Console.WriteLine(await agent.RunAsync("How long does standard shipping usually take?", thread));
53+
54+
Console.WriteLine("\n>> Asking about product care\n");
55+
Console.WriteLine(await agent.RunAsync("What is the best way to maintain the TrailRunner tent fabric?", thread));
56+
57+
// Cleanup
58+
await fileClient.DeleteFileAsync(uploadResult.Value.Id);
59+
await vectorStoreClient.DeleteVectorStoreAsync(vectorStoreCreate.Value.Id);
60+
await aiProjectClient.Agents.DeleteAgentAsync(agent.Name);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Contoso Outdoors Knowledge Base
2+
3+
## Contoso Outdoors Return Policy
4+
5+
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.
6+
7+
## Contoso Outdoors Shipping Guide
8+
9+
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.
10+
11+
## Product Information
12+
13+
### TrailRunner Tent
14+
15+
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.
16+
17+
#### Care Instructions
18+
19+
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.

dotnet/samples/GettingStarted/AgentWithRAG/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ These samples show how to create an agent with the Agent Framework that uses Ret
77
|[Basic Text RAG](./AgentWithRAG_Step01_BasicTextRAG/)|This sample demonstrates how to create and run a basic agent with simple text Retrieval Augmented Generation (RAG).|
88
|[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.|
99
|[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.|
10+
|[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.|

0 commit comments

Comments
 (0)