默认情况下,SDK 使用你的 GitHub Copilot 订阅调用模型。但 BYOK(Bring Your Own Key) 模式允许你直接使用自己的 API 密钥:
session = await client.create_session({ "model": "moonshot-v1-8k", # Model must be specified when using BYOK "provider": { "type": "openai", # Supports: "openai", "azure", "anthropic" "base_url": "https://api.moonshot.cn/v1", "api_key": os.environ["MOONSHOT_API_KEY"], # Optional: wire_api controls API format # "completions" (default) - Standard Chat Completions API
# "responses" - New Responses API (for GPT-5 series) } })
Azure AI Foundry(OpenAI 兼容端点):使用 type: "openai",base_url 需包含 /openai/v1/
本地 Ollama:使用 type: "openai",base_url 为 http://localhost:11434/v1,无需 API 密钥
国内提供商(如 Moonshot):使用 type: "openai"(OpenAI 兼容协议),配置对应的 base_url 和 API 密钥
工具调用的 " 智能 " 在哪里?
关键问题:AI 如何知道该调用哪个工具?
答案:你提供的 description 和 parameters 会被注入到 LLM 的上下文中。
实际发送给 LLM 的 prompt 类似:
System: You can use the following tools: 1. get_weather Description: Get current weather for a specified city Parameters: {"city": "string (City name)"} 2. get_stock_price Description: Query real-time stock price Parameters: {"symbol": "string (Stock symbol)"} User: What's the weather in Beijing today? [AI Reasoning] → User asks about weather, should call get_weather → Parameters: {"city": "Beijing"}
session = await client.create_session({ "model": "gpt-4.1", "mcp_servers": { "github": { "type": "http", "url": "https://api.githubcopilot.com/mcp/", "headers": {"Authorization": "Bearer ${TOKEN}"}, "tools": ["*"], } } }) # AI can now interact with GitHub directly await session.send_and_wait({ "prompt": "List the last 10 Issues from kubernetes/kubernetes repository" })
2. 自定义 Agent:构建专业化角色
为不同场景创建定制化的 AI 角色:
session = await client.create_session({ "custom_agents": [{ "name": "code-reviewer", "display_name": "Code Review Expert", "description":
"Focus on code quality, security, and performance optimization", "prompt": """You are a senior code review expert. Review criteria: 1. Security vulnerabilities (SQL injection, XSS, etc.) 2. Performance bottlenecks (N+1 queries, memory leaks) 3. Maintainability (naming, comments, test coverage) Always point out specific issues and provide improvement suggestions.""" }] })
Agent 可以携带长期记忆、专业词汇表、特定工具集,成为你团队的“虚拟专家”。
3. 多客户端协作与调试
开发阶段,可以手动启动 CLI 服务器,多个 SDK 客户端连接到同一服务器:
# Terminal 1: Start CLI server copilot --headless --log-level debug --port 9999 # Terminal 2: Python client # Terminal 3: Node.js client # Both connect to the same server
client = CopilotClient({ 'cli_url': 'http://localhost:9999', }) await client.start() # Connects directly, doesn't start new process