diff --git a/03.ExploerAgentFramework/README.md b/03.ExploreAgentFramework/README.md similarity index 97% rename from 03.ExploerAgentFramework/README.md rename to 03.ExploreAgentFramework/README.md index 1a6d57b..54f9316 100644 --- a/03.ExploerAgentFramework/README.md +++ b/03.ExploreAgentFramework/README.md @@ -1,342 +1,342 @@ -# Using the Agent Framework to Connect Different AI Solutions - -The Agent Framework provides a unified interface for interacting with various AI models and services. This tutorial demonstrates how to connect to four different AI solutions—Azure OpenAI Service, GitHub Models, Azure AI Foundry, and Foundry Local—using the framework's implementations in both .NET (C\#) and Python. - -## Configuration and Setup - -Before running any of the examples, you need to configure your environment variables. The framework uses these variables to connect to the different AI endpoints. Create a `.env` file in the root of your project and populate it with the necessary credentials and endpoints. - -[cite\_start]You can use the `.env.examples` file as a template[cite: 1]: - -```env -# For GitHub Models -GITHUB_TOKEN="Your GitHub Models Token" -GITHUB_ENDPOINT="Your GitHub Models Endpoint" -GITHUB_MODEL_ID="Your GitHub Model ID" - -# For Azure OpenAI Service -AZURE_OPENAI_ENDPOINT="Your Azure OpenAI Endpoint" -AZURE_OPENAI_CHAT_DEPLOYMENT_NAME ="Your Azure OpenAI Model Deployment Name" - -# For Foundry Local -FOUNDRYLOCAL_ENDPOINT="http://localhost:5272/v1" -FOUNDRYLOCAL_MODEL_DEPLOYMENT_NAME="Your Local Model Name (e.g., Qwen3-0.6b-cpu)" - -# For Azure AI Foundry (Azure AI Studio) -AZURE_AI_PROJECT_ENDPOINT ="Your Azure AI Foundry Project Endpoint" -AZURE_AI_MODEL_DEPLOYMENT_NAME ="Your Azure AI Foundry Project Deployment Name" -``` - ------ - -## 1\. Connecting to Azure OpenAI Service - -This section shows how to use the Agent Framework to connect to models deployed in Azure OpenAI Service. - -### Connection Architecture - -The framework uses a dedicated `AzureOpenAIClient` to handle authentication and communication with the Azure OpenAI endpoint. - -```mermaid -graph TD - A[Your Application] --> B{Agent Framework}; - B --> C[AzureOpenAIClient]; - C --> D[Azure OpenAI Service Endpoint]; -``` - -### .NET / C\# Implementation - -The .NET example uses the `Azure.AI.OpenAI` and `Microsoft.Agents.AI` libraries to create a client and run the agent. - -1. **Dependencies**: NuGet packages like `Azure.AI.OpenAI`, `Azure.Identity`, and the local `Microsoft.Agents.AI.dll` are required. -2. **Client Initialization**: The code initializes an `AzureOpenAIClient`, providing the service endpoint and credentials (e.g., `AzureCliCredential`). -3. **Agent Creation**: The `CreateAIAgent` extension method is called on the chat client to get an agent instance with system instructions. -4. **Execution**: The agent is run using `RunAsync` or `RunStreamingAsync`. - -**Key Code Snippet:** - -```csharp -// Load endpoint and model ID from environment variables -var aoai_endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT"); -var aoai_model_id = Environment.GetEnvironmentVariable("AZURE_OPENAI_RESPONSES_DEPLOYMENT_NAME"); - -// Create the agent -AIAgent agent = new AzureOpenAIClient( - new Uri(aoai_endpoint), - new AzureCliCredential()) - .GetChatClient(aoai_model_id) - .CreateAIAgent("You are a helpful assistant."); - -// Run the agent and get a response -Console.WriteLine(await agent.RunAsync("Write a haiku about Agent Framework.")); -``` - -### Python Implementation - -The Python example uses the `agent_framework.azure` library to achieve the same result. - -1. **Dependencies**: The key libraries are `agent_framework` and `azure-identity`. -2. **Agent Creation**: The `AzureOpenAIChatClient` is instantiated with a credential object. The `create_agent` method is then called to configure the agent with its instructions. -3. **Execution**: The `agent.run_stream()` method is used to invoke the agent and stream the response. - -**Key Code Snippet:** - -```python -from azure.identity import AzureCliCredential -from agent_framework.azure import AzureOpenAIChatClient - -# Create an agent with Azure CLI credentials -agent = AzureOpenAIChatClient(credential=AzureCliCredential()).create_agent( - instructions="You are a helpful weather agent." -) - -query = "Write a haiku about Agent Framework." - -# Stream the agent's response -async for chunk in agent.run_stream(query): - if chunk.text: - print(chunk.text, end="", flush=True) -``` - ------ - -## 2\. Connecting to GitHub Models - -GitHub Models provides an OpenAI-compatible API endpoint. This allows the Agent Framework to use its standard `OpenAIClient` to connect. - -### Connection Architecture - -The connection is made to the GitHub Models endpoint, which behaves like an OpenAI API. - -```mermaid -graph TD - A[Your Application] --> B{Agent Framework}; - B --> C[OpenAIClient]; - C --> D[GitHub Models Endpoint]; -``` - -### .NET / C\# Implementation - -The approach is similar to connecting to any OpenAI-compatible service. - -1. **Dependencies**: This uses the `Microsoft.Extensions.AI.OpenAI` package and local Agent Framework assemblies. -2. **Client Initialization**: An `OpenAIClient` is created. The `OpenAIClientOptions` are configured with the GitHub Models endpoint URL, and an `ApiKeyCredential` is used to pass the GitHub token. -3. **Agent Creation**: The `CreateAIAgent` method is used to instantiate the agent with its system prompt. - -**Key Code Snippet:** - -```csharp -// Load connection details from environment variables -var github_endpoint = Environment.GetEnvironmentVariable("GITHUB_ENDPOINT"); -var github_model_id = Environment.GetEnvironmentVariable("GITHUB_MODEL_ID"); -var github_token = Environment.GetEnvironmentVariable("GITHUB_TOKEN"); - -// Configure client options with the endpoint -var openAIOptions = new OpenAIClientOptions() -{ - Endpoint= new Uri(github_endpoint) -}; - -// Create the agent with an API key credential -AIAgent agent = new OpenAIClient(new ApiKeyCredential(github_token), openAIOptions) - .GetChatClient(github_model_id) - .CreateAIAgent(instructions:"You are a helpful assistant."); - -// Run the agent -Console.WriteLine(await agent.RunAsync("Write a haiku about Agent Framework.")); -``` - -### Python Implementation - -The Python example uses the `OpenAIChatClient` from the framework. - -1. **Dependencies**: The `agent_framework.openai` and `python-dotenv` libraries are used. -2. **Client Initialization**: The `OpenAIChatClient` is initialized with the `base_url` pointing to the GitHub endpoint and the `api_key` set to the GitHub token. -3. **Execution**: A list of `ChatMessage` objects is created and passed to the `client.get_response` method to get a completion. - -**Key Code Snippet:** - -```python -import os -from dotenv import load_dotenv -from agent_framework import ChatMessage, Role -from agent_framework.openai import OpenAIChatClient - -load_dotenv() - -# Initialize the client with GitHub endpoint and token -client = OpenAIChatClient( - base_url=os.environ.get("GITHUB_ENDPOINT"), - api_key=os.environ.get("GITHUB_TOKEN"), - ai_model_id=os.environ.get("GITHUB_MODEL_ID") -) - -# Prepare the messages and get a response -messages = [ - ChatMessage(role=Role.SYSTEM, text="You are a helpful assistant."), - ChatMessage(role=Role.USER, text="Write a haiku about Agent Framework.") - ] -response = await client.get_response(messages) -print(response.messages[0].text) -``` - ------ - -## 3\. Connecting to Azure AI Foundry (Azure AI Studio) - -Azure AI Foundry allows you to create and manage persistent agents. The Agent Framework provides a client to interact with these stateful agents. - -### Connection Architecture - -The framework communicates with the Azure AI Project endpoint to interact with specific, persistent agents by name or ID. - -```mermaid -graph TD - A[Your Application] --> B{Agent Framework}; - B --> C[PersistentAgentsClient]; - C --> D[Azure AI Project Endpoint]; -``` - -### .NET / C\# Implementation - -This example uses the `Azure.AI.Agents.Persistent` client library. - -1. **Dependencies**: The primary dependency is the `Azure.AI.Agents.Persistent` NuGet package. -2. **Client Initialization**: A `PersistentAgentsClient` is created using the Azure AI Project endpoint and a credential. -3. **Agent Creation/Retrieval**: The code first creates a new persistent agent with `CreateAgentAsync`. The resulting agent metadata is then used to get a runnable `AIAgent` instance with `GetAIAgentAsync`. -4. **Execution**: Agents in AI Foundry are stateful and operate on threads. A new thread is created with `agent.GetNewThread()`, and the `RunAsync` method is called with both the prompt and the thread. - -**Key Code Snippet:** - -```csharp -// Load endpoint and model ID from environment variables -var azure_foundry_endpoint = Environment.GetEnvironmentVariable("AZURE_AI_PROJECT_ENDPOINT"); -var azure_foundry_model_id = Environment.GetEnvironmentVariable("AZURE_AI_MODEL_DEPLOYMENT_NAME"); - -// Create the client -var persistentAgentsClient = new PersistentAgentsClient(azure_foundry_endpoint, new AzureCliCredential()); - -// Create a new persistent agent -var agentMetadata = await persistentAgentsClient.Administration.CreateAgentAsync( - model: azure_foundry_model_id, - name: "Agent-Framework", - instructions: "You are an AI assistant that helps people find information."); - -// Get the agent instance and a new thread for conversation -AIAgent agent = await persistentAgentsClient.GetAIAgentAsync(agentMetadata.Value.Id); -AgentThread thread = agent.GetNewThread(); - -// Run the agent within the context of the thread -Console.WriteLine(await agent.RunAsync("Write a haiku about Agent Framework", thread)); -``` - -### Python Implementation - -The Python example uses `AzureAIAgentClient` to interact with persistent agents. - -1. **Dependencies**: `agent_framework.azure` and `azure-identity` are required. -2. **Client Initialization**: The `AzureAIAgentClient` is initialized with an async credential. -3. **Agent and Thread Management**: Inside an `async with` block, `client.create_agent` defines a persistent agent. A new conversational context is created with `agent.get_new_thread()`. -4. **Execution**: `agent.run` is called with the query and the thread object. - -**Key Code Snippet:** - -```python -from azure.identity.aio import AzureCliCredential -from agent_framework.azure import AzureAIAgentClient - -async with AzureCliCredential() as credential, AzureAIAgentClient(async_credential=credential) as client: - # Create a persistent agent - agent = client.create_agent( - name="AgentDemo", - instructions="You are a helpful assistant." - ) - - # Start a new conversation thread - thread = agent.get_new_thread() - query = "Write a haiku about Agent Framework." - - # Get the agent's response - result = await agent.run(query, thread=thread) - print(f"Agent: {result}\\n") -``` - ------ - -## 4\. Connecting to Foundry Local - -Foundry Local serves AI models via a local, OpenAI-compatible API. This makes it easy to test agents with local models without needing cloud resources. - -### Connection Architecture - -The connection pattern is identical to that of GitHub Models, but the endpoint is a local server address. - -```mermaid -graph TD - A[Your Application] --> B{Agent Framework}; - B --> C[OpenAIClient]; - C --> D[Foundry Local Endpoint (e.g., http://localhost:5272/v1)]; -``` - -### .NET / C\# Implementation - -The code is nearly identical to the GitHub Models example, with changes only to the endpoint, model ID, and API key. - -1. **Client Initialization**: The `OpenAIClient` is configured to point to the `FOUNDRYLOCAL_ENDPOINT` (e.g., `http://localhost:5272/v1`). -2. **Authentication**: Since Foundry Local does not require authentication by default, a dummy API key like `"nokey"` is used. - -**Key Code Snippet:** - -```csharp -// Get local endpoint and model ID -var foundrylocal_endpoint = Environment.GetEnvironmentVariable("FOUNDRYLOCAL_ENDPOINT"); -var foundrylocal_model_id = Environment.GetEnvironmentVariable("FOUNDRYLOCAL_MODEL_DEPLOYMENT_NAME"); - -// Configure options to point to the local server -var openAIOptions = new OpenAIClientOptions() -{ - Endpoint= new Uri(foundrylocal_endpoint) -}; - -// Create the agent with a dummy API key -AIAgent agent = new OpenAIClient(new ApiKeyCredential("nokey"), openAIOptions) - .GetChatClient(foundrylocal_model_id) - .CreateAIAgent(instructions:"You are a helpful assistant."); - -// Run the agent -Console.WriteLine(await agent.RunAsync("Can you introduce yourself?")); -``` - -### Python Implementation - -The Python code is also very similar to the GitHub example, differing only in the client's connection parameters. - -1. **Client Initialization**: The `OpenAIChatClient` is initialized with the `base_url` set to the `FOUNDRYLOCAL_ENDPOINT` from the environment variables. -2. **Authentication**: The `api_key` is set to `"nokey"`. - -**Key Code Snippet:** - -```python -import os -from dotenv import load_dotenv -from agent_framework import ChatMessage, Role -from agent_framework.openai import OpenAIChatClient - -load_dotenv() - -# Initialize client for Foundry Local -client = OpenAIChatClient( - base_url=os.environ.get("FOUNDRYLOCAL_ENDPOINT"), - api_key="nokey", - ai_model_id=os.environ.get("FOUNDRYLOCAL_MODEL_DEPLOYMENT_NAME") -) - -# Prepare messages and execute -messages = [ - ChatMessage(role=Role.SYSTEM, text="You are a helpful assistant."), - ChatMessage(role=Role.USER, text="Can you introduce yourself?") - ] - -response = await client.get_response(messages) -print(response.messages[0].text) +# Using the Agent Framework to Connect Different AI Solutions + +The Agent Framework provides a unified interface for interacting with various AI models and services. This tutorial demonstrates how to connect to four different AI solutions—Azure OpenAI Service, GitHub Models, Azure AI Foundry, and Foundry Local—using the framework's implementations in both .NET (C\#) and Python. + +## Configuration and Setup + +Before running any of the examples, you need to configure your environment variables. The framework uses these variables to connect to the different AI endpoints. Create a `.env` file in the root of your project and populate it with the necessary credentials and endpoints. + +[cite\_start]You can use the `.env.examples` file as a template[cite: 1]: + +```env +# For GitHub Models +GITHUB_TOKEN="Your GitHub Models Token" +GITHUB_ENDPOINT="Your GitHub Models Endpoint" +GITHUB_MODEL_ID="Your GitHub Model ID" + +# For Azure OpenAI Service +AZURE_OPENAI_ENDPOINT="Your Azure OpenAI Endpoint" +AZURE_OPENAI_CHAT_DEPLOYMENT_NAME ="Your Azure OpenAI Model Deployment Name" + +# For Foundry Local +FOUNDRYLOCAL_ENDPOINT="http://localhost:5272/v1" +FOUNDRYLOCAL_MODEL_DEPLOYMENT_NAME="Your Local Model Name (e.g., Qwen3-0.6b-cpu)" + +# For Azure AI Foundry (Azure AI Studio) +AZURE_AI_PROJECT_ENDPOINT ="Your Azure AI Foundry Project Endpoint" +AZURE_AI_MODEL_DEPLOYMENT_NAME ="Your Azure AI Foundry Project Deployment Name" +``` + +----- + +## 1\. Connecting to Azure OpenAI Service + +This section shows how to use the Agent Framework to connect to models deployed in Azure OpenAI Service. + +### Connection Architecture + +The framework uses a dedicated `AzureOpenAIClient` to handle authentication and communication with the Azure OpenAI endpoint. + +```mermaid +graph TD + A[Your Application] --> B{Agent Framework}; + B --> C[AzureOpenAIClient]; + C --> D[Azure OpenAI Service Endpoint]; +``` + +### .NET / C\# Implementation + +The .NET example uses the `Azure.AI.OpenAI` and `Microsoft.Agents.AI` libraries to create a client and run the agent. + +1. **Dependencies**: NuGet packages like `Azure.AI.OpenAI`, `Azure.Identity`, and the local `Microsoft.Agents.AI.dll` are required. +2. **Client Initialization**: The code initializes an `AzureOpenAIClient`, providing the service endpoint and credentials (e.g., `AzureCliCredential`). +3. **Agent Creation**: The `CreateAIAgent` extension method is called on the chat client to get an agent instance with system instructions. +4. **Execution**: The agent is run using `RunAsync` or `RunStreamingAsync`. + +**Key Code Snippet:** + +```csharp +// Load endpoint and model ID from environment variables +var aoai_endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT"); +var aoai_model_id = Environment.GetEnvironmentVariable("AZURE_OPENAI_RESPONSES_DEPLOYMENT_NAME"); + +// Create the agent +AIAgent agent = new AzureOpenAIClient( + new Uri(aoai_endpoint), + new AzureCliCredential()) + .GetChatClient(aoai_model_id) + .CreateAIAgent("You are a helpful assistant."); + +// Run the agent and get a response +Console.WriteLine(await agent.RunAsync("Write a haiku about Agent Framework.")); +``` + +### Python Implementation + +The Python example uses the `agent_framework.azure` library to achieve the same result. + +1. **Dependencies**: The key libraries are `agent_framework` and `azure-identity`. +2. **Agent Creation**: The `AzureOpenAIChatClient` is instantiated with a credential object. The `create_agent` method is then called to configure the agent with its instructions. +3. **Execution**: The `agent.run_stream()` method is used to invoke the agent and stream the response. + +**Key Code Snippet:** + +```python +from azure.identity import AzureCliCredential +from agent_framework.azure import AzureOpenAIChatClient + +# Create an agent with Azure CLI credentials +agent = AzureOpenAIChatClient(credential=AzureCliCredential()).create_agent( + instructions="You are a helpful weather agent." +) + +query = "Write a haiku about Agent Framework." + +# Stream the agent's response +async for chunk in agent.run_stream(query): + if chunk.text: + print(chunk.text, end="", flush=True) +``` + +----- + +## 2\. Connecting to GitHub Models + +GitHub Models provides an OpenAI-compatible API endpoint. This allows the Agent Framework to use its standard `OpenAIClient` to connect. + +### Connection Architecture + +The connection is made to the GitHub Models endpoint, which behaves like an OpenAI API. + +```mermaid +graph TD + A[Your Application] --> B{Agent Framework}; + B --> C[OpenAIClient]; + C --> D[GitHub Models Endpoint]; +``` + +### .NET / C\# Implementation + +The approach is similar to connecting to any OpenAI-compatible service. + +1. **Dependencies**: This uses the `Microsoft.Extensions.AI.OpenAI` package and local Agent Framework assemblies. +2. **Client Initialization**: An `OpenAIClient` is created. The `OpenAIClientOptions` are configured with the GitHub Models endpoint URL, and an `ApiKeyCredential` is used to pass the GitHub token. +3. **Agent Creation**: The `CreateAIAgent` method is used to instantiate the agent with its system prompt. + +**Key Code Snippet:** + +```csharp +// Load connection details from environment variables +var github_endpoint = Environment.GetEnvironmentVariable("GITHUB_ENDPOINT"); +var github_model_id = Environment.GetEnvironmentVariable("GITHUB_MODEL_ID"); +var github_token = Environment.GetEnvironmentVariable("GITHUB_TOKEN"); + +// Configure client options with the endpoint +var openAIOptions = new OpenAIClientOptions() +{ + Endpoint= new Uri(github_endpoint) +}; + +// Create the agent with an API key credential +AIAgent agent = new OpenAIClient(new ApiKeyCredential(github_token), openAIOptions) + .GetChatClient(github_model_id) + .CreateAIAgent(instructions:"You are a helpful assistant."); + +// Run the agent +Console.WriteLine(await agent.RunAsync("Write a haiku about Agent Framework.")); +``` + +### Python Implementation + +The Python example uses the `OpenAIChatClient` from the framework. + +1. **Dependencies**: The `agent_framework.openai` and `python-dotenv` libraries are used. +2. **Client Initialization**: The `OpenAIChatClient` is initialized with the `base_url` pointing to the GitHub endpoint and the `api_key` set to the GitHub token. +3. **Execution**: A list of `ChatMessage` objects is created and passed to the `client.get_response` method to get a completion. + +**Key Code Snippet:** + +```python +import os +from dotenv import load_dotenv +from agent_framework import ChatMessage, Role +from agent_framework.openai import OpenAIChatClient + +load_dotenv() + +# Initialize the client with GitHub endpoint and token +client = OpenAIChatClient( + base_url=os.environ.get("GITHUB_ENDPOINT"), + api_key=os.environ.get("GITHUB_TOKEN"), + ai_model_id=os.environ.get("GITHUB_MODEL_ID") +) + +# Prepare the messages and get a response +messages = [ + ChatMessage(role=Role.SYSTEM, text="You are a helpful assistant."), + ChatMessage(role=Role.USER, text="Write a haiku about Agent Framework.") + ] +response = await client.get_response(messages) +print(response.messages[0].text) +``` + +----- + +## 3\. Connecting to Azure AI Foundry (Azure AI Studio) + +Azure AI Foundry allows you to create and manage persistent agents. The Agent Framework provides a client to interact with these stateful agents. + +### Connection Architecture + +The framework communicates with the Azure AI Project endpoint to interact with specific, persistent agents by name or ID. + +```mermaid +graph TD + A[Your Application] --> B{Agent Framework}; + B --> C[PersistentAgentsClient]; + C --> D[Azure AI Project Endpoint]; +``` + +### .NET / C\# Implementation + +This example uses the `Azure.AI.Agents.Persistent` client library. + +1. **Dependencies**: The primary dependency is the `Azure.AI.Agents.Persistent` NuGet package. +2. **Client Initialization**: A `PersistentAgentsClient` is created using the Azure AI Project endpoint and a credential. +3. **Agent Creation/Retrieval**: The code first creates a new persistent agent with `CreateAgentAsync`. The resulting agent metadata is then used to get a runnable `AIAgent` instance with `GetAIAgentAsync`. +4. **Execution**: Agents in AI Foundry are stateful and operate on threads. A new thread is created with `agent.GetNewThread()`, and the `RunAsync` method is called with both the prompt and the thread. + +**Key Code Snippet:** + +```csharp +// Load endpoint and model ID from environment variables +var azure_foundry_endpoint = Environment.GetEnvironmentVariable("AZURE_AI_PROJECT_ENDPOINT"); +var azure_foundry_model_id = Environment.GetEnvironmentVariable("AZURE_AI_MODEL_DEPLOYMENT_NAME"); + +// Create the client +var persistentAgentsClient = new PersistentAgentsClient(azure_foundry_endpoint, new AzureCliCredential()); + +// Create a new persistent agent +var agentMetadata = await persistentAgentsClient.Administration.CreateAgentAsync( + model: azure_foundry_model_id, + name: "Agent-Framework", + instructions: "You are an AI assistant that helps people find information."); + +// Get the agent instance and a new thread for conversation +AIAgent agent = await persistentAgentsClient.GetAIAgentAsync(agentMetadata.Value.Id); +AgentThread thread = agent.GetNewThread(); + +// Run the agent within the context of the thread +Console.WriteLine(await agent.RunAsync("Write a haiku about Agent Framework", thread)); +``` + +### Python Implementation + +The Python example uses `AzureAIAgentClient` to interact with persistent agents. + +1. **Dependencies**: `agent_framework.azure` and `azure-identity` are required. +2. **Client Initialization**: The `AzureAIAgentClient` is initialized with an async credential. +3. **Agent and Thread Management**: Inside an `async with` block, `client.create_agent` defines a persistent agent. A new conversational context is created with `agent.get_new_thread()`. +4. **Execution**: `agent.run` is called with the query and the thread object. + +**Key Code Snippet:** + +```python +from azure.identity.aio import AzureCliCredential +from agent_framework.azure import AzureAIAgentClient + +async with AzureCliCredential() as credential, AzureAIAgentClient(async_credential=credential) as client: + # Create a persistent agent + agent = client.create_agent( + name="AgentDemo", + instructions="You are a helpful assistant." + ) + + # Start a new conversation thread + thread = agent.get_new_thread() + query = "Write a haiku about Agent Framework." + + # Get the agent's response + result = await agent.run(query, thread=thread) + print(f"Agent: {result}\\n") +``` + +----- + +## 4\. Connecting to Foundry Local + +Foundry Local serves AI models via a local, OpenAI-compatible API. This makes it easy to test agents with local models without needing cloud resources. + +### Connection Architecture + +The connection pattern is identical to that of GitHub Models, but the endpoint is a local server address. + +```mermaid +graph TD + A[Your Application] --> B{Agent Framework}; + B --> C[OpenAIClient]; + C --> D[Foundry Local Endpoint (e.g., http://localhost:5272/v1)]; +``` + +### .NET / C\# Implementation + +The code is nearly identical to the GitHub Models example, with changes only to the endpoint, model ID, and API key. + +1. **Client Initialization**: The `OpenAIClient` is configured to point to the `FOUNDRYLOCAL_ENDPOINT` (e.g., `http://localhost:5272/v1`). +2. **Authentication**: Since Foundry Local does not require authentication by default, a dummy API key like `"nokey"` is used. + +**Key Code Snippet:** + +```csharp +// Get local endpoint and model ID +var foundrylocal_endpoint = Environment.GetEnvironmentVariable("FOUNDRYLOCAL_ENDPOINT"); +var foundrylocal_model_id = Environment.GetEnvironmentVariable("FOUNDRYLOCAL_MODEL_DEPLOYMENT_NAME"); + +// Configure options to point to the local server +var openAIOptions = new OpenAIClientOptions() +{ + Endpoint= new Uri(foundrylocal_endpoint) +}; + +// Create the agent with a dummy API key +AIAgent agent = new OpenAIClient(new ApiKeyCredential("nokey"), openAIOptions) + .GetChatClient(foundrylocal_model_id) + .CreateAIAgent(instructions:"You are a helpful assistant."); + +// Run the agent +Console.WriteLine(await agent.RunAsync("Can you introduce yourself?")); +``` + +### Python Implementation + +The Python code is also very similar to the GitHub example, differing only in the client's connection parameters. + +1. **Client Initialization**: The `OpenAIChatClient` is initialized with the `base_url` set to the `FOUNDRYLOCAL_ENDPOINT` from the environment variables. +2. **Authentication**: The `api_key` is set to `"nokey"`. + +**Key Code Snippet:** + +```python +import os +from dotenv import load_dotenv +from agent_framework import ChatMessage, Role +from agent_framework.openai import OpenAIChatClient + +load_dotenv() + +# Initialize client for Foundry Local +client = OpenAIChatClient( + base_url=os.environ.get("FOUNDRYLOCAL_ENDPOINT"), + api_key="nokey", + ai_model_id=os.environ.get("FOUNDRYLOCAL_MODEL_DEPLOYMENT_NAME") +) + +# Prepare messages and execute +messages = [ + ChatMessage(role=Role.SYSTEM, text="You are a helpful assistant."), + ChatMessage(role=Role.USER, text="Can you introduce yourself?") + ] + +response = await client.get_response(messages) +print(response.messages[0].text) ``` \ No newline at end of file diff --git a/03.ExploerAgentFramework/code_samples/dotNET/01-dotnet-agent-framework-aoai/01-dotnet-agent-framework-aoai.csproj b/03.ExploreAgentFramework/code_samples/dotNET/01-dotnet-agent-framework-aoai/01-dotnet-agent-framework-aoai.csproj similarity index 100% rename from 03.ExploerAgentFramework/code_samples/dotNET/01-dotnet-agent-framework-aoai/01-dotnet-agent-framework-aoai.csproj rename to 03.ExploreAgentFramework/code_samples/dotNET/01-dotnet-agent-framework-aoai/01-dotnet-agent-framework-aoai.csproj diff --git a/03.ExploerAgentFramework/code_samples/dotNET/01-dotnet-agent-framework-aoai/Program.cs b/03.ExploreAgentFramework/code_samples/dotNET/01-dotnet-agent-framework-aoai/Program.cs similarity index 100% rename from 03.ExploerAgentFramework/code_samples/dotNET/01-dotnet-agent-framework-aoai/Program.cs rename to 03.ExploreAgentFramework/code_samples/dotNET/01-dotnet-agent-framework-aoai/Program.cs diff --git a/03.ExploerAgentFramework/code_samples/dotNET/02-dotnet-agent-framework-ghmodel/02-dotnet-agent-framework-ghmodel.csproj b/03.ExploreAgentFramework/code_samples/dotNET/02-dotnet-agent-framework-ghmodel/02-dotnet-agent-framework-ghmodel.csproj similarity index 100% rename from 03.ExploerAgentFramework/code_samples/dotNET/02-dotnet-agent-framework-ghmodel/02-dotnet-agent-framework-ghmodel.csproj rename to 03.ExploreAgentFramework/code_samples/dotNET/02-dotnet-agent-framework-ghmodel/02-dotnet-agent-framework-ghmodel.csproj diff --git a/03.ExploerAgentFramework/code_samples/dotNET/02-dotnet-agent-framework-ghmodel/Program.cs b/03.ExploreAgentFramework/code_samples/dotNET/02-dotnet-agent-framework-ghmodel/Program.cs similarity index 100% rename from 03.ExploerAgentFramework/code_samples/dotNET/02-dotnet-agent-framework-ghmodel/Program.cs rename to 03.ExploreAgentFramework/code_samples/dotNET/02-dotnet-agent-framework-ghmodel/Program.cs diff --git a/03.ExploerAgentFramework/code_samples/dotNET/03-dotnet-agent-framework-msfoundry/03-dotnet-agent-framework-msfoundry.csproj b/03.ExploreAgentFramework/code_samples/dotNET/03-dotnet-agent-framework-msfoundry/03-dotnet-agent-framework-msfoundry.csproj similarity index 100% rename from 03.ExploerAgentFramework/code_samples/dotNET/03-dotnet-agent-framework-msfoundry/03-dotnet-agent-framework-msfoundry.csproj rename to 03.ExploreAgentFramework/code_samples/dotNET/03-dotnet-agent-framework-msfoundry/03-dotnet-agent-framework-msfoundry.csproj diff --git a/03.ExploerAgentFramework/code_samples/dotNET/03-dotnet-agent-framework-msfoundry/Program.cs b/03.ExploreAgentFramework/code_samples/dotNET/03-dotnet-agent-framework-msfoundry/Program.cs similarity index 100% rename from 03.ExploerAgentFramework/code_samples/dotNET/03-dotnet-agent-framework-msfoundry/Program.cs rename to 03.ExploreAgentFramework/code_samples/dotNET/03-dotnet-agent-framework-msfoundry/Program.cs diff --git a/03.ExploerAgentFramework/code_samples/dotNET/04-dotnet-agent-framework-foundrylocal/04-dotnet-agent-framework-foundrylocal.csproj b/03.ExploreAgentFramework/code_samples/dotNET/04-dotnet-agent-framework-foundrylocal/04-dotnet-agent-framework-foundrylocal.csproj similarity index 100% rename from 03.ExploerAgentFramework/code_samples/dotNET/04-dotnet-agent-framework-foundrylocal/04-dotnet-agent-framework-foundrylocal.csproj rename to 03.ExploreAgentFramework/code_samples/dotNET/04-dotnet-agent-framework-foundrylocal/04-dotnet-agent-framework-foundrylocal.csproj diff --git a/03.ExploerAgentFramework/code_samples/dotNET/04-dotnet-agent-framework-foundrylocal/Program.cs b/03.ExploreAgentFramework/code_samples/dotNET/04-dotnet-agent-framework-foundrylocal/Program.cs similarity index 100% rename from 03.ExploerAgentFramework/code_samples/dotNET/04-dotnet-agent-framework-foundrylocal/Program.cs rename to 03.ExploreAgentFramework/code_samples/dotNET/04-dotnet-agent-framework-foundrylocal/Program.cs diff --git a/03.ExploerAgentFramework/code_samples/python/01-python-agent-framework-aoai.ipynb b/03.ExploreAgentFramework/code_samples/python/01-python-agent-framework-aoai.ipynb similarity index 100% rename from 03.ExploerAgentFramework/code_samples/python/01-python-agent-framework-aoai.ipynb rename to 03.ExploreAgentFramework/code_samples/python/01-python-agent-framework-aoai.ipynb diff --git a/03.ExploerAgentFramework/code_samples/python/02-python-agent-framrwork-ghmodel.ipynb b/03.ExploreAgentFramework/code_samples/python/02-python-agent-framrwork-ghmodel.ipynb similarity index 100% rename from 03.ExploerAgentFramework/code_samples/python/02-python-agent-framrwork-ghmodel.ipynb rename to 03.ExploreAgentFramework/code_samples/python/02-python-agent-framrwork-ghmodel.ipynb diff --git a/03.ExploerAgentFramework/code_samples/python/03-python-agent-framework-msfoundry.ipynb b/03.ExploreAgentFramework/code_samples/python/03-python-agent-framework-msfoundry.ipynb similarity index 100% rename from 03.ExploerAgentFramework/code_samples/python/03-python-agent-framework-msfoundry.ipynb rename to 03.ExploreAgentFramework/code_samples/python/03-python-agent-framework-msfoundry.ipynb diff --git a/03.ExploerAgentFramework/code_samples/python/04-python-agent-framework-foundrylocal.ipynb b/03.ExploreAgentFramework/code_samples/python/04-python-agent-framework-foundrylocal.ipynb similarity index 100% rename from 03.ExploerAgentFramework/code_samples/python/04-python-agent-framework-foundrylocal.ipynb rename to 03.ExploreAgentFramework/code_samples/python/04-python-agent-framework-foundrylocal.ipynb diff --git a/README.md b/README.md index 31ebffd..835f23f 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ This repository provides step-by-step tutorials and real-world examples covering | **[00.ForBeginners](./00.ForBeginners/README.md)** | **Beginner-friendly Microsoft Agent Framework examples extending [AI Agents for Beginners](https://github.com/microsoft/ai-agents-for-beginners)** | [Travel Agent](./00.ForBeginners/01-intro-to-ai-agents/code_samples/dotnet-agent-framework-travelagent/)
[Basic Agent](./00.ForBeginners/02-explore-agentic-frameworks/code_samples/dotnet-agent-framework-basicagent/)
[Design Patterns](./00.ForBeginners/03-agentic-design-patterns/code_samples/dotnet-agent-framework-basicagent/)
[Tool Use](./00.ForBeginners/04-tool-use/code_samples/dotnet-agent-framework-ghmodels-tool/)
[RAG Search](./00.ForBeginners/05-agentic-rag/code_samples/dotnet-agent-framework-msfoundry-file-search/)
[Planning](./00.ForBeginners/07-planning-design/code_samples/dotnet-agent-framrwork-ghmodel-planningdesign/)
[Multi-Agent](./00.ForBeginners/08-multi-agent/code_samples/dotnet-agent-framework-ghmodel-workflow-multi-agents/) | [Travel Agent](./00.ForBeginners/01-intro-to-ai-agents/code_samples/python-agent-framework-travelagent.ipynb)
[Basic Agent](./00.ForBeginners/02-explore-agentic-frameworks/code_samples/python-agent-framework-basicagent.ipynb)
[Design Patterns](./00.ForBeginners/03-agentic-design-patterns/code_samples/python-agent-framework-ghmodel-basicagent.ipynb)
[Tool Use](./00.ForBeginners/04-tool-use/code_samples/python-agent-framework-ghmodel-tools.ipynb)
[RAG Search](./00.ForBeginners/05-agentic-rag/code_samples/python-agent-framework-msfoundry-file-search.ipynb)
[Planning](./00.ForBeginners/07-planning-design/code_samples/python-agent-framrwork-ghmodel-planningdesign.ipynb)
[Multi-Agent](./00.ForBeginners/08-multi-agent/code_samples/python-agent-framework-ghmodel-workflow-multi-agents.ipynb) | | **[01.AgentFoundation](./01.AgentFoundation/README.md)** | Core concepts and architecture of Microsoft Agent Framework | *Documentation Only* | *Documentation Only* | | **[02.CreateYourFirstAgent](./02.CreateYourFirstAgent/README.md)** | Build your first travel planning agent from scratch | [Travel Agent with GitHub Models](./02.CreateYourFirstAgent/code_samples/dotNET/dotnet-travelagent-ghmodel/) | [Travel Agent with GitHub Models](./02.CreateYourFirstAgent/code_samples/python/python-travelagent-ghmodel.ipynb) | -| **[03.ExploreAgentFramework](./03.ExploerAgentFramework/README.md)** | Deep dive into different providers and configurations | [Azure OpenAI](./03.ExploerAgentFramework/code_samples/dotNET/01-dotnet-agent-framework-aoai/)
[GitHub Models](./03.ExploerAgentFramework/code_samples/dotNET/02-dotnet-agent-framework-ghmodel/)
[MS Foundry](./03.ExploerAgentFramework/code_samples/dotNET/03-dotnet-agent-framework-msfoundry/)
[Foundry Local](./03.ExploerAgentFramework/code_samples/dotNET/04-dotnet-agent-framework-foundrylocal/) | [Azure OpenAI](./03.ExploerAgentFramework/code_samples/python/01-python-agent-framework-aoai.ipynb)
[GitHub Models](./03.ExploerAgentFramework/code_samples/python/02-python-agent-framrwork-ghmodel.ipynb)
[MS Foundry](./03.ExploerAgentFramework/code_samples/python/03-python-agent-framework-msfoundry.ipynb)
[Foundry Local](./03.ExploerAgentFramework/code_samples/python/04-python-agent-framrwork-foundrylocal.ipynb) | +| **[03.ExploreAgentFramework](./03.ExploreAgentFramework/README.md)** | Deep dive into different providers and configurations | [Azure OpenAI](./03.ExploreAgentFramework/code_samples/dotNET/01-dotnet-agent-framework-aoai/)
[GitHub Models](./03.ExploreAgentFramework/code_samples/dotNET/02-dotnet-agent-framework-ghmodel/)
[MS Foundry](./03.ExploreAgentFramework/code_samples/dotNET/03-dotnet-agent-framework-msfoundry/)
[Foundry Local](./03.ExploreAgentFramework/code_samples/dotNET/04-dotnet-agent-framework-foundrylocal/) | [Azure OpenAI](./03.ExploreAgentFramework/code_samples/python/01-python-agent-framework-aoai.ipynb)
[GitHub Models](./03.ExploreAgentFramework/code_samples/python/02-python-agent-framrwork-ghmodel.ipynb)
[MS Foundry](./03.ExploreAgentFramework/code_samples/python/03-python-agent-framework-msfoundry.ipynb)
[Foundry Local](./03.ExploreAgentFramework/code_samples/python/04-python-agent-framrwork-foundrylocal.ipynb) | | **[04.Tools](./04.Tools/README.md)** | Vision, code interpretation, and custom tool integration | [Vision](./04.Tools/code_samples/dotNET/msfoundry/01-dotnet-agent-framework-msfoundry-vision/)
[Code Interpreter](./04.Tools/code_samples/dotNET/msfoundry/02-dotnet-agent-framework-msfoundry-code-interpreter/)
[Bing Grounding](./04.Tools/code_samples/dotNET/msfoundry/03-dotnet-agent-framework-msfoundry-binggrounding/)
[File Search](./04.Tools/code_samples/dotNET/msfoundry/04-dotnet-agent-framework-msfoundry-file-search/) | [Vision](./04.Tools/code_samples/python/msfoundry/01.python-agent-framework-msfoundry-vision.ipynb)
[Code Interpreter](./04.Tools/code_samples/python/msfoundry/02.python-agent-framework-msfoundry-code-interpreter.ipynb)
[Bing Grounding](./04.Tools/code_samples/python/msfoundry/03.python-agent-framework-msfoundry-binggrounding.ipynb)
[File Search](./04.Tools/code_samples/python/msfoundry/04.python-agent-framework-msfoundry-file-search.ipynb) | | **[05.Providers](./05.Providers/README.md)** | MCP (Model Context Protocol) and Agent-to-Agent communication | [MCP with Microsoft Learn](./05.Providers/code_samples/dotNET/01-dotnet-agent-framework-aifoundry-mcp/AgentMCP.Console/)| [MCP with Microsoft Learn](./05.Providers/code_samples/python/01-python-agent-framework-aifoundry-mcp.ipynb) | | **[06.RAGs](./06.RAGs/README.md)** | Knowledge-enhanced agents with file search capabilities | [File Search RAG](./06.RAGs/code_samples/dotNET/dotnet-agent-framework-msfoundry-file-search/) | [File Search RAG](./06.RAGs/code_samples/python/python-agent-framework-msfoundry-file-search.ipynb) |