Repository navigation
episode15 - I Built an AI Agent in C# (Azure OpenAI + Qdrant)
·
8 commits
to series/dotnet-ai
since this release
EP15: AI Agent with Tool Calling (Phase 3, Episode 4)
Watch the episode: https://www.youtube.com/watch?v=p7XXdovsRAc&t
Builds on: ep14 (AI Document Analyzer). Download this release so your code matches the video.
WHAT'S NEW
Backend (DotNetAI.API)
- Services/AgentTools.cs
- 3 tools: list_documents, search_documents, calculate
- Tool definitions (name, description, JSON schema) sent to the model
- ExecuteAsync runs the tool the model asks for
- Calculate accepts only digits and + - * / ( ) . and spaces, then evaluates with DataTable.Compute
- Services/AgentService.cs
- RunAsync: streaming agent loop, max 6 steps
- System prompt: search before answering, cite file and page, use calculate for maths, say "I couldn't find that in your documents"
- ToolCallAccumulator rebuilds streamed tool calls
- Tool errors are sent back to the model so it can recover
- AgentMessage and AgentEvent records (tool_call, tool_result, token)
- Services/DocumentService.cs
- New SearchAllAsync: search all PDFs, or one by file name
- New DocHit record: file name, page, text, score
- Controllers/AgentV1Controller.cs
- POST api/AgentV1/stream (Server-Sent Events)
- Streams tool calls, tool results and tokens, then [DONE]
- Program.cs: AgentTools and AgentService registered as singletons
Frontend (dotnetai-chat)
- src/api/agentStream.js: reads the SSE stream chunk by chunk
- AgentPage.jsx and Agent.css: chat with a live tool trace and a Stop button
- Root.jsx: new Agent tab (all tabs keep their state)
RUN IT
- Start Qdrant in Docker (ports 6333 and 6334)
- Run DotNetAI.API (https://localhost:7036)
- In dotnetai-chat: npm install, then npm run dev
- Open http://localhost:5173, upload a PDF in the Documents tab
- Ask in the Agent tab, for example:
"I travel for 5 nights. What's the most I can spend on hotel plus 5 days of meals?"
REQUIREMENTS
- .NET 10 SDK, Node.js
- Azure OpenAI deployments: gpt-4.1 (chat) and text-embedding-ada-002 (embeddings)
- Qdrant (Docker)
- ep14 code (DocumentService, Documents tab)
NOTES
- The agent can only use read-only tools. EP16 attacks this agent and hardens it.
- Always check an AI answer against the source PDF. The agent cites the file and page.
- The route is api/AgentV1/stream. Using api/Agent/stream returns 404.
NEXT: EP16 AI Red Teaming