mirascope 2.0.0a5__py3-none-any.whl → 2.0.1__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.
- mirascope/__init__.py +10 -1
- mirascope/_stubs.py +363 -0
- mirascope/api/__init__.py +8 -0
- mirascope/api/_generated/__init__.py +285 -2
- mirascope/api/_generated/annotations/__init__.py +33 -0
- mirascope/api/_generated/annotations/client.py +506 -0
- mirascope/api/_generated/annotations/raw_client.py +1414 -0
- mirascope/api/_generated/annotations/types/__init__.py +31 -0
- mirascope/api/_generated/annotations/types/annotations_create_request_label.py +5 -0
- mirascope/api/_generated/annotations/types/annotations_create_response.py +48 -0
- mirascope/api/_generated/annotations/types/annotations_create_response_label.py +5 -0
- mirascope/api/_generated/annotations/types/annotations_get_response.py +48 -0
- mirascope/api/_generated/annotations/types/annotations_get_response_label.py +5 -0
- mirascope/api/_generated/annotations/types/annotations_list_request_label.py +5 -0
- mirascope/api/_generated/annotations/types/annotations_list_response.py +21 -0
- mirascope/api/_generated/annotations/types/annotations_list_response_annotations_item.py +50 -0
- mirascope/api/_generated/annotations/types/annotations_list_response_annotations_item_label.py +5 -0
- mirascope/api/_generated/annotations/types/annotations_update_request_label.py +5 -0
- mirascope/api/_generated/annotations/types/annotations_update_response.py +48 -0
- mirascope/api/_generated/annotations/types/annotations_update_response_label.py +5 -0
- mirascope/api/_generated/api_keys/__init__.py +12 -2
- mirascope/api/_generated/api_keys/client.py +77 -0
- mirascope/api/_generated/api_keys/raw_client.py +422 -39
- mirascope/api/_generated/api_keys/types/__init__.py +7 -1
- mirascope/api/_generated/api_keys/types/api_keys_create_response.py +4 -12
- mirascope/api/_generated/api_keys/types/api_keys_get_response.py +4 -12
- mirascope/api/_generated/api_keys/types/api_keys_list_all_for_org_response_item.py +40 -0
- mirascope/api/_generated/api_keys/types/api_keys_list_response_item.py +4 -12
- mirascope/api/_generated/client.py +42 -0
- mirascope/api/_generated/core/client_wrapper.py +2 -14
- mirascope/api/_generated/core/datetime_utils.py +1 -3
- mirascope/api/_generated/core/file.py +2 -5
- mirascope/api/_generated/core/http_client.py +36 -112
- mirascope/api/_generated/core/jsonable_encoder.py +1 -3
- mirascope/api/_generated/core/pydantic_utilities.py +19 -74
- mirascope/api/_generated/core/query_encoder.py +1 -3
- mirascope/api/_generated/core/serialization.py +4 -10
- mirascope/api/_generated/docs/client.py +2 -6
- mirascope/api/_generated/docs/raw_client.py +51 -5
- mirascope/api/_generated/environment.py +3 -3
- mirascope/api/_generated/environments/__init__.py +6 -0
- mirascope/api/_generated/environments/client.py +117 -0
- mirascope/api/_generated/environments/raw_client.py +530 -51
- mirascope/api/_generated/environments/types/__init__.py +10 -0
- mirascope/api/_generated/environments/types/environments_create_response.py +1 -3
- mirascope/api/_generated/environments/types/environments_get_analytics_response.py +60 -0
- mirascope/api/_generated/environments/types/environments_get_analytics_response_top_functions_item.py +24 -0
- mirascope/api/_generated/environments/types/environments_get_analytics_response_top_models_item.py +22 -0
- mirascope/api/_generated/environments/types/environments_get_response.py +1 -3
- mirascope/api/_generated/environments/types/environments_list_response_item.py +1 -3
- mirascope/api/_generated/environments/types/environments_update_response.py +1 -3
- mirascope/api/_generated/errors/__init__.py +8 -0
- mirascope/api/_generated/errors/bad_request_error.py +1 -2
- mirascope/api/_generated/errors/conflict_error.py +1 -2
- mirascope/api/_generated/errors/forbidden_error.py +1 -5
- mirascope/api/_generated/errors/internal_server_error.py +1 -6
- mirascope/api/_generated/errors/not_found_error.py +1 -5
- mirascope/api/_generated/errors/payment_required_error.py +15 -0
- mirascope/api/_generated/errors/service_unavailable_error.py +14 -0
- mirascope/api/_generated/errors/too_many_requests_error.py +15 -0
- mirascope/api/_generated/errors/unauthorized_error.py +11 -0
- mirascope/api/_generated/functions/__init__.py +39 -0
- mirascope/api/_generated/functions/client.py +647 -0
- mirascope/api/_generated/functions/raw_client.py +1890 -0
- mirascope/api/_generated/functions/types/__init__.py +53 -0
- mirascope/api/_generated/functions/types/functions_create_request_dependencies_value.py +20 -0
- mirascope/api/_generated/functions/types/functions_create_response.py +37 -0
- mirascope/api/_generated/functions/types/functions_create_response_dependencies_value.py +20 -0
- mirascope/api/_generated/functions/types/functions_find_by_hash_response.py +39 -0
- mirascope/api/_generated/functions/types/functions_find_by_hash_response_dependencies_value.py +20 -0
- mirascope/api/_generated/functions/types/functions_get_by_env_response.py +53 -0
- mirascope/api/_generated/functions/types/functions_get_by_env_response_dependencies_value.py +22 -0
- mirascope/api/_generated/functions/types/functions_get_response.py +37 -0
- mirascope/api/_generated/functions/types/functions_get_response_dependencies_value.py +20 -0
- mirascope/api/_generated/functions/types/functions_list_by_env_response.py +25 -0
- mirascope/api/_generated/functions/types/functions_list_by_env_response_functions_item.py +56 -0
- mirascope/api/_generated/functions/types/functions_list_by_env_response_functions_item_dependencies_value.py +22 -0
- mirascope/api/_generated/functions/types/functions_list_response.py +21 -0
- mirascope/api/_generated/functions/types/functions_list_response_functions_item.py +41 -0
- mirascope/api/_generated/functions/types/functions_list_response_functions_item_dependencies_value.py +20 -0
- mirascope/api/_generated/health/client.py +2 -6
- mirascope/api/_generated/health/raw_client.py +51 -5
- mirascope/api/_generated/health/types/health_check_response.py +1 -3
- mirascope/api/_generated/organization_invitations/__init__.py +33 -0
- mirascope/api/_generated/organization_invitations/client.py +546 -0
- mirascope/api/_generated/organization_invitations/raw_client.py +1519 -0
- mirascope/api/_generated/organization_invitations/types/__init__.py +53 -0
- mirascope/api/_generated/organization_invitations/types/organization_invitations_accept_response.py +34 -0
- mirascope/api/_generated/organization_invitations/types/organization_invitations_accept_response_role.py +7 -0
- mirascope/api/_generated/organization_invitations/types/organization_invitations_create_request_role.py +7 -0
- mirascope/api/_generated/organization_invitations/types/organization_invitations_create_response.py +48 -0
- mirascope/api/_generated/organization_invitations/types/organization_invitations_create_response_role.py +7 -0
- mirascope/api/_generated/organization_invitations/types/organization_invitations_create_response_status.py +7 -0
- mirascope/api/_generated/organization_invitations/types/organization_invitations_get_response.py +48 -0
- mirascope/api/_generated/organization_invitations/types/organization_invitations_get_response_role.py +7 -0
- mirascope/api/_generated/organization_invitations/types/organization_invitations_get_response_status.py +7 -0
- mirascope/api/_generated/organization_invitations/types/organization_invitations_list_response_item.py +48 -0
- mirascope/api/_generated/organization_invitations/types/organization_invitations_list_response_item_role.py +7 -0
- mirascope/api/_generated/organization_invitations/types/organization_invitations_list_response_item_status.py +7 -0
- mirascope/api/_generated/organization_memberships/__init__.py +19 -0
- mirascope/api/_generated/organization_memberships/client.py +302 -0
- mirascope/api/_generated/organization_memberships/raw_client.py +736 -0
- mirascope/api/_generated/organization_memberships/types/__init__.py +27 -0
- mirascope/api/_generated/organization_memberships/types/organization_memberships_list_response_item.py +33 -0
- mirascope/api/_generated/organization_memberships/types/organization_memberships_list_response_item_role.py +7 -0
- mirascope/api/_generated/organization_memberships/types/organization_memberships_update_request_role.py +7 -0
- mirascope/api/_generated/organization_memberships/types/organization_memberships_update_response.py +31 -0
- mirascope/api/_generated/organization_memberships/types/organization_memberships_update_response_role.py +7 -0
- mirascope/api/_generated/organizations/__init__.py +26 -0
- mirascope/api/_generated/organizations/client.py +465 -0
- mirascope/api/_generated/organizations/raw_client.py +1799 -108
- mirascope/api/_generated/organizations/types/__init__.py +48 -0
- mirascope/api/_generated/organizations/types/organizations_create_payment_intent_response.py +24 -0
- mirascope/api/_generated/organizations/types/organizations_create_response.py +4 -3
- mirascope/api/_generated/organizations/types/organizations_create_response_role.py +1 -3
- mirascope/api/_generated/organizations/types/organizations_get_response.py +4 -3
- mirascope/api/_generated/organizations/types/organizations_get_response_role.py +1 -3
- mirascope/api/_generated/organizations/types/organizations_list_response_item.py +4 -3
- mirascope/api/_generated/organizations/types/organizations_list_response_item_role.py +1 -3
- mirascope/api/_generated/organizations/types/organizations_preview_subscription_change_request_target_plan.py +7 -0
- mirascope/api/_generated/organizations/types/organizations_preview_subscription_change_response.py +47 -0
- mirascope/api/_generated/organizations/types/organizations_preview_subscription_change_response_validation_errors_item.py +33 -0
- mirascope/api/_generated/organizations/types/organizations_preview_subscription_change_response_validation_errors_item_resource.py +7 -0
- mirascope/api/_generated/organizations/types/organizations_router_balance_response.py +24 -0
- mirascope/api/_generated/organizations/types/organizations_subscription_response.py +53 -0
- mirascope/api/_generated/organizations/types/organizations_subscription_response_current_plan.py +7 -0
- mirascope/api/_generated/organizations/types/organizations_subscription_response_payment_method.py +26 -0
- mirascope/api/_generated/organizations/types/organizations_subscription_response_scheduled_change.py +34 -0
- mirascope/api/_generated/organizations/types/organizations_subscription_response_scheduled_change_target_plan.py +7 -0
- mirascope/api/_generated/organizations/types/organizations_update_response.py +4 -3
- mirascope/api/_generated/organizations/types/organizations_update_response_role.py +1 -3
- mirascope/api/_generated/organizations/types/organizations_update_subscription_request_target_plan.py +7 -0
- mirascope/api/_generated/organizations/types/organizations_update_subscription_response.py +35 -0
- mirascope/api/_generated/project_memberships/__init__.py +25 -0
- mirascope/api/_generated/project_memberships/client.py +437 -0
- mirascope/api/_generated/project_memberships/raw_client.py +1039 -0
- mirascope/api/_generated/project_memberships/types/__init__.py +29 -0
- mirascope/api/_generated/project_memberships/types/project_memberships_create_request_role.py +7 -0
- mirascope/api/_generated/project_memberships/types/project_memberships_create_response.py +35 -0
- mirascope/api/_generated/project_memberships/types/project_memberships_create_response_role.py +7 -0
- mirascope/api/_generated/project_memberships/types/project_memberships_list_response_item.py +33 -0
- mirascope/api/_generated/project_memberships/types/project_memberships_list_response_item_role.py +7 -0
- mirascope/api/_generated/project_memberships/types/project_memberships_update_request_role.py +7 -0
- mirascope/api/_generated/project_memberships/types/project_memberships_update_response.py +35 -0
- mirascope/api/_generated/project_memberships/types/project_memberships_update_response_role.py +7 -0
- mirascope/api/_generated/projects/__init__.py +2 -12
- mirascope/api/_generated/projects/client.py +17 -71
- mirascope/api/_generated/projects/raw_client.py +295 -51
- mirascope/api/_generated/projects/types/__init__.py +1 -6
- mirascope/api/_generated/projects/types/projects_create_response.py +3 -9
- mirascope/api/_generated/projects/types/projects_get_response.py +3 -9
- mirascope/api/_generated/projects/types/projects_list_response_item.py +3 -9
- mirascope/api/_generated/projects/types/projects_update_response.py +3 -9
- mirascope/api/_generated/reference.md +3619 -182
- mirascope/api/_generated/tags/__init__.py +19 -0
- mirascope/api/_generated/tags/client.py +504 -0
- mirascope/api/_generated/tags/raw_client.py +1288 -0
- mirascope/api/_generated/tags/types/__init__.py +17 -0
- mirascope/api/_generated/tags/types/tags_create_response.py +41 -0
- mirascope/api/_generated/tags/types/tags_get_response.py +41 -0
- mirascope/api/_generated/tags/types/tags_list_response.py +23 -0
- mirascope/api/_generated/tags/types/tags_list_response_tags_item.py +41 -0
- mirascope/api/_generated/tags/types/tags_update_response.py +41 -0
- mirascope/api/_generated/token_cost/__init__.py +7 -0
- mirascope/api/_generated/token_cost/client.py +160 -0
- mirascope/api/_generated/token_cost/raw_client.py +264 -0
- mirascope/api/_generated/token_cost/types/__init__.py +8 -0
- mirascope/api/_generated/token_cost/types/token_cost_calculate_request_usage.py +54 -0
- mirascope/api/_generated/token_cost/types/token_cost_calculate_response.py +52 -0
- mirascope/api/_generated/traces/__init__.py +42 -0
- mirascope/api/_generated/traces/client.py +941 -0
- mirascope/api/_generated/traces/raw_client.py +2177 -23
- mirascope/api/_generated/traces/types/__init__.py +60 -0
- mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item.py +4 -11
- mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_resource.py +2 -6
- mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_resource_attributes_item.py +1 -3
- mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_resource_attributes_item_value.py +8 -24
- mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_resource_attributes_item_value_array_value.py +2 -6
- mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_resource_attributes_item_value_kvlist_value.py +3 -9
- mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_resource_attributes_item_value_kvlist_value_values_item.py +2 -6
- mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_scope_spans_item.py +3 -9
- mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_scope_spans_item_scope.py +4 -8
- mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_scope_spans_item_scope_attributes_item.py +2 -6
- mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_scope_spans_item_scope_attributes_item_value.py +8 -24
- mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_scope_spans_item_scope_attributes_item_value_array_value.py +2 -6
- mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_scope_spans_item_scope_attributes_item_value_kvlist_value.py +3 -9
- mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_scope_spans_item_scope_attributes_item_value_kvlist_value_values_item.py +1 -3
- mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_scope_spans_item_spans_item.py +6 -18
- mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_scope_spans_item_spans_item_attributes_item.py +3 -9
- mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_scope_spans_item_spans_item_attributes_item_value.py +8 -24
- mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_scope_spans_item_spans_item_attributes_item_value_array_value.py +2 -6
- mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_scope_spans_item_spans_item_attributes_item_value_kvlist_value.py +2 -6
- mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_scope_spans_item_spans_item_attributes_item_value_kvlist_value_values_item.py +1 -3
- mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_scope_spans_item_spans_item_status.py +2 -6
- mirascope/api/_generated/traces/types/traces_create_response.py +2 -5
- mirascope/api/_generated/traces/types/traces_create_response_partial_success.py +3 -9
- mirascope/api/_generated/traces/types/traces_get_analytics_summary_response.py +60 -0
- mirascope/api/_generated/traces/types/traces_get_analytics_summary_response_top_functions_item.py +24 -0
- mirascope/api/_generated/traces/types/traces_get_analytics_summary_response_top_models_item.py +22 -0
- mirascope/api/_generated/traces/types/traces_get_trace_detail_by_env_response.py +33 -0
- mirascope/api/_generated/traces/types/traces_get_trace_detail_by_env_response_spans_item.py +88 -0
- mirascope/api/_generated/traces/types/traces_get_trace_detail_response.py +33 -0
- mirascope/api/_generated/traces/types/traces_get_trace_detail_response_spans_item.py +88 -0
- mirascope/api/_generated/traces/types/traces_list_by_function_hash_response.py +25 -0
- mirascope/api/_generated/traces/types/traces_list_by_function_hash_response_traces_item.py +44 -0
- mirascope/api/_generated/traces/types/traces_search_by_env_request_attribute_filters_item.py +26 -0
- mirascope/api/_generated/traces/types/traces_search_by_env_request_attribute_filters_item_operator.py +7 -0
- mirascope/api/_generated/traces/types/traces_search_by_env_request_sort_by.py +7 -0
- mirascope/api/_generated/traces/types/traces_search_by_env_request_sort_order.py +7 -0
- mirascope/api/_generated/traces/types/traces_search_by_env_response.py +26 -0
- mirascope/api/_generated/traces/types/traces_search_by_env_response_spans_item.py +50 -0
- mirascope/api/_generated/traces/types/traces_search_request_attribute_filters_item.py +26 -0
- mirascope/api/_generated/traces/types/traces_search_request_attribute_filters_item_operator.py +7 -0
- mirascope/api/_generated/traces/types/traces_search_request_sort_by.py +7 -0
- mirascope/api/_generated/traces/types/traces_search_request_sort_order.py +5 -0
- mirascope/api/_generated/traces/types/traces_search_response.py +26 -0
- mirascope/api/_generated/traces/types/traces_search_response_spans_item.py +50 -0
- mirascope/api/_generated/types/__init__.py +48 -0
- mirascope/api/_generated/types/already_exists_error.py +1 -3
- mirascope/api/_generated/types/bad_request_error_body.py +50 -0
- mirascope/api/_generated/types/click_house_error.py +22 -0
- mirascope/api/_generated/types/database_error.py +1 -3
- mirascope/api/_generated/types/date.py +3 -0
- mirascope/api/_generated/types/http_api_decode_error.py +1 -3
- mirascope/api/_generated/types/immutable_resource_error.py +22 -0
- mirascope/api/_generated/types/internal_server_error_body.py +49 -0
- mirascope/api/_generated/types/issue.py +1 -3
- mirascope/api/_generated/types/issue_tag.py +1 -8
- mirascope/api/_generated/types/not_found_error_body.py +1 -3
- mirascope/api/_generated/types/number_from_string.py +3 -0
- mirascope/api/_generated/types/permission_denied_error.py +1 -3
- mirascope/api/_generated/types/permission_denied_error_tag.py +1 -3
- mirascope/api/_generated/types/plan_limit_exceeded_error.py +32 -0
- mirascope/api/_generated/types/plan_limit_exceeded_error_tag.py +7 -0
- mirascope/api/_generated/types/pricing_unavailable_error.py +23 -0
- mirascope/api/_generated/types/property_key_key.py +1 -3
- mirascope/api/_generated/types/rate_limit_error.py +31 -0
- mirascope/api/_generated/types/rate_limit_error_tag.py +5 -0
- mirascope/api/_generated/types/service_unavailable_error_body.py +24 -0
- mirascope/api/_generated/types/service_unavailable_error_tag.py +7 -0
- mirascope/api/_generated/types/stripe_error.py +20 -0
- mirascope/api/_generated/types/subscription_past_due_error.py +31 -0
- mirascope/api/_generated/types/subscription_past_due_error_tag.py +7 -0
- mirascope/api/_generated/types/unauthorized_error_body.py +21 -0
- mirascope/api/_generated/types/unauthorized_error_tag.py +5 -0
- mirascope/api/settings.py +19 -1
- mirascope/llm/__init__.py +55 -8
- mirascope/llm/calls/__init__.py +2 -1
- mirascope/llm/calls/calls.py +3 -1
- mirascope/llm/calls/decorator.py +21 -7
- mirascope/llm/content/tool_call.py +6 -0
- mirascope/llm/content/tool_output.py +22 -5
- mirascope/llm/exceptions.py +284 -71
- mirascope/llm/formatting/__init__.py +19 -2
- mirascope/llm/formatting/format.py +219 -30
- mirascope/llm/formatting/output_parser.py +178 -0
- mirascope/llm/formatting/partial.py +80 -7
- mirascope/llm/formatting/primitives.py +192 -0
- mirascope/llm/formatting/types.py +21 -64
- mirascope/llm/mcp/__init__.py +2 -2
- mirascope/llm/mcp/mcp_client.py +130 -0
- mirascope/llm/messages/__init__.py +3 -0
- mirascope/llm/messages/_utils.py +34 -0
- mirascope/llm/models/__init__.py +5 -0
- mirascope/llm/models/models.py +137 -69
- mirascope/llm/{providers/base → models}/params.py +16 -37
- mirascope/llm/models/thinking_config.py +61 -0
- mirascope/llm/prompts/_utils.py +0 -32
- mirascope/llm/prompts/decorator.py +16 -5
- mirascope/llm/prompts/prompts.py +131 -68
- mirascope/llm/providers/__init__.py +18 -2
- mirascope/llm/providers/anthropic/__init__.py +3 -21
- mirascope/llm/providers/anthropic/_utils/__init__.py +2 -0
- mirascope/llm/providers/anthropic/_utils/beta_decode.py +22 -11
- mirascope/llm/providers/anthropic/_utils/beta_encode.py +75 -25
- mirascope/llm/providers/anthropic/_utils/decode.py +22 -11
- mirascope/llm/providers/anthropic/_utils/encode.py +82 -20
- mirascope/llm/providers/anthropic/_utils/errors.py +2 -2
- mirascope/llm/providers/anthropic/beta_provider.py +64 -18
- mirascope/llm/providers/anthropic/provider.py +91 -33
- mirascope/llm/providers/base/__init__.py +0 -2
- mirascope/llm/providers/base/_utils.py +55 -11
- mirascope/llm/providers/base/base_provider.py +116 -37
- mirascope/llm/providers/google/__init__.py +2 -17
- mirascope/llm/providers/google/_utils/__init__.py +2 -0
- mirascope/llm/providers/google/_utils/decode.py +37 -15
- mirascope/llm/providers/google/_utils/encode.py +127 -19
- mirascope/llm/providers/google/_utils/errors.py +3 -2
- mirascope/llm/providers/google/model_info.py +1 -0
- mirascope/llm/providers/google/provider.py +68 -19
- mirascope/llm/providers/mirascope/__init__.py +5 -0
- mirascope/llm/providers/mirascope/_utils.py +73 -0
- mirascope/llm/providers/mirascope/provider.py +349 -0
- mirascope/llm/providers/mlx/__init__.py +2 -17
- mirascope/llm/providers/mlx/_utils.py +8 -3
- mirascope/llm/providers/mlx/encoding/base.py +5 -2
- mirascope/llm/providers/mlx/encoding/transformers.py +5 -2
- mirascope/llm/providers/mlx/mlx.py +23 -6
- mirascope/llm/providers/mlx/provider.py +42 -13
- mirascope/llm/providers/ollama/__init__.py +1 -13
- mirascope/llm/providers/openai/_utils/errors.py +2 -2
- mirascope/llm/providers/openai/completions/__init__.py +2 -20
- mirascope/llm/providers/openai/completions/_utils/decode.py +14 -3
- mirascope/llm/providers/openai/completions/_utils/encode.py +35 -28
- mirascope/llm/providers/openai/completions/base_provider.py +40 -11
- mirascope/llm/providers/openai/provider.py +40 -10
- mirascope/llm/providers/openai/responses/__init__.py +1 -17
- mirascope/llm/providers/openai/responses/_utils/__init__.py +2 -0
- mirascope/llm/providers/openai/responses/_utils/decode.py +21 -8
- mirascope/llm/providers/openai/responses/_utils/encode.py +59 -19
- mirascope/llm/providers/openai/responses/provider.py +56 -18
- mirascope/llm/providers/provider_id.py +1 -0
- mirascope/llm/providers/provider_registry.py +96 -19
- mirascope/llm/providers/together/__init__.py +1 -13
- mirascope/llm/responses/__init__.py +6 -1
- mirascope/llm/responses/_utils.py +102 -12
- mirascope/llm/responses/base_response.py +5 -2
- mirascope/llm/responses/base_stream_response.py +139 -45
- mirascope/llm/responses/response.py +2 -1
- mirascope/llm/responses/root_response.py +89 -17
- mirascope/llm/responses/stream_response.py +6 -9
- mirascope/llm/tools/decorator.py +17 -8
- mirascope/llm/tools/tool_schema.py +43 -10
- mirascope/llm/tools/toolkit.py +35 -27
- mirascope/llm/tools/tools.py +123 -30
- mirascope/ops/__init__.py +64 -109
- mirascope/ops/_internal/configuration.py +82 -31
- mirascope/ops/_internal/exporters/exporters.py +64 -11
- mirascope/ops/_internal/instrumentation/llm/common.py +530 -0
- mirascope/ops/_internal/instrumentation/llm/cost.py +190 -0
- mirascope/ops/_internal/instrumentation/llm/encode.py +1 -1
- mirascope/ops/_internal/instrumentation/llm/llm.py +116 -1243
- mirascope/ops/_internal/instrumentation/llm/model.py +1798 -0
- mirascope/ops/_internal/instrumentation/llm/response.py +521 -0
- mirascope/ops/_internal/instrumentation/llm/serialize.py +300 -0
- mirascope/ops/_internal/protocols.py +83 -1
- mirascope/ops/_internal/traced_calls.py +4 -0
- mirascope/ops/_internal/traced_functions.py +141 -12
- mirascope/ops/_internal/tracing.py +78 -1
- mirascope/ops/_internal/utils.py +52 -4
- mirascope/ops/_internal/versioned_functions.py +54 -43
- {mirascope-2.0.0a5.dist-info → mirascope-2.0.1.dist-info}/METADATA +14 -13
- mirascope-2.0.1.dist-info/RECORD +423 -0
- {mirascope-2.0.0a5.dist-info → mirascope-2.0.1.dist-info}/licenses/LICENSE +1 -1
- mirascope/llm/formatting/_utils.py +0 -78
- mirascope/llm/mcp/client.py +0 -118
- mirascope/llm/providers/_missing_import_stubs.py +0 -49
- mirascope-2.0.0a5.dist-info/RECORD +0 -265
- {mirascope-2.0.0a5.dist-info → mirascope-2.0.1.dist-info}/WHEEL +0 -0
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
"""Mirascope Router provider that routes requests through the Mirascope Router API."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
from collections.abc import Sequence
|
|
7
|
+
from typing import TYPE_CHECKING
|
|
8
|
+
from typing_extensions import Unpack
|
|
9
|
+
|
|
10
|
+
from ...context import Context, DepsT
|
|
11
|
+
from ...formatting import Format, FormattableT, OutputParser
|
|
12
|
+
from ...messages import Message
|
|
13
|
+
from ...responses import (
|
|
14
|
+
AsyncContextResponse,
|
|
15
|
+
AsyncContextStreamResponse,
|
|
16
|
+
AsyncResponse,
|
|
17
|
+
AsyncStreamResponse,
|
|
18
|
+
ContextResponse,
|
|
19
|
+
ContextStreamResponse,
|
|
20
|
+
Response,
|
|
21
|
+
StreamResponse,
|
|
22
|
+
)
|
|
23
|
+
from ...tools import (
|
|
24
|
+
AsyncContextTool,
|
|
25
|
+
AsyncContextToolkit,
|
|
26
|
+
AsyncTool,
|
|
27
|
+
AsyncToolkit,
|
|
28
|
+
ContextTool,
|
|
29
|
+
ContextToolkit,
|
|
30
|
+
Tool,
|
|
31
|
+
Toolkit,
|
|
32
|
+
)
|
|
33
|
+
from ..base import BaseProvider, Provider
|
|
34
|
+
from . import _utils
|
|
35
|
+
|
|
36
|
+
if TYPE_CHECKING:
|
|
37
|
+
from ...models import Params
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class MirascopeProvider(BaseProvider[None]):
|
|
41
|
+
"""Provider that routes LLM requests through the Mirascope Router API.
|
|
42
|
+
|
|
43
|
+
The Mirascope Router provides a unified API for multiple LLM providers
|
|
44
|
+
(Anthropic, Google, OpenAI) with usage tracking and cost calculation.
|
|
45
|
+
|
|
46
|
+
This provider:
|
|
47
|
+
- Takes model IDs in the format "scope/model-name" (e.g., "openai/gpt-4")
|
|
48
|
+
- Routes requests to the Mirascope Router endpoint
|
|
49
|
+
- Delegates to the appropriate underlying provider (Anthropic, Google, or OpenAI)
|
|
50
|
+
- Uses MIRASCOPE_API_KEY for authentication
|
|
51
|
+
|
|
52
|
+
Environment Variables:
|
|
53
|
+
MIRASCOPE_API_KEY: Required API key for Mirascope Router authentication
|
|
54
|
+
MIRASCOPE_ROUTER_BASE_URL: Optional base URL override
|
|
55
|
+
(default: https://mirascope.com/router/v2)
|
|
56
|
+
|
|
57
|
+
Example:
|
|
58
|
+
```python
|
|
59
|
+
import os
|
|
60
|
+
from mirascope import llm
|
|
61
|
+
|
|
62
|
+
os.environ["MIRASCOPE_API_KEY"] = "mk..."
|
|
63
|
+
|
|
64
|
+
# Register the Mirascope provider
|
|
65
|
+
llm.register_provider(
|
|
66
|
+
"mirascope",
|
|
67
|
+
scope=["anthropic/", "google/", "openai/"],
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
# Use with llm.call decorator
|
|
71
|
+
@llm.call("openai/gpt-4")
|
|
72
|
+
def recommend_book(genre: str):
|
|
73
|
+
return f"Recommend a {genre} book"
|
|
74
|
+
|
|
75
|
+
response = recommend_book("fantasy")
|
|
76
|
+
print(response.content)
|
|
77
|
+
```
|
|
78
|
+
"""
|
|
79
|
+
|
|
80
|
+
id = "mirascope"
|
|
81
|
+
default_scope = ["anthropic/", "google/", "openai/"]
|
|
82
|
+
error_map = {}
|
|
83
|
+
"""Empty error map since MirascopeProvider delegates to underlying providers.
|
|
84
|
+
|
|
85
|
+
Error handling is performed by the underlying provider instances (Anthropic,
|
|
86
|
+
Google, OpenAI), which have their own error maps. Any exceptions that bubble
|
|
87
|
+
up from underlying providers are already converted to Mirascope exceptions.
|
|
88
|
+
"""
|
|
89
|
+
|
|
90
|
+
def __init__(
|
|
91
|
+
self, *, api_key: str | None = None, base_url: str | None = None
|
|
92
|
+
) -> None:
|
|
93
|
+
"""Initialize the Mirascope provider.
|
|
94
|
+
|
|
95
|
+
Args:
|
|
96
|
+
api_key: Mirascope API key. If not provided, reads from MIRASCOPE_API_KEY
|
|
97
|
+
environment variable.
|
|
98
|
+
base_url: Optional base URL override for the Mirascope Router. If not
|
|
99
|
+
provided, reads from MIRASCOPE_ROUTER_BASE_URL environment variable
|
|
100
|
+
or defaults to https://mirascope.com/router/v2
|
|
101
|
+
"""
|
|
102
|
+
api_key = api_key or os.environ.get("MIRASCOPE_API_KEY")
|
|
103
|
+
if not api_key:
|
|
104
|
+
raise ValueError(
|
|
105
|
+
"Mirascope API key not found. "
|
|
106
|
+
"Set MIRASCOPE_API_KEY environment variable or pass api_key parameter."
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
self.api_key = api_key
|
|
110
|
+
self.router_base_url = base_url or _utils.get_default_router_base_url()
|
|
111
|
+
self.client = None # No single client; we create per-provider clients
|
|
112
|
+
|
|
113
|
+
def get_error_status(self, e: Exception) -> int | None:
|
|
114
|
+
"""Extract HTTP status code from exception.
|
|
115
|
+
|
|
116
|
+
Since MirascopeProvider delegates to underlying providers, this method
|
|
117
|
+
is not used for direct error extraction. Underlying providers handle
|
|
118
|
+
their own status code extraction.
|
|
119
|
+
|
|
120
|
+
Args:
|
|
121
|
+
e: The exception to extract status code from.
|
|
122
|
+
|
|
123
|
+
Returns:
|
|
124
|
+
None, as status extraction is handled by underlying providers.
|
|
125
|
+
"""
|
|
126
|
+
return None
|
|
127
|
+
|
|
128
|
+
def _get_underlying_provider(self, model_id: str) -> Provider:
|
|
129
|
+
"""Get the underlying provider for a model ID.
|
|
130
|
+
|
|
131
|
+
Args:
|
|
132
|
+
model_id: Model identifier in format "scope/model-name"
|
|
133
|
+
|
|
134
|
+
Returns:
|
|
135
|
+
The appropriate cached provider instance (Anthropic, Google, or OpenAI)
|
|
136
|
+
|
|
137
|
+
Raises:
|
|
138
|
+
ValueError: If the model ID format is invalid or provider is unsupported
|
|
139
|
+
"""
|
|
140
|
+
model_scope = _utils.extract_model_scope(model_id)
|
|
141
|
+
if not model_scope:
|
|
142
|
+
raise ValueError(
|
|
143
|
+
f"Invalid model ID format: {model_id}. "
|
|
144
|
+
f"Expected format 'scope/model-name' (e.g., 'openai/gpt-4')"
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
# Use the cached function to get/create the provider
|
|
148
|
+
return _utils.create_underlying_provider(
|
|
149
|
+
model_scope=model_scope,
|
|
150
|
+
api_key=self.api_key,
|
|
151
|
+
router_base_url=self.router_base_url,
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
def _call(
|
|
155
|
+
self,
|
|
156
|
+
*,
|
|
157
|
+
model_id: str,
|
|
158
|
+
messages: Sequence[Message],
|
|
159
|
+
tools: Sequence[Tool] | Toolkit | None = None,
|
|
160
|
+
format: type[FormattableT]
|
|
161
|
+
| Format[FormattableT]
|
|
162
|
+
| OutputParser[FormattableT]
|
|
163
|
+
| None = None,
|
|
164
|
+
**params: Unpack[Params],
|
|
165
|
+
) -> Response | Response[FormattableT]:
|
|
166
|
+
"""Generate an `llm.Response` by calling through the Mirascope Router."""
|
|
167
|
+
provider = self._get_underlying_provider(model_id)
|
|
168
|
+
return provider.call(
|
|
169
|
+
model_id=model_id,
|
|
170
|
+
messages=messages,
|
|
171
|
+
tools=tools,
|
|
172
|
+
format=format,
|
|
173
|
+
**params,
|
|
174
|
+
)
|
|
175
|
+
|
|
176
|
+
def _context_call(
|
|
177
|
+
self,
|
|
178
|
+
*,
|
|
179
|
+
ctx: Context[DepsT],
|
|
180
|
+
model_id: str,
|
|
181
|
+
messages: Sequence[Message],
|
|
182
|
+
tools: Sequence[Tool | ContextTool[DepsT]]
|
|
183
|
+
| ContextToolkit[DepsT]
|
|
184
|
+
| None = None,
|
|
185
|
+
format: type[FormattableT]
|
|
186
|
+
| Format[FormattableT]
|
|
187
|
+
| OutputParser[FormattableT]
|
|
188
|
+
| None = None,
|
|
189
|
+
**params: Unpack[Params],
|
|
190
|
+
) -> ContextResponse[DepsT, None] | ContextResponse[DepsT, FormattableT]:
|
|
191
|
+
"""Generate an `llm.ContextResponse` by calling through the Mirascope Router."""
|
|
192
|
+
provider = self._get_underlying_provider(model_id)
|
|
193
|
+
return provider.context_call(
|
|
194
|
+
ctx=ctx,
|
|
195
|
+
model_id=model_id,
|
|
196
|
+
messages=messages,
|
|
197
|
+
tools=tools,
|
|
198
|
+
format=format,
|
|
199
|
+
**params,
|
|
200
|
+
)
|
|
201
|
+
|
|
202
|
+
async def _call_async(
|
|
203
|
+
self,
|
|
204
|
+
*,
|
|
205
|
+
model_id: str,
|
|
206
|
+
messages: Sequence[Message],
|
|
207
|
+
tools: Sequence[AsyncTool] | AsyncToolkit | None = None,
|
|
208
|
+
format: type[FormattableT]
|
|
209
|
+
| Format[FormattableT]
|
|
210
|
+
| OutputParser[FormattableT]
|
|
211
|
+
| None = None,
|
|
212
|
+
**params: Unpack[Params],
|
|
213
|
+
) -> AsyncResponse | AsyncResponse[FormattableT]:
|
|
214
|
+
"""Generate an `llm.AsyncResponse` by calling through the Mirascope Router."""
|
|
215
|
+
provider = self._get_underlying_provider(model_id)
|
|
216
|
+
return await provider.call_async(
|
|
217
|
+
model_id=model_id,
|
|
218
|
+
messages=messages,
|
|
219
|
+
tools=tools,
|
|
220
|
+
format=format,
|
|
221
|
+
**params,
|
|
222
|
+
)
|
|
223
|
+
|
|
224
|
+
async def _context_call_async(
|
|
225
|
+
self,
|
|
226
|
+
*,
|
|
227
|
+
ctx: Context[DepsT],
|
|
228
|
+
model_id: str,
|
|
229
|
+
messages: Sequence[Message],
|
|
230
|
+
tools: Sequence[AsyncTool | AsyncContextTool[DepsT]]
|
|
231
|
+
| AsyncContextToolkit[DepsT]
|
|
232
|
+
| None = None,
|
|
233
|
+
format: type[FormattableT]
|
|
234
|
+
| Format[FormattableT]
|
|
235
|
+
| OutputParser[FormattableT]
|
|
236
|
+
| None = None,
|
|
237
|
+
**params: Unpack[Params],
|
|
238
|
+
) -> AsyncContextResponse[DepsT, None] | AsyncContextResponse[DepsT, FormattableT]:
|
|
239
|
+
"""Generate an `llm.AsyncContextResponse` by calling through the Mirascope Router."""
|
|
240
|
+
provider = self._get_underlying_provider(model_id)
|
|
241
|
+
return await provider.context_call_async(
|
|
242
|
+
ctx=ctx,
|
|
243
|
+
model_id=model_id,
|
|
244
|
+
messages=messages,
|
|
245
|
+
tools=tools,
|
|
246
|
+
format=format,
|
|
247
|
+
**params,
|
|
248
|
+
)
|
|
249
|
+
|
|
250
|
+
def _stream(
|
|
251
|
+
self,
|
|
252
|
+
*,
|
|
253
|
+
model_id: str,
|
|
254
|
+
messages: Sequence[Message],
|
|
255
|
+
tools: Sequence[Tool] | Toolkit | None = None,
|
|
256
|
+
format: type[FormattableT]
|
|
257
|
+
| Format[FormattableT]
|
|
258
|
+
| OutputParser[FormattableT]
|
|
259
|
+
| None = None,
|
|
260
|
+
**params: Unpack[Params],
|
|
261
|
+
) -> StreamResponse | StreamResponse[FormattableT]:
|
|
262
|
+
"""Stream an `llm.StreamResponse` by calling through the Mirascope Router."""
|
|
263
|
+
provider = self._get_underlying_provider(model_id)
|
|
264
|
+
return provider.stream(
|
|
265
|
+
model_id=model_id,
|
|
266
|
+
messages=messages,
|
|
267
|
+
tools=tools,
|
|
268
|
+
format=format,
|
|
269
|
+
**params,
|
|
270
|
+
)
|
|
271
|
+
|
|
272
|
+
def _context_stream(
|
|
273
|
+
self,
|
|
274
|
+
*,
|
|
275
|
+
ctx: Context[DepsT],
|
|
276
|
+
model_id: str,
|
|
277
|
+
messages: Sequence[Message],
|
|
278
|
+
tools: Sequence[Tool | ContextTool[DepsT]]
|
|
279
|
+
| ContextToolkit[DepsT]
|
|
280
|
+
| None = None,
|
|
281
|
+
format: type[FormattableT]
|
|
282
|
+
| Format[FormattableT]
|
|
283
|
+
| OutputParser[FormattableT]
|
|
284
|
+
| None = None,
|
|
285
|
+
**params: Unpack[Params],
|
|
286
|
+
) -> (
|
|
287
|
+
ContextStreamResponse[DepsT, None] | ContextStreamResponse[DepsT, FormattableT]
|
|
288
|
+
):
|
|
289
|
+
"""Stream an `llm.ContextStreamResponse` by calling through the Mirascope Router."""
|
|
290
|
+
provider = self._get_underlying_provider(model_id)
|
|
291
|
+
return provider.context_stream(
|
|
292
|
+
ctx=ctx,
|
|
293
|
+
model_id=model_id,
|
|
294
|
+
messages=messages,
|
|
295
|
+
tools=tools,
|
|
296
|
+
format=format,
|
|
297
|
+
**params,
|
|
298
|
+
)
|
|
299
|
+
|
|
300
|
+
async def _stream_async(
|
|
301
|
+
self,
|
|
302
|
+
*,
|
|
303
|
+
model_id: str,
|
|
304
|
+
messages: Sequence[Message],
|
|
305
|
+
tools: Sequence[AsyncTool] | AsyncToolkit | None = None,
|
|
306
|
+
format: type[FormattableT]
|
|
307
|
+
| Format[FormattableT]
|
|
308
|
+
| OutputParser[FormattableT]
|
|
309
|
+
| None = None,
|
|
310
|
+
**params: Unpack[Params],
|
|
311
|
+
) -> AsyncStreamResponse | AsyncStreamResponse[FormattableT]:
|
|
312
|
+
"""Stream an `llm.AsyncStreamResponse` by calling through the Mirascope Router."""
|
|
313
|
+
provider = self._get_underlying_provider(model_id)
|
|
314
|
+
return await provider.stream_async(
|
|
315
|
+
model_id=model_id,
|
|
316
|
+
messages=messages,
|
|
317
|
+
tools=tools,
|
|
318
|
+
format=format,
|
|
319
|
+
**params,
|
|
320
|
+
)
|
|
321
|
+
|
|
322
|
+
async def _context_stream_async(
|
|
323
|
+
self,
|
|
324
|
+
*,
|
|
325
|
+
ctx: Context[DepsT],
|
|
326
|
+
model_id: str,
|
|
327
|
+
messages: Sequence[Message],
|
|
328
|
+
tools: Sequence[AsyncTool | AsyncContextTool[DepsT]]
|
|
329
|
+
| AsyncContextToolkit[DepsT]
|
|
330
|
+
| None = None,
|
|
331
|
+
format: type[FormattableT]
|
|
332
|
+
| Format[FormattableT]
|
|
333
|
+
| OutputParser[FormattableT]
|
|
334
|
+
| None = None,
|
|
335
|
+
**params: Unpack[Params],
|
|
336
|
+
) -> (
|
|
337
|
+
AsyncContextStreamResponse[DepsT, None]
|
|
338
|
+
| AsyncContextStreamResponse[DepsT, FormattableT]
|
|
339
|
+
):
|
|
340
|
+
"""Stream an `llm.AsyncContextStreamResponse` by calling through the Mirascope Router."""
|
|
341
|
+
provider = self._get_underlying_provider(model_id)
|
|
342
|
+
return await provider.context_stream_async(
|
|
343
|
+
ctx=ctx,
|
|
344
|
+
model_id=model_id,
|
|
345
|
+
messages=messages,
|
|
346
|
+
tools=tools,
|
|
347
|
+
format=format,
|
|
348
|
+
**params,
|
|
349
|
+
)
|
|
@@ -1,22 +1,7 @@
|
|
|
1
1
|
"""MLX client implementation."""
|
|
2
2
|
|
|
3
|
-
from
|
|
4
|
-
|
|
5
|
-
if TYPE_CHECKING:
|
|
6
|
-
from .model_id import MLXModelId
|
|
7
|
-
from .provider import MLXProvider
|
|
8
|
-
else:
|
|
9
|
-
try:
|
|
10
|
-
from .model_id import MLXModelId
|
|
11
|
-
from .provider import MLXProvider
|
|
12
|
-
except ImportError: # pragma: no cover
|
|
13
|
-
from .._missing_import_stubs import (
|
|
14
|
-
create_import_error_stub,
|
|
15
|
-
create_provider_stub,
|
|
16
|
-
)
|
|
17
|
-
|
|
18
|
-
MLXProvider = create_provider_stub("mlx", "MLXProvider")
|
|
19
|
-
MLXModelId = str
|
|
3
|
+
from .model_id import MLXModelId
|
|
4
|
+
from .provider import MLXProvider
|
|
20
5
|
|
|
21
6
|
__all__ = [
|
|
22
7
|
"MLXModelId",
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
1
3
|
from collections.abc import Callable
|
|
2
|
-
from typing import TypeAlias, TypedDict
|
|
4
|
+
from typing import TYPE_CHECKING, TypeAlias, TypedDict
|
|
3
5
|
|
|
4
6
|
import mlx.core as mx
|
|
5
7
|
from huggingface_hub.errors import LocalEntryNotFoundError
|
|
@@ -8,7 +10,10 @@ from mlx_lm.sample_utils import make_sampler
|
|
|
8
10
|
|
|
9
11
|
from ...exceptions import NotFoundError
|
|
10
12
|
from ...responses import FinishReason, Usage
|
|
11
|
-
from ..base import
|
|
13
|
+
from ..base import ProviderErrorMap, _utils as _base_utils
|
|
14
|
+
|
|
15
|
+
if TYPE_CHECKING:
|
|
16
|
+
from ...models import Params
|
|
12
17
|
|
|
13
18
|
Sampler: TypeAlias = Callable[[mx.array], mx.array]
|
|
14
19
|
|
|
@@ -76,7 +81,7 @@ def encode_params(params: Params) -> tuple[int | None, StreamGenerateKwargs]:
|
|
|
76
81
|
with _base_utils.ensure_all_params_accessed(
|
|
77
82
|
params=params,
|
|
78
83
|
provider_id="mlx",
|
|
79
|
-
unsupported_params=["stop_sequences", "thinking"
|
|
84
|
+
unsupported_params=["stop_sequences", "thinking"],
|
|
80
85
|
) as param_accessor:
|
|
81
86
|
if param_accessor.max_tokens is not None:
|
|
82
87
|
kwargs["max_tokens"] = param_accessor.max_tokens
|
|
@@ -6,7 +6,7 @@ from typing import TypeAlias
|
|
|
6
6
|
|
|
7
7
|
from mlx_lm.generate import GenerationResponse
|
|
8
8
|
|
|
9
|
-
from ....formatting import Format, FormattableT
|
|
9
|
+
from ....formatting import Format, FormattableT, OutputParser
|
|
10
10
|
from ....messages import AssistantContent, Message
|
|
11
11
|
from ....responses import ChunkIterator
|
|
12
12
|
from ....tools import AnyToolSchema, BaseToolkit
|
|
@@ -22,7 +22,10 @@ class BaseEncoder(abc.ABC):
|
|
|
22
22
|
self,
|
|
23
23
|
messages: Sequence[Message],
|
|
24
24
|
tools: Sequence[AnyToolSchema] | BaseToolkit[AnyToolSchema] | None,
|
|
25
|
-
format: type[FormattableT]
|
|
25
|
+
format: type[FormattableT]
|
|
26
|
+
| Format[FormattableT]
|
|
27
|
+
| OutputParser[FormattableT]
|
|
28
|
+
| None,
|
|
26
29
|
) -> tuple[Sequence[Message], Format[FormattableT] | None, TokenIds]:
|
|
27
30
|
"""Encode the request messages into a format suitable for the model.
|
|
28
31
|
|
|
@@ -8,7 +8,7 @@ from mlx_lm.generate import GenerationResponse
|
|
|
8
8
|
from transformers import PreTrainedTokenizer
|
|
9
9
|
|
|
10
10
|
from ....content import ContentPart, TextChunk, TextEndChunk, TextStartChunk
|
|
11
|
-
from ....formatting import Format, FormattableT
|
|
11
|
+
from ....formatting import Format, FormattableT, OutputParser
|
|
12
12
|
from ....messages import AssistantContent, Message
|
|
13
13
|
from ....responses import (
|
|
14
14
|
ChunkIterator,
|
|
@@ -81,7 +81,10 @@ class TransformersEncoder(BaseEncoder):
|
|
|
81
81
|
self,
|
|
82
82
|
messages: Sequence[Message],
|
|
83
83
|
tools: Sequence[AnyToolSchema] | BaseToolkit[AnyToolSchema] | None,
|
|
84
|
-
format: type[FormattableT]
|
|
84
|
+
format: type[FormattableT]
|
|
85
|
+
| Format[FormattableT]
|
|
86
|
+
| OutputParser[FormattableT]
|
|
87
|
+
| None,
|
|
85
88
|
) -> tuple[Sequence[Message], Format[FormattableT] | None, TokenIds]:
|
|
86
89
|
"""Encode a request into a format suitable for the model."""
|
|
87
90
|
tool_schemas = tools.tools if isinstance(tools, BaseToolkit) else tools or []
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
1
3
|
import asyncio
|
|
2
4
|
import threading
|
|
3
5
|
from collections.abc import Iterable, Sequence
|
|
4
6
|
from dataclasses import dataclass, field
|
|
7
|
+
from typing import TYPE_CHECKING
|
|
5
8
|
from typing_extensions import Unpack
|
|
6
9
|
|
|
7
10
|
import mlx.core as mx
|
|
@@ -10,15 +13,17 @@ from mlx_lm import stream_generate # type: ignore[reportPrivateImportUsage]
|
|
|
10
13
|
from mlx_lm.generate import GenerationResponse
|
|
11
14
|
from transformers import PreTrainedTokenizer
|
|
12
15
|
|
|
13
|
-
from ...formatting import Format, FormattableT
|
|
16
|
+
from ...formatting import Format, FormattableT, OutputParser
|
|
14
17
|
from ...messages import AssistantMessage, Message, assistant
|
|
15
18
|
from ...responses import AsyncChunkIterator, ChunkIterator, StreamResponseChunk
|
|
16
19
|
from ...tools import AnyToolSchema, BaseToolkit
|
|
17
|
-
from ..base import Params
|
|
18
20
|
from . import _utils
|
|
19
21
|
from .encoding import BaseEncoder, TokenIds
|
|
20
22
|
from .model_id import MLXModelId
|
|
21
23
|
|
|
24
|
+
if TYPE_CHECKING:
|
|
25
|
+
from ...models import Params
|
|
26
|
+
|
|
22
27
|
|
|
23
28
|
def _consume_sync_stream_into_queue(
|
|
24
29
|
generation_stream: ChunkIterator,
|
|
@@ -133,7 +138,10 @@ class MLX:
|
|
|
133
138
|
self,
|
|
134
139
|
messages: Sequence[Message],
|
|
135
140
|
tools: Sequence[AnyToolSchema] | BaseToolkit[AnyToolSchema] | None,
|
|
136
|
-
format: type[FormattableT]
|
|
141
|
+
format: type[FormattableT]
|
|
142
|
+
| Format[FormattableT]
|
|
143
|
+
| OutputParser[FormattableT]
|
|
144
|
+
| None,
|
|
137
145
|
params: Params,
|
|
138
146
|
) -> tuple[Sequence[Message], Format[FormattableT] | None, ChunkIterator]:
|
|
139
147
|
"""Stream response chunks synchronously.
|
|
@@ -156,7 +164,10 @@ class MLX:
|
|
|
156
164
|
self,
|
|
157
165
|
messages: Sequence[Message],
|
|
158
166
|
tools: Sequence[AnyToolSchema] | BaseToolkit[AnyToolSchema] | None,
|
|
159
|
-
format: type[FormattableT]
|
|
167
|
+
format: type[FormattableT]
|
|
168
|
+
| Format[FormattableT]
|
|
169
|
+
| OutputParser[FormattableT]
|
|
170
|
+
| None,
|
|
160
171
|
params: Params,
|
|
161
172
|
) -> tuple[Sequence[Message], Format[FormattableT] | None, AsyncChunkIterator]:
|
|
162
173
|
"""Stream response chunks asynchronously.
|
|
@@ -180,7 +191,10 @@ class MLX:
|
|
|
180
191
|
self,
|
|
181
192
|
messages: Sequence[Message],
|
|
182
193
|
tools: Sequence[AnyToolSchema] | BaseToolkit[AnyToolSchema] | None,
|
|
183
|
-
format: type[FormattableT]
|
|
194
|
+
format: type[FormattableT]
|
|
195
|
+
| Format[FormattableT]
|
|
196
|
+
| OutputParser[FormattableT]
|
|
197
|
+
| None,
|
|
184
198
|
params: Params,
|
|
185
199
|
) -> tuple[
|
|
186
200
|
Sequence[Message],
|
|
@@ -216,7 +230,10 @@ class MLX:
|
|
|
216
230
|
self,
|
|
217
231
|
messages: Sequence[Message],
|
|
218
232
|
tools: Sequence[AnyToolSchema] | BaseToolkit[AnyToolSchema] | None,
|
|
219
|
-
format: type[FormattableT]
|
|
233
|
+
format: type[FormattableT]
|
|
234
|
+
| Format[FormattableT]
|
|
235
|
+
| OutputParser[FormattableT]
|
|
236
|
+
| None,
|
|
220
237
|
params: Params,
|
|
221
238
|
) -> tuple[
|
|
222
239
|
Sequence[Message],
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
1
3
|
from collections.abc import Sequence
|
|
2
4
|
from functools import cache, lru_cache
|
|
3
|
-
from typing import cast
|
|
5
|
+
from typing import TYPE_CHECKING, cast
|
|
4
6
|
from typing_extensions import Unpack
|
|
5
7
|
|
|
6
8
|
import mlx.nn as nn
|
|
@@ -8,7 +10,7 @@ from mlx_lm import load as mlx_load
|
|
|
8
10
|
from transformers import PreTrainedTokenizer
|
|
9
11
|
|
|
10
12
|
from ...context import Context, DepsT
|
|
11
|
-
from ...formatting import Format, FormattableT
|
|
13
|
+
from ...formatting import Format, FormattableT, OutputParser
|
|
12
14
|
from ...messages import Message
|
|
13
15
|
from ...responses import (
|
|
14
16
|
AsyncContextResponse,
|
|
@@ -30,20 +32,23 @@ from ...tools import (
|
|
|
30
32
|
Tool,
|
|
31
33
|
Toolkit,
|
|
32
34
|
)
|
|
33
|
-
from ..base import BaseProvider
|
|
35
|
+
from ..base import BaseProvider
|
|
34
36
|
from . import _utils
|
|
35
37
|
from .encoding import TransformersEncoder
|
|
36
38
|
from .mlx import MLX
|
|
37
39
|
from .model_id import MLXModelId
|
|
38
40
|
|
|
41
|
+
if TYPE_CHECKING:
|
|
42
|
+
from ...models import Params
|
|
43
|
+
|
|
39
44
|
|
|
40
45
|
@cache
|
|
41
|
-
def _mlx_client_singleton() ->
|
|
46
|
+
def _mlx_client_singleton() -> MLXProvider:
|
|
42
47
|
"""Get or create the singleton MLX client instance."""
|
|
43
48
|
return MLXProvider()
|
|
44
49
|
|
|
45
50
|
|
|
46
|
-
def client() ->
|
|
51
|
+
def client() -> MLXProvider:
|
|
47
52
|
"""Get the MLX client singleton instance."""
|
|
48
53
|
return _mlx_client_singleton()
|
|
49
54
|
|
|
@@ -85,7 +90,10 @@ class MLXProvider(BaseProvider[None]):
|
|
|
85
90
|
model_id: MLXModelId,
|
|
86
91
|
messages: Sequence[Message],
|
|
87
92
|
tools: Sequence[Tool] | Toolkit | None = None,
|
|
88
|
-
format: type[FormattableT]
|
|
93
|
+
format: type[FormattableT]
|
|
94
|
+
| Format[FormattableT]
|
|
95
|
+
| OutputParser[FormattableT]
|
|
96
|
+
| None = None,
|
|
89
97
|
**params: Unpack[Params],
|
|
90
98
|
) -> Response | Response[FormattableT]:
|
|
91
99
|
"""Generate an `llm.Response` using MLX model.
|
|
@@ -129,7 +137,10 @@ class MLXProvider(BaseProvider[None]):
|
|
|
129
137
|
tools: Sequence[Tool | ContextTool[DepsT]]
|
|
130
138
|
| ContextToolkit[DepsT]
|
|
131
139
|
| None = None,
|
|
132
|
-
format: type[FormattableT]
|
|
140
|
+
format: type[FormattableT]
|
|
141
|
+
| Format[FormattableT]
|
|
142
|
+
| OutputParser[FormattableT]
|
|
143
|
+
| None = None,
|
|
133
144
|
**params: Unpack[Params],
|
|
134
145
|
) -> ContextResponse[DepsT, None] | ContextResponse[DepsT, FormattableT]:
|
|
135
146
|
"""Generate an `llm.ContextResponse` using MLX model.
|
|
@@ -171,7 +182,10 @@ class MLXProvider(BaseProvider[None]):
|
|
|
171
182
|
model_id: MLXModelId,
|
|
172
183
|
messages: Sequence[Message],
|
|
173
184
|
tools: Sequence[AsyncTool] | AsyncToolkit | None = None,
|
|
174
|
-
format: type[FormattableT]
|
|
185
|
+
format: type[FormattableT]
|
|
186
|
+
| Format[FormattableT]
|
|
187
|
+
| OutputParser[FormattableT]
|
|
188
|
+
| None = None,
|
|
175
189
|
**params: Unpack[Params],
|
|
176
190
|
) -> AsyncResponse | AsyncResponse[FormattableT]:
|
|
177
191
|
"""Generate an `llm.AsyncResponse` using MLX model by asynchronously calloing
|
|
@@ -219,7 +233,10 @@ class MLXProvider(BaseProvider[None]):
|
|
|
219
233
|
tools: Sequence[AsyncTool | AsyncContextTool[DepsT]]
|
|
220
234
|
| AsyncContextToolkit[DepsT]
|
|
221
235
|
| None = None,
|
|
222
|
-
format: type[FormattableT]
|
|
236
|
+
format: type[FormattableT]
|
|
237
|
+
| Format[FormattableT]
|
|
238
|
+
| OutputParser[FormattableT]
|
|
239
|
+
| None = None,
|
|
223
240
|
**params: Unpack[Params],
|
|
224
241
|
) -> AsyncContextResponse[DepsT, None] | AsyncContextResponse[DepsT, FormattableT]:
|
|
225
242
|
"""Generate an `llm.AsyncResponse` using MLX model by asynchronously calloing
|
|
@@ -265,7 +282,10 @@ class MLXProvider(BaseProvider[None]):
|
|
|
265
282
|
model_id: MLXModelId,
|
|
266
283
|
messages: Sequence[Message],
|
|
267
284
|
tools: Sequence[Tool] | Toolkit | None = None,
|
|
268
|
-
format: type[FormattableT]
|
|
285
|
+
format: type[FormattableT]
|
|
286
|
+
| Format[FormattableT]
|
|
287
|
+
| OutputParser[FormattableT]
|
|
288
|
+
| None = None,
|
|
269
289
|
**params: Unpack[Params],
|
|
270
290
|
) -> StreamResponse | StreamResponse[FormattableT]:
|
|
271
291
|
"""Generate an `llm.StreamResponse` by synchronously streaming from MLX model output.
|
|
@@ -306,7 +326,10 @@ class MLXProvider(BaseProvider[None]):
|
|
|
306
326
|
tools: Sequence[Tool | ContextTool[DepsT]]
|
|
307
327
|
| ContextToolkit[DepsT]
|
|
308
328
|
| None = None,
|
|
309
|
-
format: type[FormattableT]
|
|
329
|
+
format: type[FormattableT]
|
|
330
|
+
| Format[FormattableT]
|
|
331
|
+
| OutputParser[FormattableT]
|
|
332
|
+
| None = None,
|
|
310
333
|
**params: Unpack[Params],
|
|
311
334
|
) -> ContextStreamResponse[DepsT] | ContextStreamResponse[DepsT, FormattableT]:
|
|
312
335
|
"""Generate an `llm.ContextStreamResponse` by synchronously streaming from MLX model output.
|
|
@@ -345,7 +368,10 @@ class MLXProvider(BaseProvider[None]):
|
|
|
345
368
|
model_id: MLXModelId,
|
|
346
369
|
messages: Sequence[Message],
|
|
347
370
|
tools: Sequence[AsyncTool] | AsyncToolkit | None = None,
|
|
348
|
-
format: type[FormattableT]
|
|
371
|
+
format: type[FormattableT]
|
|
372
|
+
| Format[FormattableT]
|
|
373
|
+
| OutputParser[FormattableT]
|
|
374
|
+
| None = None,
|
|
349
375
|
**params: Unpack[Params],
|
|
350
376
|
) -> AsyncStreamResponse | AsyncStreamResponse[FormattableT]:
|
|
351
377
|
"""Generate an `llm.AsyncStreamResponse` by asynchronously streaming from MLX model output.
|
|
@@ -386,7 +412,10 @@ class MLXProvider(BaseProvider[None]):
|
|
|
386
412
|
tools: Sequence[AsyncTool | AsyncContextTool[DepsT]]
|
|
387
413
|
| AsyncContextToolkit[DepsT]
|
|
388
414
|
| None = None,
|
|
389
|
-
format: type[FormattableT]
|
|
415
|
+
format: type[FormattableT]
|
|
416
|
+
| Format[FormattableT]
|
|
417
|
+
| OutputParser[FormattableT]
|
|
418
|
+
| None = None,
|
|
390
419
|
**params: Unpack[Params],
|
|
391
420
|
) -> (
|
|
392
421
|
AsyncContextStreamResponse[DepsT]
|
|
@@ -1,18 +1,6 @@
|
|
|
1
1
|
"""Ollama provider implementation."""
|
|
2
2
|
|
|
3
|
-
from
|
|
4
|
-
|
|
5
|
-
if TYPE_CHECKING:
|
|
6
|
-
from .provider import OllamaProvider
|
|
7
|
-
else:
|
|
8
|
-
try:
|
|
9
|
-
from .provider import OllamaProvider
|
|
10
|
-
except ImportError: # pragma: no cover
|
|
11
|
-
from .._missing_import_stubs import (
|
|
12
|
-
create_provider_stub,
|
|
13
|
-
)
|
|
14
|
-
|
|
15
|
-
OllamaProvider = create_provider_stub("openai", "OllamaProvider")
|
|
3
|
+
from .provider import OllamaProvider
|
|
16
4
|
|
|
17
5
|
__all__ = [
|
|
18
6
|
"OllamaProvider",
|