jianmu.model¶
适用对象:模型接入开发者 / Provider 维护者 / 高阶使用者
是否必读:按需
相关模块:jianmu.message, jianmu.node, jianmu.swarm
1. 模块职责¶
jianmu.model 是 Jianmu 的模型系统,负责把“调用大模型”抽象成框架内部可复用、可替换、可观测的一层能力。
它主要覆盖以下职责:
- model provider 抽象
- 统一调用门面
- 流式与非流式返回统一
- 消息编码与解码协议
- provider 注册、解析、重试与降级
- token usage 与流式增量遥测发射
它不是具体业务节点,而是 LLM 调用能力的抽象层。
2. 适合查什么¶
- 基础类型:
ModelConfig、MessageChunk - provider 协议:
ModelProviderProtocol - 消息编码:
MessageEncoderProtocol - 主入口:
ModelClient - 注册机制:
ModelClient.register_provider()、ModelClient.resolve() - 内置 provider:
OpenAIProvider、LiteLLMProvider
如果你是第一次进入这个模块,建议按这个顺序看:
ModelConfigModelProviderProtocolModelClientinvoke_model()OpenAIProvider/LiteLLMProviderOpenAIChatEncoder/LiteLLMChatEncoder
3. 使用建议¶
- 普通应用通常直接使用
ModelClient.resolve()即可,不必先手动实例化所有 provider - 需要接入新模型后端时,再实现 provider 协议并通过
ModelClient.register_provider()注册 - 想理解一次统一调用该传什么参数时,优先看
ModelConfig - 想理解为什么流式 tool call 能恢复成统一消息对象,继续看
invoke_model()和stream.py
4. 注意事项¶
OpenAIProvider和LiteLLMProvider在这个模块中是 lazy export- 实际可用性依赖环境变量、端点配置和对应依赖
- 这里的编码协议和
jianmu.message的消息领域模型是分层设计,不应混用职责 ModelClient是入口,但它不等于整个模型系统;真正的调用链还包括invoke.py、stream.py、codecs.py和providers/
5. 最小示例¶
from jianmu.model import ModelClient, ModelConfig
client = ModelClient.resolve()
config = ModelConfig(model="gpt-4.1-mini", temperature=0.2)
6. 常见入口¶
- 想拿默认 provider:看
ModelClient.resolve() - 想注册自定义 provider:看
ModelClient.register_provider() - 想理解统一调用参数:看
ModelConfig
7. API 参考¶
model
¶
LLM provider interfaces, model client facade, and registry helpers for Jianmu.
MessageChunk
dataclass
¶
MessageChunk(
text: str = "",
tool_calls: Optional[List[Dict[str, Any]]] = None,
metadata: Optional[Dict[str, Any]] = None,
raw: Optional[Any] = None,
)
Single streamed chunk emitted by a model provider.
属性:
| 名称 | 类型 | 描述 |
|---|---|---|
text |
str
|
Incremental text content emitted in this chunk. |
tool_calls |
Optional[List[Dict[str, Any]]]
|
Optional tool-call deltas emitted by the provider. |
metadata |
Optional[Dict[str, Any]]
|
Optional provider-specific metadata for the chunk. |
raw |
Optional[Any]
|
Optional raw provider payload preserved for advanced consumers. |
ModelConfig
dataclass
¶
ModelConfig(
model: str,
temperature: float = 0.7,
top_p: float = 0.95,
top_k: int = 40,
max_tokens: Optional[int] = None,
timeout: float = 120.0,
tools: Optional[List[Dict[str, Any]]] = None,
tool_choice: Optional[Any] = None,
strict_tools: bool = False,
response_format: Optional[Dict[str, Any]] = None,
max_retries: Optional[int] = None,
fallback_model: Optional[str] = None,
disable_fallback: bool = False,
extra: Dict[str, Any] = dict(),
)
Configuration for one provider model call.
ModelConfig is the transport-neutral call configuration handed to model
providers. It keeps common generation settings, tool-calling settings, and
provider-specific extensions in one object.
属性:
| 名称 | 类型 | 描述 |
|---|---|---|
model |
str
|
Provider-specific model name or identifier. |
temperature |
float
|
Sampling temperature for generation. |
top_p |
float
|
Nucleus-sampling cutoff. |
top_k |
int
|
Top-k sampling cutoff when supported. |
max_tokens |
Optional[int]
|
Optional maximum generated-token limit. |
timeout |
float
|
Request timeout in seconds. |
tools |
Optional[List[Dict[str, Any]]]
|
Optional tool schema list exposed to the model. |
tool_choice |
Optional[Any]
|
Optional provider-specific tool selection directive. |
strict_tools |
bool
|
Whether tool arguments must strictly match the schema. |
response_format |
Optional[Dict[str, Any]]
|
Optional structured-output configuration. |
extra |
Dict[str, Any]
|
Provider-specific extra request options. |
with_tools
¶
Return a copy with tool-calling settings replaced.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
tools
|
List[Dict[str, Any]]
|
The list of tool schemas to use. |
必需 |
strict
|
bool
|
Whether to enforce strict schema adherence. Defaults to False. |
False
|
返回:
| 类型 | 描述 |
|---|---|
'ModelConfig'
|
A new |
源代码位于: jianmu/model/types.py
with_extra
¶
Return a copy with merged provider-specific extras.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
kwargs
|
Any
|
Key-value pairs to merge into the extra configuration dictionary. |
{}
|
返回:
| 类型 | 描述 |
|---|---|
'ModelConfig'
|
A new |
源代码位于: jianmu/model/types.py
ModelProviderProtocol
¶
Bases: Protocol
Protocol for model transport clients used by Jianmu.
generate
async
¶
Generate a single assistant message.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
messages
|
List[Message]
|
Ordered conversation history for the current request. |
必需 |
config
|
ModelConfig
|
Model configuration, including tool and response-format options. |
必需 |
返回:
| 类型 | 描述 |
|---|---|
Message
|
A decoded assistant message in Jianmu's message format. |
源代码位于: jianmu/model/base.py
stream
async
¶
Stream assistant output as incremental message chunks.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
messages
|
List[Message]
|
Ordered conversation history for the current request. |
必需 |
config
|
ModelConfig
|
Model configuration, including tool and response-format options. |
必需 |
产生:
| 类型 | 描述 |
|---|---|
AsyncIterator[MessageChunk]
|
Incremental output chunks decoded into Jianmu's streaming format. |
返回:
| 类型 | 描述 |
|---|---|
AsyncIterator[MessageChunk]
|
An async iterator of message chunks. |
源代码位于: jianmu/model/base.py
ModelClient
¶
Invocation facade bound to one resolved model provider.
ModelClient is the object to use when callers want Jianmu's normalized
invocation surface via :meth:invoke.
Resolution helpers are intentionally split by return type:
- :meth:
resolve_providerreturns the raw provider instance - :meth:
resolvereturns aModelClientwrapping that provider
属性:
| 名称 | 类型 | 描述 |
|---|---|---|
provider |
Bound provider implementation used to service invocations. |
Bind the client to a concrete provider instance.
源代码位于: jianmu/model/client.py
resolve
classmethod
¶
resolve(
name: str | None = None,
preference: list[str] | None = None,
env_override: bool = False,
**kwargs: Any,
) -> "ModelClient"
Resolve a provider and wrap it in a ModelClient.
Use this when the caller wants to invoke the model through
:meth:invoke. Callers that need the provider object itself should use
:meth:resolve_provider directly.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
name
|
str | None
|
Name used by the operation. |
None
|
preference
|
list[str] | None
|
The |
None
|
env_override
|
bool
|
The |
False
|
**kwargs
|
Any
|
Additional keyword arguments. |
{}
|
返回:
| 类型 | 描述 |
|---|---|
'ModelClient'
|
The resulting |
源代码位于: jianmu/model/client.py
resolve_provider
classmethod
¶
resolve_provider(
name: str | None = None,
preference: list[str] | None = None,
env_override: bool = False,
**kwargs: Any,
) -> ModelProviderProtocol
Resolve and instantiate the first usable provider.
This returns the raw provider instance rather than a ModelClient.
Use it when framework code or examples need a provider object to inject
into nodes, runtimes, or contexts.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
name
|
str | None
|
Explicit provider name. When omitted, providers are selected
from |
None
|
preference
|
list[str] | None
|
Provider resolution order. |
None
|
env_override
|
bool
|
Whether project |
False
|
**kwargs
|
Any
|
Extra provider-specific constructor arguments. |
{}
|
返回:
| 类型 | 描述 |
|---|---|
ModelProviderProtocol
|
The resulting |
引发:
| 类型 | 描述 |
|---|---|
RuntimeError
|
If the operation cannot be completed at runtime. |
源代码位于: jianmu/model/client.py
invoke
async
¶
invoke(
messages: list[Message],
config: ModelConfig,
*,
stream: bool = False,
on_text_update: Callable[[str], None] | None = None,
trace_node: str | None = None,
) -> tuple[Message, str]
Invoke the bound provider using Jianmu's normalized call path.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
messages
|
list[Message]
|
Messages to process. |
必需 |
config
|
ModelConfig
|
Configuration object for the operation. |
必需 |
stream
|
bool
|
The |
False
|
on_text_update
|
Callable[[str], None] | None
|
The |
None
|
trace_node
|
str | None
|
The |
None
|
返回:
| 类型 | 描述 |
|---|---|
tuple[Message, str]
|
The resulting tuple value. |
引发:
| 类型 | 描述 |
|---|---|
RuntimeError
|
If the operation cannot be completed at runtime. |
last_exc
|
If the operation fails. |
源代码位于: jianmu/model/client.py
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | |
register_provider
staticmethod
¶
register_provider(
name: str,
provider: type[ModelProviderProtocol]
| Callable[..., ModelProviderProtocol],
) -> None
Register a custom provider factory in the shared registry.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
name
|
str
|
Name used by the operation. |
必需 |
provider
|
type[ModelProviderProtocol] | Callable[..., ModelProviderProtocol]
|
The |
必需 |
源代码位于: jianmu/model/client.py
unregister_provider
staticmethod
¶
Remove a custom provider factory from the shared registry.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
name
|
str
|
Name used by the operation. |
必需 |
返回:
| 类型 | 描述 |
|---|---|
bool
|
True if the operation succeeds; otherwise False. |
源代码位于: jianmu/model/client.py
list_providers
staticmethod
¶
List builtin and custom provider names.
返回:
| 类型 | 描述 |
|---|---|
list[str]
|
The resulting list of values. |
源代码位于: jianmu/model/client.py
MessageEncoderProtocol
¶
Bases: Protocol
Encode jianmu Message objects into provider-specific payloads.
OpenAIChatEncoder
¶
Bases: MessageEncoderProtocol
Encoder for OpenAI-compatible chat providers.
encode_messages
¶
Encode Jianmu messages into OpenAI-compatible chat payloads.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
messages
|
Sequence[Message]
|
A sequence of jianmu Message objects. |
必需 |
返回:
| 类型 | 描述 |
|---|---|
List[Dict[str, Any]]
|
A list of dictionary payloads formatted for OpenAI API request. |
源代码位于: jianmu/model/codecs.py
LiteLLMChatEncoder
¶
Bases: OpenAIChatEncoder
Encoder for LiteLLM completion payloads.
Keep parity with OpenAIChatEncoder so switching providers does not change Jianmu's message normalization semantics.
ProviderErrorInfo
dataclass
¶
Normalized provider/model error semantics.
require_model_client
¶
Validate that a value is Jianmu's ModelClient facade type.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
model_client
|
Any
|
Candidate value expected to be a |
必需 |
source
|
str
|
Human-readable source label used in the validation error. |
必需 |
返回:
| 类型 | 描述 |
|---|---|
'ModelClient'
|
The validated |
引发:
| 类型 | 描述 |
|---|---|
TypeError
|
If |
源代码位于: jianmu/model/client.py
classify_provider_exception
¶
Classify one provider/model exception into a stable error code.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
exc
|
Exception
|
Provider- or model-facing exception raised during resolution or invocation. |
必需 |
返回:
| 类型 | 描述 |
|---|---|
ProviderErrorInfo | None
|
A normalized |
ProviderErrorInfo | None
|
provider/model failure category; otherwise |