lionagi 0.5.5__py3-none-any.whl → 0.6.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 +18 -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 +19 -16
- 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 +2 -2
- 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 +26 -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/operations/types.py +13 -0
- lionagi/operations/utils.py +6 -3
- lionagi/operatives/action/function_calling.py +136 -0
- lionagi/operatives/action/manager.py +236 -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/{core/models/__init__.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/_concepts.py +94 -0
- lionagi/protocols/adapters/adapter.py +23 -7
- lionagi/protocols/adapters/json_adapter.py +72 -14
- lionagi/protocols/adapters/pandas_/csv_adapter.py +50 -0
- lionagi/protocols/adapters/pandas_/excel_adapter.py +52 -0
- lionagi/protocols/adapters/pandas_/pd_dataframe_adapter.py +31 -0
- lionagi/protocols/adapters/pandas_/pd_series_adapter.py +17 -0
- lionagi/protocols/adapters/types.py +18 -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 +193 -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 +125 -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 +1 -16
- 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 +146 -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/service/types.py +18 -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.1.dist-info}/METADATA +10 -9
- lionagi-0.6.1.dist-info/RECORD +169 -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/_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/anthropic_/version.py +0 -5
- 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/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 → protocols/adapters/pandas_}/__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.1.dist-info}/WHEEL +0 -0
- {lionagi-0.5.5.dist-info → lionagi-0.6.1.dist-info}/licenses/LICENSE +0 -0
@@ -1,256 +0,0 @@
|
|
1
|
-
# Copyright (c) 2023 - 2024, HaiyangLi <quantocean.li at gmail dot com>
|
2
|
-
#
|
3
|
-
# SPDX-License-Identifier: Apache-2.0
|
4
|
-
|
5
|
-
from lionagi.core.typing import (
|
6
|
-
ID,
|
7
|
-
UNDEFINED,
|
8
|
-
Any,
|
9
|
-
BaseModel,
|
10
|
-
IDError,
|
11
|
-
IDType,
|
12
|
-
Literal,
|
13
|
-
Note,
|
14
|
-
)
|
15
|
-
from lionagi.integrations.pydantic_ import break_down_pydantic_annotation
|
16
|
-
from lionagi.libs.utils import time
|
17
|
-
|
18
|
-
DEFAULT_SYSTEM = "You are a helpful AI assistant. Let's think step by step."
|
19
|
-
|
20
|
-
|
21
|
-
def format_system_content(
|
22
|
-
system_datetime: bool | str | None,
|
23
|
-
system_message: str,
|
24
|
-
) -> Note:
|
25
|
-
"""
|
26
|
-
Format system message content with optional datetime information.
|
27
|
-
|
28
|
-
Args:
|
29
|
-
system_datetime: Flag or string for datetime inclusion
|
30
|
-
system_message: The system message content
|
31
|
-
|
32
|
-
Returns:
|
33
|
-
Note: Formatted system content
|
34
|
-
"""
|
35
|
-
content = Note(system=str(system_message or DEFAULT_SYSTEM))
|
36
|
-
if system_datetime:
|
37
|
-
if isinstance(system_datetime, str):
|
38
|
-
content["system_datetime"] = system_datetime
|
39
|
-
else:
|
40
|
-
content["system_datetime"] = time(type_="iso", timespec="minutes")
|
41
|
-
return content
|
42
|
-
|
43
|
-
|
44
|
-
def prepare_request_response_format(request_fields: dict) -> str:
|
45
|
-
"""
|
46
|
-
Prepare a standardized format for request responses.
|
47
|
-
|
48
|
-
Args:
|
49
|
-
request_fields: Dictionary of fields to include in response
|
50
|
-
|
51
|
-
Returns:
|
52
|
-
str: Formatted response template
|
53
|
-
"""
|
54
|
-
return (
|
55
|
-
"**MUST RETURN JSON-PARSEABLE RESPONSE ENCLOSED BY JSON CODE BLO"
|
56
|
-
f"CKS.** \n```json\n{request_fields}\n```"
|
57
|
-
).strip()
|
58
|
-
|
59
|
-
|
60
|
-
def format_image_item(idx: str, x: str, /) -> dict[str, Any]:
|
61
|
-
"""
|
62
|
-
Create an image_url dictionary for content formatting.
|
63
|
-
|
64
|
-
Args:
|
65
|
-
idx: Base64 encoded image data
|
66
|
-
x: Image detail level
|
67
|
-
|
68
|
-
Returns:
|
69
|
-
dict: Formatted image item
|
70
|
-
"""
|
71
|
-
return {
|
72
|
-
"type": "image_url",
|
73
|
-
"image_url": {
|
74
|
-
"url": f"data:image/jpeg;base64,{idx}",
|
75
|
-
"detail": x,
|
76
|
-
},
|
77
|
-
}
|
78
|
-
|
79
|
-
|
80
|
-
def format_text_item(item: Any) -> str:
|
81
|
-
"""
|
82
|
-
Format a text item or list of items into a string.
|
83
|
-
|
84
|
-
Args:
|
85
|
-
item: Text item(s) to format
|
86
|
-
|
87
|
-
Returns:
|
88
|
-
str: Formatted text
|
89
|
-
"""
|
90
|
-
msg = ""
|
91
|
-
item = [item] if not isinstance(item, list) else item
|
92
|
-
for j in item:
|
93
|
-
if isinstance(j, dict):
|
94
|
-
for k, v in j.items():
|
95
|
-
if v is not None:
|
96
|
-
msg += f"- {k}: {v} \n\n"
|
97
|
-
else:
|
98
|
-
if j is not None:
|
99
|
-
msg += f"{j}\n"
|
100
|
-
return msg
|
101
|
-
|
102
|
-
|
103
|
-
def format_text_content(content: dict) -> str:
|
104
|
-
"""
|
105
|
-
Format dictionary content into a structured text format.
|
106
|
-
|
107
|
-
Args:
|
108
|
-
content: Dictionary containing content sections
|
109
|
-
|
110
|
-
Returns:
|
111
|
-
str: Formatted text content
|
112
|
-
"""
|
113
|
-
if "plain_content" in content and isinstance(
|
114
|
-
content["plain_content"], str
|
115
|
-
):
|
116
|
-
return content["plain_content"]
|
117
|
-
|
118
|
-
msg = "\n---\n # Task\n"
|
119
|
-
for k, v in content.items():
|
120
|
-
if k in [
|
121
|
-
"guidance",
|
122
|
-
"instruction",
|
123
|
-
"context",
|
124
|
-
"request_response_format",
|
125
|
-
"tool_schemas",
|
126
|
-
]:
|
127
|
-
if k == "request_response_format":
|
128
|
-
k = "response format"
|
129
|
-
msg += f"## **Task {k}**\n{format_text_item(v)}\n\n"
|
130
|
-
msg += "\n\n---\n"
|
131
|
-
return msg
|
132
|
-
|
133
|
-
|
134
|
-
def format_image_content(
|
135
|
-
text_content: str,
|
136
|
-
images: list,
|
137
|
-
image_detail: Literal["low", "high", "auto"],
|
138
|
-
) -> dict[str, Any]:
|
139
|
-
"""
|
140
|
-
Format text content with images for message content.
|
141
|
-
|
142
|
-
Args:
|
143
|
-
text_content: The text content to format
|
144
|
-
images: List of images to include
|
145
|
-
image_detail: Level of detail for images
|
146
|
-
|
147
|
-
Returns:
|
148
|
-
dict: Formatted content with text and images
|
149
|
-
"""
|
150
|
-
content = [{"type": "text", "text": text_content}]
|
151
|
-
content.extend(format_image_item(i, image_detail) for i in images)
|
152
|
-
return content
|
153
|
-
|
154
|
-
|
155
|
-
def prepare_instruction_content(
|
156
|
-
guidance: str | None = None,
|
157
|
-
instruction: str | None = None,
|
158
|
-
context: str | dict | list | None = None,
|
159
|
-
request_fields: dict | list[str] | None = None,
|
160
|
-
plain_content: str | None = None,
|
161
|
-
request_model: BaseModel = None,
|
162
|
-
images: str | list | None = None,
|
163
|
-
image_detail: Literal["low", "high", "auto"] | None = None,
|
164
|
-
tool_schemas: dict | None = None,
|
165
|
-
) -> Note:
|
166
|
-
"""
|
167
|
-
Prepare the content for an instruction message.
|
168
|
-
|
169
|
-
Args:
|
170
|
-
guidance: Optional guidance text
|
171
|
-
instruction: Main instruction content
|
172
|
-
context: Additional context information
|
173
|
-
request_fields: Fields to request in response
|
174
|
-
plain_content: Plain text content
|
175
|
-
request_model: Pydantic model for structured requests
|
176
|
-
images: Images to include
|
177
|
-
image_detail: Level of detail for images
|
178
|
-
tool_schemas: Tool schemas to include
|
179
|
-
|
180
|
-
Returns:
|
181
|
-
Note: Prepared instruction content
|
182
|
-
|
183
|
-
Raises:
|
184
|
-
ValueError: If both request_fields and request_model are provided
|
185
|
-
"""
|
186
|
-
if request_fields and request_model:
|
187
|
-
raise ValueError(
|
188
|
-
"only one of request_fields or request_model can be provided"
|
189
|
-
)
|
190
|
-
|
191
|
-
out_ = {"context": []}
|
192
|
-
if guidance:
|
193
|
-
out_["guidance"] = guidance
|
194
|
-
if instruction:
|
195
|
-
out_["instruction"] = instruction
|
196
|
-
if context:
|
197
|
-
if isinstance(context, list):
|
198
|
-
out_["context"].extend(context)
|
199
|
-
else:
|
200
|
-
out_["context"].append(context)
|
201
|
-
if images:
|
202
|
-
out_["images"] = images if isinstance(images, list) else [images]
|
203
|
-
out_["image_detail"] = image_detail or "low"
|
204
|
-
|
205
|
-
if tool_schemas:
|
206
|
-
out_["tool_schemas"] = tool_schemas
|
207
|
-
|
208
|
-
if request_model:
|
209
|
-
out_["request_model"] = request_model
|
210
|
-
request_fields = break_down_pydantic_annotation(request_model)
|
211
|
-
out_["context"].append(
|
212
|
-
{"respond_schema_info": request_model.model_json_schema()}
|
213
|
-
)
|
214
|
-
|
215
|
-
if request_fields:
|
216
|
-
_fields = request_fields if isinstance(request_fields, dict) else {}
|
217
|
-
if not isinstance(request_fields, dict):
|
218
|
-
_fields = {i: "..." for i in request_fields}
|
219
|
-
out_["request_fields"] = _fields
|
220
|
-
out_["request_response_format"] = prepare_request_response_format(
|
221
|
-
request_fields=_fields
|
222
|
-
)
|
223
|
-
|
224
|
-
if plain_content:
|
225
|
-
out_["plain_content"] = plain_content
|
226
|
-
|
227
|
-
return Note(
|
228
|
-
**{k: v for k, v in out_.items() if v not in [None, UNDEFINED]},
|
229
|
-
)
|
230
|
-
|
231
|
-
|
232
|
-
def validate_sender_recipient(
|
233
|
-
value: Any, /
|
234
|
-
) -> IDType | Literal["system", "user", "N/A", "assistant"]:
|
235
|
-
"""
|
236
|
-
Validate sender and recipient fields for mail-like communication.
|
237
|
-
|
238
|
-
Args:
|
239
|
-
value: The value to validate
|
240
|
-
|
241
|
-
Returns:
|
242
|
-
Union[IDType, Literal]: Valid sender/recipient value
|
243
|
-
|
244
|
-
Raises:
|
245
|
-
ValueError: If value is not a valid sender or recipient
|
246
|
-
"""
|
247
|
-
if value in ["system", "user", "N/A", "assistant"]:
|
248
|
-
return value
|
249
|
-
|
250
|
-
if value is None:
|
251
|
-
return "N/A"
|
252
|
-
|
253
|
-
try:
|
254
|
-
return ID.get_id(value)
|
255
|
-
except IDError as e:
|
256
|
-
raise ValueError("Invalid sender or recipient") from e
|
lionagi/core/forms/types.py
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
# Copyright (c) 2023 - 2024, HaiyangLi <quantocean.li at gmail dot com>
|
2
|
-
#
|
3
|
-
# SPDX-License-Identifier: Apache-2.0
|
4
|
-
|
5
|
-
from .base import BaseForm
|
6
|
-
from .form import Form
|
7
|
-
from .report import Report
|
8
|
-
|
9
|
-
__all__ = (
|
10
|
-
"BaseForm",
|
11
|
-
"Form",
|
12
|
-
"Report",
|
13
|
-
)
|
@@ -1,422 +0,0 @@
|
|
1
|
-
# Copyright (c) 2023 - 2024, HaiyangLi <quantocean.li at gmail dot com>
|
2
|
-
#
|
3
|
-
# SPDX-License-Identifier: Apache-2.0
|
4
|
-
|
5
|
-
from pydantic import field_serializer
|
6
|
-
from typing_extensions import override
|
7
|
-
|
8
|
-
from lionagi.core._class_registry import get_class
|
9
|
-
from lionagi.core.typing import (
|
10
|
-
UNDEFINED,
|
11
|
-
Any,
|
12
|
-
ClassVar,
|
13
|
-
Field,
|
14
|
-
FieldInfo,
|
15
|
-
FieldModel,
|
16
|
-
Note,
|
17
|
-
OperableModel,
|
18
|
-
PydanticUndefined,
|
19
|
-
TypeVar,
|
20
|
-
)
|
21
|
-
from lionagi.libs.utils import copy, time
|
22
|
-
from lionagi.protocols.adapters.adapter import Adapter, AdapterRegistry
|
23
|
-
from lionagi.protocols.registries._component_registry import (
|
24
|
-
ComponentAdapterRegistry,
|
25
|
-
)
|
26
|
-
|
27
|
-
from .element import Element
|
28
|
-
|
29
|
-
FIELD_NAME = TypeVar("FIELD_NAME", bound=str)
|
30
|
-
|
31
|
-
DEFAULT_SERIALIZATION_INCLUDE: set[str] = {
|
32
|
-
"ln_id",
|
33
|
-
"timestamp",
|
34
|
-
"metadata",
|
35
|
-
"content",
|
36
|
-
"embedding",
|
37
|
-
}
|
38
|
-
|
39
|
-
|
40
|
-
class Component(Element, OperableModel):
|
41
|
-
"""Extended base class for components in the Lion framework.
|
42
|
-
|
43
|
-
The Component class extends Element with additional functionality for:
|
44
|
-
- Metadata management through Note objects
|
45
|
-
- Content storage of any type
|
46
|
-
- Vector embeddings storage
|
47
|
-
- Dynamic field management
|
48
|
-
- Adapter registry integration
|
49
|
-
- Enhanced serialization
|
50
|
-
|
51
|
-
Key Features:
|
52
|
-
- Flexible metadata storage
|
53
|
-
- Content of any type
|
54
|
-
- Vector embeddings support
|
55
|
-
- Dynamic field addition/updates
|
56
|
-
- Last update tracking
|
57
|
-
- Recursive serialization
|
58
|
-
- Adapter pattern support
|
59
|
-
|
60
|
-
Attributes:
|
61
|
-
metadata (Note): Additional metadata for the component. Stores arbitrary
|
62
|
-
nested data.
|
63
|
-
content (Any): The main content of the Component. Can be of any type.
|
64
|
-
embedding (list[float]): Vector embedding for the component. Used for
|
65
|
-
similarity operations.
|
66
|
-
"""
|
67
|
-
|
68
|
-
metadata: Note = Field(
|
69
|
-
default_factory=Note,
|
70
|
-
description="Additional metadata for the component",
|
71
|
-
)
|
72
|
-
|
73
|
-
content: Any = Field(
|
74
|
-
default=None,
|
75
|
-
description="The main content of the Component",
|
76
|
-
)
|
77
|
-
|
78
|
-
embedding: list[float] = Field(default_factory=list)
|
79
|
-
|
80
|
-
_adapter_registry: ClassVar = ComponentAdapterRegistry
|
81
|
-
|
82
|
-
@field_serializer("metadata")
|
83
|
-
def _serialize_metadata(self, value: Note) -> dict:
|
84
|
-
"""Serialize metadata Note recursively."""
|
85
|
-
return self._serialize_note_recursive(value)
|
86
|
-
|
87
|
-
def _serialize_note_recursive(self, note: Note) -> dict:
|
88
|
-
"""Recursively serialize a Note object and its nested Notes."""
|
89
|
-
result = {}
|
90
|
-
for key, value in note.items():
|
91
|
-
if isinstance(value, Note):
|
92
|
-
result[key] = self._serialize_note_recursive(value)
|
93
|
-
else:
|
94
|
-
result[key] = value
|
95
|
-
return result
|
96
|
-
|
97
|
-
def add_field(
|
98
|
-
self,
|
99
|
-
field_name: FIELD_NAME,
|
100
|
-
/,
|
101
|
-
value: Any = UNDEFINED,
|
102
|
-
annotation: type = UNDEFINED,
|
103
|
-
field_obj: FieldInfo = UNDEFINED,
|
104
|
-
field_model: FieldModel = UNDEFINED,
|
105
|
-
**kwargs,
|
106
|
-
) -> None:
|
107
|
-
"""
|
108
|
-
Add a new field to the component's extra fields.
|
109
|
-
|
110
|
-
Args:
|
111
|
-
field_name: The name of the field to add.
|
112
|
-
value: The value of the field.
|
113
|
-
annotation: Type annotation for the field.
|
114
|
-
field_obj: A pre-configured FieldInfo object.
|
115
|
-
**kwargs: Additional keyword arguments for Field configuration.
|
116
|
-
|
117
|
-
Raises:
|
118
|
-
ValueError: If the field already exists.
|
119
|
-
"""
|
120
|
-
if field_name in self.all_fields:
|
121
|
-
raise ValueError(f"Field '{field_name}' already exists")
|
122
|
-
|
123
|
-
self.update_field(
|
124
|
-
field_name,
|
125
|
-
value=value,
|
126
|
-
annotation=annotation,
|
127
|
-
field_obj=field_obj,
|
128
|
-
field_model=field_model,
|
129
|
-
**kwargs,
|
130
|
-
)
|
131
|
-
|
132
|
-
def update_field(
|
133
|
-
self,
|
134
|
-
field_name: FIELD_NAME,
|
135
|
-
/,
|
136
|
-
value: Any = UNDEFINED,
|
137
|
-
annotation: type = UNDEFINED,
|
138
|
-
field_obj: FieldInfo = UNDEFINED,
|
139
|
-
field_model: FieldModel = UNDEFINED,
|
140
|
-
**kwargs,
|
141
|
-
) -> None:
|
142
|
-
"""
|
143
|
-
Update an existing field or create a new one if it doesn't exist.
|
144
|
-
|
145
|
-
Args:
|
146
|
-
field_name: The name of the field to update or create.
|
147
|
-
value: The new value for the field.
|
148
|
-
annotation: Type annotation for the field.
|
149
|
-
field_obj: A pre-configured FieldInfo object.
|
150
|
-
**kwargs: Additional keyword arguments for Field configuration.
|
151
|
-
|
152
|
-
Raises:
|
153
|
-
ValueError: If both 'default' and 'default_factory' are
|
154
|
-
provided in kwargs.
|
155
|
-
"""
|
156
|
-
super().update_field(
|
157
|
-
field_name,
|
158
|
-
value=value,
|
159
|
-
annotation=annotation,
|
160
|
-
field_obj=field_obj,
|
161
|
-
field_model=field_model,
|
162
|
-
**kwargs,
|
163
|
-
)
|
164
|
-
self._add_last_update(field_name)
|
165
|
-
|
166
|
-
def _add_last_update(self, field_name: FIELD_NAME, /) -> None:
|
167
|
-
current_time = time()
|
168
|
-
self.metadata.set(["last_updated", field_name], current_time)
|
169
|
-
|
170
|
-
@override
|
171
|
-
def to_dict(self, **kwargs: Any) -> dict[str, Any]:
|
172
|
-
"""
|
173
|
-
Convert the component to a dictionary representation.
|
174
|
-
|
175
|
-
Args:
|
176
|
-
**kwargs: Additional arguments to pass to model_dump.
|
177
|
-
|
178
|
-
Returns:
|
179
|
-
dict[str, Any]: A dictionary representation of the component.
|
180
|
-
"""
|
181
|
-
dict_ = self.model_dump(**kwargs)
|
182
|
-
if isinstance(self.content, Note):
|
183
|
-
dict_["content"] = self._serialize_note_recursive(self.content)
|
184
|
-
extra_fields = dict_.pop("extra_fields", {})
|
185
|
-
dict_ = {**dict_, **extra_fields, "lion_class": self.class_name()}
|
186
|
-
for i in list(dict_.keys()):
|
187
|
-
if dict_[i] is UNDEFINED:
|
188
|
-
dict_.pop(i)
|
189
|
-
return dict_
|
190
|
-
|
191
|
-
def to_note(self, **kwargs: Any) -> Note:
|
192
|
-
"""Convert the component to a Note object."""
|
193
|
-
return Note(**self.to_dict(**kwargs))
|
194
|
-
|
195
|
-
@override
|
196
|
-
@classmethod
|
197
|
-
def from_dict(cls, data: dict[str, Any], /, **kwargs: Any) -> "Component":
|
198
|
-
"""
|
199
|
-
Create a component instance from a dictionary.
|
200
|
-
|
201
|
-
Args:
|
202
|
-
data: The dictionary containing component data.
|
203
|
-
**kwargs: Additional arguments for Pydantic model validation.
|
204
|
-
|
205
|
-
Returns:
|
206
|
-
T: An instance of the Component class or its subclass.
|
207
|
-
"""
|
208
|
-
input_data = copy(data)
|
209
|
-
if "lion_class" in input_data:
|
210
|
-
cls = get_class(input_data.pop("lion_class"))
|
211
|
-
if cls.from_dict.__func__ != Component.from_dict.__func__:
|
212
|
-
return cls.from_dict(input_data, **kwargs)
|
213
|
-
|
214
|
-
extra_fields = {}
|
215
|
-
for k, v in list(input_data.items()):
|
216
|
-
if k not in cls.model_fields:
|
217
|
-
extra_fields[k] = input_data.pop(k)
|
218
|
-
obj = cls.model_validate(input_data, **kwargs)
|
219
|
-
for k, v in extra_fields.items():
|
220
|
-
obj.update_field(k, value=v)
|
221
|
-
|
222
|
-
metadata = copy(data.get("metadata", {}))
|
223
|
-
last_updated = metadata.get("last_updated", None)
|
224
|
-
if last_updated is not None:
|
225
|
-
obj.metadata.set(["last_updated"], last_updated)
|
226
|
-
else:
|
227
|
-
obj.metadata.pop(["last_updated"], None)
|
228
|
-
return obj
|
229
|
-
|
230
|
-
@override
|
231
|
-
def __setattr__(self, field_name: str, value: Any) -> None:
|
232
|
-
"""Set attribute value with metadata tracking.
|
233
|
-
|
234
|
-
This method prevents direct assignment to metadata and extra_fields,
|
235
|
-
and tracks the last update time of modified fields.
|
236
|
-
|
237
|
-
Args:
|
238
|
-
field_name: Name of the field to set.
|
239
|
-
value: Value to set.
|
240
|
-
|
241
|
-
Raises:
|
242
|
-
AttributeError: If attempting to directly assign to metadata or
|
243
|
-
extra_fields.
|
244
|
-
"""
|
245
|
-
if field_name == "metadata":
|
246
|
-
raise AttributeError("Cannot directly assign to metadata.")
|
247
|
-
elif field_name == "extra_fields":
|
248
|
-
raise AttributeError("Cannot directly assign to extra_fields")
|
249
|
-
if field_name in self.extra_fields:
|
250
|
-
object.__setattr__(self, field_name, value)
|
251
|
-
else:
|
252
|
-
super().__setattr__(field_name, value)
|
253
|
-
|
254
|
-
self._add_last_update(field_name)
|
255
|
-
|
256
|
-
@override
|
257
|
-
def __getattr__(self, field_name: str) -> Any:
|
258
|
-
"""Get attribute value with default handling.
|
259
|
-
|
260
|
-
This method provides access to extra fields with proper default value
|
261
|
-
handling.
|
262
|
-
|
263
|
-
Args:
|
264
|
-
field_name: Name of the field to get.
|
265
|
-
|
266
|
-
Returns:
|
267
|
-
The field value or UNDEFINED if no default exists.
|
268
|
-
|
269
|
-
Raises:
|
270
|
-
AttributeError: If the field doesn't exist.
|
271
|
-
"""
|
272
|
-
if field_name in self.extra_fields:
|
273
|
-
default_ = self.extra_fields[field_name].default
|
274
|
-
if default_ is not PydanticUndefined:
|
275
|
-
return default_
|
276
|
-
return UNDEFINED
|
277
|
-
|
278
|
-
cls_name = self.__class__.__name__
|
279
|
-
raise AttributeError(
|
280
|
-
f"'{cls_name}' object has no attribute '{field_name}'",
|
281
|
-
)
|
282
|
-
|
283
|
-
@override
|
284
|
-
def __str__(self) -> str:
|
285
|
-
"""Return a concise string representation of the component."""
|
286
|
-
content_preview = str(self.content)[:50]
|
287
|
-
if len(content_preview) == 50:
|
288
|
-
content_preview += "..."
|
289
|
-
|
290
|
-
output_str = (
|
291
|
-
f"{self.__class__.__name__}("
|
292
|
-
f"ln_id={str(self.ln_id)[:8]}..., "
|
293
|
-
f"created_timestamp={str(self.created_datetime)[:-6]}, "
|
294
|
-
f"content='{content_preview}', "
|
295
|
-
f"metadata_keys={list(self.metadata.keys())}, "
|
296
|
-
)
|
297
|
-
|
298
|
-
for i, j in self.model_dump().items():
|
299
|
-
if i not in DEFAULT_SERIALIZATION_INCLUDE:
|
300
|
-
if isinstance(j, dict):
|
301
|
-
output_str += f"{i}={list(j.keys())}, "
|
302
|
-
elif isinstance(j, str):
|
303
|
-
j_preview = j[:50]
|
304
|
-
if len(j_preview) == 50:
|
305
|
-
j_preview = j_preview + "..."
|
306
|
-
output_str += f"{i}={j_preview}, "
|
307
|
-
else:
|
308
|
-
output_str += f"{i}={j}, "
|
309
|
-
|
310
|
-
output_str += f"extra_fields_keys={list(self.extra_fields.keys())})"
|
311
|
-
|
312
|
-
return output_str
|
313
|
-
|
314
|
-
@override
|
315
|
-
def __repr__(self) -> str:
|
316
|
-
"""Return a detailed string representation of the component."""
|
317
|
-
|
318
|
-
def truncate_dict(
|
319
|
-
d: dict[str, Any], max_items: int = 5, max_str_len: int = 50
|
320
|
-
) -> dict[str, Any]:
|
321
|
-
items = list(d.items())[:max_items]
|
322
|
-
truncated = {
|
323
|
-
k: (
|
324
|
-
v[:max_str_len] + "..."
|
325
|
-
if isinstance(v, str) and len(v) > max_str_len
|
326
|
-
else v
|
327
|
-
)
|
328
|
-
for k, v in items
|
329
|
-
}
|
330
|
-
if len(d) > max_items:
|
331
|
-
truncated["..."] = f"({len(d) - max_items} more items)"
|
332
|
-
return truncated
|
333
|
-
|
334
|
-
content_repr = repr(self.content)
|
335
|
-
if len(content_repr) > 100:
|
336
|
-
content_repr = content_repr[:97] + "..."
|
337
|
-
|
338
|
-
dict_ = self.model_dump()
|
339
|
-
extra_fields = dict_.pop("extra_fields", {})
|
340
|
-
|
341
|
-
repr_str = (
|
342
|
-
f"{self.class_name()}("
|
343
|
-
f"ln_id=IDType({str(self.ln_id)}), "
|
344
|
-
f"created_timestamp={str(self.created_datetime)[:-6]}, "
|
345
|
-
f"content={content_repr}, "
|
346
|
-
f"metadata={truncate_dict(self.metadata.content)}, "
|
347
|
-
)
|
348
|
-
|
349
|
-
for i, j in dict_.items():
|
350
|
-
if i not in DEFAULT_SERIALIZATION_INCLUDE:
|
351
|
-
if isinstance(j, dict):
|
352
|
-
repr_str += f"{i}={truncate_dict(j)}, "
|
353
|
-
elif isinstance(j, str):
|
354
|
-
j_repr = j
|
355
|
-
if len(j) > 100:
|
356
|
-
j_repr = j[:97] + "..."
|
357
|
-
repr_str += f"{i}={j_repr}, "
|
358
|
-
else:
|
359
|
-
repr_str += f"{i}={j}, "
|
360
|
-
|
361
|
-
repr_str += f"extra_fields={truncate_dict(extra_fields)})"
|
362
|
-
return repr_str
|
363
|
-
|
364
|
-
def adapt_to(self, obj_key: str, /, *args, **kwargs: Any) -> Any:
|
365
|
-
"""Convert this component to another format using registered adapters.
|
366
|
-
|
367
|
-
Args:
|
368
|
-
obj_key: Key identifying the target format.
|
369
|
-
*args: Additional positional arguments for the adapter.
|
370
|
-
**kwargs: Additional keyword arguments for the adapter.
|
371
|
-
|
372
|
-
Returns:
|
373
|
-
Any: The component converted to the target format.
|
374
|
-
"""
|
375
|
-
return self._get_adapter_registry().adapt_to(
|
376
|
-
self, obj_key, *args, **kwargs
|
377
|
-
)
|
378
|
-
|
379
|
-
@classmethod
|
380
|
-
def list_adapters(cls):
|
381
|
-
"""List all registered adapters for this component type.
|
382
|
-
|
383
|
-
Returns:
|
384
|
-
list: Available adapters for this component type.
|
385
|
-
"""
|
386
|
-
return cls._get_adapter_registry().list_adapters()
|
387
|
-
|
388
|
-
@classmethod
|
389
|
-
def register_adapter(cls, adapter: type[Adapter]):
|
390
|
-
"""Register a new adapter for this component type.
|
391
|
-
|
392
|
-
Args:
|
393
|
-
adapter: The adapter class to register.
|
394
|
-
"""
|
395
|
-
cls._get_adapter_registry().register(adapter)
|
396
|
-
|
397
|
-
@classmethod
|
398
|
-
def _get_adapter_registry(cls) -> AdapterRegistry:
|
399
|
-
"""Get the converter registry for the class."""
|
400
|
-
if isinstance(cls._adapter_registry, type):
|
401
|
-
cls._adapter_registry = cls._adapter_registry()
|
402
|
-
return cls._adapter_registry
|
403
|
-
|
404
|
-
@classmethod
|
405
|
-
def adapt_from(cls, obj: Any, obj_key: str, /, **kwargs: Any):
|
406
|
-
"""Create a component instance from another format using registered adapters.
|
407
|
-
|
408
|
-
Args:
|
409
|
-
obj: The object to convert from.
|
410
|
-
obj_key: Key identifying the source format.
|
411
|
-
**kwargs: Additional arguments for the adapter.
|
412
|
-
|
413
|
-
Returns:
|
414
|
-
Component: A new component instance.
|
415
|
-
"""
|
416
|
-
dict_ = cls._get_adapter_registry().adapt_from(
|
417
|
-
cls, obj, obj_key, **kwargs
|
418
|
-
)
|
419
|
-
return cls.from_dict(dict_)
|
420
|
-
|
421
|
-
|
422
|
-
__all__ = ["Component"]
|