AI代理功能

一个基于.NET 9和Azure OpenAI的Azure Function,提供AI代理功能,支持依赖注入、强类型模型和重试逻辑。

  • by firstcrazydeveloper
  • Dockerfile
  • 本地部署
  • 更新于
在 GitHub 查看 firstcrazydeveloper/simaira-agent-mcp-langchain

使用说明

MCP人工智能代理项目

A. .NET 9 Azure函数 使用 Azure OpenAI 与:

  • 依赖注入
  • 通过配置 local.settings.json
  • 强类型模型
  • 重试逻辑(Polly)
  • 逐令牌流式传输AI响应

1.先决条件


2.安装Azure功能核心工具

全局安装Azure Functions CLI:

npm install -g azure-functions-core-tools@4 --unsafe-perm true

3.本地运行

第一步——清洁和建造

转到 src 文件夹并运行:

dotnet clean ./Simaira-MCP-LangChain-Agent 
dotnet build ./Simaira-MCP-LangChain-Agent

步骤2–启动功能主机

运行详细日志记录:

func start --script-root ./Simaira-MCP-LangChain-Agent --verbose

您的功能将在以下网址提供:

http://localhost:7072/api/<FunctionName>

4.VS代码调试

  1. 启动您的功能:
    func start --script-root ./Simaira-MCP-LangChain-Agent --dotnet-isolated-debug
    
  2. 在VS Code终端中, 查找工作进程ID (寻找 dotnet 过程)。
  3. 更新 processId 在 .vscode/launch.json 在...之下 Attach to Isolated Worker 配置:
    {
        "name": "Attach to Isolated Worker",
        "type": "coreclr",
        "request": "attach",
        "processId": "12345"
    }
    
  4. 首选 运行和调试 → 选择 “依附于孤立的工人” → Press F5.

5.Docker命令

构建和运行:

docker build  -f Simaira-MCP-LangChain-Agent/Dockerfile -t my-azure-functionv1.1 .
docker run -p 8080:80 --name myfunctiondebug my-azure-functionv1.1

然后访问:

http://localhost:8080/api/<FunctionName>

6.配置

选项A-作为对象

在中使用强类型节 local.settings.json:

"AzureOpenAI": {
  "Endpoint": "https://your-endpoint.openai.azure.com/",
  "Key": "your-key",
  "DeploymentName": "gpt-35-turbo",
  "ApiVersion": "2025-01-01-preview"
}

选项B——作为价值

使用嵌套值(在使用Docker/env覆盖时很有用):

"Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
    "AzureOpenAI:Endpoint": "https://your-endpoint.openai.azure.com/",
    "AzureOpenAI:Key": "your-key",
    "AzureOpenAI:DeploymentName": "gpt-35-turbo",
    "AzureOpenAI:ApiVersion": "2025-01-01-preview",
    "Host:LocalHttpPort": "7072"
}

注:
如果在这些配置中找不到值,应用程序将抛出错误,以防止在缺少设置的情况下运行。


7.API终点

此Azure Function应用程序中提供以下HTTP终结点:

1. MCPAgentAPI

  • 网址: /api/MCPAgentAPI
  • 方法: GET, POST
  • 请求正文(POST):
{
  "message": "Write a function for binary search in C#."
}
  • 答复:
{
  "response": "public int BinarySearch(int[] arr, int target) { ... }"
}

2.AskSimairaAPI

  • 网址: /api/AskSimairaAPI
  • 方法: GET, POST
  • 请求正文(POST):
{
  "message": "Explain how binary search works."
}
  • 答复:
{
  "response": "Binary search is an algorithm that finds the position of a target value within a sorted array..."
}

3.AskSimairaSDK

  • 网址: /api/AskSimairaSDK
  • 方法: GET, POST
  • 请求正文(POST):
{
  "message": "Write a function for quicksort in C#."
}
  • 答复:
{
  "response": "public void QuickSort(int[] arr, int left, int right) { ... }"
}

4.MCPAgentSDK

  • 网址: /api/MCPAgentSDK
  • 方法: GET, POST
  • 请求正文(POST):
{
  "message": "Write a function for merge sort in C#."
}
  • 答复:
{
  "response": "public void MergeSort(int[] arr) { ... }"
}

注:

  • 所有端点都接受 message 在请求正文中(for POST).
  • 响应是使用Azure OpenAI GPT模型生成的AI内容。