lionagi 0.4.0__py3-none-any.whl → 0.5.1__py3-none-any.whl
Sign up to get free protection for your applications and to get access to all the features.
- lionagi/__init__.py +14 -46
- lionagi/core/__init__.py +3 -1
- lionagi/core/_class_registry.py +69 -0
- lionagi/core/action/__init__.py +3 -13
- lionagi/core/action/action_manager.py +287 -0
- lionagi/core/action/base.py +109 -0
- lionagi/core/action/function_calling.py +127 -92
- lionagi/core/action/tool.py +172 -70
- lionagi/core/action/types.py +16 -0
- lionagi/core/communication/__init__.py +3 -0
- lionagi/core/communication/action_request.py +163 -0
- lionagi/core/communication/action_response.py +149 -0
- lionagi/core/communication/assistant_response.py +161 -0
- lionagi/core/communication/base_mail.py +49 -0
- lionagi/core/communication/instruction.py +376 -0
- lionagi/core/communication/message.py +286 -0
- lionagi/core/communication/message_manager.py +530 -0
- lionagi/core/communication/system.py +116 -0
- lionagi/core/communication/templates/README.md +28 -0
- lionagi/core/communication/templates/action_request.jinja2 +5 -0
- lionagi/core/communication/templates/action_response.jinja2 +9 -0
- lionagi/core/communication/templates/assistant_response.jinja2 +2 -0
- lionagi/core/communication/templates/instruction_message.jinja2 +61 -0
- lionagi/core/communication/templates/system_message.jinja2 +11 -0
- lionagi/core/communication/templates/tool_schemas.jinja2 +7 -0
- lionagi/core/communication/types.py +27 -0
- lionagi/core/communication/utils.py +254 -0
- lionagi/core/forms/__init__.py +3 -0
- lionagi/core/forms/base.py +232 -0
- lionagi/core/forms/form.py +791 -0
- lionagi/core/forms/report.py +321 -0
- lionagi/core/forms/types.py +13 -0
- lionagi/core/forms/utils.py +26 -0
- lionagi/core/generic/__init__.py +3 -6
- lionagi/core/generic/component.py +422 -0
- lionagi/core/generic/edge.py +143 -101
- lionagi/core/generic/element.py +195 -0
- lionagi/core/generic/graph.py +297 -180
- lionagi/core/generic/log.py +151 -0
- lionagi/core/generic/log_manager.py +320 -0
- lionagi/core/generic/node.py +7 -229
- lionagi/core/generic/pile.py +1017 -0
- lionagi/core/generic/progression.py +388 -0
- lionagi/core/generic/types.py +23 -0
- lionagi/core/generic/utils.py +50 -0
- lionagi/core/models/__init__.py +5 -0
- lionagi/core/models/base.py +85 -0
- lionagi/core/models/field_model.py +122 -0
- lionagi/core/models/new_model_params.py +195 -0
- lionagi/core/models/note.py +351 -0
- lionagi/core/models/operable_model.py +392 -0
- lionagi/core/models/schema_model.py +50 -0
- lionagi/core/models/types.py +10 -0
- lionagi/core/session/__init__.py +3 -0
- lionagi/core/session/branch.py +115 -415
- lionagi/core/session/branch_mixins.py +545 -0
- lionagi/core/session/session.py +122 -257
- lionagi/core/session/types.py +8 -0
- lionagi/core/typing/__init__.py +9 -0
- lionagi/core/typing/concepts.py +132 -0
- lionagi/core/typing/config.py +15 -0
- lionagi/core/typing/id.py +221 -0
- lionagi/core/typing/pydantic_.py +33 -0
- lionagi/core/typing/typing_.py +54 -0
- lionagi/integrations/__init__.py +0 -1
- lionagi/integrations/anthropic_/AnthropicModel.py +268 -0
- lionagi/integrations/anthropic_/AnthropicService.py +117 -0
- lionagi/integrations/anthropic_/__init__.py +3 -0
- lionagi/integrations/anthropic_/anthropic_max_output_token_data.yaml +7 -0
- lionagi/integrations/anthropic_/anthropic_price_data.yaml +14 -0
- lionagi/integrations/anthropic_/api_endpoints/__init__.py +3 -0
- lionagi/integrations/anthropic_/api_endpoints/api_request.py +277 -0
- lionagi/integrations/anthropic_/api_endpoints/data_models.py +40 -0
- lionagi/integrations/anthropic_/api_endpoints/match_response.py +119 -0
- lionagi/integrations/anthropic_/api_endpoints/messages/__init__.py +3 -0
- lionagi/integrations/anthropic_/api_endpoints/messages/request/__init__.py +3 -0
- lionagi/integrations/anthropic_/api_endpoints/messages/request/message_models.py +14 -0
- lionagi/integrations/anthropic_/api_endpoints/messages/request/request_body.py +74 -0
- lionagi/integrations/anthropic_/api_endpoints/messages/response/content_models.py +32 -0
- lionagi/integrations/anthropic_/api_endpoints/messages/response/response_body.py +101 -0
- lionagi/integrations/anthropic_/api_endpoints/messages/response/usage_models.py +25 -0
- lionagi/integrations/anthropic_/version.py +5 -0
- lionagi/integrations/groq_/GroqModel.py +318 -0
- lionagi/integrations/groq_/GroqService.py +151 -0
- lionagi/integrations/groq_/__init__.py +3 -0
- lionagi/integrations/groq_/api_endpoints/data_models.py +187 -0
- lionagi/integrations/groq_/api_endpoints/groq_request.py +288 -0
- lionagi/integrations/groq_/api_endpoints/match_response.py +106 -0
- lionagi/integrations/groq_/api_endpoints/response_utils.py +105 -0
- lionagi/integrations/groq_/groq_max_output_token_data.yaml +21 -0
- lionagi/integrations/groq_/groq_price_data.yaml +58 -0
- lionagi/integrations/groq_/groq_rate_limits.yaml +105 -0
- lionagi/integrations/groq_/version.py +5 -0
- lionagi/integrations/litellm_/__init__.py +3 -0
- lionagi/integrations/litellm_/imodel.py +73 -0
- lionagi/integrations/ollama_/OllamaModel.py +244 -0
- lionagi/integrations/ollama_/OllamaService.py +142 -0
- lionagi/integrations/ollama_/__init__.py +3 -0
- lionagi/integrations/ollama_/api_endpoints/__init__.py +3 -0
- lionagi/integrations/ollama_/api_endpoints/api_request.py +179 -0
- lionagi/integrations/ollama_/api_endpoints/chat_completion/__init__.py +3 -0
- lionagi/integrations/ollama_/api_endpoints/chat_completion/message_models.py +31 -0
- lionagi/integrations/ollama_/api_endpoints/chat_completion/request_body.py +46 -0
- lionagi/integrations/ollama_/api_endpoints/chat_completion/response_body.py +67 -0
- lionagi/integrations/ollama_/api_endpoints/chat_completion/tool_models.py +49 -0
- lionagi/integrations/ollama_/api_endpoints/completion/request_body.py +72 -0
- lionagi/integrations/ollama_/api_endpoints/completion/response_body.py +59 -0
- lionagi/integrations/ollama_/api_endpoints/data_models.py +15 -0
- lionagi/integrations/ollama_/api_endpoints/embedding/request_body.py +33 -0
- lionagi/integrations/ollama_/api_endpoints/embedding/response_body.py +29 -0
- lionagi/integrations/ollama_/api_endpoints/match_data_model.py +62 -0
- lionagi/integrations/ollama_/api_endpoints/match_response.py +190 -0
- lionagi/integrations/ollama_/api_endpoints/model/__init__.py +3 -0
- lionagi/integrations/ollama_/api_endpoints/model/copy_model.py +13 -0
- lionagi/integrations/ollama_/api_endpoints/model/create_model.py +28 -0
- lionagi/integrations/ollama_/api_endpoints/model/delete_model.py +11 -0
- lionagi/integrations/ollama_/api_endpoints/model/list_model.py +60 -0
- lionagi/integrations/ollama_/api_endpoints/model/pull_model.py +34 -0
- lionagi/integrations/ollama_/api_endpoints/model/push_model.py +35 -0
- lionagi/integrations/ollama_/api_endpoints/model/show_model.py +36 -0
- lionagi/integrations/ollama_/api_endpoints/option_models.py +68 -0
- lionagi/integrations/openai_/OpenAIModel.py +418 -0
- lionagi/integrations/openai_/OpenAIService.py +426 -0
- lionagi/integrations/openai_/api_endpoints/__init__.py +3 -0
- lionagi/integrations/openai_/api_endpoints/api_request.py +277 -0
- lionagi/integrations/openai_/api_endpoints/audio/__init__.py +9 -0
- lionagi/integrations/openai_/api_endpoints/audio/speech_models.py +34 -0
- lionagi/integrations/openai_/api_endpoints/audio/transcription_models.py +136 -0
- lionagi/integrations/openai_/api_endpoints/audio/translation_models.py +41 -0
- lionagi/integrations/openai_/api_endpoints/audio/types.py +41 -0
- lionagi/integrations/openai_/api_endpoints/batch/__init__.py +17 -0
- lionagi/integrations/openai_/api_endpoints/batch/batch_models.py +146 -0
- lionagi/integrations/openai_/api_endpoints/batch/cancel_batch.py +7 -0
- lionagi/integrations/openai_/api_endpoints/batch/create_batch.py +26 -0
- lionagi/integrations/openai_/api_endpoints/batch/list_batch.py +37 -0
- lionagi/integrations/openai_/api_endpoints/batch/request_object_models.py +65 -0
- lionagi/integrations/openai_/api_endpoints/batch/retrieve_batch.py +7 -0
- lionagi/integrations/openai_/api_endpoints/batch/types.py +4 -0
- lionagi/integrations/openai_/api_endpoints/chat_completions/__init__.py +1 -0
- lionagi/integrations/openai_/api_endpoints/chat_completions/request/__init__.py +39 -0
- lionagi/integrations/openai_/api_endpoints/chat_completions/request/message_models.py +121 -0
- lionagi/integrations/openai_/api_endpoints/chat_completions/request/request_body.py +221 -0
- lionagi/integrations/openai_/api_endpoints/chat_completions/request/response_format.py +71 -0
- lionagi/integrations/openai_/api_endpoints/chat_completions/request/stream_options.py +14 -0
- lionagi/integrations/openai_/api_endpoints/chat_completions/request/tool_choice_models.py +17 -0
- lionagi/integrations/openai_/api_endpoints/chat_completions/request/tool_models.py +54 -0
- lionagi/integrations/openai_/api_endpoints/chat_completions/request/types.py +18 -0
- lionagi/integrations/openai_/api_endpoints/chat_completions/response/choice_models.py +62 -0
- lionagi/integrations/openai_/api_endpoints/chat_completions/response/function_models.py +16 -0
- lionagi/integrations/openai_/api_endpoints/chat_completions/response/log_prob_models.py +47 -0
- lionagi/integrations/openai_/api_endpoints/chat_completions/response/message_models.py +25 -0
- lionagi/integrations/openai_/api_endpoints/chat_completions/response/response_body.py +99 -0
- lionagi/integrations/openai_/api_endpoints/chat_completions/response/types.py +8 -0
- lionagi/integrations/openai_/api_endpoints/chat_completions/response/usage_models.py +24 -0
- lionagi/integrations/openai_/api_endpoints/chat_completions/util.py +46 -0
- lionagi/integrations/openai_/api_endpoints/data_models.py +23 -0
- lionagi/integrations/openai_/api_endpoints/embeddings/__init__.py +3 -0
- lionagi/integrations/openai_/api_endpoints/embeddings/request_body.py +79 -0
- lionagi/integrations/openai_/api_endpoints/embeddings/response_body.py +67 -0
- lionagi/integrations/openai_/api_endpoints/files/__init__.py +11 -0
- lionagi/integrations/openai_/api_endpoints/files/delete_file.py +20 -0
- lionagi/integrations/openai_/api_endpoints/files/file_models.py +56 -0
- lionagi/integrations/openai_/api_endpoints/files/list_files.py +27 -0
- lionagi/integrations/openai_/api_endpoints/files/retrieve_file.py +9 -0
- lionagi/integrations/openai_/api_endpoints/files/upload_file.py +38 -0
- lionagi/integrations/openai_/api_endpoints/fine_tuning/__init__.py +37 -0
- lionagi/integrations/openai_/api_endpoints/fine_tuning/cancel_jobs.py +9 -0
- lionagi/integrations/openai_/api_endpoints/fine_tuning/create_jobs.py +133 -0
- lionagi/integrations/openai_/api_endpoints/fine_tuning/fine_tuning_job_checkpoint_models.py +58 -0
- lionagi/integrations/openai_/api_endpoints/fine_tuning/fine_tuning_job_event_models.py +31 -0
- lionagi/integrations/openai_/api_endpoints/fine_tuning/fine_tuning_job_models.py +140 -0
- lionagi/integrations/openai_/api_endpoints/fine_tuning/list_fine_tuning_checkpoints.py +51 -0
- lionagi/integrations/openai_/api_endpoints/fine_tuning/list_fine_tuning_events.py +42 -0
- lionagi/integrations/openai_/api_endpoints/fine_tuning/list_fine_tuning_jobs.py +31 -0
- lionagi/integrations/openai_/api_endpoints/fine_tuning/retrieve_jobs.py +9 -0
- lionagi/integrations/openai_/api_endpoints/fine_tuning/training_format.py +30 -0
- lionagi/integrations/openai_/api_endpoints/images/__init__.py +9 -0
- lionagi/integrations/openai_/api_endpoints/images/image_edit_models.py +69 -0
- lionagi/integrations/openai_/api_endpoints/images/image_models.py +56 -0
- lionagi/integrations/openai_/api_endpoints/images/image_variation_models.py +56 -0
- lionagi/integrations/openai_/api_endpoints/images/response_body.py +30 -0
- lionagi/integrations/openai_/api_endpoints/match_data_model.py +197 -0
- lionagi/integrations/openai_/api_endpoints/match_response.py +336 -0
- lionagi/integrations/openai_/api_endpoints/models/__init__.py +7 -0
- lionagi/integrations/openai_/api_endpoints/models/delete_fine_tuned_model.py +17 -0
- lionagi/integrations/openai_/api_endpoints/models/models_models.py +31 -0
- lionagi/integrations/openai_/api_endpoints/models/retrieve_model.py +9 -0
- lionagi/integrations/openai_/api_endpoints/moderations/__init__.py +3 -0
- lionagi/integrations/openai_/api_endpoints/moderations/request_body.py +20 -0
- lionagi/integrations/openai_/api_endpoints/moderations/response_body.py +139 -0
- lionagi/integrations/openai_/api_endpoints/uploads/__init__.py +19 -0
- lionagi/integrations/openai_/api_endpoints/uploads/add_upload_part.py +11 -0
- lionagi/integrations/openai_/api_endpoints/uploads/cancel_upload.py +7 -0
- lionagi/integrations/openai_/api_endpoints/uploads/complete_upload.py +18 -0
- lionagi/integrations/openai_/api_endpoints/uploads/create_upload.py +17 -0
- lionagi/integrations/openai_/api_endpoints/uploads/uploads_models.py +52 -0
- lionagi/integrations/openai_/image_token_calculator/image_token_calculator.py +92 -0
- lionagi/integrations/openai_/image_token_calculator/openai_image_token_data.yaml +15 -0
- lionagi/integrations/openai_/openai_max_output_token_data.yaml +12 -0
- lionagi/integrations/openai_/openai_price_data.yaml +26 -0
- lionagi/integrations/openai_/version.py +1 -0
- lionagi/integrations/pandas_/__init__.py +24 -0
- lionagi/integrations/pandas_/extend_df.py +61 -0
- lionagi/integrations/pandas_/read.py +103 -0
- lionagi/integrations/pandas_/remove_rows.py +61 -0
- lionagi/integrations/pandas_/replace_keywords.py +65 -0
- lionagi/integrations/pandas_/save.py +131 -0
- lionagi/integrations/pandas_/search_keywords.py +69 -0
- lionagi/integrations/pandas_/to_df.py +196 -0
- lionagi/integrations/pandas_/update_cells.py +54 -0
- lionagi/integrations/perplexity_/PerplexityModel.py +269 -0
- lionagi/integrations/perplexity_/PerplexityService.py +113 -0
- lionagi/integrations/perplexity_/__init__.py +3 -0
- lionagi/integrations/perplexity_/api_endpoints/api_request.py +171 -0
- lionagi/integrations/perplexity_/api_endpoints/chat_completions/request/request_body.py +121 -0
- lionagi/integrations/perplexity_/api_endpoints/chat_completions/response/response_body.py +146 -0
- lionagi/integrations/perplexity_/api_endpoints/data_models.py +63 -0
- lionagi/integrations/perplexity_/api_endpoints/match_response.py +26 -0
- lionagi/integrations/perplexity_/perplexity_max_output_token_data.yaml +3 -0
- lionagi/integrations/perplexity_/perplexity_price_data.yaml +10 -0
- lionagi/integrations/perplexity_/version.py +1 -0
- lionagi/integrations/pydantic_/__init__.py +8 -0
- lionagi/integrations/pydantic_/break_down_annotation.py +81 -0
- lionagi/integrations/pydantic_/new_model.py +208 -0
- lionagi/integrations/services.py +17 -0
- lionagi/libs/__init__.py +0 -55
- lionagi/libs/compress/models.py +62 -0
- lionagi/libs/compress/utils.py +81 -0
- lionagi/libs/constants.py +98 -0
- lionagi/libs/file/chunk.py +265 -0
- lionagi/libs/file/file_ops.py +114 -0
- lionagi/libs/file/params.py +212 -0
- lionagi/libs/file/path.py +301 -0
- lionagi/libs/file/process.py +139 -0
- lionagi/libs/file/save.py +90 -0
- lionagi/libs/file/types.py +22 -0
- lionagi/libs/func/async_calls/__init__.py +21 -0
- lionagi/libs/func/async_calls/alcall.py +157 -0
- lionagi/libs/func/async_calls/bcall.py +82 -0
- lionagi/libs/func/async_calls/mcall.py +134 -0
- lionagi/libs/func/async_calls/pcall.py +149 -0
- lionagi/libs/func/async_calls/rcall.py +185 -0
- lionagi/libs/func/async_calls/tcall.py +114 -0
- lionagi/libs/func/async_calls/ucall.py +85 -0
- lionagi/libs/func/decorators.py +277 -0
- lionagi/libs/func/lcall.py +57 -0
- lionagi/libs/func/params.py +64 -0
- lionagi/libs/func/throttle.py +119 -0
- lionagi/libs/func/types.py +39 -0
- lionagi/libs/func/utils.py +96 -0
- lionagi/libs/package/imports.py +162 -0
- lionagi/libs/package/management.py +58 -0
- lionagi/libs/package/params.py +26 -0
- lionagi/libs/package/system.py +18 -0
- lionagi/libs/package/types.py +26 -0
- lionagi/libs/parse/__init__.py +1 -0
- lionagi/libs/parse/flatten/__init__.py +9 -0
- lionagi/libs/parse/flatten/flatten.py +168 -0
- lionagi/libs/parse/flatten/params.py +52 -0
- lionagi/libs/parse/flatten/unflatten.py +79 -0
- lionagi/libs/parse/json/__init__.py +27 -0
- lionagi/libs/parse/json/as_readable.py +104 -0
- lionagi/libs/parse/json/extract.py +102 -0
- lionagi/libs/parse/json/parse.py +179 -0
- lionagi/libs/parse/json/schema.py +227 -0
- lionagi/libs/parse/json/to_json.py +71 -0
- lionagi/libs/parse/nested/__init__.py +33 -0
- lionagi/libs/parse/nested/nfilter.py +55 -0
- lionagi/libs/parse/nested/nget.py +40 -0
- lionagi/libs/parse/nested/ninsert.py +103 -0
- lionagi/libs/parse/nested/nmerge.py +155 -0
- lionagi/libs/parse/nested/npop.py +66 -0
- lionagi/libs/parse/nested/nset.py +89 -0
- lionagi/libs/parse/nested/to_flat_list.py +64 -0
- lionagi/libs/parse/nested/utils.py +185 -0
- lionagi/libs/parse/string_parse/__init__.py +11 -0
- lionagi/libs/parse/string_parse/code_block.py +73 -0
- lionagi/libs/parse/string_parse/docstring.py +179 -0
- lionagi/libs/parse/string_parse/function_.py +92 -0
- lionagi/libs/parse/type_convert/__init__.py +19 -0
- lionagi/libs/parse/type_convert/params.py +145 -0
- lionagi/libs/parse/type_convert/to_dict.py +333 -0
- lionagi/libs/parse/type_convert/to_list.py +186 -0
- lionagi/libs/parse/type_convert/to_num.py +358 -0
- lionagi/libs/parse/type_convert/to_str.py +195 -0
- lionagi/libs/parse/types.py +9 -0
- lionagi/libs/parse/validate/__init__.py +14 -0
- lionagi/libs/parse/validate/boolean.py +96 -0
- lionagi/libs/parse/validate/keys.py +150 -0
- lionagi/libs/parse/validate/mapping.py +109 -0
- lionagi/libs/parse/validate/params.py +62 -0
- lionagi/libs/parse/xml/__init__.py +10 -0
- lionagi/libs/parse/xml/convert.py +56 -0
- lionagi/libs/parse/xml/parser.py +93 -0
- lionagi/libs/string_similarity/__init__.py +32 -0
- lionagi/libs/string_similarity/algorithms.py +219 -0
- lionagi/libs/string_similarity/matcher.py +102 -0
- lionagi/libs/string_similarity/utils.py +15 -0
- lionagi/libs/utils.py +255 -0
- lionagi/operations/__init__.py +3 -6
- lionagi/operations/brainstorm/__init__.py +3 -0
- lionagi/operations/brainstorm/brainstorm.py +204 -0
- lionagi/operations/brainstorm/prompt.py +1 -0
- lionagi/operations/plan/__init__.py +3 -0
- lionagi/operations/plan/plan.py +172 -0
- lionagi/operations/plan/prompt.py +21 -0
- lionagi/operations/select/__init__.py +3 -0
- lionagi/operations/select/prompt.py +1 -0
- lionagi/operations/select/select.py +100 -0
- lionagi/operations/select/utils.py +107 -0
- lionagi/operations/utils.py +35 -0
- lionagi/protocols/adapters/adapter.py +79 -0
- lionagi/protocols/adapters/json_adapter.py +43 -0
- lionagi/protocols/adapters/pandas_adapter.py +96 -0
- lionagi/protocols/configs/__init__.py +15 -0
- lionagi/protocols/configs/branch_config.py +86 -0
- lionagi/protocols/configs/id_config.py +15 -0
- lionagi/protocols/configs/imodel_config.py +73 -0
- lionagi/protocols/configs/log_config.py +93 -0
- lionagi/protocols/configs/retry_config.py +29 -0
- lionagi/protocols/operatives/__init__.py +15 -0
- lionagi/protocols/operatives/action.py +181 -0
- lionagi/protocols/operatives/instruct.py +196 -0
- lionagi/protocols/operatives/operative.py +182 -0
- lionagi/protocols/operatives/prompts.py +232 -0
- lionagi/protocols/operatives/reason.py +56 -0
- lionagi/protocols/operatives/step.py +217 -0
- lionagi/protocols/registries/_component_registry.py +19 -0
- lionagi/protocols/registries/_pile_registry.py +26 -0
- lionagi/service/__init__.py +13 -0
- lionagi/service/complete_request_info.py +11 -0
- lionagi/service/imodel.py +125 -0
- lionagi/service/rate_limiter.py +108 -0
- lionagi/service/service.py +41 -0
- lionagi/service/service_match_util.py +131 -0
- lionagi/service/service_util.py +72 -0
- lionagi/service/token_calculator.py +51 -0
- lionagi/settings.py +136 -0
- lionagi/strategies/base.py +53 -0
- lionagi/strategies/concurrent.py +71 -0
- lionagi/strategies/concurrent_chunk.py +43 -0
- lionagi/strategies/concurrent_sequential_chunk.py +104 -0
- lionagi/strategies/params.py +128 -0
- lionagi/strategies/sequential.py +23 -0
- lionagi/strategies/sequential_chunk.py +89 -0
- lionagi/strategies/sequential_concurrent_chunk.py +100 -0
- lionagi/strategies/types.py +21 -0
- lionagi/strategies/utils.py +49 -0
- lionagi/version.py +1 -1
- lionagi-0.5.1.dist-info/METADATA +545 -0
- lionagi-0.5.1.dist-info/RECORD +373 -0
- {lionagi-0.4.0.dist-info → lionagi-0.5.1.dist-info}/WHEEL +1 -1
- lionagi/core/_setting/_setting.py +0 -59
- lionagi/core/action/README.md +0 -20
- lionagi/core/action/manual.py +0 -1
- lionagi/core/action/node.py +0 -94
- lionagi/core/action/tool_manager.py +0 -342
- lionagi/core/agent/README.md +0 -1
- lionagi/core/agent/base_agent.py +0 -82
- lionagi/core/agent/eval/README.md +0 -1
- lionagi/core/agent/eval/evaluator.py +0 -1
- lionagi/core/agent/eval/vote.py +0 -40
- lionagi/core/agent/learn/learner.py +0 -59
- lionagi/core/agent/plan/unit_template.py +0 -1
- lionagi/core/collections/README.md +0 -23
- lionagi/core/collections/__init__.py +0 -16
- lionagi/core/collections/_logger.py +0 -312
- lionagi/core/collections/abc/README.md +0 -63
- lionagi/core/collections/abc/__init__.py +0 -53
- lionagi/core/collections/abc/component.py +0 -620
- lionagi/core/collections/abc/concepts.py +0 -277
- lionagi/core/collections/abc/exceptions.py +0 -136
- lionagi/core/collections/abc/util.py +0 -45
- lionagi/core/collections/exchange.py +0 -146
- lionagi/core/collections/flow.py +0 -416
- lionagi/core/collections/model.py +0 -465
- lionagi/core/collections/pile.py +0 -1232
- lionagi/core/collections/progression.py +0 -221
- lionagi/core/collections/util.py +0 -73
- lionagi/core/director/README.md +0 -1
- lionagi/core/director/direct.py +0 -298
- lionagi/core/director/director.py +0 -2
- lionagi/core/director/operations/select.py +0 -3
- lionagi/core/director/operations/utils.py +0 -6
- lionagi/core/engine/branch_engine.py +0 -361
- lionagi/core/engine/instruction_map_engine.py +0 -213
- lionagi/core/engine/sandbox_.py +0 -16
- lionagi/core/engine/script_engine.py +0 -89
- lionagi/core/executor/base_executor.py +0 -97
- lionagi/core/executor/graph_executor.py +0 -335
- lionagi/core/executor/neo4j_executor.py +0 -394
- lionagi/core/generic/README.md +0 -0
- lionagi/core/generic/edge_condition.py +0 -17
- lionagi/core/generic/hyperedge.py +0 -1
- lionagi/core/generic/tree.py +0 -49
- lionagi/core/generic/tree_node.py +0 -85
- lionagi/core/mail/__init__.py +0 -11
- lionagi/core/mail/mail.py +0 -26
- lionagi/core/mail/mail_manager.py +0 -185
- lionagi/core/mail/package.py +0 -49
- lionagi/core/mail/start_mail.py +0 -36
- lionagi/core/message/__init__.py +0 -18
- lionagi/core/message/action_request.py +0 -114
- lionagi/core/message/action_response.py +0 -121
- lionagi/core/message/assistant_response.py +0 -80
- lionagi/core/message/instruction.py +0 -194
- lionagi/core/message/message.py +0 -86
- lionagi/core/message/system.py +0 -71
- lionagi/core/message/util.py +0 -274
- lionagi/core/report/__init__.py +0 -4
- lionagi/core/report/base.py +0 -201
- lionagi/core/report/form.py +0 -212
- lionagi/core/report/report.py +0 -150
- lionagi/core/report/util.py +0 -15
- lionagi/core/rule/_default.py +0 -17
- lionagi/core/rule/action.py +0 -87
- lionagi/core/rule/base.py +0 -234
- lionagi/core/rule/boolean.py +0 -56
- lionagi/core/rule/choice.py +0 -48
- lionagi/core/rule/mapping.py +0 -82
- lionagi/core/rule/number.py +0 -73
- lionagi/core/rule/rulebook.py +0 -45
- lionagi/core/rule/string.py +0 -43
- lionagi/core/rule/util.py +0 -0
- lionagi/core/session/directive_mixin.py +0 -307
- lionagi/core/structure/__init__.py +0 -1
- lionagi/core/structure/chain.py +0 -1
- lionagi/core/structure/forest.py +0 -1
- lionagi/core/structure/graph.py +0 -1
- lionagi/core/structure/tree.py +0 -1
- lionagi/core/unit/__init__.py +0 -4
- lionagi/core/unit/parallel_unit.py +0 -234
- lionagi/core/unit/template/action.py +0 -65
- lionagi/core/unit/template/base.py +0 -35
- lionagi/core/unit/template/plan.py +0 -69
- lionagi/core/unit/template/predict.py +0 -95
- lionagi/core/unit/template/score.py +0 -108
- lionagi/core/unit/template/select.py +0 -91
- lionagi/core/unit/unit.py +0 -452
- lionagi/core/unit/unit_form.py +0 -290
- lionagi/core/unit/unit_mixin.py +0 -1166
- lionagi/core/unit/util.py +0 -103
- lionagi/core/validator/validator.py +0 -376
- lionagi/core/work/work.py +0 -59
- lionagi/core/work/work_edge.py +0 -102
- lionagi/core/work/work_function.py +0 -114
- lionagi/core/work/work_function_node.py +0 -50
- lionagi/core/work/work_queue.py +0 -90
- lionagi/core/work/work_task.py +0 -151
- lionagi/core/work/worker.py +0 -410
- lionagi/core/work/worker_engine.py +0 -208
- lionagi/core/work/worklog.py +0 -108
- lionagi/experimental/compressor/base.py +0 -47
- lionagi/experimental/compressor/llm_compressor.py +0 -265
- lionagi/experimental/compressor/llm_summarizer.py +0 -61
- lionagi/experimental/compressor/util.py +0 -70
- lionagi/experimental/directive/README.md +0 -1
- lionagi/experimental/directive/__init__.py +0 -19
- lionagi/experimental/directive/parser/base_parser.py +0 -294
- lionagi/experimental/directive/parser/base_syntax.txt +0 -200
- lionagi/experimental/directive/template/base_template.py +0 -71
- lionagi/experimental/directive/template/schema.py +0 -36
- lionagi/experimental/directive/tokenizer.py +0 -59
- lionagi/experimental/evaluator/README.md +0 -1
- lionagi/experimental/evaluator/ast_evaluator.py +0 -119
- lionagi/experimental/evaluator/base_evaluator.py +0 -213
- lionagi/experimental/knowledge/__init__.py +0 -0
- lionagi/experimental/knowledge/base.py +0 -10
- lionagi/experimental/knowledge/graph.py +0 -0
- lionagi/experimental/memory/__init__.py +0 -0
- lionagi/experimental/strategies/__init__.py +0 -0
- lionagi/experimental/strategies/base.py +0 -1
- lionagi/integrations/bridge/__init__.py +0 -4
- lionagi/integrations/bridge/autogen_/__init__.py +0 -0
- lionagi/integrations/bridge/autogen_/autogen_.py +0 -127
- lionagi/integrations/bridge/langchain_/__init__.py +0 -0
- lionagi/integrations/bridge/langchain_/documents.py +0 -138
- lionagi/integrations/bridge/langchain_/langchain_bridge.py +0 -68
- lionagi/integrations/bridge/llamaindex_/__init__.py +0 -0
- lionagi/integrations/bridge/llamaindex_/index.py +0 -36
- lionagi/integrations/bridge/llamaindex_/llama_index_bridge.py +0 -108
- lionagi/integrations/bridge/llamaindex_/llama_pack.py +0 -256
- lionagi/integrations/bridge/llamaindex_/node_parser.py +0 -92
- lionagi/integrations/bridge/llamaindex_/reader.py +0 -201
- lionagi/integrations/bridge/llamaindex_/textnode.py +0 -59
- lionagi/integrations/bridge/pydantic_/__init__.py +0 -0
- lionagi/integrations/bridge/pydantic_/pydantic_bridge.py +0 -7
- lionagi/integrations/bridge/transformers_/__init__.py +0 -0
- lionagi/integrations/bridge/transformers_/install_.py +0 -39
- lionagi/integrations/chunker/__init__.py +0 -0
- lionagi/integrations/chunker/chunk.py +0 -314
- lionagi/integrations/config/__init__.py +0 -4
- lionagi/integrations/config/mlx_configs.py +0 -1
- lionagi/integrations/config/oai_configs.py +0 -154
- lionagi/integrations/config/ollama_configs.py +0 -1
- lionagi/integrations/config/openrouter_configs.py +0 -74
- lionagi/integrations/langchain_/__init__.py +0 -0
- lionagi/integrations/llamaindex_/__init__.py +0 -0
- lionagi/integrations/loader/__init__.py +0 -0
- lionagi/integrations/loader/load.py +0 -257
- lionagi/integrations/loader/load_util.py +0 -214
- lionagi/integrations/provider/__init__.py +0 -11
- lionagi/integrations/provider/_mapping.py +0 -47
- lionagi/integrations/provider/litellm.py +0 -53
- lionagi/integrations/provider/mistralai.py +0 -1
- lionagi/integrations/provider/mlx_service.py +0 -55
- lionagi/integrations/provider/oai.py +0 -196
- lionagi/integrations/provider/ollama.py +0 -55
- lionagi/integrations/provider/openrouter.py +0 -170
- lionagi/integrations/provider/services.py +0 -138
- lionagi/integrations/provider/transformers.py +0 -108
- lionagi/integrations/storage/__init__.py +0 -3
- lionagi/integrations/storage/neo4j.py +0 -681
- lionagi/integrations/storage/storage_util.py +0 -302
- lionagi/integrations/storage/structure_excel.py +0 -291
- lionagi/integrations/storage/to_csv.py +0 -70
- lionagi/integrations/storage/to_excel.py +0 -91
- lionagi/libs/ln_api.py +0 -944
- lionagi/libs/ln_async.py +0 -208
- lionagi/libs/ln_context.py +0 -37
- lionagi/libs/ln_convert.py +0 -671
- lionagi/libs/ln_dataframe.py +0 -187
- lionagi/libs/ln_func_call.py +0 -1328
- lionagi/libs/ln_image.py +0 -114
- lionagi/libs/ln_knowledge_graph.py +0 -422
- lionagi/libs/ln_nested.py +0 -822
- lionagi/libs/ln_parse.py +0 -750
- lionagi/libs/ln_queue.py +0 -107
- lionagi/libs/ln_tokenize.py +0 -179
- lionagi/libs/ln_validate.py +0 -299
- lionagi/libs/special_tokens.py +0 -172
- lionagi/libs/sys_util.py +0 -710
- lionagi/lions/__init__.py +0 -0
- lionagi/lions/coder/__init__.py +0 -0
- lionagi/lions/coder/add_feature.py +0 -20
- lionagi/lions/coder/base_prompts.py +0 -22
- lionagi/lions/coder/code_form.py +0 -15
- lionagi/lions/coder/coder.py +0 -184
- lionagi/lions/coder/util.py +0 -101
- lionagi/lions/director/__init__.py +0 -0
- lionagi/lions/judge/__init__.py +0 -0
- lionagi/lions/judge/config.py +0 -8
- lionagi/lions/judge/data/__init__.py +0 -0
- lionagi/lions/judge/data/sample_codes.py +0 -526
- lionagi/lions/judge/data/sample_rurbic.py +0 -48
- lionagi/lions/judge/forms/__init__.py +0 -0
- lionagi/lions/judge/forms/code_analysis_form.py +0 -126
- lionagi/lions/judge/rubric.py +0 -34
- lionagi/lions/judge/services/__init__.py +0 -0
- lionagi/lions/judge/services/judge_code.py +0 -49
- lionagi/lions/researcher/__init__.py +0 -0
- lionagi/lions/researcher/data_source/__init__.py +0 -0
- lionagi/lions/researcher/data_source/finhub_.py +0 -192
- lionagi/lions/researcher/data_source/google_.py +0 -207
- lionagi/lions/researcher/data_source/wiki_.py +0 -98
- lionagi/lions/researcher/data_source/yfinance_.py +0 -21
- lionagi/operations/brainstorm.py +0 -87
- lionagi/operations/config.py +0 -6
- lionagi/operations/rank.py +0 -102
- lionagi/operations/score.py +0 -144
- lionagi/operations/select.py +0 -141
- lionagi-0.4.0.dist-info/METADATA +0 -241
- lionagi-0.4.0.dist-info/RECORD +0 -249
- /lionagi/{core/_setting → integrations/anthropic_/api_endpoints/messages/response}/__init__.py +0 -0
- /lionagi/{core/agent → integrations/groq_/api_endpoints}/__init__.py +0 -0
- /lionagi/{core/agent/eval → integrations/ollama_/api_endpoints/completion}/__init__.py +0 -0
- /lionagi/{core/agent/learn → integrations/ollama_/api_endpoints/embedding}/__init__.py +0 -0
- /lionagi/{core/agent/plan → integrations/openai_}/__init__.py +0 -0
- /lionagi/{core/director → integrations/openai_/api_endpoints/chat_completions/response}/__init__.py +0 -0
- /lionagi/{core/director/operations → integrations/openai_/image_token_calculator}/__init__.py +0 -0
- /lionagi/{core/engine → integrations/perplexity_/api_endpoints}/__init__.py +0 -0
- /lionagi/{core/executor → integrations/perplexity_/api_endpoints/chat_completions}/__init__.py +0 -0
- /lionagi/{core/generic/registry/component_registry → integrations/perplexity_/api_endpoints/chat_completions/request}/__init__.py +0 -0
- /lionagi/{core/rule → integrations/perplexity_/api_endpoints/chat_completions/response}/__init__.py +0 -0
- /lionagi/{core/unit/template → libs/compress}/__init__.py +0 -0
- /lionagi/{core/validator → libs/file}/__init__.py +0 -0
- /lionagi/{core/work → libs/func}/__init__.py +0 -0
- /lionagi/{experimental → libs/package}/__init__.py +0 -0
- /lionagi/{core/agent/plan/plan.py → libs/parse/params.py} +0 -0
- /lionagi/{experimental/compressor → protocols}/__init__.py +0 -0
- /lionagi/{experimental/directive/parser → protocols/adapters}/__init__.py +0 -0
- /lionagi/{experimental/directive/template → protocols/registries}/__init__.py +0 -0
- /lionagi/{experimental/evaluator → strategies}/__init__.py +0 -0
- {lionagi-0.4.0.dist-info → lionagi-0.5.1.dist-info/licenses}/LICENSE +0 -0
lionagi/libs/ln_api.py
DELETED
@@ -1,944 +0,0 @@
|
|
1
|
-
import asyncio
|
2
|
-
import contextlib
|
3
|
-
import logging
|
4
|
-
import re
|
5
|
-
from abc import ABC
|
6
|
-
from collections.abc import Callable, Mapping, Sequence
|
7
|
-
from dataclasses import dataclass
|
8
|
-
from typing import Any, NoReturn, Type
|
9
|
-
|
10
|
-
import aiohttp
|
11
|
-
|
12
|
-
import lionagi.libs.ln_func_call as func_call
|
13
|
-
from lionagi.libs.ln_async import AsyncUtil
|
14
|
-
from lionagi.libs.ln_convert import strip_lower, to_dict, to_str
|
15
|
-
from lionagi.libs.ln_nested import nget
|
16
|
-
|
17
|
-
|
18
|
-
class APIUtil:
|
19
|
-
"""
|
20
|
-
A utility class for assisting with common API usage patterns.
|
21
|
-
"""
|
22
|
-
|
23
|
-
@staticmethod
|
24
|
-
def api_method(
|
25
|
-
http_session: aiohttp.ClientSession, method: str = "post"
|
26
|
-
) -> Callable:
|
27
|
-
"""
|
28
|
-
Returns the corresponding HTTP method function from the http_session object.
|
29
|
-
|
30
|
-
Args:
|
31
|
-
http_session: The session object from the aiohttp library.
|
32
|
-
method: The HTTP method as a string.
|
33
|
-
|
34
|
-
Returns:
|
35
|
-
The Callable for the specified HTTP method.
|
36
|
-
|
37
|
-
Raises:
|
38
|
-
ValueError: If the method is not one of the allowed ones.
|
39
|
-
|
40
|
-
Examples:
|
41
|
-
>>> session = aiohttp.ClientSession()
|
42
|
-
>>> post_method = APIUtil.api_method(session, "post")
|
43
|
-
>>> print(post_method)
|
44
|
-
<bound method ClientSession._request of <aiohttp.client.ClientSession object at 0x...>>
|
45
|
-
"""
|
46
|
-
if method in {"post", "delete", "head", "options", "patch"}:
|
47
|
-
return getattr(http_session, method)
|
48
|
-
else:
|
49
|
-
raise ValueError(
|
50
|
-
"Invalid request, method must be in ['post', 'delete', 'head', 'options', 'patch']"
|
51
|
-
)
|
52
|
-
|
53
|
-
@staticmethod
|
54
|
-
def api_error(response_json: Mapping[str, Any]) -> bool:
|
55
|
-
"""
|
56
|
-
Checks if the given response_json dictionary contains an "error" key.
|
57
|
-
|
58
|
-
Args:
|
59
|
-
response_json: The JSON assistant_response as a dictionary.
|
60
|
-
|
61
|
-
Returns:
|
62
|
-
True if there is an error, False otherwise.
|
63
|
-
|
64
|
-
Examples:
|
65
|
-
>>> response_json_with_error = {"error": "Something went wrong"}
|
66
|
-
>>> APIUtil.api_error(response_json_with_error)
|
67
|
-
True
|
68
|
-
>>> response_json_without_error = {"result": "Success"}
|
69
|
-
>>> APIUtil.api_error(response_json_without_error)
|
70
|
-
False
|
71
|
-
"""
|
72
|
-
if "error" in response_json:
|
73
|
-
logging.warning(
|
74
|
-
f"API call failed with error: {response_json['error']}"
|
75
|
-
)
|
76
|
-
return True
|
77
|
-
return False
|
78
|
-
|
79
|
-
@staticmethod
|
80
|
-
def api_rate_limit_error(response_json: Mapping[str, Any]) -> bool:
|
81
|
-
"""
|
82
|
-
Checks if the error message in the response_json dictionary contains the phrase "Rate limit".
|
83
|
-
|
84
|
-
Args:
|
85
|
-
response_json: The JSON assistant_response as a dictionary.
|
86
|
-
|
87
|
-
Returns:
|
88
|
-
True if the phrase "Rate limit" is found, False otherwise.
|
89
|
-
|
90
|
-
Examples:
|
91
|
-
>>> response_json_with_rate_limit = {"error": {"message": "Rate limit exceeded"}}
|
92
|
-
>>> api_rate_limit_error(response_json_with_rate_limit)
|
93
|
-
True
|
94
|
-
>>> response_json_without_rate_limit = {"error": {"message": "Another error"}}
|
95
|
-
>>> api_rate_limit_error(response_json_without_rate_limit)
|
96
|
-
False
|
97
|
-
"""
|
98
|
-
return "Rate limit" in response_json.get("error", {}).get(
|
99
|
-
"message", ""
|
100
|
-
)
|
101
|
-
|
102
|
-
@staticmethod
|
103
|
-
@func_call.lru_cache(maxsize=128)
|
104
|
-
def api_endpoint_from_url(request_url: str) -> str:
|
105
|
-
"""
|
106
|
-
Extracts the API endpoint from a given URL using a regular expression.
|
107
|
-
|
108
|
-
Args:
|
109
|
-
request_url: The full URL to the API endpoint.
|
110
|
-
|
111
|
-
Returns:
|
112
|
-
The extracted endpoint or an empty string if the pattern does not match.
|
113
|
-
|
114
|
-
Examples:
|
115
|
-
>>> valid_url = "https://api.example.com/v1/users"
|
116
|
-
>>> api_endpoint_from_url(valid_url)
|
117
|
-
'users'
|
118
|
-
>>> invalid_url = "https://api.example.com/users"
|
119
|
-
>>> api_endpoint_from_url(invalid_url)
|
120
|
-
''
|
121
|
-
"""
|
122
|
-
match = re.search(r"^https://[^/]+(/.+)?/v\d+/(.+)$", request_url)
|
123
|
-
return match[2] if match else ""
|
124
|
-
|
125
|
-
@staticmethod
|
126
|
-
async def unified_api_call(
|
127
|
-
http_session: aiohttp.ClientSession, method: str, url: str, **kwargs
|
128
|
-
) -> Any:
|
129
|
-
"""
|
130
|
-
Makes an API call and automatically retries on rate limit error.
|
131
|
-
|
132
|
-
Args:
|
133
|
-
http_session: The session object from the aiohttp library.
|
134
|
-
method: The HTTP method as a string.
|
135
|
-
url: The URL to which the request is made.
|
136
|
-
**kwargs: Additional keyword arguments to pass to the API call.
|
137
|
-
|
138
|
-
Returns:
|
139
|
-
The JSON assistant_response as a dictionary.
|
140
|
-
|
141
|
-
Examples:
|
142
|
-
>>> session = aiohttp.ClientSession()
|
143
|
-
>>> success_url = "https://api.example.com/v1/success"
|
144
|
-
>>> print(await unified_api_call(session, 'get', success_url))
|
145
|
-
{'result': 'Success'}
|
146
|
-
>>> rate_limit_url = "https://api.example.com/v1/rate_limit"
|
147
|
-
>>> print(await unified_api_call(session, 'get', rate_limit_url))
|
148
|
-
{'error': {'message': 'Rate limit exceeded'}}
|
149
|
-
"""
|
150
|
-
api_call = APIUtil.api_method(http_session, method)
|
151
|
-
retry_count = 3
|
152
|
-
retry_delay = 5 # seconds
|
153
|
-
|
154
|
-
for attempt in range(retry_count):
|
155
|
-
async with api_call(url, **kwargs) as response:
|
156
|
-
response_json = await response.json()
|
157
|
-
|
158
|
-
if not APIUtil.api_error(response_json):
|
159
|
-
return response_json
|
160
|
-
|
161
|
-
if (
|
162
|
-
APIUtil.api_rate_limit_error(response_json)
|
163
|
-
and attempt < retry_count - 1
|
164
|
-
):
|
165
|
-
logging.warning(
|
166
|
-
f"Rate limit error detected. Retrying in {retry_delay} seconds..."
|
167
|
-
)
|
168
|
-
await AsyncUtil.sleep(retry_delay)
|
169
|
-
else:
|
170
|
-
break
|
171
|
-
|
172
|
-
return response_json
|
173
|
-
|
174
|
-
@staticmethod
|
175
|
-
def get_cache_key(url: str, params: Mapping[str, Any] | None) -> str:
|
176
|
-
"""
|
177
|
-
Creates a unique cache key based on the URL and parameters.
|
178
|
-
"""
|
179
|
-
import hashlib
|
180
|
-
|
181
|
-
param_str = to_str(params, sort_keys=True) if params else ""
|
182
|
-
return hashlib.md5((url + param_str).encode("utf-8")).hexdigest()
|
183
|
-
|
184
|
-
@staticmethod
|
185
|
-
async def retry_api_call(
|
186
|
-
http_session: aiohttp.ClientSession,
|
187
|
-
url: str,
|
188
|
-
retries: int = 3,
|
189
|
-
backoff_factor: float = 0.5,
|
190
|
-
**kwargs,
|
191
|
-
) -> Any:
|
192
|
-
"""
|
193
|
-
Retries an API call on failure, with exponential backoff.
|
194
|
-
|
195
|
-
Args:
|
196
|
-
http_session: The aiohttp client session.
|
197
|
-
url: The URL to make the API call.
|
198
|
-
retries: The number of times to retry.
|
199
|
-
backoff_factor: The backoff factor for retries.
|
200
|
-
**kwargs: Additional arguments for the API call.
|
201
|
-
|
202
|
-
Returns:
|
203
|
-
The assistant_response from the API call, if successful; otherwise, None.
|
204
|
-
"""
|
205
|
-
for attempt in range(retries):
|
206
|
-
try:
|
207
|
-
async with http_session.get(url, **kwargs) as response:
|
208
|
-
response.raise_for_status()
|
209
|
-
return await response.json()
|
210
|
-
except aiohttp.ClientError:
|
211
|
-
if attempt < retries - 1:
|
212
|
-
delay = backoff_factor * (2**attempt)
|
213
|
-
logging.info(f"Retrying {url} in {delay} seconds...")
|
214
|
-
await AsyncUtil.sleep(delay)
|
215
|
-
else:
|
216
|
-
logging.error(
|
217
|
-
f"Failed to retrieve data from {url} after {retries} attempts."
|
218
|
-
)
|
219
|
-
return None
|
220
|
-
|
221
|
-
@staticmethod
|
222
|
-
async def upload_file_with_retry(
|
223
|
-
http_session: aiohttp.ClientSession,
|
224
|
-
url: str,
|
225
|
-
file_path: str,
|
226
|
-
param_name: str = "file",
|
227
|
-
additional_data: Mapping[str, Any] = None,
|
228
|
-
retries: int = 3,
|
229
|
-
) -> Any:
|
230
|
-
"""
|
231
|
-
Uploads a file to a specified URL with a retry mechanism for handling failures.
|
232
|
-
|
233
|
-
Args:
|
234
|
-
http_session: The HTTP session object to use for making the request.
|
235
|
-
url: The URL to which the file will be uploaded.
|
236
|
-
file_path: The path to the file that will be uploaded.
|
237
|
-
param_name: The name of the parameter expected by the server for the file upload.
|
238
|
-
additional_data: Additional data to be sent with the upload.
|
239
|
-
retries: The number of times to retry the upload in case of failure.
|
240
|
-
|
241
|
-
Returns:
|
242
|
-
The HTTP assistant_response object.
|
243
|
-
|
244
|
-
Examples:
|
245
|
-
>>> session = aiohttp.ClientSession()
|
246
|
-
>>> assistant_response = await APIUtil.upload_file_with_retry(session, 'http://example.com/upload', 'path/to/file.txt')
|
247
|
-
>>> assistant_response.status
|
248
|
-
200
|
249
|
-
"""
|
250
|
-
for attempt in range(retries):
|
251
|
-
try:
|
252
|
-
with open(file_path, "rb") as file:
|
253
|
-
files = {param_name: file}
|
254
|
-
additional_data = additional_data or {}
|
255
|
-
async with http_session.post(
|
256
|
-
url, data={**files, **additional_data}
|
257
|
-
) as response:
|
258
|
-
response.raise_for_status()
|
259
|
-
return await response.json()
|
260
|
-
except aiohttp.ClientError as e:
|
261
|
-
if attempt == retries - 1:
|
262
|
-
raise e
|
263
|
-
backoff = 2**attempt
|
264
|
-
logging.info(f"Retrying {url} in {backoff} seconds...")
|
265
|
-
await AsyncUtil.sleep(backoff)
|
266
|
-
|
267
|
-
@staticmethod
|
268
|
-
@AsyncUtil.cached(ttl=10 * 60) # Cache the result for 10 minutes
|
269
|
-
async def get_oauth_token_with_cache(
|
270
|
-
http_session: aiohttp.ClientSession,
|
271
|
-
auth_url: str,
|
272
|
-
client_id: str,
|
273
|
-
client_secret: str,
|
274
|
-
scope: str,
|
275
|
-
) -> str:
|
276
|
-
"""
|
277
|
-
Retrieves an OAuth token from the authentication server and caches it to avoid unnecessary requests.
|
278
|
-
|
279
|
-
Args:
|
280
|
-
http_session: The HTTP session object to use for making the request.
|
281
|
-
auth_url: The URL of the authentication server.
|
282
|
-
client_id: The client ID for OAuth authentication.
|
283
|
-
client_secret: The client secret for OAuth authentication.
|
284
|
-
scope: The scope for which the OAuth token is requested.
|
285
|
-
|
286
|
-
Returns:
|
287
|
-
The OAuth token as a string.
|
288
|
-
|
289
|
-
Examples:
|
290
|
-
>>> session = aiohttp.ClientSession()
|
291
|
-
>>> token = await APIUtil.get_oauth_token_with_cache(session, 'http://auth.example.com', 'client_id', 'client_secret', 'read')
|
292
|
-
>>> token
|
293
|
-
'mock_access_token'
|
294
|
-
"""
|
295
|
-
async with http_session.post(
|
296
|
-
auth_url,
|
297
|
-
data={
|
298
|
-
"grant_type": "client_credentials",
|
299
|
-
"client_id": client_id,
|
300
|
-
"client_secret": client_secret,
|
301
|
-
"scope": scope,
|
302
|
-
},
|
303
|
-
) as auth_response:
|
304
|
-
auth_response.raise_for_status()
|
305
|
-
return (await auth_response.json()).get("access_token")
|
306
|
-
|
307
|
-
@staticmethod
|
308
|
-
@AsyncUtil.cached(ttl=10 * 60)
|
309
|
-
async def cached_api_call(
|
310
|
-
http_session: aiohttp.ClientSession, url: str, **kwargs
|
311
|
-
) -> Any:
|
312
|
-
"""
|
313
|
-
Makes an API call.
|
314
|
-
|
315
|
-
Args:
|
316
|
-
http_session: The aiohttp client session.
|
317
|
-
url: The URL for the API call.
|
318
|
-
**kwargs: Additional arguments for the API call.
|
319
|
-
|
320
|
-
Returns:
|
321
|
-
The assistant_response from the API call, if successful; otherwise, None.
|
322
|
-
"""
|
323
|
-
try:
|
324
|
-
async with http_session.get(url, **kwargs) as response:
|
325
|
-
response.raise_for_status()
|
326
|
-
return await response.json()
|
327
|
-
except aiohttp.ClientError as e:
|
328
|
-
logging.error(f"API call to {url} failed: {e}")
|
329
|
-
return None
|
330
|
-
|
331
|
-
@staticmethod
|
332
|
-
def calculate_num_token(
|
333
|
-
payload: Mapping[str, Any] = None,
|
334
|
-
api_endpoint: str = None,
|
335
|
-
token_encoding_name: str = None,
|
336
|
-
) -> int: # sourcery skip: avoid-builtin-shadow
|
337
|
-
"""
|
338
|
-
Calculates the number of tokens required for a request based on the payload and API endpoint.
|
339
|
-
|
340
|
-
The token calculation logic might vary based on different API endpoints and payload content.
|
341
|
-
This method should be implemented in a subclass to provide the specific calculation logic
|
342
|
-
for the OpenAI API.
|
343
|
-
|
344
|
-
Parameters:
|
345
|
-
payload (Mapping[str, Any]): The payload of the request.
|
346
|
-
|
347
|
-
api_endpoint (str): The specific API endpoint for the request.
|
348
|
-
|
349
|
-
token_encoding_name (str): The name of the token encoding method.
|
350
|
-
|
351
|
-
Returns:
|
352
|
-
int: The estimated number of tokens required for the request.
|
353
|
-
|
354
|
-
Example:
|
355
|
-
>>> rate_limiter = OpenAIRateLimiter(100, 200)
|
356
|
-
>>> payload = {'prompt': 'Translate the following text:', 'max_tokens': 50}
|
357
|
-
>>> rate_limiter.calculate_num_token(payload, 'completions')
|
358
|
-
# Expected token calculation for the given payload and endpoint.
|
359
|
-
"""
|
360
|
-
import tiktoken
|
361
|
-
|
362
|
-
from .ln_image import ImageUtil
|
363
|
-
|
364
|
-
token_encoding_name = token_encoding_name or "cl100k_base"
|
365
|
-
encoding = tiktoken.get_encoding(token_encoding_name)
|
366
|
-
if api_endpoint.endswith("completions"):
|
367
|
-
max_tokens = payload.get("max_tokens", 15)
|
368
|
-
n = payload.get("n", 1)
|
369
|
-
completion_tokens = n * max_tokens
|
370
|
-
if api_endpoint.startswith("chat/"):
|
371
|
-
num_tokens = 0
|
372
|
-
|
373
|
-
for message in payload["messages"]:
|
374
|
-
num_tokens += 4 # every message follows <im_start>{role/name}\n{content}<im_end>\n
|
375
|
-
|
376
|
-
_content = message.get("content")
|
377
|
-
if isinstance(_content, str):
|
378
|
-
num_tokens += len(encoding.encode(_content))
|
379
|
-
|
380
|
-
elif isinstance(_content, list):
|
381
|
-
for item in _content:
|
382
|
-
if isinstance(item, dict):
|
383
|
-
if "text" in item:
|
384
|
-
num_tokens += len(
|
385
|
-
encoding.encode(to_str(item["text"]))
|
386
|
-
)
|
387
|
-
elif "image_url" in item:
|
388
|
-
a: str = item["image_url"]["url"]
|
389
|
-
if "data:image/jpeg;base64," in a:
|
390
|
-
a = a.split("data:image/jpeg;base64,")[
|
391
|
-
1
|
392
|
-
].strip()
|
393
|
-
num_tokens += ImageUtil.calculate_image_token_usage_from_base64(
|
394
|
-
a, item.get("detail", "low")
|
395
|
-
)
|
396
|
-
num_tokens += 20 # for every image we add 20 tokens buffer
|
397
|
-
elif isinstance(item, str):
|
398
|
-
num_tokens += len(encoding.encode(item))
|
399
|
-
else:
|
400
|
-
num_tokens += len(encoding.encode(str(item)))
|
401
|
-
|
402
|
-
num_tokens += (
|
403
|
-
2 # every reply is primed with <im_start>assistant
|
404
|
-
)
|
405
|
-
return num_tokens + completion_tokens
|
406
|
-
else:
|
407
|
-
prompt = payload["format_prompt"]
|
408
|
-
if isinstance(prompt, str): # single format_prompt
|
409
|
-
prompt_tokens = len(encoding.encode(prompt))
|
410
|
-
return prompt_tokens + completion_tokens
|
411
|
-
elif isinstance(prompt, list): # multiple prompts
|
412
|
-
prompt_tokens = sum(
|
413
|
-
len(encoding.encode(p)) for p in prompt
|
414
|
-
)
|
415
|
-
return prompt_tokens + completion_tokens * len(prompt)
|
416
|
-
else:
|
417
|
-
raise TypeError(
|
418
|
-
'Expecting either string or list of strings for "format_prompt" field in completion request'
|
419
|
-
)
|
420
|
-
elif api_endpoint == "embeddings":
|
421
|
-
input = payload["input"]
|
422
|
-
if isinstance(input, str): # single input
|
423
|
-
return len(encoding.encode(input))
|
424
|
-
elif isinstance(input, list): # multiple inputs
|
425
|
-
return sum(len(encoding.encode(i)) for i in input)
|
426
|
-
else:
|
427
|
-
raise TypeError(
|
428
|
-
'Expecting either string or list of strings for "inputs" field in embedding request'
|
429
|
-
)
|
430
|
-
else:
|
431
|
-
raise NotImplementedError(
|
432
|
-
f'API endpoint "{api_endpoint}" not implemented in this script'
|
433
|
-
)
|
434
|
-
|
435
|
-
@staticmethod
|
436
|
-
def create_payload(
|
437
|
-
input_, config, required_, optional_, input_key, **kwargs
|
438
|
-
):
|
439
|
-
config = {**config, **kwargs}
|
440
|
-
payload = {input_key: input_}
|
441
|
-
|
442
|
-
for key in required_:
|
443
|
-
payload[key] = config[key]
|
444
|
-
|
445
|
-
for key in optional_:
|
446
|
-
if bool(config[key]) and strip_lower(config[key]) != "none":
|
447
|
-
payload[key] = config[key]
|
448
|
-
|
449
|
-
return payload
|
450
|
-
|
451
|
-
|
452
|
-
@dataclass
|
453
|
-
class StatusTracker:
|
454
|
-
"""
|
455
|
-
Keeps track of various task statuses within a system.
|
456
|
-
|
457
|
-
Attributes:
|
458
|
-
num_tasks_started (int): The number of tasks that have been initiated.
|
459
|
-
num_tasks_in_progress (int): The number of tasks currently being processed.
|
460
|
-
num_tasks_succeeded (int): The number of tasks that have completed successfully.
|
461
|
-
num_tasks_failed (int): The number of tasks that have failed.
|
462
|
-
num_rate_limit_errors (int): The number of tasks that failed due to rate limiting.
|
463
|
-
num_api_errors (int): The number of tasks that failed due to API errors.
|
464
|
-
num_other_errors (int): The number of tasks that failed due to other errors.
|
465
|
-
|
466
|
-
Examples:
|
467
|
-
>>> tracker = StatusTracker()
|
468
|
-
>>> tracker.num_tasks_started += 1
|
469
|
-
>>> tracker.num_tasks_succeeded += 1
|
470
|
-
"""
|
471
|
-
|
472
|
-
num_tasks_started: int = 0
|
473
|
-
num_tasks_in_progress: int = 0
|
474
|
-
num_tasks_succeeded: int = 0
|
475
|
-
num_tasks_failed: int = 0
|
476
|
-
num_rate_limit_errors: int = 0
|
477
|
-
num_api_errors: int = 0
|
478
|
-
num_other_errors: int = 0
|
479
|
-
|
480
|
-
|
481
|
-
class BaseRateLimiter(ABC):
|
482
|
-
"""
|
483
|
-
Abstract base class for implementing rate limiters.
|
484
|
-
|
485
|
-
This class provides the basic structure for rate limiters, including
|
486
|
-
the replenishment of request and token capacities at regular intervals.
|
487
|
-
|
488
|
-
Attributes:
|
489
|
-
interval: The time interval in seconds for replenishing capacities.
|
490
|
-
max_requests: The maximum number of requests allowed per interval.
|
491
|
-
max_tokens: The maximum number of tokens allowed per interval.
|
492
|
-
available_request_capacity: The current available request capacity.
|
493
|
-
available_token_capacity: The current available token capacity.
|
494
|
-
rate_limit_replenisher_task: The asyncio task for replenishing capacities.
|
495
|
-
"""
|
496
|
-
|
497
|
-
def __init__(
|
498
|
-
self,
|
499
|
-
max_requests: int,
|
500
|
-
max_tokens: int,
|
501
|
-
interval: int = 60,
|
502
|
-
token_encoding_name=None,
|
503
|
-
) -> None:
|
504
|
-
self.interval: int = interval
|
505
|
-
self.max_requests: int = max_requests
|
506
|
-
self.max_tokens: int = max_tokens
|
507
|
-
self.available_request_capacity: int = max_requests
|
508
|
-
self.available_token_capacity: int = max_tokens
|
509
|
-
self.rate_limit_replenisher_task: AsyncUtil.Task | None = None
|
510
|
-
self._stop_replenishing: AsyncUtil.Event = AsyncUtil.create_event()
|
511
|
-
self._lock: AsyncUtil.Lock = AsyncUtil.create_lock()
|
512
|
-
self.token_encoding_name = token_encoding_name
|
513
|
-
|
514
|
-
async def start_replenishing(self) -> NoReturn:
|
515
|
-
"""Starts the replenishment of rate limit capacities at regular intervals."""
|
516
|
-
try:
|
517
|
-
while not self._stop_replenishing.is_set():
|
518
|
-
await AsyncUtil.sleep(self.interval)
|
519
|
-
async with self._lock:
|
520
|
-
self.available_request_capacity = self.max_requests
|
521
|
-
self.available_token_capacity = self.max_tokens
|
522
|
-
except asyncio.CancelledError:
|
523
|
-
logging.info("Rate limit replenisher task cancelled.")
|
524
|
-
except Exception as e:
|
525
|
-
logging.error(
|
526
|
-
f"An error occurred in the rate limit replenisher: {e}"
|
527
|
-
)
|
528
|
-
|
529
|
-
async def stop_replenishing(self) -> None:
|
530
|
-
"""Stops the replenishment task."""
|
531
|
-
if self.rate_limit_replenisher_task:
|
532
|
-
self.rate_limit_replenisher_task.cancel()
|
533
|
-
await self.rate_limit_replenisher_task
|
534
|
-
self._stop_replenishing.set()
|
535
|
-
|
536
|
-
async def request_permission(self, required_tokens) -> bool:
|
537
|
-
"""Requests permission to make an API call.
|
538
|
-
|
539
|
-
Returns True if the request can be made immediately, otherwise False.
|
540
|
-
"""
|
541
|
-
async with self._lock:
|
542
|
-
if (
|
543
|
-
self.available_request_capacity > 0
|
544
|
-
and self.available_token_capacity > 0
|
545
|
-
):
|
546
|
-
self.available_request_capacity -= 1
|
547
|
-
self.available_token_capacity -= (
|
548
|
-
required_tokens
|
549
|
-
# Assuming 1 token per request for simplicity
|
550
|
-
)
|
551
|
-
return True
|
552
|
-
return False
|
553
|
-
|
554
|
-
async def _call_api(
|
555
|
-
self,
|
556
|
-
http_session,
|
557
|
-
endpoint: str,
|
558
|
-
base_url: str,
|
559
|
-
api_key: str,
|
560
|
-
max_attempts: int = 3,
|
561
|
-
method: str = "post",
|
562
|
-
payload: Mapping[str, any] = None,
|
563
|
-
required_tokens: int = None,
|
564
|
-
**kwargs,
|
565
|
-
) -> Mapping[str, any] | None:
|
566
|
-
"""
|
567
|
-
Makes an API call to the specified endpoint using the provided HTTP session.
|
568
|
-
|
569
|
-
Args:
|
570
|
-
http_session: The aiohttp client session to use for the API call.
|
571
|
-
endpoint: The API endpoint to call.
|
572
|
-
base_url: The base URL of the API.
|
573
|
-
api_key: The API key for authentication.
|
574
|
-
max_attempts: The maximum number of attempts for the API call.
|
575
|
-
method: The HTTP method to use for the API call.
|
576
|
-
payload: The payload to send with the API call.
|
577
|
-
|
578
|
-
Returns:
|
579
|
-
The JSON assistant_response from the API call if successful, otherwise None.
|
580
|
-
"""
|
581
|
-
endpoint = APIUtil.api_endpoint_from_url(base_url + endpoint)
|
582
|
-
while True:
|
583
|
-
if (
|
584
|
-
self.available_request_capacity < 1
|
585
|
-
or self.available_token_capacity < 10
|
586
|
-
): # Minimum token count
|
587
|
-
await AsyncUtil.sleep(1) # Wait for capacity
|
588
|
-
continue
|
589
|
-
|
590
|
-
if not required_tokens:
|
591
|
-
required_tokens = APIUtil.calculate_num_token(
|
592
|
-
payload, endpoint, self.token_encoding_name, **kwargs
|
593
|
-
)
|
594
|
-
|
595
|
-
if await self.request_permission(required_tokens):
|
596
|
-
request_headers = {"Authorization": f"Bearer {api_key}"}
|
597
|
-
attempts_left = max_attempts
|
598
|
-
|
599
|
-
while attempts_left > 0:
|
600
|
-
try:
|
601
|
-
method = APIUtil.api_method(http_session, method)
|
602
|
-
async with method(
|
603
|
-
url=(base_url + endpoint),
|
604
|
-
headers=request_headers,
|
605
|
-
json=payload,
|
606
|
-
) as response:
|
607
|
-
response_json = await response.json()
|
608
|
-
|
609
|
-
if "error" not in response_json:
|
610
|
-
return response_json
|
611
|
-
logging.warning(
|
612
|
-
f"API call failed with error: {response_json['error']}"
|
613
|
-
)
|
614
|
-
attempts_left -= 1
|
615
|
-
|
616
|
-
if "Rate limit" in response_json["error"].get(
|
617
|
-
"message", ""
|
618
|
-
):
|
619
|
-
await AsyncUtil.sleep(15)
|
620
|
-
except Exception as e:
|
621
|
-
logging.warning(f"API call failed with exception: {e}")
|
622
|
-
attempts_left -= 1
|
623
|
-
|
624
|
-
logging.error("API call failed after all attempts.")
|
625
|
-
break
|
626
|
-
else:
|
627
|
-
await AsyncUtil.sleep(1)
|
628
|
-
|
629
|
-
@classmethod
|
630
|
-
async def create(
|
631
|
-
cls,
|
632
|
-
max_requests: int,
|
633
|
-
max_tokens: int,
|
634
|
-
interval: int = 60,
|
635
|
-
token_encoding_name=None,
|
636
|
-
) -> "BaseRateLimiter":
|
637
|
-
"""
|
638
|
-
Creates an instance of BaseRateLimiter and starts the replenisher task.
|
639
|
-
|
640
|
-
Args:
|
641
|
-
max_requests: The maximum number of requests allowed per interval.
|
642
|
-
max_tokens: The maximum number of tokens allowed per interval.
|
643
|
-
interval: The time interval in seconds for replenishing capacities.
|
644
|
-
token_encoding_name: The name of the token encoding to use.
|
645
|
-
|
646
|
-
Returns:
|
647
|
-
An instance of BaseRateLimiter with the replenisher task started.
|
648
|
-
"""
|
649
|
-
instance = cls(max_requests, max_tokens, interval, token_encoding_name)
|
650
|
-
instance.rate_limit_replenisher_task = AsyncUtil.create_task(
|
651
|
-
instance.start_replenishing(), obj=False
|
652
|
-
)
|
653
|
-
return instance
|
654
|
-
|
655
|
-
|
656
|
-
class SimpleRateLimiter(BaseRateLimiter):
|
657
|
-
"""
|
658
|
-
A simple implementation of a rate limiter.
|
659
|
-
|
660
|
-
Inherits from BaseRateLimiter and provides a basic rate limiting mechanism.
|
661
|
-
"""
|
662
|
-
|
663
|
-
def __init__(
|
664
|
-
self,
|
665
|
-
max_requests: int,
|
666
|
-
max_tokens: int,
|
667
|
-
interval: int = 60,
|
668
|
-
token_encoding_name=None,
|
669
|
-
) -> None:
|
670
|
-
"""Initializes the SimpleRateLimiter with the specified parameters."""
|
671
|
-
super().__init__(
|
672
|
-
max_requests, max_tokens, interval, token_encoding_name
|
673
|
-
)
|
674
|
-
|
675
|
-
|
676
|
-
class EndPoint:
|
677
|
-
"""
|
678
|
-
Represents an API endpoint with rate limiting capabilities.
|
679
|
-
|
680
|
-
This class encapsulates the details of an API endpoint, including its rate limiter.
|
681
|
-
|
682
|
-
Attributes:
|
683
|
-
endpoint (str): The API endpoint path.
|
684
|
-
rate_limiter_class (Type[li.BaseRateLimiter]): The class used for rate limiting requests to the endpoint.
|
685
|
-
max_requests (int): The maximum number of requests allowed per interval.
|
686
|
-
max_tokens (int): The maximum number of tokens allowed per interval.
|
687
|
-
interval (int): The time interval in seconds for replenishing rate limit capacities.
|
688
|
-
config (Mapping): Configuration parameters for the endpoint.
|
689
|
-
rate_limiter (Optional[li.BaseRateLimiter]): The rate limiter instance for this endpoint.
|
690
|
-
|
691
|
-
Examples:
|
692
|
-
# Example usage of EndPoint with SimpleRateLimiter
|
693
|
-
endpoint = EndPoint(
|
694
|
-
max_requests=100,
|
695
|
-
max_tokens=1000,
|
696
|
-
interval=60,
|
697
|
-
endpoint_='chat/completions',
|
698
|
-
rate_limiter_class=li.SimpleRateLimiter,
|
699
|
-
config={'param1': 'value1'}
|
700
|
-
)
|
701
|
-
asyncio.run(endpoint.init_rate_limiter())
|
702
|
-
"""
|
703
|
-
|
704
|
-
def __init__(
|
705
|
-
self,
|
706
|
-
max_requests: int = 1000,
|
707
|
-
max_tokens: int = 100000,
|
708
|
-
interval: int = 60,
|
709
|
-
endpoint_: str | None = None,
|
710
|
-
rate_limiter_class: type[BaseRateLimiter] = SimpleRateLimiter,
|
711
|
-
token_encoding_name=None,
|
712
|
-
config: Mapping = None,
|
713
|
-
) -> None:
|
714
|
-
self.endpoint = endpoint_ or "chat/completions"
|
715
|
-
self.rate_limiter_class = rate_limiter_class
|
716
|
-
self.max_requests = max_requests
|
717
|
-
self.max_tokens = max_tokens
|
718
|
-
self.interval = interval
|
719
|
-
self.token_encoding_name = token_encoding_name
|
720
|
-
self.config = config or {}
|
721
|
-
self.rate_limiter: BaseRateLimiter | None = None
|
722
|
-
self._has_initialized = False
|
723
|
-
|
724
|
-
async def init_rate_limiter(self) -> None:
|
725
|
-
"""Initializes the rate limiter for the endpoint."""
|
726
|
-
self.rate_limiter = await self.rate_limiter_class.create(
|
727
|
-
self.max_requests,
|
728
|
-
self.max_tokens,
|
729
|
-
self.interval,
|
730
|
-
self.token_encoding_name,
|
731
|
-
)
|
732
|
-
self._has_initialized = True
|
733
|
-
|
734
|
-
|
735
|
-
class BaseService:
|
736
|
-
"""
|
737
|
-
Base class for services that interact with API endpoints.
|
738
|
-
|
739
|
-
This class provides a foundation for services that need to make API calls with rate limiting.
|
740
|
-
|
741
|
-
Attributes:
|
742
|
-
api_key (Optional[str]): The API key used for authentication.
|
743
|
-
schema (Mapping[str, Any]): The schema defining the service's endpoints.
|
744
|
-
status_tracker (StatusTracker): The object tracking the status of API calls.
|
745
|
-
endpoints (Mapping[str, EndPoint]): A dictionary of endpoint objects.
|
746
|
-
"""
|
747
|
-
|
748
|
-
base_url: str = ""
|
749
|
-
available_endpoints: list = []
|
750
|
-
|
751
|
-
def __init__(
|
752
|
-
self,
|
753
|
-
api_key: str | None = None,
|
754
|
-
schema: Mapping[str, Any] = None,
|
755
|
-
token_encoding_name: str = None,
|
756
|
-
max_tokens: int = 100_000,
|
757
|
-
max_requests: int = 1_000,
|
758
|
-
interval: int = 60,
|
759
|
-
) -> None:
|
760
|
-
self.api_key = api_key
|
761
|
-
self.schema = schema or {}
|
762
|
-
self.status_tracker = StatusTracker()
|
763
|
-
self.endpoints: Mapping[str, EndPoint] = {}
|
764
|
-
self.token_encoding_name = token_encoding_name
|
765
|
-
self.chat_config_rate_limit = {
|
766
|
-
"max_requests": max_requests,
|
767
|
-
"max_tokens": max_tokens,
|
768
|
-
"interval": interval,
|
769
|
-
}
|
770
|
-
|
771
|
-
async def init_endpoint(
|
772
|
-
self,
|
773
|
-
endpoint_: Sequence | str | EndPoint | None = None,
|
774
|
-
) -> None:
|
775
|
-
"""
|
776
|
-
Initializes the specified endpoint or all endpoints if none is specified.
|
777
|
-
|
778
|
-
Args:
|
779
|
-
endpoint_: The endpoint(s) to initialize. Can be a string, an EndPoint, a list of strings, or a list of EndPoints.
|
780
|
-
"""
|
781
|
-
|
782
|
-
if endpoint_:
|
783
|
-
if not isinstance(endpoint_, list):
|
784
|
-
endpoint_ = [endpoint_]
|
785
|
-
for ep in endpoint_:
|
786
|
-
if ep not in self.available_endpoints:
|
787
|
-
raise ValueError(
|
788
|
-
f"Endpoint {ep} not available for service {self.__class__.__name__}"
|
789
|
-
)
|
790
|
-
|
791
|
-
if ep not in self.endpoints:
|
792
|
-
endpoint_config = nget(self.schema, [ep, "config"])
|
793
|
-
self.schema.get(ep, {})
|
794
|
-
if isinstance(ep, EndPoint):
|
795
|
-
self.endpoints[ep.endpoint] = ep
|
796
|
-
elif ep == "chat/completions":
|
797
|
-
self.endpoints[ep] = EndPoint(
|
798
|
-
max_requests=self.chat_config_rate_limit.get(
|
799
|
-
"max_requests", 1000
|
800
|
-
),
|
801
|
-
max_tokens=self.chat_config_rate_limit.get(
|
802
|
-
"max_tokens", 100000
|
803
|
-
),
|
804
|
-
interval=self.chat_config_rate_limit.get(
|
805
|
-
"interval", 60
|
806
|
-
),
|
807
|
-
endpoint_=ep,
|
808
|
-
token_encoding_name=self.token_encoding_name,
|
809
|
-
config=endpoint_config,
|
810
|
-
)
|
811
|
-
else:
|
812
|
-
self.endpoints[ep] = EndPoint(
|
813
|
-
max_requests=(
|
814
|
-
endpoint_config.get("max_requests", 1000)
|
815
|
-
if endpoint_config.get("max_requests", 1000)
|
816
|
-
is not None
|
817
|
-
else 1000
|
818
|
-
),
|
819
|
-
max_tokens=(
|
820
|
-
endpoint_config.get("max_tokens", 100000)
|
821
|
-
if endpoint_config.get("max_tokens", 100000)
|
822
|
-
is not None
|
823
|
-
else 100000
|
824
|
-
),
|
825
|
-
interval=(
|
826
|
-
endpoint_config.get("interval", 60)
|
827
|
-
if endpoint_config.get("interval", 60)
|
828
|
-
is not None
|
829
|
-
else 60
|
830
|
-
),
|
831
|
-
endpoint_=ep,
|
832
|
-
token_encoding_name=self.token_encoding_name,
|
833
|
-
config=endpoint_config,
|
834
|
-
)
|
835
|
-
|
836
|
-
if not self.endpoints[ep]._has_initialized:
|
837
|
-
await self.endpoints[ep].init_rate_limiter()
|
838
|
-
|
839
|
-
else:
|
840
|
-
for ep in self.available_endpoints:
|
841
|
-
endpoint_config = nget(self.schema, [ep, "config"])
|
842
|
-
self.schema.get(ep, {})
|
843
|
-
if ep not in self.endpoints:
|
844
|
-
self.endpoints[ep] = EndPoint(
|
845
|
-
max_requests=endpoint_config.get("max_requests", 1000),
|
846
|
-
max_tokens=endpoint_config.get("max_tokens", 100000),
|
847
|
-
interval=endpoint_config.get("interval", 60),
|
848
|
-
endpoint_=ep,
|
849
|
-
token_encoding_name=self.token_encoding_name,
|
850
|
-
config=endpoint_config,
|
851
|
-
)
|
852
|
-
if not self.endpoints[ep]._has_initialized:
|
853
|
-
await self.endpoints[ep].init_rate_limiter()
|
854
|
-
|
855
|
-
async def call_api(
|
856
|
-
self, payload, endpoint, method, required_tokens=None, **kwargs
|
857
|
-
):
|
858
|
-
"""
|
859
|
-
Calls the specified API endpoint with the given payload and method.
|
860
|
-
|
861
|
-
Args:
|
862
|
-
payload: The payload to send with the API call.
|
863
|
-
endpoint: The endpoint to call.
|
864
|
-
method: The HTTP method to use for the call.
|
865
|
-
|
866
|
-
Returns:
|
867
|
-
The assistant_response from the API call.
|
868
|
-
|
869
|
-
Raises:
|
870
|
-
ValueError: If the endpoint has not been initialized.
|
871
|
-
"""
|
872
|
-
if endpoint not in self.endpoints.keys():
|
873
|
-
raise ValueError(f"The endpoint {endpoint} has not initialized.")
|
874
|
-
async with aiohttp.ClientSession() as http_session:
|
875
|
-
return await self.endpoints[endpoint].rate_limiter._call_api(
|
876
|
-
http_session=http_session,
|
877
|
-
endpoint=endpoint,
|
878
|
-
base_url=self.base_url,
|
879
|
-
api_key=self.api_key,
|
880
|
-
method=method,
|
881
|
-
payload=payload,
|
882
|
-
required_tokens=required_tokens,
|
883
|
-
**kwargs,
|
884
|
-
)
|
885
|
-
|
886
|
-
|
887
|
-
class PayloadPackage:
|
888
|
-
|
889
|
-
@classmethod
|
890
|
-
def embeddings(cls, embed_str, llmconfig, schema, **kwargs):
|
891
|
-
return APIUtil.create_payload(
|
892
|
-
input_=embed_str,
|
893
|
-
config=llmconfig,
|
894
|
-
required_=schema["required"],
|
895
|
-
optional_=schema["optional"],
|
896
|
-
input_key="input",
|
897
|
-
**kwargs,
|
898
|
-
)
|
899
|
-
|
900
|
-
@classmethod
|
901
|
-
def chat_completion(cls, messages, llmconfig, schema, **kwargs):
|
902
|
-
"""
|
903
|
-
Creates a payload for the chat completion operation.
|
904
|
-
|
905
|
-
Args:
|
906
|
-
messages: The messages to include in the chat completion.
|
907
|
-
llmconfig: Configuration for the language model.
|
908
|
-
schema: The schema describing required and optional fields.
|
909
|
-
**kwargs: Additional keyword arguments.
|
910
|
-
|
911
|
-
Returns:
|
912
|
-
The constructed payload.
|
913
|
-
"""
|
914
|
-
return APIUtil.create_payload(
|
915
|
-
input_=messages,
|
916
|
-
config=llmconfig,
|
917
|
-
required_=schema["required"],
|
918
|
-
optional_=schema["optional"],
|
919
|
-
input_key="messages",
|
920
|
-
**kwargs,
|
921
|
-
)
|
922
|
-
|
923
|
-
@classmethod
|
924
|
-
def fine_tuning(cls, training_file, llmconfig, schema, **kwargs):
|
925
|
-
"""
|
926
|
-
Creates a payload for the fine-tuning operation.
|
927
|
-
|
928
|
-
Args:
|
929
|
-
training_file: The file containing training data.
|
930
|
-
llmconfig: Configuration for the language model.
|
931
|
-
schema: The schema describing required and optional fields.
|
932
|
-
**kwargs: Additional keyword arguments.
|
933
|
-
|
934
|
-
Returns:
|
935
|
-
The constructed payload.
|
936
|
-
"""
|
937
|
-
return APIUtil.create_payload(
|
938
|
-
input_=training_file,
|
939
|
-
config=llmconfig,
|
940
|
-
required_=schema["required"],
|
941
|
-
optional_=schema["optional"],
|
942
|
-
input_key="training_file",
|
943
|
-
**kwargs,
|
944
|
-
)
|