mbxai 0.6.12__tar.gz → 0.6.14__tar.gz
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.
- {mbxai-0.6.12 → mbxai-0.6.14}/PKG-INFO +1 -1
- {mbxai-0.6.12 → mbxai-0.6.14}/pyproject.toml +1 -1
- {mbxai-0.6.12 → mbxai-0.6.14}/setup.py +1 -1
- {mbxai-0.6.12 → mbxai-0.6.14}/src/mbxai/openrouter/client.py +29 -34
- {mbxai-0.6.12 → mbxai-0.6.14}/src/mbxai/tools/client.py +94 -31
- {mbxai-0.6.12 → mbxai-0.6.14}/uv.lock +7 -7
- {mbxai-0.6.12 → mbxai-0.6.14}/.gitignore +0 -0
- {mbxai-0.6.12 → mbxai-0.6.14}/LICENSE +0 -0
- {mbxai-0.6.12 → mbxai-0.6.14}/README.md +0 -0
- {mbxai-0.6.12 → mbxai-0.6.14}/src/mbxai/__init__.py +0 -0
- {mbxai-0.6.12 → mbxai-0.6.14}/src/mbxai/core.py +0 -0
- {mbxai-0.6.12 → mbxai-0.6.14}/src/mbxai/mcp/__init__.py +0 -0
- {mbxai-0.6.12 → mbxai-0.6.14}/src/mbxai/mcp/client.py +0 -0
- {mbxai-0.6.12 → mbxai-0.6.14}/src/mbxai/mcp/example.py +0 -0
- {mbxai-0.6.12 → mbxai-0.6.14}/src/mbxai/mcp/server.py +0 -0
- {mbxai-0.6.12 → mbxai-0.6.14}/src/mbxai/openrouter/__init__.py +0 -0
- {mbxai-0.6.12 → mbxai-0.6.14}/src/mbxai/openrouter/config.py +0 -0
- {mbxai-0.6.12 → mbxai-0.6.14}/src/mbxai/openrouter/models.py +0 -0
- {mbxai-0.6.12 → mbxai-0.6.14}/src/mbxai/tools/__init__.py +0 -0
- {mbxai-0.6.12 → mbxai-0.6.14}/src/mbxai/tools/example.py +0 -0
- {mbxai-0.6.12 → mbxai-0.6.14}/src/mbxai/tools/types.py +0 -0
- {mbxai-0.6.12 → mbxai-0.6.14}/tests/test_core.py +0 -0
- {mbxai-0.6.12 → mbxai-0.6.14}/tests/test_mcp.py +0 -0
- {mbxai-0.6.12 → mbxai-0.6.14}/tests/test_openrouter.py +0 -0
- {mbxai-0.6.12 → mbxai-0.6.14}/tests/test_tools.py +0 -0
@@ -245,7 +245,6 @@ class OpenRouterClient:
|
|
245
245
|
response_format: type[BaseModel],
|
246
246
|
*,
|
247
247
|
model: Optional[Union[str, OpenRouterModel]] = None,
|
248
|
-
stream: bool = False,
|
249
248
|
**kwargs: Any,
|
250
249
|
) -> Any:
|
251
250
|
"""Create a chat completion and parse the response.
|
@@ -254,7 +253,6 @@ class OpenRouterClient:
|
|
254
253
|
messages: list of messages
|
255
254
|
response_format: Pydantic model to parse the response into
|
256
255
|
model: Optional model override
|
257
|
-
stream: Whether to stream the response
|
258
256
|
**kwargs: Additional parameters
|
259
257
|
|
260
258
|
Returns:
|
@@ -267,43 +265,40 @@ class OpenRouterClient:
|
|
267
265
|
ValueError: If response parsing fails
|
268
266
|
"""
|
269
267
|
try:
|
270
|
-
#
|
271
|
-
|
272
|
-
|
273
|
-
"role": "system",
|
274
|
-
"content": "You are a helpful assistant that responds in valid JSON format."
|
275
|
-
})
|
268
|
+
# Log the request details
|
269
|
+
logger.info(f"Sending chat completion request to OpenRouter with model: {model or self.model}")
|
270
|
+
logger.info(f"Message count: {len(messages)}")
|
276
271
|
|
277
|
-
#
|
278
|
-
|
279
|
-
|
280
|
-
format_desc = f"Respond with valid JSON matching this Pydantic model: {response_format.__name__}"
|
281
|
-
last_user_msg["content"] = f"{format_desc}\n\n{last_user_msg['content']}"
|
272
|
+
# Calculate total message size for logging
|
273
|
+
total_size = sum(len(str(msg)) for msg in messages)
|
274
|
+
logger.info(f"Total message size: {total_size} bytes")
|
282
275
|
|
283
|
-
response = self.
|
284
|
-
messages,
|
285
|
-
model=model,
|
286
|
-
|
287
|
-
|
288
|
-
**kwargs
|
276
|
+
response = self._client.beta.chat.completions.parse(
|
277
|
+
messages=messages,
|
278
|
+
model=model or self.model,
|
279
|
+
response_format=response_format,
|
280
|
+
**kwargs,
|
289
281
|
)
|
290
282
|
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
parsed = adapter.validate_json(content)
|
299
|
-
response.choices[0].message.parsed = parsed
|
300
|
-
return response
|
301
|
-
except Exception as e:
|
302
|
-
raise ValueError(f"Failed to parse response as {response_format.__name__}: {str(e)}")
|
303
|
-
except ValueError as e:
|
304
|
-
raise e
|
283
|
+
# Log response details
|
284
|
+
logger.info("Received response from OpenRouter")
|
285
|
+
if hasattr(response, 'choices') and response.choices:
|
286
|
+
logger.info(f"Response has {len(response.choices)} choices")
|
287
|
+
|
288
|
+
return response
|
289
|
+
|
305
290
|
except Exception as e:
|
306
|
-
|
291
|
+
logger.error(f"Error in chat completion: {str(e)}")
|
292
|
+
if hasattr(e, 'response') and e.response is not None:
|
293
|
+
logger.error(f"Response status: {e.response.status_code}")
|
294
|
+
logger.error(f"Response headers: {e.response.headers}")
|
295
|
+
try:
|
296
|
+
content = e.response.text
|
297
|
+
logger.error(f"Response content length: {len(content)} bytes")
|
298
|
+
logger.error(f"Response content preview: {content[:1000]}...")
|
299
|
+
except:
|
300
|
+
logger.error("Could not read response content")
|
301
|
+
self._handle_api_error("chat completion", e)
|
307
302
|
|
308
303
|
@with_retry()
|
309
304
|
def embeddings(
|
@@ -312,7 +312,6 @@ class ToolClient:
|
|
312
312
|
response_format: type[T],
|
313
313
|
*,
|
314
314
|
model: str | None = None,
|
315
|
-
stream: bool = False,
|
316
315
|
**kwargs: Any,
|
317
316
|
) -> Any:
|
318
317
|
"""Chat with the model and parse the response into a Pydantic model.
|
@@ -321,42 +320,106 @@ class ToolClient:
|
|
321
320
|
messages: The conversation messages
|
322
321
|
response_format: The Pydantic model to parse the response into
|
323
322
|
model: Optional model override
|
324
|
-
stream: Whether to stream the response
|
325
323
|
**kwargs: Additional parameters for the chat completion
|
326
324
|
|
327
325
|
Returns:
|
328
326
|
The parsed response from the model
|
329
327
|
"""
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
328
|
+
tools = [tool.to_openai_function() for tool in self._tools.values()]
|
329
|
+
|
330
|
+
if tools:
|
331
|
+
logger.info(f"Available tools: {[tool['function']['name'] for tool in tools]}")
|
332
|
+
kwargs["tools"] = tools
|
333
|
+
kwargs["tool_choice"] = "auto"
|
334
|
+
|
335
|
+
while True:
|
336
|
+
# Get the model's response
|
337
|
+
response = self._client.chat_completion_parse(
|
338
|
+
messages=messages,
|
339
|
+
response_format=response_format,
|
340
|
+
model=model,
|
341
|
+
**kwargs,
|
342
|
+
)
|
343
343
|
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
{
|
344
|
+
message = response.choices[0].message
|
345
|
+
# Add the assistant's message with tool calls
|
346
|
+
assistant_message = {
|
348
347
|
"role": "assistant",
|
349
|
-
"content":
|
348
|
+
"content": message.content or None,
|
349
|
+
"parsed": message.parsed or None,
|
350
350
|
}
|
351
|
-
|
351
|
+
if message.tool_calls:
|
352
|
+
assistant_message["tool_calls"] = [
|
353
|
+
{
|
354
|
+
"id": tool_call.id,
|
355
|
+
"type": "function",
|
356
|
+
"function": {
|
357
|
+
"name": tool_call.function.name,
|
358
|
+
"arguments": tool_call.function.arguments,
|
359
|
+
},
|
360
|
+
}
|
361
|
+
for tool_call in message.tool_calls
|
362
|
+
]
|
363
|
+
messages.append(assistant_message)
|
364
|
+
logger.info(f"Message count: {len(messages)}, Added assistant message with tool calls: {[tc.function.name for tc in message.tool_calls] if message.tool_calls else None}")
|
352
365
|
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
366
|
+
# If there are no tool calls, we're done
|
367
|
+
if not message.tool_calls:
|
368
|
+
return response
|
369
|
+
|
370
|
+
# Process all tool calls
|
371
|
+
tool_responses = []
|
372
|
+
for tool_call in message.tool_calls:
|
373
|
+
tool = self._tools.get(tool_call.function.name)
|
374
|
+
if not tool:
|
375
|
+
raise ValueError(f"Unknown tool: {tool_call.function.name}")
|
376
|
+
|
377
|
+
# Parse arguments if they're a string
|
378
|
+
arguments = tool_call.function.arguments
|
379
|
+
if isinstance(arguments, str):
|
380
|
+
try:
|
381
|
+
arguments = json.loads(arguments)
|
382
|
+
except json.JSONDecodeError as e:
|
383
|
+
logger.error(f"Failed to parse tool arguments: {e}")
|
384
|
+
raise ValueError(f"Invalid tool arguments format: {arguments}")
|
385
|
+
|
386
|
+
# Call the tool
|
387
|
+
logger.info(f"Calling tool: {tool.name} with args: {self._truncate_dict(arguments)}")
|
388
|
+
try:
|
389
|
+
if inspect.iscoroutinefunction(tool.function):
|
390
|
+
result = await asyncio.wait_for(tool.function(**arguments), timeout=300.0) # 5 minutes timeout
|
391
|
+
else:
|
392
|
+
result = tool.function(**arguments)
|
393
|
+
logger.info(f"Tool {tool.name} completed successfully")
|
394
|
+
except asyncio.TimeoutError:
|
395
|
+
logger.error(f"Tool {tool.name} timed out after 5 minutes")
|
396
|
+
result = {"error": "Tool execution timed out after 5 minutes"}
|
397
|
+
except Exception as e:
|
398
|
+
logger.error(f"Error calling tool {tool.name}: {str(e)}")
|
399
|
+
result = {"error": f"Tool execution failed: {str(e)}"}
|
400
|
+
|
401
|
+
# Convert result to JSON string if it's not already
|
402
|
+
if not isinstance(result, str):
|
403
|
+
result = json.dumps(result)
|
404
|
+
|
405
|
+
# Create the tool response
|
406
|
+
tool_response = {
|
407
|
+
"role": "tool",
|
408
|
+
"tool_call_id": tool_call.id,
|
409
|
+
"content": result,
|
410
|
+
}
|
411
|
+
tool_responses.append(tool_response)
|
412
|
+
logger.info(f"Created tool response for call ID {tool_call.id}")
|
413
|
+
|
414
|
+
# Add all tool responses to the messages
|
415
|
+
messages.extend(tool_responses)
|
416
|
+
logger.info(f"Message count: {len(messages)}, Added {len(tool_responses)} tool responses to messages")
|
417
|
+
|
418
|
+
# Validate the message sequence
|
419
|
+
self._validate_message_sequence(messages, validate_responses=True)
|
420
|
+
|
421
|
+
# Log the messages we're about to send
|
422
|
+
self._log_messages(messages, validate_responses=False)
|
423
|
+
|
424
|
+
# Continue the loop to get the next response
|
425
|
+
continue
|
@@ -292,11 +292,11 @@ wheels = [
|
|
292
292
|
|
293
293
|
[[package]]
|
294
294
|
name = "httpx-sse"
|
295
|
-
version = "0.6.
|
295
|
+
version = "0.6.14"
|
296
296
|
source = { registry = "https://pypi.org/simple" }
|
297
|
-
sdist = { url = "https://files.pythonhosted.org/packages/4c/60/8f4281fa9bbf3c8034fd54c0e7412e66edbab6bc74c4996bd616f8d0406e/httpx-sse-0.6.
|
297
|
+
sdist = { url = "https://files.pythonhosted.org/packages/4c/60/8f4281fa9bbf3c8034fd54c0e7412e66edbab6bc74c4996bd616f8d0406e/httpx-sse-0.6.14.tar.gz", hash = "sha256:1e81a3a3070ce322add1d3529ed42eb5f70817f45ed6ec915ab753f961139721", size = 12624 }
|
298
298
|
wheels = [
|
299
|
-
{ url = "https://files.pythonhosted.org/packages/e1/9b/a181f281f65d776426002f330c31849b86b31fc9d848db62e16f03ff739f/httpx_sse-0.6.
|
299
|
+
{ url = "https://files.pythonhosted.org/packages/e1/9b/a181f281f65d776426002f330c31849b86b31fc9d848db62e16f03ff739f/httpx_sse-0.6.14-py3-none-any.whl", hash = "sha256:f329af6eae57eaa2bdfd962b42524764af68075ea87370a2de920af5341e318f", size = 7819 },
|
300
300
|
]
|
301
301
|
|
302
302
|
[[package]]
|
@@ -446,7 +446,7 @@ wheels = [
|
|
446
446
|
|
447
447
|
[[package]]
|
448
448
|
name = "mbxai"
|
449
|
-
version = "0.6.
|
449
|
+
version = "0.6.14"
|
450
450
|
source = { editable = "." }
|
451
451
|
dependencies = [
|
452
452
|
{ name = "fastapi" },
|
@@ -980,14 +980,14 @@ wheels = [
|
|
980
980
|
|
981
981
|
[[package]]
|
982
982
|
name = "typing-inspection"
|
983
|
-
version = "0.6.
|
983
|
+
version = "0.6.14"
|
984
984
|
source = { registry = "https://pypi.org/simple" }
|
985
985
|
dependencies = [
|
986
986
|
{ name = "typing-extensions" },
|
987
987
|
]
|
988
|
-
sdist = { url = "https://files.pythonhosted.org/packages/82/5c/e6082df02e215b846b4b8c0b887a64d7d08ffaba30605502639d44c06b82/typing_inspection-0.6.
|
988
|
+
sdist = { url = "https://files.pythonhosted.org/packages/82/5c/e6082df02e215b846b4b8c0b887a64d7d08ffaba30605502639d44c06b82/typing_inspection-0.6.14.tar.gz", hash = "sha256:9765c87de36671694a67904bf2c96e395be9c6439bb6c87b5142569dcdd65122", size = 76222 }
|
989
989
|
wheels = [
|
990
|
-
{ url = "https://files.pythonhosted.org/packages/31/08/aa4fdfb71f7de5176385bd9e90852eaf6b5d622735020ad600f2bab54385/typing_inspection-0.6.
|
990
|
+
{ url = "https://files.pythonhosted.org/packages/31/08/aa4fdfb71f7de5176385bd9e90852eaf6b5d622735020ad600f2bab54385/typing_inspection-0.6.14-py3-none-any.whl", hash = "sha256:50e72559fcd2a6367a19f7a7e610e6afcb9fac940c650290eed893d61386832f", size = 14125 },
|
991
991
|
]
|
992
992
|
|
993
993
|
[[package]]
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|