A C# console application that demonstrates building a sophisticated AI agent—the "Code Standards Expert"—which leverages Azure AI services to enforce custom coding standards defined in a local policy file.
The agent provides an interactive assistant that can:
- Review C# code snippets in real-time
- Answer questions about coding best practices
- Generate code that strictly adheres to user-defined rules
- Utilize Retrieval-Augmented Generation (RAG) to ground responses in a specific document (
CodeStandards.md), ensuring authoritative and consistent answers
- Dynamic Agent Creation: Uses the
Azure.AI.AgentsSDK to create and configure an AI agent at runtime - Custom Knowledge Grounding (RAG): Implements a
CodeStandardsToolthat reads a localCodeStandards.mdfile, providing the agent with necessary context - Interactive Console Interface: User-friendly chat loop for real-time interaction
- Multi-line Input Support: Paste large code blocks or multi-line questions directly into the console
- Secure Authentication: Uses
DefaultAzureCredentialfor passwordless authentication to Azure services
- .NET 9 SDK or later
- Active Azure Subscription
- Azure AI Hub and Azure AI Project
- Deployed model (e.g.,
gpt-4o-mini) within your Azure AI Project - Azure CLI installed and authenticated (
az login)
git clone https://github.com/Majdi-Zlitni/FoundryLabCSharp
cd FoundryLabCSharp- Open the solution in your IDE (e.g., Visual Studio 2022)
- Locate
CodeStandards.mdin the Solution Explorer - Right-click the file and select Properties
- Set Copy to Output Directory to Copy if newer
- Edit
CodeStandards.mdto define your custom coding rules
Open Program.cs and update these constants with your Azure AI Project details:
// The endpoint for your Azure AI Project
// Found on your project's overview page in Azure AI Studio
const string ENDPOINT = "https://<YOUR-PROJECT-ENDPOINT>.agents.ai.azure.com";
// The exact name of your model deployment
// Found on the "Deployments" page in your Azure AI Project
const string MODEL_DEPLOYMENT_NAME = "<YOUR-MODEL-DEPLOYMENT-NAME>";dotnet build
dotnet runAfter launching, you'll see: --- C# Code Standards Agent Initialized ---
- Ask Questions: Type a question about coding standards and press Enter
- Submit Code for Review: Paste multi-line C# code directly into the console
- Submit Input: Press Enter on an empty line to send your input
- Exit: Type
exitorquitand press Enter
How can I help? (Paste your code or question. Press Enter on an empty line to submit.)
You:
public class Calculator {
public int calc(int a, int b) {
if(a>b) return a*2;
else return b*2;
}
}
AI: Thinking...
Based on the coding standards, here is the corrected version:
/// <summary>
/// Représente une calculatrice simple.
/// </summary>
public class Calculatrice
{
/// <summary>
/// Calcule une valeur basée sur la comparaison de deux entiers.
/// </summary>
/// <param name="a">Le premier entier.</param>
/// <param name="b">Le deuxième entier.</param>
/// <returns>Le double de la plus grande valeur.</returns>
public int Calculer(int a, int b)
{
if (a > b)
{
return a * 2;
}
else
{
return b * 2;
}
}
}
Program.cs: Main application entry point handling Azure client setup, agent creation, and the interactive console loopCodeStandardsTool.cs: AI agent plugin containing theSearchCodeStandardsfunction, which readsCodeStandards.mdand provides contextCodeStandards.md: Custom knowledge base defining coding rules, naming conventions, and best practices
| Issue | Solution |
|---|---|
Unknown model Error |
Verify MODEL_DEPLOYMENT_NAME matches your deployment name in Azure AI Studio's Deployments page |
FATAL ERROR for CodeStandards.md not found |
Ensure the file's Copy to Output Directory property is set to Copy if newer |
| Authentication Errors | Run az login and verify your account has "Azure AI Developer" permissions on the Azure AI Project |
Contributions are welcome! Please open an issue or submit a pull request.
This project is licensed under the MIT License.