跳转到主要内容

Documentation Index

Fetch the complete documentation index at: https://simplellmfunc.cn/llms.txt

Use this file to discover all available pages before exploring further.

接口 API 参考

LLM_Interface (ABC)

from SimpleLLMFunc.interface import LLM_Interface
所有模型接口的抽象基类(Abstract Base Class)。

构造函数

LLM_Interface(
    api_key_pool: APIKeyPool,
    model_name: str,
    base_url: Optional[str] = None,
    context_window: int = 200_000,
)

抽象方法

方法签名描述
chatasync def chat(*, trace_id, stream=False, messages, timeout=None, **kwargs)非流式调用
chat_streamasync def chat_stream(*, trace_id, stream=True, messages, timeout=None, **kwargs) -> AsyncGenerator流式调用

属性

属性类型描述
model_namestr模型标识符
base_urlstr | NoneAPI 端点
context_windowint上下文窗口大小(以 token 为单位)
api_key_poolAPIKeyPoolAPI 密钥轮换池

OpenAICompatible

from SimpleLLMFunc import OpenAICompatible
适配 OpenAI Chat Completions API 及其兼容端点。

构造函数

OpenAICompatible(
    api_key_pool: APIKeyPool,
    model_name: str,
    base_url: str,
    max_retries: int = 5,
    retry_delay: float = 1.0,
    rate_limit_capacity: int = 10,
    rate_limit_refill_rate: float = 1.0,
    context_window: int = 200_000,
)

类方法

方法返回值描述
load_from_json_file(path)Dict[str, Dict[str, OpenAICompatible]]从 provider.json 加载所有模型

实例方法

方法返回值描述
get_rate_limit_status()Dict[str, Any]当前速率限制器状态
reset_rate_limit()None重置令牌桶

示例

from SimpleLLMFunc import OpenAICompatible, APIKeyPool

# From file
models = OpenAICompatible.load_from_json_file("provider.json")
llm = models["openrouter"]["openai/gpt-4o"]

# Direct
llm = OpenAICompatible(
    api_key_pool=APIKeyPool(api_keys=["sk-..."], provider_id="openai"),
    model_name="gpt-4o",
    base_url="https://api.openai.com/v1",
    rate_limit_capacity=20,
    rate_limit_refill_rate=5.0,
)

OpenAIResponsesCompatible

from SimpleLLMFunc import OpenAIResponsesCompatible
适配 OpenAI Responses API。构造函数和加载模式与 OpenAICompatible 相同。

与 OpenAICompatible 的区别

方面OpenAICompatibleOpenAIResponsesCompatible
系统提示词messages[0].role="system"instructions 字段
流式格式Chat Completion 数据块Responses 流事件
推理支持不适用reasoning={...} kwargs
传输协议Chat Completions APIResponses API

示例

from SimpleLLMFunc import OpenAIResponsesCompatible, APIKeyPool

llm = OpenAIResponsesCompatible(
    api_key_pool=APIKeyPool(api_keys=["sk-..."], provider_id="openai"),
    model_name="gpt-4o",
    base_url="https://api.openai.com/v1",
)

@llm_chat(llm_interface=llm, reasoning_effort="high")
async def reasoning_agent(message: str, history: list | None = None):
    """Reason carefully about the question."""
    pass

APIKeyPool

from SimpleLLMFunc import APIKeyPool
轮询式密钥轮换(round-robin key rotation),用于负载分发。

构造函数

APIKeyPool(
    api_keys: List[str],
    provider_id: str,
)

示例

pool = APIKeyPool(
    api_keys=["sk-key-1", "sk-key-2", "sk-key-3"],
    provider_id="openrouter-gpt4o",
)
# Keys are rotated on each call automatically

TokenBucket

from SimpleLLMFunc import TokenBucket
令牌桶速率限制器(Token Bucket Rate Limiter)。

构造函数

TokenBucket(
    capacity: int,
    refill_rate: float,
)
参数描述
capacity桶中最大令牌数
refill_rate每秒补充的令牌数
OpenAICompatible 内部使用。暴露给外部以便自定义实现。