工具函数 API¶
llm_rosetta.auto_detect ¶
LLM Provider Auto-Detection
自动检测 LLM provider 请求体格式的工具函数 Utility functions for auto-detecting LLM provider request body formats
convert ¶
convert(source_body: dict[str, Any], target_provider: ProviderType | str, source_provider: ProviderType | str | None = None, *, model: str | None = None, force_conversion: bool = False) -> dict[str, Any]
Auto-detect source provider and convert to target provider format.
This is a convenience function that auto-detects the source format and performs conversion through the IR (Intermediate Representation).
When source_provider or target_provider is a registered shim name
(e.g. "deepseek"), the shim's transforms are applied around the
base converter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_body
|
dict[str, Any]
|
Source provider request body. |
required |
target_provider
|
ProviderType | str
|
Target provider type or registered shim name. |
required |
source_provider
|
ProviderType | str | None
|
Optional source provider type or shim name. Auto-detected from source_body when not provided. |
None
|
model
|
str | None
|
Optional model name (currently unused, reserved for future use). |
None
|
force_conversion
|
bool
|
When |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Target provider format request body. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If source provider cannot be detected or conversion fails. |
Examples:
>>> openai_body = {"messages": [{"role": "user", "content": "Hello"}]}
>>> google_body = convert(openai_body, "google")
>>> anthropic_body = {"messages": [...]}
>>> openai_body = convert(anthropic_body, "openai_chat", source_provider="anthropic")
>>> # With shim transforms
>>> body = convert(req, "anthropic", source_provider="deepseek", model="deepseek-r1")
>>> # Force normalisation even for same-provider passthrough
>>> body = {"messages": [...], "max_tokens": 256}
>>> normalised = convert(body, "openai_chat", force_conversion=True)
Source code in src/llm_rosetta/auto_detect.py
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 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 | |
detect_provider ¶
Auto-detect provider type from request body structure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
body
|
dict[str, Any]
|
Provider request body dict. |
required |
Returns:
| Type | Description |
|---|---|
ProviderType | None
|
Detected provider type, or |
Examples:
>>> detect_provider({"messages": [{"role": "user", "content": "Hello"}]})
'openai_chat'
>>> detect_provider({"input": [{"type": "message", "role": "user"}]})
'openai_responses'
>>> detect_provider({"messages": [{"role": "user", "content": [{"type": "text"}]}]})
'anthropic'
>>> detect_provider({"contents": [{"role": "user", "parts": [{"text": "Hi"}]}]})
'google'
Source code in src/llm_rosetta/auto_detect.py
get_converter_for_provider ¶
Get the corresponding converter for a provider type or shim name.
Accepts both base converter types (e.g. "openai_chat") and
registered shim names (e.g. "deepseek"). Shim names are
resolved to their base converter type via the shim registry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider
|
str
|
Provider type string or registered shim name. |
required |
Returns:
| Type | Description |
|---|---|
|
Corresponding converter instance. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the provider is not a known type or shim name. |