prompture 0.0.40.dev1__py3-none-any.whl → 0.0.42__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- prompture/_version.py +2 -2
- prompture/agent.py +11 -11
- prompture/async_agent.py +11 -11
- prompture/async_groups.py +63 -0
- prompture/cost_mixin.py +25 -0
- prompture/drivers/__init__.py +39 -0
- prompture/drivers/async_azure_driver.py +3 -2
- prompture/drivers/async_modelscope_driver.py +286 -0
- prompture/drivers/async_moonshot_driver.py +312 -0
- prompture/drivers/async_openai_driver.py +3 -2
- prompture/drivers/async_openrouter_driver.py +192 -3
- prompture/drivers/async_registry.py +30 -0
- prompture/drivers/async_zai_driver.py +303 -0
- prompture/drivers/azure_driver.py +3 -2
- prompture/drivers/modelscope_driver.py +303 -0
- prompture/drivers/moonshot_driver.py +342 -0
- prompture/drivers/openai_driver.py +3 -2
- prompture/drivers/openrouter_driver.py +244 -40
- prompture/drivers/zai_driver.py +318 -0
- prompture/groups.py +42 -0
- prompture/model_rates.py +2 -0
- prompture/settings.py +16 -1
- {prompture-0.0.40.dev1.dist-info → prompture-0.0.42.dist-info}/METADATA +1 -1
- {prompture-0.0.40.dev1.dist-info → prompture-0.0.42.dist-info}/RECORD +28 -22
- {prompture-0.0.40.dev1.dist-info → prompture-0.0.42.dist-info}/WHEEL +0 -0
- {prompture-0.0.40.dev1.dist-info → prompture-0.0.42.dist-info}/entry_points.txt +0 -0
- {prompture-0.0.40.dev1.dist-info → prompture-0.0.42.dist-info}/licenses/LICENSE +0 -0
- {prompture-0.0.40.dev1.dist-info → prompture-0.0.42.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
"""Moonshot AI (Kimi) driver implementation.
|
|
2
|
+
Requires the `requests` package. Uses MOONSHOT_API_KEY env var.
|
|
3
|
+
|
|
4
|
+
The Moonshot API is fully OpenAI-compatible (/v1/chat/completions).
|
|
5
|
+
All pricing comes from models.dev (provider: "moonshotai") — no hardcoded pricing.
|
|
6
|
+
|
|
7
|
+
Moonshot-specific constraints:
|
|
8
|
+
- Temperature clamped to [0, 1] (OpenAI allows [0, 2])
|
|
9
|
+
- tool_choice: "required" not supported — only "auto" or "none"
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
import json
|
|
13
|
+
import os
|
|
14
|
+
from collections.abc import Iterator
|
|
15
|
+
from typing import Any
|
|
16
|
+
|
|
17
|
+
import requests
|
|
18
|
+
|
|
19
|
+
from ..cost_mixin import CostMixin, prepare_strict_schema
|
|
20
|
+
from ..driver import Driver
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class MoonshotDriver(CostMixin, Driver):
|
|
24
|
+
supports_json_mode = True
|
|
25
|
+
supports_json_schema = True
|
|
26
|
+
supports_tool_use = True
|
|
27
|
+
supports_streaming = True
|
|
28
|
+
supports_vision = True
|
|
29
|
+
|
|
30
|
+
# All pricing resolved live from models.dev (provider: "moonshotai")
|
|
31
|
+
MODEL_PRICING: dict[str, dict[str, Any]] = {}
|
|
32
|
+
|
|
33
|
+
def __init__(
|
|
34
|
+
self,
|
|
35
|
+
api_key: str | None = None,
|
|
36
|
+
model: str = "kimi-k2-0905-preview",
|
|
37
|
+
endpoint: str = "https://api.moonshot.ai/v1",
|
|
38
|
+
):
|
|
39
|
+
"""Initialize Moonshot driver.
|
|
40
|
+
|
|
41
|
+
Args:
|
|
42
|
+
api_key: Moonshot API key. If not provided, will look for MOONSHOT_API_KEY env var.
|
|
43
|
+
model: Model to use. Defaults to kimi-k2-0905-preview.
|
|
44
|
+
endpoint: API base URL. Defaults to https://api.moonshot.ai/v1.
|
|
45
|
+
Use https://api.moonshot.cn/v1 for the China endpoint.
|
|
46
|
+
"""
|
|
47
|
+
self.api_key = api_key or os.getenv("MOONSHOT_API_KEY")
|
|
48
|
+
if not self.api_key:
|
|
49
|
+
raise ValueError("Moonshot API key not found. Set MOONSHOT_API_KEY env var.")
|
|
50
|
+
|
|
51
|
+
self.model = model
|
|
52
|
+
self.base_url = endpoint.rstrip("/")
|
|
53
|
+
|
|
54
|
+
self.headers = {
|
|
55
|
+
"Authorization": f"Bearer {self.api_key}",
|
|
56
|
+
"Content-Type": "application/json",
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
supports_messages = True
|
|
60
|
+
|
|
61
|
+
@staticmethod
|
|
62
|
+
def _clamp_temperature(opts: dict[str, Any]) -> dict[str, Any]:
|
|
63
|
+
"""Clamp temperature to Moonshot's supported range [0, 1]."""
|
|
64
|
+
if "temperature" in opts:
|
|
65
|
+
opts["temperature"] = max(0.0, min(1.0, float(opts["temperature"])))
|
|
66
|
+
return opts
|
|
67
|
+
|
|
68
|
+
@staticmethod
|
|
69
|
+
def _sanitize_tool_choice(data: dict[str, Any]) -> dict[str, Any]:
|
|
70
|
+
"""Downgrade tool_choice='required' to 'auto' (unsupported by Moonshot)."""
|
|
71
|
+
if data.get("tool_choice") == "required":
|
|
72
|
+
data["tool_choice"] = "auto"
|
|
73
|
+
return data
|
|
74
|
+
|
|
75
|
+
def _prepare_messages(self, messages: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
|
76
|
+
from .vision_helpers import _prepare_openai_vision_messages
|
|
77
|
+
|
|
78
|
+
return _prepare_openai_vision_messages(messages)
|
|
79
|
+
|
|
80
|
+
def generate(self, prompt: str, options: dict[str, Any]) -> dict[str, Any]:
|
|
81
|
+
messages = [{"role": "user", "content": prompt}]
|
|
82
|
+
return self._do_generate(messages, options)
|
|
83
|
+
|
|
84
|
+
def generate_messages(self, messages: list[dict[str, str]], options: dict[str, Any]) -> dict[str, Any]:
|
|
85
|
+
return self._do_generate(self._prepare_messages(messages), options)
|
|
86
|
+
|
|
87
|
+
def _do_generate(self, messages: list[dict[str, str]], options: dict[str, Any]) -> dict[str, Any]:
|
|
88
|
+
if not self.api_key:
|
|
89
|
+
raise RuntimeError("Moonshot API key not found")
|
|
90
|
+
|
|
91
|
+
model = options.get("model", self.model)
|
|
92
|
+
|
|
93
|
+
model_config = self._get_model_config("moonshot", model)
|
|
94
|
+
tokens_param = model_config["tokens_param"]
|
|
95
|
+
supports_temperature = model_config["supports_temperature"]
|
|
96
|
+
|
|
97
|
+
self._validate_model_capabilities(
|
|
98
|
+
"moonshot",
|
|
99
|
+
model,
|
|
100
|
+
using_json_schema=bool(options.get("json_schema")),
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
opts = {"temperature": 1.0, "max_tokens": 512, **options}
|
|
104
|
+
opts = self._clamp_temperature(opts)
|
|
105
|
+
|
|
106
|
+
data: dict[str, Any] = {
|
|
107
|
+
"model": model,
|
|
108
|
+
"messages": messages,
|
|
109
|
+
}
|
|
110
|
+
data[tokens_param] = opts.get("max_tokens", 512)
|
|
111
|
+
|
|
112
|
+
if supports_temperature and "temperature" in opts:
|
|
113
|
+
data["temperature"] = opts["temperature"]
|
|
114
|
+
|
|
115
|
+
# Native JSON mode support
|
|
116
|
+
if options.get("json_mode"):
|
|
117
|
+
json_schema = options.get("json_schema")
|
|
118
|
+
if json_schema:
|
|
119
|
+
schema_copy = prepare_strict_schema(json_schema)
|
|
120
|
+
data["response_format"] = {
|
|
121
|
+
"type": "json_schema",
|
|
122
|
+
"json_schema": {
|
|
123
|
+
"name": "extraction",
|
|
124
|
+
"strict": True,
|
|
125
|
+
"schema": schema_copy,
|
|
126
|
+
},
|
|
127
|
+
}
|
|
128
|
+
else:
|
|
129
|
+
data["response_format"] = {"type": "json_object"}
|
|
130
|
+
|
|
131
|
+
try:
|
|
132
|
+
response = requests.post(
|
|
133
|
+
f"{self.base_url}/chat/completions",
|
|
134
|
+
headers=self.headers,
|
|
135
|
+
json=data,
|
|
136
|
+
timeout=120,
|
|
137
|
+
)
|
|
138
|
+
response.raise_for_status()
|
|
139
|
+
resp = response.json()
|
|
140
|
+
except requests.exceptions.HTTPError as e:
|
|
141
|
+
error_msg = f"Moonshot API request failed: {e!s}"
|
|
142
|
+
raise RuntimeError(error_msg) from e
|
|
143
|
+
except requests.exceptions.RequestException as e:
|
|
144
|
+
raise RuntimeError(f"Moonshot API request failed: {e!s}") from e
|
|
145
|
+
|
|
146
|
+
usage = resp.get("usage", {})
|
|
147
|
+
prompt_tokens = usage.get("prompt_tokens", 0)
|
|
148
|
+
completion_tokens = usage.get("completion_tokens", 0)
|
|
149
|
+
total_tokens = usage.get("total_tokens", 0)
|
|
150
|
+
|
|
151
|
+
total_cost = self._calculate_cost("moonshot", model, prompt_tokens, completion_tokens)
|
|
152
|
+
|
|
153
|
+
meta = {
|
|
154
|
+
"prompt_tokens": prompt_tokens,
|
|
155
|
+
"completion_tokens": completion_tokens,
|
|
156
|
+
"total_tokens": total_tokens,
|
|
157
|
+
"cost": round(total_cost, 6),
|
|
158
|
+
"raw_response": resp,
|
|
159
|
+
"model_name": model,
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
text = resp["choices"][0]["message"]["content"]
|
|
163
|
+
return {"text": text, "meta": meta}
|
|
164
|
+
|
|
165
|
+
# ------------------------------------------------------------------
|
|
166
|
+
# Tool use
|
|
167
|
+
# ------------------------------------------------------------------
|
|
168
|
+
|
|
169
|
+
def generate_messages_with_tools(
|
|
170
|
+
self,
|
|
171
|
+
messages: list[dict[str, Any]],
|
|
172
|
+
tools: list[dict[str, Any]],
|
|
173
|
+
options: dict[str, Any],
|
|
174
|
+
) -> dict[str, Any]:
|
|
175
|
+
"""Generate a response that may include tool calls."""
|
|
176
|
+
if not self.api_key:
|
|
177
|
+
raise RuntimeError("Moonshot API key not found")
|
|
178
|
+
|
|
179
|
+
model = options.get("model", self.model)
|
|
180
|
+
model_config = self._get_model_config("moonshot", model)
|
|
181
|
+
tokens_param = model_config["tokens_param"]
|
|
182
|
+
supports_temperature = model_config["supports_temperature"]
|
|
183
|
+
|
|
184
|
+
self._validate_model_capabilities("moonshot", model, using_tool_use=True)
|
|
185
|
+
|
|
186
|
+
opts = {"temperature": 1.0, "max_tokens": 512, **options}
|
|
187
|
+
opts = self._clamp_temperature(opts)
|
|
188
|
+
|
|
189
|
+
data: dict[str, Any] = {
|
|
190
|
+
"model": model,
|
|
191
|
+
"messages": messages,
|
|
192
|
+
"tools": tools,
|
|
193
|
+
}
|
|
194
|
+
data[tokens_param] = opts.get("max_tokens", 512)
|
|
195
|
+
|
|
196
|
+
if supports_temperature and "temperature" in opts:
|
|
197
|
+
data["temperature"] = opts["temperature"]
|
|
198
|
+
|
|
199
|
+
if "tool_choice" in options:
|
|
200
|
+
data["tool_choice"] = options["tool_choice"]
|
|
201
|
+
|
|
202
|
+
data = self._sanitize_tool_choice(data)
|
|
203
|
+
|
|
204
|
+
try:
|
|
205
|
+
response = requests.post(
|
|
206
|
+
f"{self.base_url}/chat/completions",
|
|
207
|
+
headers=self.headers,
|
|
208
|
+
json=data,
|
|
209
|
+
timeout=120,
|
|
210
|
+
)
|
|
211
|
+
response.raise_for_status()
|
|
212
|
+
resp = response.json()
|
|
213
|
+
except requests.exceptions.HTTPError as e:
|
|
214
|
+
error_msg = f"Moonshot API request failed: {e!s}"
|
|
215
|
+
raise RuntimeError(error_msg) from e
|
|
216
|
+
except requests.exceptions.RequestException as e:
|
|
217
|
+
raise RuntimeError(f"Moonshot API request failed: {e!s}") from e
|
|
218
|
+
|
|
219
|
+
usage = resp.get("usage", {})
|
|
220
|
+
prompt_tokens = usage.get("prompt_tokens", 0)
|
|
221
|
+
completion_tokens = usage.get("completion_tokens", 0)
|
|
222
|
+
total_tokens = usage.get("total_tokens", 0)
|
|
223
|
+
total_cost = self._calculate_cost("moonshot", model, prompt_tokens, completion_tokens)
|
|
224
|
+
|
|
225
|
+
meta = {
|
|
226
|
+
"prompt_tokens": prompt_tokens,
|
|
227
|
+
"completion_tokens": completion_tokens,
|
|
228
|
+
"total_tokens": total_tokens,
|
|
229
|
+
"cost": round(total_cost, 6),
|
|
230
|
+
"raw_response": resp,
|
|
231
|
+
"model_name": model,
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
choice = resp["choices"][0]
|
|
235
|
+
text = choice["message"].get("content") or ""
|
|
236
|
+
stop_reason = choice.get("finish_reason")
|
|
237
|
+
|
|
238
|
+
tool_calls_out: list[dict[str, Any]] = []
|
|
239
|
+
for tc in choice["message"].get("tool_calls", []):
|
|
240
|
+
try:
|
|
241
|
+
args = json.loads(tc["function"]["arguments"])
|
|
242
|
+
except (json.JSONDecodeError, TypeError):
|
|
243
|
+
args = {}
|
|
244
|
+
tool_calls_out.append(
|
|
245
|
+
{
|
|
246
|
+
"id": tc["id"],
|
|
247
|
+
"name": tc["function"]["name"],
|
|
248
|
+
"arguments": args,
|
|
249
|
+
}
|
|
250
|
+
)
|
|
251
|
+
|
|
252
|
+
return {
|
|
253
|
+
"text": text,
|
|
254
|
+
"meta": meta,
|
|
255
|
+
"tool_calls": tool_calls_out,
|
|
256
|
+
"stop_reason": stop_reason,
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
# ------------------------------------------------------------------
|
|
260
|
+
# Streaming
|
|
261
|
+
# ------------------------------------------------------------------
|
|
262
|
+
|
|
263
|
+
def generate_messages_stream(
|
|
264
|
+
self,
|
|
265
|
+
messages: list[dict[str, Any]],
|
|
266
|
+
options: dict[str, Any],
|
|
267
|
+
) -> Iterator[dict[str, Any]]:
|
|
268
|
+
"""Yield response chunks via Moonshot streaming API."""
|
|
269
|
+
if not self.api_key:
|
|
270
|
+
raise RuntimeError("Moonshot API key not found")
|
|
271
|
+
|
|
272
|
+
model = options.get("model", self.model)
|
|
273
|
+
model_config = self._get_model_config("moonshot", model)
|
|
274
|
+
tokens_param = model_config["tokens_param"]
|
|
275
|
+
supports_temperature = model_config["supports_temperature"]
|
|
276
|
+
|
|
277
|
+
opts = {"temperature": 1.0, "max_tokens": 512, **options}
|
|
278
|
+
opts = self._clamp_temperature(opts)
|
|
279
|
+
|
|
280
|
+
data: dict[str, Any] = {
|
|
281
|
+
"model": model,
|
|
282
|
+
"messages": messages,
|
|
283
|
+
"stream": True,
|
|
284
|
+
"stream_options": {"include_usage": True},
|
|
285
|
+
}
|
|
286
|
+
data[tokens_param] = opts.get("max_tokens", 512)
|
|
287
|
+
|
|
288
|
+
if supports_temperature and "temperature" in opts:
|
|
289
|
+
data["temperature"] = opts["temperature"]
|
|
290
|
+
|
|
291
|
+
response = requests.post(
|
|
292
|
+
f"{self.base_url}/chat/completions",
|
|
293
|
+
headers=self.headers,
|
|
294
|
+
json=data,
|
|
295
|
+
stream=True,
|
|
296
|
+
timeout=120,
|
|
297
|
+
)
|
|
298
|
+
response.raise_for_status()
|
|
299
|
+
|
|
300
|
+
full_text = ""
|
|
301
|
+
prompt_tokens = 0
|
|
302
|
+
completion_tokens = 0
|
|
303
|
+
|
|
304
|
+
for line in response.iter_lines(decode_unicode=True):
|
|
305
|
+
if not line or not line.startswith("data: "):
|
|
306
|
+
continue
|
|
307
|
+
payload = line[len("data: ") :]
|
|
308
|
+
if payload.strip() == "[DONE]":
|
|
309
|
+
break
|
|
310
|
+
try:
|
|
311
|
+
chunk = json.loads(payload)
|
|
312
|
+
except json.JSONDecodeError:
|
|
313
|
+
continue
|
|
314
|
+
|
|
315
|
+
usage = chunk.get("usage")
|
|
316
|
+
if usage:
|
|
317
|
+
prompt_tokens = usage.get("prompt_tokens", 0)
|
|
318
|
+
completion_tokens = usage.get("completion_tokens", 0)
|
|
319
|
+
|
|
320
|
+
choices = chunk.get("choices", [])
|
|
321
|
+
if choices:
|
|
322
|
+
delta = choices[0].get("delta", {})
|
|
323
|
+
content = delta.get("content", "")
|
|
324
|
+
if content:
|
|
325
|
+
full_text += content
|
|
326
|
+
yield {"type": "delta", "text": content}
|
|
327
|
+
|
|
328
|
+
total_tokens = prompt_tokens + completion_tokens
|
|
329
|
+
total_cost = self._calculate_cost("moonshot", model, prompt_tokens, completion_tokens)
|
|
330
|
+
|
|
331
|
+
yield {
|
|
332
|
+
"type": "done",
|
|
333
|
+
"text": full_text,
|
|
334
|
+
"meta": {
|
|
335
|
+
"prompt_tokens": prompt_tokens,
|
|
336
|
+
"completion_tokens": completion_tokens,
|
|
337
|
+
"total_tokens": total_tokens,
|
|
338
|
+
"cost": round(total_cost, 6),
|
|
339
|
+
"raw_response": {},
|
|
340
|
+
"model_name": model,
|
|
341
|
+
},
|
|
342
|
+
}
|
|
@@ -12,7 +12,7 @@ try:
|
|
|
12
12
|
except Exception:
|
|
13
13
|
OpenAI = None
|
|
14
14
|
|
|
15
|
-
from ..cost_mixin import CostMixin
|
|
15
|
+
from ..cost_mixin import CostMixin, prepare_strict_schema
|
|
16
16
|
from ..driver import Driver
|
|
17
17
|
|
|
18
18
|
|
|
@@ -125,12 +125,13 @@ class OpenAIDriver(CostMixin, Driver):
|
|
|
125
125
|
if options.get("json_mode"):
|
|
126
126
|
json_schema = options.get("json_schema")
|
|
127
127
|
if json_schema:
|
|
128
|
+
schema_copy = prepare_strict_schema(json_schema)
|
|
128
129
|
kwargs["response_format"] = {
|
|
129
130
|
"type": "json_schema",
|
|
130
131
|
"json_schema": {
|
|
131
132
|
"name": "extraction",
|
|
132
133
|
"strict": True,
|
|
133
|
-
"schema":
|
|
134
|
+
"schema": schema_copy,
|
|
134
135
|
},
|
|
135
136
|
}
|
|
136
137
|
else:
|