Skip to content

Majdi-Zlitni/FoundryLabCSharp

Repository files navigation

C# Code Standards AI Agent

1. Overview

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

2. Features

  • Dynamic Agent Creation: Uses the Azure.AI.Agents SDK to create and configure an AI agent at runtime
  • Custom Knowledge Grounding (RAG): Implements a CodeStandardsTool that reads a local CodeStandards.md file, 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 DefaultAzureCredential for passwordless authentication to Azure services

3. Prerequisites

  • .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)

4. Setup and Configuration

Step 1: Clone the Repository

git clone https://github.com/Majdi-Zlitni/FoundryLabCSharp
cd FoundryLabCSharp

Step 2: Configure the Code Standards File

  1. Open the solution in your IDE (e.g., Visual Studio 2022)
  2. Locate CodeStandards.md in the Solution Explorer
  3. Right-click the file and select Properties
  4. Set Copy to Output Directory to Copy if newer
  5. Edit CodeStandards.md to define your custom coding rules

Step 3: Update Application Constants

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>";

Step 4: Build and Run

dotnet build
dotnet run

5. Usage

After 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 exit or quit and press Enter

Example Interaction

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;
        }
    }
}

6. Project Structure

  • Program.cs: Main application entry point handling Azure client setup, agent creation, and the interactive console loop
  • CodeStandardsTool.cs: AI agent plugin containing the SearchCodeStandards function, which reads CodeStandards.md and provides context
  • CodeStandards.md: Custom knowledge base defining coding rules, naming conventions, and best practices

7. Troubleshooting

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

8. Contributing

Contributions are welcome! Please open an issue or submit a pull request.

9. License

This project is licensed under the MIT License.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages