- 修改 template_manager.py、k8s_manager.py 中的端口映射 - 更新 jina_search_agent、azure_blob_agent 系列、a2a_litellm_agent 的代码和 Dockerfile 为 8000 - 添加端口修改脚本和测试脚本 Made-with: Cursor
146 lines
3.8 KiB
Python
146 lines
3.8 KiB
Python
"""
|
|
LiteLLM Agent 配置模块
|
|
|
|
支持用户传入密钥和模型名称,同时支持从环境变量获取
|
|
"""
|
|
import os
|
|
from dataclasses import dataclass, field
|
|
from typing import Optional
|
|
from dotenv import load_dotenv
|
|
|
|
# 加载环境变量
|
|
load_dotenv()
|
|
|
|
|
|
@dataclass
|
|
class LiteLLMConfig:
|
|
"""LiteLLM 配置"""
|
|
# 基础URL - 用户提供的LiteLLM服务地址
|
|
base_url: str = "https://litellm.graystone-fb459c5d.southeastasia.azurecontainerapps.io"
|
|
|
|
# 完整的chat completions端点
|
|
chat_endpoint: str = field(init=False)
|
|
|
|
# API密钥 - 优先使用传入的,否则从环境变量获取
|
|
api_key: Optional[str] = None
|
|
|
|
# 模型名称 - 优先使用传入的,否则从环境变量获取
|
|
model: Optional[str] = None
|
|
|
|
# 请求超时时间(秒)
|
|
timeout: int = 120
|
|
|
|
# 最大重试次数
|
|
max_retries: int = 3
|
|
|
|
# 温度参数
|
|
temperature: float = 0.7
|
|
|
|
# 最大token数
|
|
max_tokens: int = 4096
|
|
|
|
def __post_init__(self):
|
|
self.chat_endpoint = f"{self.base_url}/chat/completions"
|
|
|
|
# 从环境变量读取(如果未直接提供)
|
|
if self.api_key is None:
|
|
self.api_key = os.getenv("LITELLM_API_KEY")
|
|
if self.model is None:
|
|
self.model = os.getenv("MODEL_NAME") or os.getenv("LITELLM_MODEL", "gpt-4")
|
|
|
|
def validate(self) -> bool:
|
|
"""验证配置是否完整"""
|
|
if not self.api_key:
|
|
raise ValueError("API密钥未设置! 请设置 LITELLM_API_KEY 环境变量或直接传入 api_key")
|
|
if not self.model:
|
|
raise ValueError("模型名称未设置! 请设置 MODEL_NAME 环境变量或直接传入 model")
|
|
return True
|
|
|
|
|
|
@dataclass
|
|
class AgentConfig:
|
|
"""Agent 配置"""
|
|
# Agent名称
|
|
name: str = "xiaohei-agent"
|
|
|
|
# Agent描述
|
|
description: str = "一个基于LiteLLM的智能Agent,支持A2A协议"
|
|
|
|
# Agent版本
|
|
version: str = "1.0.0"
|
|
|
|
# 服务端口
|
|
port: int = 8000
|
|
|
|
# 服务主机
|
|
host: str = "0.0.0.0"
|
|
|
|
# 是否启用流式响应
|
|
enable_streaming: bool = True
|
|
|
|
# 系统提示词
|
|
system_prompt: str = """你是小黑Agent,一个智能助手。
|
|
你可以帮助用户完成各种任务,包括:
|
|
|
|
- 回答问题
|
|
|
|
- 代码编写和解释
|
|
|
|
- 文档分析
|
|
|
|
- 任务规划
|
|
|
|
请用中文回答用户的问题,保持友好和专业。"""
|
|
|
|
|
|
@dataclass
|
|
class A2AConfig:
|
|
"""A2A协议配置"""
|
|
# A2A协议版本
|
|
protocol_version: str = "1.0"
|
|
|
|
# Agent Card配置
|
|
agent_card: dict = field(default_factory=lambda: {
|
|
"name": "xiaohei-agent",
|
|
"description": "基于LiteLLM的智能Agent,支持A2A协议通信",
|
|
"version": "1.0.0",
|
|
"capabilities": {
|
|
"text": True,
|
|
"streaming": True,
|
|
"push_notifications": False
|
|
},
|
|
"skills": [
|
|
{
|
|
"id": "general-assistant",
|
|
"name": "通用助手",
|
|
"description": "回答问题、提供建议、协助任务"
|
|
},
|
|
{
|
|
"id": "code-helper",
|
|
"name": "代码助手",
|
|
"description": "编写、解释和调试代码"
|
|
}
|
|
]
|
|
})
|
|
|
|
|
|
def get_config(
|
|
api_key: Optional[str] = None,
|
|
model: Optional[str] = None
|
|
) -> tuple[LiteLLMConfig, AgentConfig, A2AConfig]:
|
|
"""
|
|
获取完整配置
|
|
|
|
Args:
|
|
api_key: LiteLLM API密钥(可选,优先使用,否则从环境变量获取)
|
|
model: 模型名称(可选,优先使用,否则从环境变量获取)
|
|
|
|
Returns:
|
|
(LiteLLMConfig, AgentConfig, A2AConfig) 配置元组
|
|
"""
|
|
litellm_config = LiteLLMConfig(api_key=api_key, model=model)
|
|
agent_config = AgentConfig()
|
|
a2a_config = A2AConfig()
|
|
|
|
return litellm_config, agent_config, a2a_config
|