第二版
This commit is contained in:
7
models/__init__.py
Normal file
7
models/__init__.py
Normal file
@@ -0,0 +1,7 @@
|
||||
"""
|
||||
Models 包初始化
|
||||
"""
|
||||
|
||||
from .qwen_config import QwenConfig, get_qwen_config, get_llm
|
||||
|
||||
__all__ = ["QwenConfig", "get_qwen_config", "get_llm"]
|
||||
83
models/qwen_config.py
Normal file
83
models/qwen_config.py
Normal file
@@ -0,0 +1,83 @@
|
||||
"""
|
||||
Qwen3.5-flash 模型配置模块
|
||||
通过 DashScope OpenAI 兼容 API 调用通义千问模型
|
||||
"""
|
||||
|
||||
import os
|
||||
from typing import Optional
|
||||
from crewai import LLM
|
||||
from pydantic_settings import BaseSettings
|
||||
|
||||
|
||||
class QwenConfig(BaseSettings):
|
||||
"""Qwen 模型配置类"""
|
||||
|
||||
api_key: str = "sk-616332b2afa94699b4572d0fe6ac370a"
|
||||
base_url: str = "https://dashscope.aliyuncs.com/compatible-mode/v1"
|
||||
model_name: str = "qwen3.5-flash"
|
||||
temperature: float = 0.7
|
||||
max_tokens: int = 4096
|
||||
|
||||
class Config:
|
||||
env_file = ".env"
|
||||
extra = "ignore"
|
||||
|
||||
def get_llm(self) -> LLM:
|
||||
"""
|
||||
获取 CrewAI LLM 实例
|
||||
|
||||
Returns:
|
||||
LLM: CrewAI LLM 对象
|
||||
"""
|
||||
return LLM(
|
||||
model=self.model_name,
|
||||
base_url=self.base_url,
|
||||
api_key=self.api_key,
|
||||
temperature=self.temperature,
|
||||
max_tokens=self.max_tokens
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def load_from_env(cls) -> "QwenConfig":
|
||||
"""
|
||||
从环境变量加载配置
|
||||
|
||||
Returns:
|
||||
QwenConfig: 配置实例
|
||||
"""
|
||||
config = cls()
|
||||
if not config.api_key:
|
||||
raise ValueError(
|
||||
"DASHSCOPE_API_KEY 未设置,请在 .env 文件中配置或在 https://dashscope.console.aliyun.com/ 获取 API Key"
|
||||
)
|
||||
return config
|
||||
|
||||
|
||||
# 全局配置实例(延迟初始化)
|
||||
_qwen_config: Optional[QwenConfig] = None
|
||||
|
||||
|
||||
def get_qwen_config() -> QwenConfig:
|
||||
"""
|
||||
获取全局 Qwen 配置实例
|
||||
|
||||
Returns:
|
||||
QwenConfig: 配置实例
|
||||
|
||||
Raises:
|
||||
ValueError: 如果 API Key 未设置
|
||||
"""
|
||||
global _qwen_config
|
||||
if _qwen_config is None:
|
||||
_qwen_config = QwenConfig.load_from_env()
|
||||
return _qwen_config
|
||||
|
||||
|
||||
def get_llm() -> LLM:
|
||||
"""
|
||||
获取全局 LLM 实例
|
||||
|
||||
Returns:
|
||||
LLM: CrewAI LLM 对象
|
||||
"""
|
||||
return get_qwen_config().get_llm()
|
||||
Reference in New Issue
Block a user