首页
/ Azure AI旅行代理系统API开发指南

Azure AI旅行代理系统API开发指南

2025-06-07 21:38:30作者:段琳惟

概述

Azure AI旅行代理系统是一个基于人工智能的旅行规划解决方案,通过API提供智能化的旅行建议、行程规划和个性化推荐服务。本指南将详细介绍该系统的API设计、功能和使用方法。

核心功能

  1. 自然语言处理:理解用户以自然语言表达的旅行需求
  2. 偏好提取:自动从对话中提取旅行偏好和约束条件
  3. 目的地推荐:基于用户偏好推荐合适的旅行目的地
  4. 行程规划:生成详细的每日行程安排
  5. 预算估算:提供准确的旅行成本估算

API基础

基本URL

环境 基本URL 描述
本地开发 http://localhost:4000/api 本地开发服务器
Docker环境 http://web-api:4000/api Docker Compose环境
生产环境 https://{app-name}.{region}.azurecontainerapps.io/api Azure容器应用部署

内容类型

  • 请求Content-Type: application/json
  • 响应Content-Type: application/json (REST端点) 或 text/event-stream (SSE端点)
  • 字符编码: UTF-8

认证与安全

开发环境

在本地开发环境中,API默认不启用认证,仅用于演示目的。

生产环境安全建议

Azure AD集成

// 推荐的认证中间件
app.use('/api', async (req, res, next) => {
  try {
    const token = req.headers.authorization?.replace('Bearer ', '');
    if (!token) {
      return res.status(401).json({ error: '需要认证' });
    }
    
    // 验证Azure AD令牌
    const credentials = new DefaultAzureCredential();
    const tokenInfo = await credentials.getToken(['https://graph.microsoft.com/.default']);
    
    // 添加用户上下文到请求
    req.user = await validateAndDecodeToken(token);
    next();
  } catch (error) {
    return res.status(401).json({ error: '无效的认证令牌' });
  }
});

API密钥认证

// 替代的API密钥认证
app.use('/api', (req, res, next) => {
  const apiKey = req.headers['x-api-key'];
  if (!apiKey || !validateApiKey(apiKey)) {
    return res.status(401).json({ error: '需要有效的API密钥' });
  }
  next();
});

关键API端点

健康检查端点

GET /health

用途:服务健康检查,用于监控和负载均衡

请求示例

GET /api/health
Accept: application/json

响应示例

{
  "status": "OK",
  "timestamp": "2024-01-01T12:00:00Z",
  "version": "1.0.0",
  "environment": "production",
  "dependencies": {
    "mcp_servers": {
      "echo-ping": "healthy",
      "customer-query": "healthy"
    },
    "external_services": {
      "azure_openai": "healthy"
    }
  }
}

工具发现端点

GET /tools

用途:获取可用的MCP工具及其功能

响应示例

{
  "tools": [
    {
      "id": "customer-query",
      "name": "客户查询处理",
      "tools": [
        {
          "name": "extract_preferences",
          "description": "从自然语言中提取旅行偏好",
          "inputSchema": {
            "type": "object",
            "properties": {
              "query": {
                "type": "string",
                "description": "用户的自然语言旅行查询"
              }
            },
            "required": ["query"]
          }
        }
      ]
    }
  ]
}

聊天处理端点

POST /chat

用途:处理旅行查询并返回AI代理的流式响应

请求示例

{
  "message": "我想规划一个7天的日本假期,预算3000美元",
  "tools": [
    {
      "id": "customer-query",
      "name": "客户查询处理",
      "selected": true
    }
  ],
  "context": {
    "user_preferences": {
      "language": "zh",
      "currency": "USD"
    }
  }
}

响应流示例

{"type":"metadata","agent":"CustomerQueryAgent","event":"AgentInput","data":{"input":"我想规划一个7天的日本假期,预算3000美元"}}

{"type":"metadata","agent":"CustomerQueryAgent","event":"AgentOutput","data":{"preferences":{"destination":"日本","duration":"7天","budget":"3000美元"}}}

{"type":"end","data":{"total_processing_time_ms":15600}}

数据模型

旅行偏好模型

interface TravelPreferences {
  destinations?: string[];
  budget?: {
    amount: number;
    currency: string;
  };
  duration?: {
    days: number;
  flexibility_days?: number;
  };
  group?: {
    size: number;
    composition: 'solo' | 'couple' | 'family';
  };
}

行程模型

interface Itinerary {
  duration_days: number;
  destinations: string[];
  daily_plans: DailyPlan[];
}

interface DailyPlan {
  day: number;
  location: string;
  activities: ScheduledActivity[];
}

最佳实践

  1. 错误处理:始终检查响应状态码并处理可能的错误
  2. 流式处理:对于长时间运行的请求,使用SSE流式处理结果
  3. 上下文保持:在对话中保持上下文信息以获得更连贯的响应
  4. 性能监控:定期检查API响应时间和服务健康状况

常见问题

Q: 如何处理API超时? A: 建议设置合理的超时时间(默认30秒),对于复杂查询可分步处理

Q: 如何提高推荐的相关性? A: 提供尽可能详细的用户偏好和上下文信息

Q: 系统支持哪些语言? A: 目前主要支持英语和中文,其他语言支持正在开发中

通过本指南,开发者可以快速了解Azure AI旅行代理系统的API功能和使用方法,构建智能化的旅行规划应用。

登录后查看全文
热门项目推荐