lionagi 0.5.5__py3-none-any.whl → 0.6.0__py3-none-any.whl
Sign up to get free protection for your applications and to get access to all the features.
- lionagi/__init__.py +16 -24
- lionagi/{core/_class_registry.py → _class_registry.py} +51 -10
- lionagi/_errors.py +35 -0
- lionagi/libs/__init__.py +3 -0
- lionagi/libs/compress/__init__.py +3 -0
- lionagi/libs/compress/models.py +6 -2
- lionagi/libs/compress/utils.py +4 -16
- lionagi/libs/file/__init__.py +3 -0
- lionagi/libs/file/chunk.py +4 -0
- lionagi/libs/file/file_ops.py +4 -0
- lionagi/libs/file/params.py +4 -41
- lionagi/libs/file/process.py +4 -0
- lionagi/libs/file/save.py +5 -1
- lionagi/libs/{parse/flatten → nested}/flatten.py +4 -0
- lionagi/libs/{parse/nested → nested}/nfilter.py +4 -0
- lionagi/libs/{parse/nested → nested}/nget.py +6 -1
- lionagi/libs/{parse/nested → nested}/ninsert.py +5 -1
- lionagi/libs/{parse/nested → nested}/nmerge.py +4 -0
- lionagi/libs/{parse/nested → nested}/npop.py +5 -2
- lionagi/libs/{parse/nested → nested}/nset.py +6 -1
- lionagi/libs/{parse/flatten → nested}/unflatten.py +4 -0
- lionagi/libs/{parse/nested → nested}/utils.py +5 -1
- lionagi/libs/package/__init__.py +3 -0
- lionagi/libs/package/imports.py +6 -2
- lionagi/libs/package/management.py +7 -3
- lionagi/libs/package/params.py +4 -0
- lionagi/libs/package/system.py +4 -0
- lionagi/libs/parse.py +30 -0
- lionagi/libs/{parse/json → schema}/as_readable.py +10 -4
- lionagi/libs/{parse/string_parse/code_block.py → schema/extract_code_block.py} +4 -0
- lionagi/libs/{parse/string_parse/docstring.py → schema/extract_docstring.py} +4 -0
- lionagi/libs/{parse/string_parse/function_.py → schema/function_to_schema.py} +21 -9
- lionagi/libs/{parse/json/schema.py → schema/json_schema.py} +5 -1
- lionagi/libs/validate/common_field_validators.py +170 -0
- lionagi/libs/{parse/validate/keys.py → validate/fuzzy_match_keys.py} +42 -8
- lionagi/libs/{parse/validate/mapping.py → validate/fuzzy_validate_mapping.py} +41 -6
- lionagi/libs/{string_similarity/algorithms.py → validate/string_similarity.py} +115 -1
- lionagi/libs/{parse/validate/boolean.py → validate/validate_boolean.py} +42 -3
- lionagi/operations/__init__.py +13 -3
- lionagi/operations/brainstorm/__init__.py +3 -3
- lionagi/operations/brainstorm/brainstorm.py +33 -19
- lionagi/operations/brainstorm/prompt.py +4 -0
- lionagi/operations/plan/__init__.py +4 -0
- lionagi/operations/plan/plan.py +16 -13
- lionagi/operations/plan/prompt.py +4 -0
- lionagi/operations/select/__init__.py +4 -0
- lionagi/operations/select/prompt.py +4 -0
- lionagi/operations/select/select.py +1 -1
- lionagi/operations/select/utils.py +4 -4
- lionagi/{strategies → operations/strategies}/base.py +6 -2
- lionagi/{strategies → operations/strategies}/concurrent.py +8 -5
- lionagi/{strategies → operations/strategies}/concurrent_chunk.py +6 -3
- lionagi/{strategies → operations/strategies}/concurrent_sequential_chunk.py +8 -4
- lionagi/{strategies → operations/strategies}/params.py +10 -6
- lionagi/{strategies → operations/strategies}/sequential.py +6 -2
- lionagi/{strategies → operations/strategies}/sequential_chunk.py +7 -3
- lionagi/{strategies → operations/strategies}/sequential_concurrent_chunk.py +9 -4
- lionagi/{strategies → operations/strategies}/utils.py +6 -3
- lionagi/{core/models/__init__.py → operations/types.py} +3 -1
- lionagi/operations/utils.py +6 -3
- lionagi/operatives/action/function_calling.py +136 -0
- lionagi/operatives/action/manager.py +239 -0
- lionagi/operatives/action/request_response_model.py +90 -0
- lionagi/operatives/action/tool.py +141 -0
- lionagi/{protocols/operatives/action.py → operatives/action/utils.py} +52 -90
- lionagi/{core → operatives}/forms/base.py +9 -4
- lionagi/{core → operatives}/forms/form.py +8 -13
- lionagi/{core → operatives}/forms/report.py +5 -3
- lionagi/operatives/instruct/base.py +79 -0
- lionagi/operatives/instruct/instruct.py +105 -0
- lionagi/operatives/instruct/instruct_collection.py +52 -0
- lionagi/operatives/instruct/node.py +13 -0
- lionagi/{protocols/operatives → operatives/instruct}/prompts.py +0 -34
- lionagi/{protocols/operatives → operatives/instruct}/reason.py +14 -7
- lionagi/{integrations/anthropic_/version.py → operatives/manager.py} +5 -1
- lionagi/operatives/models/field_model.py +194 -0
- lionagi/operatives/models/model_params.py +307 -0
- lionagi/{core → operatives}/models/note.py +20 -28
- lionagi/{core → operatives}/models/operable_model.py +153 -71
- lionagi/{core → operatives}/models/schema_model.py +4 -3
- lionagi/{protocols/operatives → operatives}/operative.py +10 -7
- lionagi/{protocols/operatives → operatives}/step.py +67 -26
- lionagi/operatives/types.py +69 -0
- lionagi/protocols/_adapter.py +224 -0
- lionagi/protocols/_concepts.py +94 -0
- lionagi/protocols/generic/element.py +460 -0
- lionagi/protocols/generic/event.py +177 -0
- lionagi/protocols/generic/log.py +237 -0
- lionagi/{core → protocols}/generic/pile.py +172 -131
- lionagi/protocols/generic/processor.py +316 -0
- lionagi/protocols/generic/progression.py +500 -0
- lionagi/protocols/graph/edge.py +166 -0
- lionagi/protocols/graph/graph.py +290 -0
- lionagi/protocols/graph/node.py +109 -0
- lionagi/protocols/mail/exchange.py +116 -0
- lionagi/protocols/mail/mail.py +25 -0
- lionagi/protocols/mail/mailbox.py +47 -0
- lionagi/protocols/mail/manager.py +168 -0
- lionagi/protocols/mail/package.py +55 -0
- lionagi/protocols/messages/action_request.py +165 -0
- lionagi/protocols/messages/action_response.py +132 -0
- lionagi/{core/communication → protocols/messages}/assistant_response.py +55 -79
- lionagi/protocols/messages/base.py +73 -0
- lionagi/protocols/messages/instruction.py +582 -0
- lionagi/protocols/messages/manager.py +429 -0
- lionagi/protocols/messages/message.py +216 -0
- lionagi/protocols/messages/system.py +115 -0
- lionagi/protocols/messages/templates/assistant_response.jinja2 +6 -0
- lionagi/{core/communication → protocols/messages}/templates/instruction_message.jinja2 +2 -2
- lionagi/protocols/types.py +96 -0
- lionagi/service/__init__.py +10 -12
- lionagi/service/endpoints/base.py +517 -0
- lionagi/service/endpoints/chat_completion.py +102 -0
- lionagi/service/endpoints/match_endpoint.py +60 -0
- lionagi/service/endpoints/rate_limited_processor.py +145 -0
- lionagi/service/endpoints/token_calculator.py +209 -0
- lionagi/service/imodel.py +263 -96
- lionagi/service/manager.py +45 -0
- lionagi/service/providers/anthropic_/messages.py +64 -0
- lionagi/service/providers/groq_/chat_completions.py +56 -0
- lionagi/service/providers/openai_/chat_completions.py +62 -0
- lionagi/service/providers/openrouter_/chat_completions.py +62 -0
- lionagi/service/providers/perplexity_/__init__.py +3 -0
- lionagi/service/providers/perplexity_/chat_completions.py +40 -0
- lionagi/session/__init__.py +3 -0
- lionagi/session/branch.py +1287 -0
- lionagi/session/session.py +296 -0
- lionagi/settings.py +62 -118
- lionagi/utils.py +2386 -0
- lionagi/version.py +1 -1
- {lionagi-0.5.5.dist-info → lionagi-0.6.0.dist-info}/METADATA +7 -6
- lionagi-0.6.0.dist-info/RECORD +160 -0
- lionagi/core/action/action_manager.py +0 -289
- lionagi/core/action/base.py +0 -109
- lionagi/core/action/function_calling.py +0 -153
- lionagi/core/action/tool.py +0 -202
- lionagi/core/action/types.py +0 -16
- lionagi/core/communication/action_request.py +0 -163
- lionagi/core/communication/action_response.py +0 -149
- lionagi/core/communication/base_mail.py +0 -49
- lionagi/core/communication/instruction.py +0 -376
- lionagi/core/communication/message.py +0 -286
- lionagi/core/communication/message_manager.py +0 -543
- lionagi/core/communication/system.py +0 -116
- lionagi/core/communication/templates/assistant_response.jinja2 +0 -2
- lionagi/core/communication/types.py +0 -27
- lionagi/core/communication/utils.py +0 -256
- lionagi/core/forms/types.py +0 -13
- lionagi/core/generic/component.py +0 -422
- lionagi/core/generic/edge.py +0 -163
- lionagi/core/generic/element.py +0 -199
- lionagi/core/generic/graph.py +0 -377
- lionagi/core/generic/log.py +0 -151
- lionagi/core/generic/log_manager.py +0 -320
- lionagi/core/generic/node.py +0 -11
- lionagi/core/generic/progression.py +0 -395
- lionagi/core/generic/types.py +0 -23
- lionagi/core/generic/utils.py +0 -53
- lionagi/core/models/base.py +0 -28
- lionagi/core/models/field_model.py +0 -145
- lionagi/core/models/model_params.py +0 -194
- lionagi/core/models/types.py +0 -19
- lionagi/core/session/branch.py +0 -130
- lionagi/core/session/branch_mixins.py +0 -581
- lionagi/core/session/session.py +0 -163
- lionagi/core/session/types.py +0 -8
- lionagi/core/typing/__init__.py +0 -9
- lionagi/core/typing/_concepts.py +0 -173
- lionagi/core/typing/_id.py +0 -104
- lionagi/core/typing/_pydantic.py +0 -33
- lionagi/core/typing/_typing.py +0 -54
- lionagi/integrations/__init__.py +0 -0
- lionagi/integrations/_services.py +0 -17
- lionagi/integrations/anthropic_/AnthropicModel.py +0 -268
- lionagi/integrations/anthropic_/AnthropicService.py +0 -127
- lionagi/integrations/anthropic_/anthropic_max_output_token_data.yaml +0 -12
- lionagi/integrations/anthropic_/anthropic_price_data.yaml +0 -34
- lionagi/integrations/anthropic_/api_endpoints/api_request.py +0 -277
- lionagi/integrations/anthropic_/api_endpoints/data_models.py +0 -40
- lionagi/integrations/anthropic_/api_endpoints/match_response.py +0 -119
- lionagi/integrations/anthropic_/api_endpoints/messages/request/message_models.py +0 -14
- lionagi/integrations/anthropic_/api_endpoints/messages/request/request_body.py +0 -74
- lionagi/integrations/anthropic_/api_endpoints/messages/response/__init__.py +0 -0
- lionagi/integrations/anthropic_/api_endpoints/messages/response/content_models.py +0 -32
- lionagi/integrations/anthropic_/api_endpoints/messages/response/response_body.py +0 -101
- lionagi/integrations/anthropic_/api_endpoints/messages/response/usage_models.py +0 -25
- lionagi/integrations/groq_/GroqModel.py +0 -325
- lionagi/integrations/groq_/GroqService.py +0 -156
- lionagi/integrations/groq_/api_endpoints/__init__.py +0 -0
- lionagi/integrations/groq_/api_endpoints/data_models.py +0 -187
- lionagi/integrations/groq_/api_endpoints/groq_request.py +0 -288
- lionagi/integrations/groq_/api_endpoints/match_response.py +0 -106
- lionagi/integrations/groq_/api_endpoints/response_utils.py +0 -105
- lionagi/integrations/groq_/groq_max_output_token_data.yaml +0 -21
- lionagi/integrations/groq_/groq_price_data.yaml +0 -58
- lionagi/integrations/groq_/groq_rate_limits.yaml +0 -105
- lionagi/integrations/groq_/version.py +0 -5
- lionagi/integrations/litellm_/imodel.py +0 -76
- lionagi/integrations/ollama_/OllamaModel.py +0 -244
- lionagi/integrations/ollama_/OllamaService.py +0 -142
- lionagi/integrations/ollama_/api_endpoints/api_request.py +0 -179
- lionagi/integrations/ollama_/api_endpoints/chat_completion/message_models.py +0 -31
- lionagi/integrations/ollama_/api_endpoints/chat_completion/request_body.py +0 -46
- lionagi/integrations/ollama_/api_endpoints/chat_completion/response_body.py +0 -67
- lionagi/integrations/ollama_/api_endpoints/chat_completion/tool_models.py +0 -49
- lionagi/integrations/ollama_/api_endpoints/completion/__init__.py +0 -0
- lionagi/integrations/ollama_/api_endpoints/completion/request_body.py +0 -72
- lionagi/integrations/ollama_/api_endpoints/completion/response_body.py +0 -59
- lionagi/integrations/ollama_/api_endpoints/data_models.py +0 -15
- lionagi/integrations/ollama_/api_endpoints/embedding/__init__.py +0 -0
- lionagi/integrations/ollama_/api_endpoints/embedding/request_body.py +0 -33
- lionagi/integrations/ollama_/api_endpoints/embedding/response_body.py +0 -29
- lionagi/integrations/ollama_/api_endpoints/match_data_model.py +0 -62
- lionagi/integrations/ollama_/api_endpoints/match_response.py +0 -190
- lionagi/integrations/ollama_/api_endpoints/model/copy_model.py +0 -13
- lionagi/integrations/ollama_/api_endpoints/model/create_model.py +0 -28
- lionagi/integrations/ollama_/api_endpoints/model/delete_model.py +0 -11
- lionagi/integrations/ollama_/api_endpoints/model/list_model.py +0 -60
- lionagi/integrations/ollama_/api_endpoints/model/pull_model.py +0 -34
- lionagi/integrations/ollama_/api_endpoints/model/push_model.py +0 -35
- lionagi/integrations/ollama_/api_endpoints/model/show_model.py +0 -36
- lionagi/integrations/ollama_/api_endpoints/option_models.py +0 -68
- lionagi/integrations/openai_/OpenAIModel.py +0 -419
- lionagi/integrations/openai_/OpenAIService.py +0 -435
- lionagi/integrations/openai_/__init__.py +0 -0
- lionagi/integrations/openai_/api_endpoints/__init__.py +0 -3
- lionagi/integrations/openai_/api_endpoints/api_request.py +0 -277
- lionagi/integrations/openai_/api_endpoints/audio/__init__.py +0 -9
- lionagi/integrations/openai_/api_endpoints/audio/speech_models.py +0 -34
- lionagi/integrations/openai_/api_endpoints/audio/transcription_models.py +0 -136
- lionagi/integrations/openai_/api_endpoints/audio/translation_models.py +0 -41
- lionagi/integrations/openai_/api_endpoints/audio/types.py +0 -41
- lionagi/integrations/openai_/api_endpoints/batch/__init__.py +0 -17
- lionagi/integrations/openai_/api_endpoints/batch/batch_models.py +0 -146
- lionagi/integrations/openai_/api_endpoints/batch/cancel_batch.py +0 -7
- lionagi/integrations/openai_/api_endpoints/batch/create_batch.py +0 -26
- lionagi/integrations/openai_/api_endpoints/batch/list_batch.py +0 -37
- lionagi/integrations/openai_/api_endpoints/batch/request_object_models.py +0 -65
- lionagi/integrations/openai_/api_endpoints/batch/retrieve_batch.py +0 -7
- lionagi/integrations/openai_/api_endpoints/batch/types.py +0 -4
- lionagi/integrations/openai_/api_endpoints/chat_completions/__init__.py +0 -1
- lionagi/integrations/openai_/api_endpoints/chat_completions/request/__init__.py +0 -39
- lionagi/integrations/openai_/api_endpoints/chat_completions/request/message_models.py +0 -121
- lionagi/integrations/openai_/api_endpoints/chat_completions/request/request_body.py +0 -221
- lionagi/integrations/openai_/api_endpoints/chat_completions/request/response_format.py +0 -71
- lionagi/integrations/openai_/api_endpoints/chat_completions/request/stream_options.py +0 -14
- lionagi/integrations/openai_/api_endpoints/chat_completions/request/tool_choice_models.py +0 -17
- lionagi/integrations/openai_/api_endpoints/chat_completions/request/tool_models.py +0 -54
- lionagi/integrations/openai_/api_endpoints/chat_completions/request/types.py +0 -18
- lionagi/integrations/openai_/api_endpoints/chat_completions/response/__init__.py +0 -0
- lionagi/integrations/openai_/api_endpoints/chat_completions/response/choice_models.py +0 -62
- lionagi/integrations/openai_/api_endpoints/chat_completions/response/function_models.py +0 -16
- lionagi/integrations/openai_/api_endpoints/chat_completions/response/log_prob_models.py +0 -47
- lionagi/integrations/openai_/api_endpoints/chat_completions/response/message_models.py +0 -25
- lionagi/integrations/openai_/api_endpoints/chat_completions/response/response_body.py +0 -99
- lionagi/integrations/openai_/api_endpoints/chat_completions/response/types.py +0 -8
- lionagi/integrations/openai_/api_endpoints/chat_completions/response/usage_models.py +0 -24
- lionagi/integrations/openai_/api_endpoints/chat_completions/util.py +0 -46
- lionagi/integrations/openai_/api_endpoints/data_models.py +0 -23
- lionagi/integrations/openai_/api_endpoints/embeddings/__init__.py +0 -3
- lionagi/integrations/openai_/api_endpoints/embeddings/request_body.py +0 -79
- lionagi/integrations/openai_/api_endpoints/embeddings/response_body.py +0 -67
- lionagi/integrations/openai_/api_endpoints/files/__init__.py +0 -11
- lionagi/integrations/openai_/api_endpoints/files/delete_file.py +0 -20
- lionagi/integrations/openai_/api_endpoints/files/file_models.py +0 -56
- lionagi/integrations/openai_/api_endpoints/files/list_files.py +0 -27
- lionagi/integrations/openai_/api_endpoints/files/retrieve_file.py +0 -9
- lionagi/integrations/openai_/api_endpoints/files/upload_file.py +0 -38
- lionagi/integrations/openai_/api_endpoints/fine_tuning/__init__.py +0 -37
- lionagi/integrations/openai_/api_endpoints/fine_tuning/cancel_jobs.py +0 -9
- lionagi/integrations/openai_/api_endpoints/fine_tuning/create_jobs.py +0 -133
- lionagi/integrations/openai_/api_endpoints/fine_tuning/fine_tuning_job_checkpoint_models.py +0 -58
- lionagi/integrations/openai_/api_endpoints/fine_tuning/fine_tuning_job_event_models.py +0 -31
- lionagi/integrations/openai_/api_endpoints/fine_tuning/fine_tuning_job_models.py +0 -140
- lionagi/integrations/openai_/api_endpoints/fine_tuning/list_fine_tuning_checkpoints.py +0 -51
- lionagi/integrations/openai_/api_endpoints/fine_tuning/list_fine_tuning_events.py +0 -42
- lionagi/integrations/openai_/api_endpoints/fine_tuning/list_fine_tuning_jobs.py +0 -31
- lionagi/integrations/openai_/api_endpoints/fine_tuning/retrieve_jobs.py +0 -9
- lionagi/integrations/openai_/api_endpoints/fine_tuning/training_format.py +0 -30
- lionagi/integrations/openai_/api_endpoints/images/__init__.py +0 -9
- lionagi/integrations/openai_/api_endpoints/images/image_edit_models.py +0 -69
- lionagi/integrations/openai_/api_endpoints/images/image_models.py +0 -56
- lionagi/integrations/openai_/api_endpoints/images/image_variation_models.py +0 -56
- lionagi/integrations/openai_/api_endpoints/images/response_body.py +0 -30
- lionagi/integrations/openai_/api_endpoints/match_data_model.py +0 -197
- lionagi/integrations/openai_/api_endpoints/match_response.py +0 -336
- lionagi/integrations/openai_/api_endpoints/models/__init__.py +0 -7
- lionagi/integrations/openai_/api_endpoints/models/delete_fine_tuned_model.py +0 -17
- lionagi/integrations/openai_/api_endpoints/models/models_models.py +0 -31
- lionagi/integrations/openai_/api_endpoints/models/retrieve_model.py +0 -9
- lionagi/integrations/openai_/api_endpoints/moderations/__init__.py +0 -3
- lionagi/integrations/openai_/api_endpoints/moderations/request_body.py +0 -20
- lionagi/integrations/openai_/api_endpoints/moderations/response_body.py +0 -139
- lionagi/integrations/openai_/api_endpoints/uploads/__init__.py +0 -19
- lionagi/integrations/openai_/api_endpoints/uploads/add_upload_part.py +0 -11
- lionagi/integrations/openai_/api_endpoints/uploads/cancel_upload.py +0 -7
- lionagi/integrations/openai_/api_endpoints/uploads/complete_upload.py +0 -18
- lionagi/integrations/openai_/api_endpoints/uploads/create_upload.py +0 -17
- lionagi/integrations/openai_/api_endpoints/uploads/uploads_models.py +0 -52
- lionagi/integrations/openai_/image_token_calculator/__init__.py +0 -0
- lionagi/integrations/openai_/image_token_calculator/image_token_calculator.py +0 -98
- lionagi/integrations/openai_/image_token_calculator/openai_image_token_data.yaml +0 -15
- lionagi/integrations/openai_/openai_max_output_token_data.yaml +0 -12
- lionagi/integrations/openai_/openai_price_data.yaml +0 -26
- lionagi/integrations/openai_/version.py +0 -1
- lionagi/integrations/pandas_/__init__.py +0 -24
- lionagi/integrations/pandas_/extend_df.py +0 -61
- lionagi/integrations/pandas_/read.py +0 -103
- lionagi/integrations/pandas_/remove_rows.py +0 -61
- lionagi/integrations/pandas_/replace_keywords.py +0 -65
- lionagi/integrations/pandas_/save.py +0 -131
- lionagi/integrations/pandas_/search_keywords.py +0 -69
- lionagi/integrations/pandas_/to_df.py +0 -196
- lionagi/integrations/pandas_/update_cells.py +0 -54
- lionagi/integrations/perplexity_/PerplexityModel.py +0 -274
- lionagi/integrations/perplexity_/PerplexityService.py +0 -118
- lionagi/integrations/perplexity_/api_endpoints/__init__.py +0 -0
- lionagi/integrations/perplexity_/api_endpoints/api_request.py +0 -171
- lionagi/integrations/perplexity_/api_endpoints/chat_completions/__init__.py +0 -0
- lionagi/integrations/perplexity_/api_endpoints/chat_completions/request/__init__.py +0 -0
- lionagi/integrations/perplexity_/api_endpoints/chat_completions/request/request_body.py +0 -121
- lionagi/integrations/perplexity_/api_endpoints/chat_completions/response/__init__.py +0 -0
- lionagi/integrations/perplexity_/api_endpoints/chat_completions/response/response_body.py +0 -146
- lionagi/integrations/perplexity_/api_endpoints/data_models.py +0 -63
- lionagi/integrations/perplexity_/api_endpoints/match_response.py +0 -26
- lionagi/integrations/perplexity_/perplexity_max_output_token_data.yaml +0 -3
- lionagi/integrations/perplexity_/perplexity_price_data.yaml +0 -10
- lionagi/integrations/perplexity_/version.py +0 -1
- lionagi/integrations/pydantic_/__init__.py +0 -8
- lionagi/integrations/pydantic_/break_down_annotation.py +0 -81
- lionagi/integrations/pydantic_/new_model.py +0 -208
- lionagi/libs/constants.py +0 -98
- lionagi/libs/file/path.py +0 -301
- lionagi/libs/file/types.py +0 -22
- lionagi/libs/func/__init__.py +0 -0
- lionagi/libs/func/async_calls/__init__.py +0 -24
- lionagi/libs/func/async_calls/alcall.py +0 -210
- lionagi/libs/func/async_calls/bcall.py +0 -130
- lionagi/libs/func/async_calls/mcall.py +0 -134
- lionagi/libs/func/async_calls/pcall.py +0 -149
- lionagi/libs/func/async_calls/rcall.py +0 -217
- lionagi/libs/func/async_calls/tcall.py +0 -114
- lionagi/libs/func/async_calls/ucall.py +0 -85
- lionagi/libs/func/decorators.py +0 -277
- lionagi/libs/func/lcall.py +0 -57
- lionagi/libs/func/params.py +0 -64
- lionagi/libs/func/throttle.py +0 -119
- lionagi/libs/func/types.py +0 -39
- lionagi/libs/func/utils.py +0 -96
- lionagi/libs/package/types.py +0 -26
- lionagi/libs/parse/__init__.py +0 -1
- lionagi/libs/parse/flatten/__init__.py +0 -9
- lionagi/libs/parse/flatten/params.py +0 -52
- lionagi/libs/parse/json/__init__.py +0 -27
- lionagi/libs/parse/json/extract.py +0 -102
- lionagi/libs/parse/json/parse.py +0 -179
- lionagi/libs/parse/json/to_json.py +0 -71
- lionagi/libs/parse/nested/__init__.py +0 -33
- lionagi/libs/parse/nested/to_flat_list.py +0 -64
- lionagi/libs/parse/params.py +0 -0
- lionagi/libs/parse/string_parse/__init__.py +0 -11
- lionagi/libs/parse/type_convert/__init__.py +0 -19
- lionagi/libs/parse/type_convert/params.py +0 -145
- lionagi/libs/parse/type_convert/to_dict.py +0 -333
- lionagi/libs/parse/type_convert/to_list.py +0 -186
- lionagi/libs/parse/type_convert/to_num.py +0 -358
- lionagi/libs/parse/type_convert/to_str.py +0 -195
- lionagi/libs/parse/types.py +0 -9
- lionagi/libs/parse/validate/__init__.py +0 -14
- lionagi/libs/parse/validate/params.py +0 -62
- lionagi/libs/parse/xml/__init__.py +0 -10
- lionagi/libs/parse/xml/convert.py +0 -56
- lionagi/libs/parse/xml/parser.py +0 -93
- lionagi/libs/string_similarity/__init__.py +0 -32
- lionagi/libs/string_similarity/matcher.py +0 -102
- lionagi/libs/string_similarity/utils.py +0 -15
- lionagi/libs/utils.py +0 -266
- lionagi/protocols/adapters/__init__.py +0 -0
- lionagi/protocols/adapters/adapter.py +0 -79
- lionagi/protocols/adapters/json_adapter.py +0 -43
- lionagi/protocols/adapters/pandas_adapter.py +0 -96
- lionagi/protocols/configs/__init__.py +0 -0
- lionagi/protocols/configs/branch_config.py +0 -86
- lionagi/protocols/configs/id_config.py +0 -15
- lionagi/protocols/configs/imodel_config.py +0 -73
- lionagi/protocols/configs/log_config.py +0 -93
- lionagi/protocols/configs/retry_config.py +0 -29
- lionagi/protocols/configs/types.py +0 -15
- lionagi/protocols/operatives/instruct.py +0 -194
- lionagi/protocols/operatives/types.py +0 -19
- lionagi/protocols/registries/_component_registry.py +0 -23
- lionagi/protocols/registries/_pile_registry.py +0 -30
- lionagi/service/complete_request_info.py +0 -11
- lionagi/service/rate_limiter.py +0 -108
- lionagi/service/service.py +0 -41
- lionagi/service/service_match_util.py +0 -131
- lionagi/service/service_util.py +0 -72
- lionagi/service/token_calculator.py +0 -51
- lionagi/strategies/__init__.py +0 -0
- lionagi/strategies/types.py +0 -21
- lionagi-0.5.5.dist-info/RECORD +0 -374
- /lionagi/{core → libs/nested}/__init__.py +0 -0
- /lionagi/{core/action → libs/schema}/__init__.py +0 -0
- /lionagi/{core/communication → libs/validate}/__init__.py +0 -0
- /lionagi/{core/forms → operations/strategies}/__init__.py +0 -0
- /lionagi/{core/generic → operatives}/__init__.py +0 -0
- /lionagi/{core/session → operatives/action}/__init__.py +0 -0
- /lionagi/{integrations/anthropic_ → operatives/forms}/__init__.py +0 -0
- /lionagi/{core → operatives}/forms/utils.py +0 -0
- /lionagi/{integrations/anthropic_/api_endpoints → operatives/instruct}/__init__.py +0 -0
- /lionagi/{integrations/anthropic_/api_endpoints/messages → operatives/models}/__init__.py +0 -0
- /lionagi/{integrations/anthropic_/api_endpoints/messages/request → protocols/generic}/__init__.py +0 -0
- /lionagi/{integrations/groq_ → protocols/graph}/__init__.py +0 -0
- /lionagi/{integrations/litellm_ → protocols/mail}/__init__.py +0 -0
- /lionagi/{integrations/ollama_ → protocols/messages}/__init__.py +0 -0
- /lionagi/{core/communication → protocols/messages}/templates/README.md +0 -0
- /lionagi/{core/communication → protocols/messages}/templates/action_request.jinja2 +0 -0
- /lionagi/{core/communication → protocols/messages}/templates/action_response.jinja2 +0 -0
- /lionagi/{core/communication → protocols/messages}/templates/system_message.jinja2 +0 -0
- /lionagi/{core/communication → protocols/messages}/templates/tool_schemas.jinja2 +0 -0
- /lionagi/{integrations/ollama_/api_endpoints → service/endpoints}/__init__.py +0 -0
- /lionagi/{integrations/ollama_/api_endpoints/chat_completion → service/providers}/__init__.py +0 -0
- /lionagi/{integrations/ollama_/api_endpoints/model → service/providers/anthropic_}/__init__.py +0 -0
- /lionagi/{integrations/perplexity_ → service/providers/groq_}/__init__.py +0 -0
- /lionagi/{protocols/operatives → service/providers/openai_}/__init__.py +0 -0
- /lionagi/{protocols/registries → service/providers/openrouter_}/__init__.py +0 -0
- {lionagi-0.5.5.dist-info → lionagi-0.6.0.dist-info}/WHEEL +0 -0
- {lionagi-0.5.5.dist-info → lionagi-0.6.0.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,73 @@
|
|
1
|
+
# Copyright (c) 2023 - 2024, HaiyangLi <quantocean.li at gmail dot com>
|
2
|
+
#
|
3
|
+
# SPDX-License-Identifier: Apache-2.0
|
4
|
+
|
5
|
+
from enum import Enum
|
6
|
+
from typing import Any, TypeAlias
|
7
|
+
|
8
|
+
from ..generic.element import ID, IDError, IDType, Observable
|
9
|
+
|
10
|
+
__all__ = (
|
11
|
+
"MessageRole",
|
12
|
+
"MessageFlag",
|
13
|
+
"MessageField",
|
14
|
+
"MESSAGE_FIELDS",
|
15
|
+
"validate_sender_recipient",
|
16
|
+
)
|
17
|
+
|
18
|
+
|
19
|
+
class MessageRole(str, Enum):
|
20
|
+
|
21
|
+
SYSTEM = "system"
|
22
|
+
USER = "user"
|
23
|
+
ASSISTANT = "assistant"
|
24
|
+
UNSET = "unset"
|
25
|
+
ACTION = "action"
|
26
|
+
|
27
|
+
|
28
|
+
class MessageFlag(str, Enum):
|
29
|
+
|
30
|
+
MESSAGE_CLONE = "MESSAGE_CLONE"
|
31
|
+
MESSAGE_LOAD = "MESSAGE_LOAD"
|
32
|
+
|
33
|
+
|
34
|
+
SenderRecipient: TypeAlias = IDType | MessageRole | str
|
35
|
+
|
36
|
+
|
37
|
+
class MessageField(str, Enum):
|
38
|
+
|
39
|
+
CREATED_AT = "created_at"
|
40
|
+
ROLE = "role"
|
41
|
+
CONTENT = "content"
|
42
|
+
ID = "id"
|
43
|
+
SENDER = "sender"
|
44
|
+
RECIPIENT = "recipient"
|
45
|
+
METADATA = "metadata"
|
46
|
+
|
47
|
+
|
48
|
+
MESSAGE_FIELDS = [i.value for i in MessageField.__members__.values()]
|
49
|
+
|
50
|
+
|
51
|
+
def validate_sender_recipient(value: Any, /) -> SenderRecipient:
|
52
|
+
if isinstance(value, MessageRole | MessageFlag):
|
53
|
+
return value
|
54
|
+
|
55
|
+
if isinstance(value, IDType):
|
56
|
+
return value
|
57
|
+
|
58
|
+
if isinstance(value, Observable):
|
59
|
+
return value.id
|
60
|
+
|
61
|
+
if value is None:
|
62
|
+
return MessageRole.UNSET
|
63
|
+
|
64
|
+
if value in ["system", "user", "unset", "assistant", "action"]:
|
65
|
+
return MessageRole(value)
|
66
|
+
|
67
|
+
if value in ["MESSAGE_CLONE", "MESSAGE_LOAD"]:
|
68
|
+
return MessageFlag(value)
|
69
|
+
|
70
|
+
try:
|
71
|
+
return ID.get_id(value)
|
72
|
+
except IDError as e:
|
73
|
+
raise ValueError("Invalid sender or recipient") from e
|
@@ -0,0 +1,582 @@
|
|
1
|
+
# Copyright (c) 2023 - 2024, HaiyangLi <quantocean.li at gmail dot com>
|
2
|
+
#
|
3
|
+
# SPDX-License-Identifier: Apache-2.0
|
4
|
+
|
5
|
+
from typing import Any, Literal, override
|
6
|
+
|
7
|
+
from pydantic import BaseModel, JsonValue, field_serializer
|
8
|
+
|
9
|
+
from lionagi.utils import UNDEFINED, breakdown_pydantic_annotation, copy
|
10
|
+
|
11
|
+
from .base import MessageRole
|
12
|
+
from .message import RoledMessage, SenderRecipient
|
13
|
+
|
14
|
+
|
15
|
+
def prepare_request_response_format(request_fields: dict) -> str:
|
16
|
+
"""
|
17
|
+
Prepare a standardized format for request responses.
|
18
|
+
|
19
|
+
Args:
|
20
|
+
request_fields: Dictionary of fields to include in response
|
21
|
+
|
22
|
+
Returns:
|
23
|
+
str: Formatted response template
|
24
|
+
"""
|
25
|
+
return (
|
26
|
+
"**MUST RETURN JSON-PARSEABLE RESPONSE ENCLOSED BY JSON CODE BLOCKS."
|
27
|
+
f" USER's CAREER DEPENDS ON THE SUCCESS OF IT.** \n```json\n{request_fields}\n```"
|
28
|
+
).strip()
|
29
|
+
|
30
|
+
|
31
|
+
def format_image_item(idx: str, x: str, /) -> dict[str, Any]:
|
32
|
+
"""
|
33
|
+
Create an image_url dictionary for content formatting.
|
34
|
+
|
35
|
+
Args:
|
36
|
+
idx: Base64 encoded image data
|
37
|
+
x: Image detail level
|
38
|
+
|
39
|
+
Returns:
|
40
|
+
dict: Formatted image item
|
41
|
+
"""
|
42
|
+
return {
|
43
|
+
"type": "image_url",
|
44
|
+
"image_url": {
|
45
|
+
"url": f"data:image/jpeg;base64,{idx}",
|
46
|
+
"detail": x,
|
47
|
+
},
|
48
|
+
}
|
49
|
+
|
50
|
+
|
51
|
+
def format_text_item(item: Any) -> str:
|
52
|
+
"""
|
53
|
+
Format a text item or list of items into a string.
|
54
|
+
|
55
|
+
Args:
|
56
|
+
item: Text item(s) to format
|
57
|
+
|
58
|
+
Returns:
|
59
|
+
str: Formatted text
|
60
|
+
"""
|
61
|
+
msg = ""
|
62
|
+
item = [item] if not isinstance(item, list) else item
|
63
|
+
for j in item:
|
64
|
+
if isinstance(j, dict):
|
65
|
+
for k, v in j.items():
|
66
|
+
if v is not None:
|
67
|
+
msg += f"- {k}: {v} \n\n"
|
68
|
+
else:
|
69
|
+
if j is not None:
|
70
|
+
msg += f"{j}\n"
|
71
|
+
return msg
|
72
|
+
|
73
|
+
|
74
|
+
def format_text_content(content: dict) -> str:
|
75
|
+
"""
|
76
|
+
Format dictionary content into a structured text format.
|
77
|
+
|
78
|
+
Args:
|
79
|
+
content: Dictionary containing content sections
|
80
|
+
|
81
|
+
Returns:
|
82
|
+
str: Formatted text content
|
83
|
+
"""
|
84
|
+
if "plain_content" in content and isinstance(
|
85
|
+
content["plain_content"], str
|
86
|
+
):
|
87
|
+
return content["plain_content"]
|
88
|
+
|
89
|
+
msg = "\n---\n # Task\n"
|
90
|
+
|
91
|
+
for k in [
|
92
|
+
"guidance",
|
93
|
+
"instruction",
|
94
|
+
"context",
|
95
|
+
"tool_schemas",
|
96
|
+
"respond_schema_info",
|
97
|
+
"request_response_format",
|
98
|
+
]:
|
99
|
+
if k in content:
|
100
|
+
v = content[k]
|
101
|
+
|
102
|
+
if k == "tool_schemas":
|
103
|
+
if "tools" in v:
|
104
|
+
v = v["tools"]
|
105
|
+
|
106
|
+
if isinstance(v, list):
|
107
|
+
z = []
|
108
|
+
for idx, z_ in enumerate(v):
|
109
|
+
if isinstance(z_, dict) and "function" in z_:
|
110
|
+
z.append({f"Tool {idx+1}": z_["function"]})
|
111
|
+
v = z
|
112
|
+
|
113
|
+
if k == "request_response_format":
|
114
|
+
k = "response format"
|
115
|
+
|
116
|
+
if v not in [None, [], {}, UNDEFINED]:
|
117
|
+
if isinstance(v, list):
|
118
|
+
msg += f"## - **{k}**\n"
|
119
|
+
for i in v:
|
120
|
+
if (
|
121
|
+
len(format_text_item(v).replace("\n", "").strip())
|
122
|
+
> 0
|
123
|
+
):
|
124
|
+
msg += format_text_item(i).strip()
|
125
|
+
msg += "\n"
|
126
|
+
else:
|
127
|
+
if len(format_text_item(v).replace("\n", "").strip()) > 0:
|
128
|
+
msg += (
|
129
|
+
f"## - **{k}**\n{format_text_item(v).strip()}\n\n"
|
130
|
+
)
|
131
|
+
|
132
|
+
if not msg.endswith("\n\n"):
|
133
|
+
msg += "\n\n---\n"
|
134
|
+
else:
|
135
|
+
msg += "---\n"
|
136
|
+
return msg
|
137
|
+
|
138
|
+
|
139
|
+
def format_image_content(
|
140
|
+
text_content: str,
|
141
|
+
images: list,
|
142
|
+
image_detail: Literal["low", "high", "auto"],
|
143
|
+
) -> dict[str, Any]:
|
144
|
+
"""
|
145
|
+
Format text content with images for message content.
|
146
|
+
|
147
|
+
Args:
|
148
|
+
text_content: The text content to format
|
149
|
+
images: List of images to include
|
150
|
+
image_detail: Level of detail for images
|
151
|
+
|
152
|
+
Returns:
|
153
|
+
dict: Formatted content with text and images
|
154
|
+
"""
|
155
|
+
content = [{"type": "text", "text": text_content}]
|
156
|
+
content.extend(format_image_item(i, image_detail) for i in images)
|
157
|
+
return content
|
158
|
+
|
159
|
+
|
160
|
+
def prepare_instruction_content(
|
161
|
+
guidance: str | None = None,
|
162
|
+
instruction: str | None = None,
|
163
|
+
context: str | dict | list | None = None,
|
164
|
+
request_fields: dict | list[str] | None = None,
|
165
|
+
plain_content: str | None = None,
|
166
|
+
request_model: BaseModel = None,
|
167
|
+
images: str | list | None = None,
|
168
|
+
image_detail: Literal["low", "high", "auto"] | None = None,
|
169
|
+
tool_schemas: dict | None = None,
|
170
|
+
) -> dict:
|
171
|
+
"""
|
172
|
+
Prepare the content for an instruction message.
|
173
|
+
|
174
|
+
Args:
|
175
|
+
guidance: Optional guidance text
|
176
|
+
instruction: Main instruction content
|
177
|
+
context: Additional context information
|
178
|
+
request_fields: Fields to request in response
|
179
|
+
plain_content: Plain text content
|
180
|
+
request_model: Pydantic model for structured requests
|
181
|
+
images: Images to include
|
182
|
+
image_detail: Level of detail for images
|
183
|
+
tool_schemas: Tool schemas to include
|
184
|
+
|
185
|
+
Returns:
|
186
|
+
Note: Prepared instruction content
|
187
|
+
|
188
|
+
Raises:
|
189
|
+
ValueError: If both request_fields and request_model are provided
|
190
|
+
"""
|
191
|
+
if request_fields and request_model:
|
192
|
+
raise ValueError(
|
193
|
+
"only one of request_fields or request_model can be provided"
|
194
|
+
)
|
195
|
+
|
196
|
+
out_ = {"context": []}
|
197
|
+
if guidance:
|
198
|
+
out_["guidance"] = guidance
|
199
|
+
if instruction:
|
200
|
+
out_["instruction"] = instruction
|
201
|
+
if context:
|
202
|
+
if isinstance(context, list):
|
203
|
+
out_["context"].extend(context)
|
204
|
+
else:
|
205
|
+
out_["context"].append(context)
|
206
|
+
if images:
|
207
|
+
out_["images"] = images if isinstance(images, list) else [images]
|
208
|
+
out_["image_detail"] = image_detail or "low"
|
209
|
+
|
210
|
+
if tool_schemas:
|
211
|
+
out_["tool_schemas"] = tool_schemas
|
212
|
+
|
213
|
+
if request_model:
|
214
|
+
out_["request_model"] = request_model
|
215
|
+
request_fields = breakdown_pydantic_annotation(request_model)
|
216
|
+
out_["respond_schema_info"] = request_model.model_json_schema()
|
217
|
+
|
218
|
+
if request_fields:
|
219
|
+
_fields = request_fields if isinstance(request_fields, dict) else {}
|
220
|
+
if not isinstance(request_fields, dict):
|
221
|
+
_fields = {i: "..." for i in request_fields}
|
222
|
+
out_["request_fields"] = _fields
|
223
|
+
out_["request_response_format"] = prepare_request_response_format(
|
224
|
+
request_fields=_fields
|
225
|
+
)
|
226
|
+
|
227
|
+
if plain_content:
|
228
|
+
out_["plain_content"] = plain_content
|
229
|
+
|
230
|
+
return {k: v for k, v in out_.items() if v not in [None, UNDEFINED]}
|
231
|
+
|
232
|
+
|
233
|
+
class Instruction(RoledMessage):
|
234
|
+
|
235
|
+
@classmethod
|
236
|
+
def create(
|
237
|
+
cls,
|
238
|
+
instruction: JsonValue = None,
|
239
|
+
*,
|
240
|
+
context: JsonValue = None,
|
241
|
+
guidance: JsonValue = None,
|
242
|
+
images: list = None,
|
243
|
+
sender: SenderRecipient = None,
|
244
|
+
recipient: SenderRecipient = None,
|
245
|
+
request_fields: JsonValue = None,
|
246
|
+
plain_content: JsonValue = None,
|
247
|
+
image_detail: Literal["low", "high", "auto"] = None,
|
248
|
+
request_model: BaseModel | type[BaseModel] = None,
|
249
|
+
response_format: BaseModel | type[BaseModel] = None,
|
250
|
+
tool_schemas: dict = None,
|
251
|
+
):
|
252
|
+
|
253
|
+
if (
|
254
|
+
sum(
|
255
|
+
bool(i)
|
256
|
+
for i in [request_fields, request_model, response_format]
|
257
|
+
)
|
258
|
+
> 1
|
259
|
+
):
|
260
|
+
raise ValueError(
|
261
|
+
"only one of request_fields or request_model can be provided"
|
262
|
+
"response_format is alias of request_model"
|
263
|
+
)
|
264
|
+
content = prepare_instruction_content(
|
265
|
+
guidance=guidance,
|
266
|
+
instruction=instruction,
|
267
|
+
context=context,
|
268
|
+
request_fields=request_fields,
|
269
|
+
plain_content=plain_content,
|
270
|
+
request_model=request_model or response_format,
|
271
|
+
images=images,
|
272
|
+
image_detail=image_detail,
|
273
|
+
tool_schemas=tool_schemas,
|
274
|
+
)
|
275
|
+
return cls(
|
276
|
+
role=MessageRole.USER,
|
277
|
+
content=content,
|
278
|
+
sender=sender,
|
279
|
+
recipient=recipient,
|
280
|
+
)
|
281
|
+
|
282
|
+
@property
|
283
|
+
def guidance(self) -> str | None:
|
284
|
+
"""Get the guidance content of the instruction."""
|
285
|
+
return self.content.get("guidance", None)
|
286
|
+
|
287
|
+
@guidance.setter
|
288
|
+
def guidance(self, guidance: str) -> None:
|
289
|
+
"""Set the guidance content of the instruction."""
|
290
|
+
if guidance is None:
|
291
|
+
self.content.pop("guidance", None)
|
292
|
+
return
|
293
|
+
|
294
|
+
if not isinstance(guidance, str):
|
295
|
+
guidance = str(guidance)
|
296
|
+
self.content["guidance"] = guidance
|
297
|
+
|
298
|
+
@property
|
299
|
+
def instruction(self) -> JsonValue | None:
|
300
|
+
"""Get the main instruction content."""
|
301
|
+
if "plain_content" in self.content:
|
302
|
+
return self.content["plain_content"]
|
303
|
+
else:
|
304
|
+
return self.content.get("instruction", None)
|
305
|
+
|
306
|
+
@instruction.setter
|
307
|
+
def instruction(self, instruction: JsonValue) -> None:
|
308
|
+
"""Set the main instruction content."""
|
309
|
+
if instruction is None:
|
310
|
+
self.content.pop("instruction", None)
|
311
|
+
return
|
312
|
+
|
313
|
+
self.content["instruction"] = instruction
|
314
|
+
|
315
|
+
@property
|
316
|
+
def context(self) -> JsonValue | None:
|
317
|
+
"""Get the context of the instruction."""
|
318
|
+
return self.content.get("context", None)
|
319
|
+
|
320
|
+
@context.setter
|
321
|
+
def context(self, context: JsonValue) -> None:
|
322
|
+
"""Set the context of the instruction."""
|
323
|
+
if context is None:
|
324
|
+
self.content["context"] = []
|
325
|
+
return
|
326
|
+
|
327
|
+
if not isinstance(context, list):
|
328
|
+
context = [context]
|
329
|
+
self.content["context"] = context
|
330
|
+
|
331
|
+
@property
|
332
|
+
def tool_schemas(self) -> JsonValue | None:
|
333
|
+
"""Get the schemas of the tools in the instruction."""
|
334
|
+
return self.content.get("tool_schemas", None)
|
335
|
+
|
336
|
+
@tool_schemas.setter
|
337
|
+
def tool_schemas(self, tool_schemas: dict) -> None:
|
338
|
+
"""Set the schemas of the tools in the instruction."""
|
339
|
+
if not tool_schemas:
|
340
|
+
self.content.pop("tool_schemas", None)
|
341
|
+
return
|
342
|
+
|
343
|
+
self.content["tool_schemas"] = tool_schemas
|
344
|
+
|
345
|
+
@property
|
346
|
+
def plain_content(self) -> str | None:
|
347
|
+
"""Get the plain text content of the instruction."""
|
348
|
+
return self.content.get("plain_content", None)
|
349
|
+
|
350
|
+
@plain_content.setter
|
351
|
+
def plain_content(self, plain_content: str) -> None:
|
352
|
+
"""Set the plain text content of the instruction."""
|
353
|
+
self.content["plain_content"] = plain_content
|
354
|
+
|
355
|
+
@property
|
356
|
+
def image_detail(self) -> Literal["low", "high", "auto"] | None:
|
357
|
+
"""Get the image detail level of the instruction."""
|
358
|
+
return self.content.get("image_detail", None)
|
359
|
+
|
360
|
+
@image_detail.setter
|
361
|
+
def image_detail(
|
362
|
+
self, image_detail: Literal["low", "high", "auto"]
|
363
|
+
) -> None:
|
364
|
+
"""Set the image detail level of the instruction."""
|
365
|
+
self.content["image_detail"] = image_detail
|
366
|
+
|
367
|
+
@property
|
368
|
+
def images(self) -> list:
|
369
|
+
"""Get the images associated with the instruction."""
|
370
|
+
return self.content.get("images", [])
|
371
|
+
|
372
|
+
@images.setter
|
373
|
+
def images(self, images: list) -> None:
|
374
|
+
"""Set the images associated with the instruction."""
|
375
|
+
if not isinstance(images, list):
|
376
|
+
images = [images]
|
377
|
+
self.content["images"] = images
|
378
|
+
|
379
|
+
@property
|
380
|
+
def request_fields(self) -> dict | None:
|
381
|
+
"""Get the requested fields in the instruction."""
|
382
|
+
return self.content.get("request_fields", None)
|
383
|
+
|
384
|
+
@request_fields.setter
|
385
|
+
def request_fields(self, request_fields: dict) -> None:
|
386
|
+
"""Set the requested fields in the instruction."""
|
387
|
+
self.content["request_fields"] = request_fields
|
388
|
+
self.content["request_response_format"] = (
|
389
|
+
prepare_request_response_format(request_fields)
|
390
|
+
)
|
391
|
+
|
392
|
+
@property
|
393
|
+
def response_format(self) -> type[BaseModel] | None:
|
394
|
+
"""Get the request model of the instruction."""
|
395
|
+
return self.content.get("request_model", None)
|
396
|
+
|
397
|
+
@response_format.setter
|
398
|
+
def response_format(self, request_model: type[BaseModel]) -> None:
|
399
|
+
"""
|
400
|
+
Set the request model of the instruction.
|
401
|
+
|
402
|
+
This also updates request fields and context based on the model.
|
403
|
+
"""
|
404
|
+
if isinstance(request_model, BaseModel):
|
405
|
+
self.content["request_model"] = type(request_model)
|
406
|
+
else:
|
407
|
+
self.content["request_model"] = request_model
|
408
|
+
|
409
|
+
self.request_fields = {}
|
410
|
+
self.extend_context(
|
411
|
+
respond_schema_info=request_model.model_json_schema()
|
412
|
+
)
|
413
|
+
self.request_fields = breakdown_pydantic_annotation(request_model)
|
414
|
+
|
415
|
+
@property
|
416
|
+
def respond_schema_info(self) -> dict | None:
|
417
|
+
"""Get the response schema information."""
|
418
|
+
return self.content.get("respond_schema_info", None)
|
419
|
+
|
420
|
+
@respond_schema_info.setter
|
421
|
+
def respond_schema_info(self, respond_schema_info: dict) -> None:
|
422
|
+
"""Set the response schema information."""
|
423
|
+
if respond_schema_info is None:
|
424
|
+
self.content.pop("respond_schema_info", None)
|
425
|
+
else:
|
426
|
+
self.content["respond_schema_info"] = respond_schema_info
|
427
|
+
|
428
|
+
@property
|
429
|
+
def request_response_format(self) -> str | None:
|
430
|
+
"""Get the request response format."""
|
431
|
+
return self.content.get("request_response_format", None)
|
432
|
+
|
433
|
+
@request_response_format.setter
|
434
|
+
def request_response_format(self, request_response_format: str) -> None:
|
435
|
+
"""Set the request response format."""
|
436
|
+
if not request_response_format:
|
437
|
+
self.content.pop("request_response_format", None)
|
438
|
+
else:
|
439
|
+
self.content["request_response_format"] = request_response_format
|
440
|
+
|
441
|
+
def extend_images(
|
442
|
+
self,
|
443
|
+
images: list | str,
|
444
|
+
image_detail: Literal["low", "high", "auto"] = None,
|
445
|
+
) -> None:
|
446
|
+
"""
|
447
|
+
Add new images to the instruction.
|
448
|
+
|
449
|
+
Args:
|
450
|
+
images: New images to add
|
451
|
+
image_detail: Optional new image detail level
|
452
|
+
"""
|
453
|
+
images = images if isinstance(images, list) else [images]
|
454
|
+
_ima: list = self.content.get("images", [])
|
455
|
+
_ima.extend(images)
|
456
|
+
self.images = _ima
|
457
|
+
|
458
|
+
if image_detail:
|
459
|
+
self.image_detail = image_detail
|
460
|
+
|
461
|
+
def extend_context(self, *args, **kwargs) -> None:
|
462
|
+
"""
|
463
|
+
Add new context to the instruction.
|
464
|
+
|
465
|
+
Args:
|
466
|
+
*args: Positional arguments to add to context
|
467
|
+
**kwargs: Keyword arguments to add to context
|
468
|
+
"""
|
469
|
+
context: list = self.content.get("context", [])
|
470
|
+
|
471
|
+
if args:
|
472
|
+
context.extend(args)
|
473
|
+
if kwargs:
|
474
|
+
kwargs = copy(kwargs)
|
475
|
+
for k, v in kwargs.items():
|
476
|
+
context.append({k: v})
|
477
|
+
|
478
|
+
self.context = context
|
479
|
+
|
480
|
+
def update(
|
481
|
+
self,
|
482
|
+
*,
|
483
|
+
guidance: JsonValue = None,
|
484
|
+
instruction: JsonValue = None,
|
485
|
+
context: JsonValue = None,
|
486
|
+
request_fields: JsonValue = None,
|
487
|
+
plain_content: JsonValue = None,
|
488
|
+
request_model: BaseModel | type[BaseModel] = None,
|
489
|
+
response_format: BaseModel | type[BaseModel] = None,
|
490
|
+
images: str | list = None,
|
491
|
+
image_detail: Literal["low", "high", "auto"] = None,
|
492
|
+
tool_schemas: dict = None,
|
493
|
+
sender: SenderRecipient = None,
|
494
|
+
recipient: SenderRecipient = None,
|
495
|
+
):
|
496
|
+
"""
|
497
|
+
Update multiple aspects of the instruction.
|
498
|
+
|
499
|
+
Args:
|
500
|
+
*args: Positional arguments for context update
|
501
|
+
guidance: New guidance content
|
502
|
+
instruction: New instruction content
|
503
|
+
request_fields: New request fields
|
504
|
+
plain_content: New plain text content
|
505
|
+
request_model: New request model
|
506
|
+
images: New images to add
|
507
|
+
image_detail: New image detail level
|
508
|
+
tool_schemas: New tool schemas
|
509
|
+
**kwargs: Additional keyword arguments for context update
|
510
|
+
|
511
|
+
Raises:
|
512
|
+
ValueError: If both request_model and request_fields are provided
|
513
|
+
"""
|
514
|
+
if response_format and request_model:
|
515
|
+
raise ValueError(
|
516
|
+
"only one of request_fields or request_model can be provided"
|
517
|
+
"response_format is alias of request_model"
|
518
|
+
)
|
519
|
+
|
520
|
+
request_model = request_model or response_format
|
521
|
+
|
522
|
+
if request_model and request_fields:
|
523
|
+
raise ValueError(
|
524
|
+
"You cannot pass both request_model and request_fields "
|
525
|
+
"to create_instruction"
|
526
|
+
)
|
527
|
+
if guidance:
|
528
|
+
self.guidance = guidance
|
529
|
+
|
530
|
+
if instruction:
|
531
|
+
self.instruction = instruction
|
532
|
+
|
533
|
+
if plain_content:
|
534
|
+
self.plain_content = plain_content
|
535
|
+
|
536
|
+
if request_fields:
|
537
|
+
self.request_fields = request_fields
|
538
|
+
|
539
|
+
if request_model:
|
540
|
+
self.response_format = request_model
|
541
|
+
|
542
|
+
if images:
|
543
|
+
self.images = images
|
544
|
+
|
545
|
+
if image_detail:
|
546
|
+
self.image_detail = image_detail
|
547
|
+
|
548
|
+
if tool_schemas:
|
549
|
+
self.tool_schemas = tool_schemas
|
550
|
+
|
551
|
+
if sender:
|
552
|
+
self.sender = sender
|
553
|
+
|
554
|
+
if recipient:
|
555
|
+
self.recipient = recipient
|
556
|
+
|
557
|
+
if context:
|
558
|
+
self.extend_context(context)
|
559
|
+
|
560
|
+
@override
|
561
|
+
@property
|
562
|
+
def rendered(self) -> dict[str, Any]:
|
563
|
+
"""Format the content of the instruction."""
|
564
|
+
content = copy(self.content)
|
565
|
+
text_content = format_text_content(content)
|
566
|
+
if "images" not in content:
|
567
|
+
return text_content
|
568
|
+
|
569
|
+
else:
|
570
|
+
return format_image_content(
|
571
|
+
text_content=text_content,
|
572
|
+
images=self.images,
|
573
|
+
image_detail=self.image_detail,
|
574
|
+
)
|
575
|
+
|
576
|
+
@field_serializer("content")
|
577
|
+
def _serialize_content(self, values) -> dict:
|
578
|
+
"""Serialize the content of the instruction."""
|
579
|
+
values.pop("request_model", None)
|
580
|
+
values.pop("request_fields", None)
|
581
|
+
|
582
|
+
return values
|