langchain 0.3.26__py3-none-any.whl → 0.4.0.dev0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- langchain/__init__.py +110 -96
- langchain/_api/__init__.py +2 -2
- langchain/_api/deprecation.py +3 -3
- langchain/_api/module_import.py +51 -46
- langchain/_api/path.py +1 -1
- langchain/adapters/openai.py +8 -8
- langchain/agents/__init__.py +15 -12
- langchain/agents/agent.py +174 -151
- langchain/agents/agent_iterator.py +50 -26
- langchain/agents/agent_toolkits/__init__.py +7 -6
- langchain/agents/agent_toolkits/ainetwork/toolkit.py +1 -1
- langchain/agents/agent_toolkits/amadeus/toolkit.py +1 -1
- langchain/agents/agent_toolkits/azure_cognitive_services.py +1 -1
- langchain/agents/agent_toolkits/clickup/toolkit.py +1 -1
- langchain/agents/agent_toolkits/conversational_retrieval/openai_functions.py +6 -4
- langchain/agents/agent_toolkits/csv/__init__.py +4 -2
- langchain/agents/agent_toolkits/file_management/__init__.py +1 -1
- langchain/agents/agent_toolkits/file_management/toolkit.py +1 -1
- langchain/agents/agent_toolkits/github/toolkit.py +9 -9
- langchain/agents/agent_toolkits/gitlab/toolkit.py +1 -1
- langchain/agents/agent_toolkits/json/base.py +1 -1
- langchain/agents/agent_toolkits/multion/toolkit.py +1 -1
- langchain/agents/agent_toolkits/office365/toolkit.py +1 -1
- langchain/agents/agent_toolkits/openapi/base.py +1 -1
- langchain/agents/agent_toolkits/openapi/planner.py +2 -2
- langchain/agents/agent_toolkits/openapi/planner_prompt.py +10 -10
- langchain/agents/agent_toolkits/openapi/prompt.py +1 -1
- langchain/agents/agent_toolkits/openapi/toolkit.py +1 -1
- langchain/agents/agent_toolkits/pandas/__init__.py +4 -2
- langchain/agents/agent_toolkits/playwright/__init__.py +1 -1
- langchain/agents/agent_toolkits/playwright/toolkit.py +1 -1
- langchain/agents/agent_toolkits/powerbi/base.py +1 -1
- langchain/agents/agent_toolkits/powerbi/chat_base.py +1 -1
- langchain/agents/agent_toolkits/powerbi/prompt.py +2 -2
- langchain/agents/agent_toolkits/powerbi/toolkit.py +1 -1
- langchain/agents/agent_toolkits/python/__init__.py +4 -2
- langchain/agents/agent_toolkits/spark/__init__.py +4 -2
- langchain/agents/agent_toolkits/spark_sql/base.py +1 -1
- langchain/agents/agent_toolkits/spark_sql/toolkit.py +1 -1
- langchain/agents/agent_toolkits/sql/prompt.py +1 -1
- langchain/agents/agent_toolkits/sql/toolkit.py +1 -1
- langchain/agents/agent_toolkits/vectorstore/base.py +4 -2
- langchain/agents/agent_toolkits/vectorstore/prompt.py +2 -4
- langchain/agents/agent_toolkits/vectorstore/toolkit.py +12 -11
- langchain/agents/agent_toolkits/xorbits/__init__.py +4 -2
- langchain/agents/agent_toolkits/zapier/toolkit.py +1 -1
- langchain/agents/agent_types.py +6 -6
- langchain/agents/chat/base.py +8 -12
- langchain/agents/chat/output_parser.py +9 -6
- langchain/agents/chat/prompt.py +3 -4
- langchain/agents/conversational/base.py +11 -5
- langchain/agents/conversational/output_parser.py +4 -2
- langchain/agents/conversational/prompt.py +2 -3
- langchain/agents/conversational_chat/base.py +9 -5
- langchain/agents/conversational_chat/output_parser.py +9 -11
- langchain/agents/conversational_chat/prompt.py +5 -6
- langchain/agents/format_scratchpad/__init__.py +3 -3
- langchain/agents/format_scratchpad/log_to_messages.py +1 -1
- langchain/agents/format_scratchpad/openai_functions.py +8 -6
- langchain/agents/format_scratchpad/tools.py +5 -3
- langchain/agents/format_scratchpad/xml.py +33 -2
- langchain/agents/initialize.py +17 -9
- langchain/agents/json_chat/base.py +19 -18
- langchain/agents/json_chat/prompt.py +2 -3
- langchain/agents/load_tools.py +2 -1
- langchain/agents/loading.py +28 -18
- langchain/agents/mrkl/base.py +11 -4
- langchain/agents/mrkl/output_parser.py +17 -13
- langchain/agents/mrkl/prompt.py +1 -2
- langchain/agents/openai_assistant/base.py +81 -71
- langchain/agents/openai_functions_agent/agent_token_buffer_memory.py +2 -0
- langchain/agents/openai_functions_agent/base.py +47 -37
- langchain/agents/openai_functions_multi_agent/base.py +40 -27
- langchain/agents/openai_tools/base.py +9 -8
- langchain/agents/output_parsers/__init__.py +3 -3
- langchain/agents/output_parsers/json.py +8 -6
- langchain/agents/output_parsers/openai_functions.py +24 -9
- langchain/agents/output_parsers/openai_tools.py +16 -4
- langchain/agents/output_parsers/react_json_single_input.py +13 -5
- langchain/agents/output_parsers/react_single_input.py +18 -11
- langchain/agents/output_parsers/self_ask.py +5 -2
- langchain/agents/output_parsers/tools.py +32 -13
- langchain/agents/output_parsers/xml.py +102 -28
- langchain/agents/react/agent.py +5 -4
- langchain/agents/react/base.py +26 -17
- langchain/agents/react/output_parser.py +7 -6
- langchain/agents/react/textworld_prompt.py +0 -1
- langchain/agents/react/wiki_prompt.py +14 -15
- langchain/agents/schema.py +5 -2
- langchain/agents/self_ask_with_search/base.py +23 -15
- langchain/agents/self_ask_with_search/prompt.py +0 -1
- langchain/agents/structured_chat/base.py +19 -11
- langchain/agents/structured_chat/output_parser.py +29 -18
- langchain/agents/structured_chat/prompt.py +3 -4
- langchain/agents/tool_calling_agent/base.py +8 -6
- langchain/agents/tools.py +5 -2
- langchain/agents/utils.py +2 -3
- langchain/agents/xml/base.py +12 -6
- langchain/agents/xml/prompt.py +1 -2
- langchain/cache.py +12 -12
- langchain/callbacks/__init__.py +11 -11
- langchain/callbacks/aim_callback.py +2 -2
- langchain/callbacks/argilla_callback.py +1 -1
- langchain/callbacks/arize_callback.py +1 -1
- langchain/callbacks/arthur_callback.py +1 -1
- langchain/callbacks/base.py +7 -7
- langchain/callbacks/clearml_callback.py +1 -1
- langchain/callbacks/comet_ml_callback.py +1 -1
- langchain/callbacks/confident_callback.py +1 -1
- langchain/callbacks/context_callback.py +1 -1
- langchain/callbacks/flyte_callback.py +1 -1
- langchain/callbacks/human.py +2 -2
- langchain/callbacks/infino_callback.py +1 -1
- langchain/callbacks/labelstudio_callback.py +1 -1
- langchain/callbacks/llmonitor_callback.py +1 -1
- langchain/callbacks/manager.py +5 -5
- langchain/callbacks/mlflow_callback.py +2 -2
- langchain/callbacks/openai_info.py +1 -1
- langchain/callbacks/promptlayer_callback.py +1 -1
- langchain/callbacks/sagemaker_callback.py +1 -1
- langchain/callbacks/streaming_aiter.py +17 -3
- langchain/callbacks/streaming_aiter_final_only.py +16 -5
- langchain/callbacks/streaming_stdout_final_only.py +10 -3
- langchain/callbacks/streamlit/__init__.py +3 -2
- langchain/callbacks/streamlit/mutable_expander.py +1 -1
- langchain/callbacks/streamlit/streamlit_callback_handler.py +3 -3
- langchain/callbacks/tracers/__init__.py +1 -1
- langchain/callbacks/tracers/comet.py +1 -1
- langchain/callbacks/tracers/evaluation.py +1 -1
- langchain/callbacks/tracers/log_stream.py +1 -1
- langchain/callbacks/tracers/logging.py +12 -1
- langchain/callbacks/tracers/stdout.py +1 -1
- langchain/callbacks/trubrics_callback.py +1 -1
- langchain/callbacks/utils.py +4 -4
- langchain/callbacks/wandb_callback.py +1 -1
- langchain/callbacks/whylabs_callback.py +1 -1
- langchain/chains/api/base.py +41 -23
- langchain/chains/api/news_docs.py +1 -2
- langchain/chains/api/open_meteo_docs.py +1 -2
- langchain/chains/api/openapi/requests_chain.py +1 -1
- langchain/chains/api/openapi/response_chain.py +1 -1
- langchain/chains/api/podcast_docs.py +1 -2
- langchain/chains/api/prompt.py +1 -2
- langchain/chains/api/tmdb_docs.py +1 -2
- langchain/chains/base.py +96 -56
- langchain/chains/chat_vector_db/prompts.py +2 -3
- langchain/chains/combine_documents/__init__.py +1 -1
- langchain/chains/combine_documents/base.py +30 -11
- langchain/chains/combine_documents/map_reduce.py +41 -30
- langchain/chains/combine_documents/map_rerank.py +39 -24
- langchain/chains/combine_documents/reduce.py +48 -26
- langchain/chains/combine_documents/refine.py +27 -17
- langchain/chains/combine_documents/stuff.py +24 -13
- langchain/chains/constitutional_ai/base.py +11 -4
- langchain/chains/constitutional_ai/principles.py +22 -25
- langchain/chains/constitutional_ai/prompts.py +25 -28
- langchain/chains/conversation/base.py +9 -4
- langchain/chains/conversation/memory.py +5 -5
- langchain/chains/conversation/prompt.py +5 -5
- langchain/chains/conversational_retrieval/base.py +108 -79
- langchain/chains/conversational_retrieval/prompts.py +2 -3
- langchain/chains/elasticsearch_database/base.py +10 -10
- langchain/chains/elasticsearch_database/prompts.py +2 -3
- langchain/chains/ernie_functions/__init__.py +2 -2
- langchain/chains/example_generator.py +3 -1
- langchain/chains/flare/base.py +28 -12
- langchain/chains/flare/prompts.py +2 -0
- langchain/chains/graph_qa/cypher.py +2 -2
- langchain/chains/graph_qa/falkordb.py +1 -1
- langchain/chains/graph_qa/gremlin.py +1 -1
- langchain/chains/graph_qa/neptune_sparql.py +1 -1
- langchain/chains/graph_qa/prompts.py +2 -2
- langchain/chains/history_aware_retriever.py +2 -1
- langchain/chains/hyde/base.py +6 -5
- langchain/chains/hyde/prompts.py +5 -6
- langchain/chains/llm.py +82 -61
- langchain/chains/llm_bash/__init__.py +3 -2
- langchain/chains/llm_checker/base.py +19 -6
- langchain/chains/llm_checker/prompt.py +3 -4
- langchain/chains/llm_math/base.py +25 -10
- langchain/chains/llm_math/prompt.py +1 -2
- langchain/chains/llm_summarization_checker/base.py +22 -7
- langchain/chains/llm_symbolic_math/__init__.py +3 -2
- langchain/chains/loading.py +155 -97
- langchain/chains/mapreduce.py +4 -3
- langchain/chains/moderation.py +11 -9
- langchain/chains/natbot/base.py +11 -9
- langchain/chains/natbot/crawler.py +102 -76
- langchain/chains/natbot/prompt.py +2 -3
- langchain/chains/openai_functions/__init__.py +7 -7
- langchain/chains/openai_functions/base.py +15 -10
- langchain/chains/openai_functions/citation_fuzzy_match.py +21 -11
- langchain/chains/openai_functions/extraction.py +19 -19
- langchain/chains/openai_functions/openapi.py +39 -35
- langchain/chains/openai_functions/qa_with_structure.py +22 -15
- langchain/chains/openai_functions/tagging.py +4 -4
- langchain/chains/openai_tools/extraction.py +7 -8
- langchain/chains/qa_generation/base.py +8 -3
- langchain/chains/qa_generation/prompt.py +5 -5
- langchain/chains/qa_with_sources/base.py +17 -6
- langchain/chains/qa_with_sources/loading.py +16 -8
- langchain/chains/qa_with_sources/map_reduce_prompt.py +8 -9
- langchain/chains/qa_with_sources/refine_prompts.py +0 -1
- langchain/chains/qa_with_sources/retrieval.py +15 -6
- langchain/chains/qa_with_sources/stuff_prompt.py +6 -7
- langchain/chains/qa_with_sources/vector_db.py +21 -8
- langchain/chains/query_constructor/base.py +37 -34
- langchain/chains/query_constructor/ir.py +4 -4
- langchain/chains/query_constructor/parser.py +101 -34
- langchain/chains/query_constructor/prompt.py +5 -6
- langchain/chains/question_answering/chain.py +21 -10
- langchain/chains/question_answering/map_reduce_prompt.py +14 -14
- langchain/chains/question_answering/map_rerank_prompt.py +3 -3
- langchain/chains/question_answering/refine_prompts.py +2 -5
- langchain/chains/question_answering/stuff_prompt.py +5 -5
- langchain/chains/retrieval.py +1 -3
- langchain/chains/retrieval_qa/base.py +38 -27
- langchain/chains/retrieval_qa/prompt.py +1 -2
- langchain/chains/router/__init__.py +3 -3
- langchain/chains/router/base.py +38 -22
- langchain/chains/router/embedding_router.py +15 -8
- langchain/chains/router/llm_router.py +23 -20
- langchain/chains/router/multi_prompt.py +5 -2
- langchain/chains/router/multi_retrieval_qa.py +28 -5
- langchain/chains/sequential.py +30 -18
- langchain/chains/sql_database/prompt.py +14 -16
- langchain/chains/sql_database/query.py +7 -5
- langchain/chains/structured_output/__init__.py +1 -1
- langchain/chains/structured_output/base.py +77 -67
- langchain/chains/summarize/chain.py +11 -5
- langchain/chains/summarize/map_reduce_prompt.py +0 -1
- langchain/chains/summarize/stuff_prompt.py +0 -1
- langchain/chains/transform.py +9 -6
- langchain/chat_loaders/facebook_messenger.py +1 -1
- langchain/chat_loaders/langsmith.py +1 -1
- langchain/chat_loaders/utils.py +3 -3
- langchain/chat_models/__init__.py +20 -19
- langchain/chat_models/anthropic.py +1 -1
- langchain/chat_models/azureml_endpoint.py +1 -1
- langchain/chat_models/baidu_qianfan_endpoint.py +1 -1
- langchain/chat_models/base.py +213 -139
- langchain/chat_models/bedrock.py +1 -1
- langchain/chat_models/fake.py +1 -1
- langchain/chat_models/meta.py +1 -1
- langchain/chat_models/pai_eas_endpoint.py +1 -1
- langchain/chat_models/promptlayer_openai.py +1 -1
- langchain/chat_models/volcengine_maas.py +1 -1
- langchain/docstore/base.py +1 -1
- langchain/document_loaders/__init__.py +9 -9
- langchain/document_loaders/airbyte.py +3 -3
- langchain/document_loaders/assemblyai.py +1 -1
- langchain/document_loaders/azure_blob_storage_container.py +1 -1
- langchain/document_loaders/azure_blob_storage_file.py +1 -1
- langchain/document_loaders/baiducloud_bos_file.py +1 -1
- langchain/document_loaders/base.py +1 -1
- langchain/document_loaders/blob_loaders/__init__.py +1 -1
- langchain/document_loaders/blob_loaders/schema.py +1 -4
- langchain/document_loaders/blockchain.py +1 -1
- langchain/document_loaders/chatgpt.py +1 -1
- langchain/document_loaders/college_confidential.py +1 -1
- langchain/document_loaders/confluence.py +1 -1
- langchain/document_loaders/email.py +1 -1
- langchain/document_loaders/facebook_chat.py +1 -1
- langchain/document_loaders/markdown.py +1 -1
- langchain/document_loaders/notebook.py +1 -1
- langchain/document_loaders/org_mode.py +1 -1
- langchain/document_loaders/parsers/__init__.py +1 -1
- langchain/document_loaders/parsers/docai.py +1 -1
- langchain/document_loaders/parsers/generic.py +1 -1
- langchain/document_loaders/parsers/html/__init__.py +1 -1
- langchain/document_loaders/parsers/html/bs4.py +1 -1
- langchain/document_loaders/parsers/language/cobol.py +1 -1
- langchain/document_loaders/parsers/language/python.py +1 -1
- langchain/document_loaders/parsers/msword.py +1 -1
- langchain/document_loaders/parsers/pdf.py +5 -5
- langchain/document_loaders/parsers/registry.py +1 -1
- langchain/document_loaders/pdf.py +8 -8
- langchain/document_loaders/powerpoint.py +1 -1
- langchain/document_loaders/pyspark_dataframe.py +1 -1
- langchain/document_loaders/telegram.py +2 -2
- langchain/document_loaders/tencent_cos_directory.py +1 -1
- langchain/document_loaders/unstructured.py +5 -5
- langchain/document_loaders/url_playwright.py +1 -1
- langchain/document_loaders/whatsapp_chat.py +1 -1
- langchain/document_loaders/youtube.py +2 -2
- langchain/document_transformers/__init__.py +3 -3
- langchain/document_transformers/beautiful_soup_transformer.py +1 -1
- langchain/document_transformers/doctran_text_extract.py +1 -1
- langchain/document_transformers/doctran_text_qa.py +1 -1
- langchain/document_transformers/doctran_text_translate.py +1 -1
- langchain/document_transformers/embeddings_redundant_filter.py +3 -3
- langchain/document_transformers/google_translate.py +1 -1
- langchain/document_transformers/html2text.py +1 -1
- langchain/document_transformers/nuclia_text_transform.py +1 -1
- langchain/embeddings/__init__.py +5 -5
- langchain/embeddings/base.py +35 -24
- langchain/embeddings/cache.py +37 -32
- langchain/embeddings/fake.py +1 -1
- langchain/embeddings/huggingface.py +2 -2
- langchain/evaluation/__init__.py +22 -22
- langchain/evaluation/agents/trajectory_eval_chain.py +26 -25
- langchain/evaluation/agents/trajectory_eval_prompt.py +6 -9
- langchain/evaluation/comparison/__init__.py +1 -1
- langchain/evaluation/comparison/eval_chain.py +21 -13
- langchain/evaluation/comparison/prompt.py +1 -2
- langchain/evaluation/criteria/__init__.py +1 -1
- langchain/evaluation/criteria/eval_chain.py +23 -11
- langchain/evaluation/criteria/prompt.py +2 -3
- langchain/evaluation/embedding_distance/base.py +34 -20
- langchain/evaluation/exact_match/base.py +14 -1
- langchain/evaluation/loading.py +16 -11
- langchain/evaluation/parsing/base.py +20 -4
- langchain/evaluation/parsing/json_distance.py +24 -10
- langchain/evaluation/parsing/json_schema.py +13 -12
- langchain/evaluation/qa/__init__.py +1 -1
- langchain/evaluation/qa/eval_chain.py +20 -5
- langchain/evaluation/qa/eval_prompt.py +7 -8
- langchain/evaluation/qa/generate_chain.py +4 -1
- langchain/evaluation/qa/generate_prompt.py +2 -4
- langchain/evaluation/regex_match/base.py +9 -1
- langchain/evaluation/schema.py +38 -30
- langchain/evaluation/scoring/__init__.py +1 -1
- langchain/evaluation/scoring/eval_chain.py +23 -15
- langchain/evaluation/scoring/prompt.py +0 -1
- langchain/evaluation/string_distance/base.py +20 -9
- langchain/globals.py +12 -11
- langchain/graphs/__init__.py +6 -6
- langchain/graphs/graph_document.py +1 -1
- langchain/graphs/networkx_graph.py +2 -2
- langchain/hub.py +9 -11
- langchain/indexes/__init__.py +3 -3
- langchain/indexes/_sql_record_manager.py +63 -46
- langchain/indexes/prompts/entity_extraction.py +1 -2
- langchain/indexes/prompts/entity_summarization.py +1 -2
- langchain/indexes/prompts/knowledge_triplet_extraction.py +1 -3
- langchain/indexes/vectorstore.py +35 -19
- langchain/llms/__init__.py +13 -13
- langchain/llms/ai21.py +1 -1
- langchain/llms/azureml_endpoint.py +4 -4
- langchain/llms/base.py +15 -7
- langchain/llms/bedrock.py +1 -1
- langchain/llms/cloudflare_workersai.py +1 -1
- langchain/llms/gradient_ai.py +1 -1
- langchain/llms/loading.py +1 -1
- langchain/llms/openai.py +1 -1
- langchain/llms/sagemaker_endpoint.py +1 -1
- langchain/load/dump.py +1 -1
- langchain/load/load.py +1 -1
- langchain/load/serializable.py +3 -3
- langchain/memory/__init__.py +3 -3
- langchain/memory/buffer.py +14 -7
- langchain/memory/buffer_window.py +2 -0
- langchain/memory/chat_memory.py +14 -8
- langchain/memory/chat_message_histories/__init__.py +1 -1
- langchain/memory/chat_message_histories/astradb.py +1 -1
- langchain/memory/chat_message_histories/cassandra.py +1 -1
- langchain/memory/chat_message_histories/cosmos_db.py +1 -1
- langchain/memory/chat_message_histories/dynamodb.py +1 -1
- langchain/memory/chat_message_histories/elasticsearch.py +1 -1
- langchain/memory/chat_message_histories/file.py +1 -1
- langchain/memory/chat_message_histories/firestore.py +1 -1
- langchain/memory/chat_message_histories/momento.py +1 -1
- langchain/memory/chat_message_histories/mongodb.py +1 -1
- langchain/memory/chat_message_histories/neo4j.py +1 -1
- langchain/memory/chat_message_histories/postgres.py +1 -1
- langchain/memory/chat_message_histories/redis.py +1 -1
- langchain/memory/chat_message_histories/rocksetdb.py +1 -1
- langchain/memory/chat_message_histories/singlestoredb.py +1 -1
- langchain/memory/chat_message_histories/streamlit.py +1 -1
- langchain/memory/chat_message_histories/upstash_redis.py +1 -1
- langchain/memory/chat_message_histories/xata.py +1 -1
- langchain/memory/chat_message_histories/zep.py +1 -1
- langchain/memory/combined.py +14 -13
- langchain/memory/entity.py +131 -61
- langchain/memory/prompt.py +10 -11
- langchain/memory/readonly.py +0 -2
- langchain/memory/simple.py +4 -3
- langchain/memory/summary.py +43 -11
- langchain/memory/summary_buffer.py +20 -8
- langchain/memory/token_buffer.py +2 -0
- langchain/memory/utils.py +3 -2
- langchain/memory/vectorstore.py +12 -5
- langchain/memory/vectorstore_token_buffer_memory.py +5 -5
- langchain/model_laboratory.py +12 -11
- langchain/output_parsers/__init__.py +4 -4
- langchain/output_parsers/boolean.py +7 -4
- langchain/output_parsers/combining.py +14 -7
- langchain/output_parsers/datetime.py +32 -31
- langchain/output_parsers/enum.py +10 -4
- langchain/output_parsers/fix.py +60 -53
- langchain/output_parsers/format_instructions.py +6 -8
- langchain/output_parsers/json.py +2 -2
- langchain/output_parsers/list.py +2 -2
- langchain/output_parsers/loading.py +9 -9
- langchain/output_parsers/openai_functions.py +3 -3
- langchain/output_parsers/openai_tools.py +1 -1
- langchain/output_parsers/pandas_dataframe.py +59 -48
- langchain/output_parsers/prompts.py +1 -2
- langchain/output_parsers/rail_parser.py +1 -1
- langchain/output_parsers/regex.py +9 -8
- langchain/output_parsers/regex_dict.py +7 -10
- langchain/output_parsers/retry.py +99 -80
- langchain/output_parsers/structured.py +21 -6
- langchain/output_parsers/yaml.py +19 -11
- langchain/prompts/__init__.py +5 -3
- langchain/prompts/base.py +5 -5
- langchain/prompts/chat.py +8 -8
- langchain/prompts/example_selector/__init__.py +3 -1
- langchain/prompts/example_selector/semantic_similarity.py +2 -2
- langchain/prompts/few_shot.py +1 -1
- langchain/prompts/loading.py +3 -3
- langchain/prompts/prompt.py +1 -1
- langchain/pydantic_v1/__init__.py +1 -1
- langchain/retrievers/__init__.py +5 -5
- langchain/retrievers/bedrock.py +2 -2
- langchain/retrievers/bm25.py +1 -1
- langchain/retrievers/contextual_compression.py +14 -8
- langchain/retrievers/docarray.py +1 -1
- langchain/retrievers/document_compressors/__init__.py +5 -4
- langchain/retrievers/document_compressors/base.py +12 -6
- langchain/retrievers/document_compressors/chain_extract.py +5 -3
- langchain/retrievers/document_compressors/chain_extract_prompt.py +2 -3
- langchain/retrievers/document_compressors/chain_filter.py +9 -9
- langchain/retrievers/document_compressors/chain_filter_prompt.py +1 -2
- langchain/retrievers/document_compressors/cohere_rerank.py +17 -15
- langchain/retrievers/document_compressors/cross_encoder_rerank.py +2 -0
- langchain/retrievers/document_compressors/embeddings_filter.py +24 -17
- langchain/retrievers/document_compressors/flashrank_rerank.py +1 -1
- langchain/retrievers/document_compressors/listwise_rerank.py +8 -5
- langchain/retrievers/ensemble.py +30 -27
- langchain/retrievers/google_cloud_documentai_warehouse.py +1 -1
- langchain/retrievers/google_vertex_ai_search.py +2 -2
- langchain/retrievers/kendra.py +10 -10
- langchain/retrievers/llama_index.py +1 -1
- langchain/retrievers/merger_retriever.py +11 -11
- langchain/retrievers/milvus.py +1 -1
- langchain/retrievers/multi_query.py +35 -27
- langchain/retrievers/multi_vector.py +24 -9
- langchain/retrievers/parent_document_retriever.py +33 -9
- langchain/retrievers/re_phraser.py +6 -5
- langchain/retrievers/self_query/base.py +157 -127
- langchain/retrievers/time_weighted_retriever.py +21 -7
- langchain/retrievers/zilliz.py +1 -1
- langchain/runnables/hub.py +12 -0
- langchain/runnables/openai_functions.py +12 -2
- langchain/schema/__init__.py +23 -23
- langchain/schema/cache.py +1 -1
- langchain/schema/callbacks/base.py +7 -7
- langchain/schema/callbacks/manager.py +19 -19
- langchain/schema/callbacks/tracers/base.py +1 -1
- langchain/schema/callbacks/tracers/evaluation.py +1 -1
- langchain/schema/callbacks/tracers/langchain.py +1 -1
- langchain/schema/callbacks/tracers/langchain_v1.py +1 -1
- langchain/schema/callbacks/tracers/log_stream.py +1 -1
- langchain/schema/callbacks/tracers/schemas.py +8 -8
- langchain/schema/callbacks/tracers/stdout.py +3 -3
- langchain/schema/document.py +1 -1
- langchain/schema/language_model.py +2 -2
- langchain/schema/messages.py +12 -12
- langchain/schema/output.py +3 -3
- langchain/schema/output_parser.py +3 -3
- langchain/schema/runnable/__init__.py +3 -3
- langchain/schema/runnable/base.py +9 -9
- langchain/schema/runnable/config.py +5 -5
- langchain/schema/runnable/configurable.py +1 -1
- langchain/schema/runnable/history.py +1 -1
- langchain/schema/runnable/passthrough.py +1 -1
- langchain/schema/runnable/utils.py +16 -16
- langchain/schema/vectorstore.py +1 -1
- langchain/smith/__init__.py +2 -1
- langchain/smith/evaluation/__init__.py +2 -2
- langchain/smith/evaluation/config.py +9 -23
- langchain/smith/evaluation/name_generation.py +3 -3
- langchain/smith/evaluation/progress.py +22 -4
- langchain/smith/evaluation/runner_utils.py +416 -247
- langchain/smith/evaluation/string_run_evaluator.py +102 -68
- langchain/storage/__init__.py +2 -2
- langchain/storage/_lc_store.py +4 -2
- langchain/storage/encoder_backed.py +7 -2
- langchain/storage/file_system.py +19 -16
- langchain/storage/in_memory.py +1 -1
- langchain/storage/upstash_redis.py +1 -1
- langchain/text_splitter.py +15 -15
- langchain/tools/__init__.py +28 -26
- langchain/tools/ainetwork/app.py +1 -1
- langchain/tools/ainetwork/base.py +1 -1
- langchain/tools/ainetwork/owner.py +1 -1
- langchain/tools/ainetwork/rule.py +1 -1
- langchain/tools/ainetwork/transfer.py +1 -1
- langchain/tools/ainetwork/value.py +1 -1
- langchain/tools/amadeus/closest_airport.py +1 -1
- langchain/tools/amadeus/flight_search.py +1 -1
- langchain/tools/azure_cognitive_services/__init__.py +1 -1
- langchain/tools/base.py +4 -4
- langchain/tools/bearly/tool.py +1 -1
- langchain/tools/bing_search/__init__.py +1 -1
- langchain/tools/bing_search/tool.py +1 -1
- langchain/tools/dataforseo_api_search/__init__.py +1 -1
- langchain/tools/dataforseo_api_search/tool.py +1 -1
- langchain/tools/ddg_search/tool.py +1 -1
- langchain/tools/e2b_data_analysis/tool.py +2 -2
- langchain/tools/edenai/__init__.py +1 -1
- langchain/tools/file_management/__init__.py +1 -1
- langchain/tools/file_management/copy.py +1 -1
- langchain/tools/file_management/delete.py +1 -1
- langchain/tools/gmail/__init__.py +2 -2
- langchain/tools/gmail/get_message.py +1 -1
- langchain/tools/gmail/search.py +1 -1
- langchain/tools/gmail/send_message.py +1 -1
- langchain/tools/google_finance/__init__.py +1 -1
- langchain/tools/google_finance/tool.py +1 -1
- langchain/tools/google_scholar/__init__.py +1 -1
- langchain/tools/google_scholar/tool.py +1 -1
- langchain/tools/google_search/__init__.py +1 -1
- langchain/tools/google_search/tool.py +1 -1
- langchain/tools/google_serper/__init__.py +1 -1
- langchain/tools/google_serper/tool.py +1 -1
- langchain/tools/google_trends/__init__.py +1 -1
- langchain/tools/google_trends/tool.py +1 -1
- langchain/tools/jira/tool.py +20 -1
- langchain/tools/json/tool.py +25 -3
- langchain/tools/memorize/tool.py +1 -1
- langchain/tools/multion/__init__.py +1 -1
- langchain/tools/multion/update_session.py +1 -1
- langchain/tools/office365/__init__.py +2 -2
- langchain/tools/office365/events_search.py +1 -1
- langchain/tools/office365/messages_search.py +1 -1
- langchain/tools/office365/send_event.py +1 -1
- langchain/tools/office365/send_message.py +1 -1
- langchain/tools/openapi/utils/api_models.py +6 -6
- langchain/tools/playwright/__init__.py +5 -5
- langchain/tools/playwright/click.py +1 -1
- langchain/tools/playwright/extract_hyperlinks.py +1 -1
- langchain/tools/playwright/get_elements.py +1 -1
- langchain/tools/playwright/navigate.py +1 -1
- langchain/tools/plugin.py +2 -2
- langchain/tools/powerbi/tool.py +1 -1
- langchain/tools/python/__init__.py +3 -2
- langchain/tools/reddit_search/tool.py +1 -1
- langchain/tools/render.py +2 -2
- langchain/tools/requests/tool.py +2 -2
- langchain/tools/searchapi/tool.py +1 -1
- langchain/tools/searx_search/tool.py +1 -1
- langchain/tools/slack/get_message.py +1 -1
- langchain/tools/spark_sql/tool.py +1 -1
- langchain/tools/sql_database/tool.py +1 -1
- langchain/tools/tavily_search/__init__.py +1 -1
- langchain/tools/tavily_search/tool.py +1 -1
- langchain/tools/zapier/__init__.py +1 -1
- langchain/tools/zapier/tool.py +24 -2
- langchain/utilities/__init__.py +4 -4
- langchain/utilities/arcee.py +4 -4
- langchain/utilities/clickup.py +4 -4
- langchain/utilities/dalle_image_generator.py +1 -1
- langchain/utilities/dataforseo_api_search.py +1 -1
- langchain/utilities/opaqueprompts.py +1 -1
- langchain/utilities/reddit_search.py +1 -1
- langchain/utilities/sql_database.py +1 -1
- langchain/utilities/tavily_search.py +1 -1
- langchain/utilities/vertexai.py +2 -2
- langchain/utils/__init__.py +1 -1
- langchain/utils/aiter.py +1 -1
- langchain/utils/html.py +3 -3
- langchain/utils/input.py +1 -1
- langchain/utils/iter.py +1 -1
- langchain/utils/json_schema.py +1 -3
- langchain/utils/strings.py +1 -1
- langchain/utils/utils.py +6 -6
- langchain/vectorstores/__init__.py +5 -5
- langchain/vectorstores/alibabacloud_opensearch.py +1 -1
- langchain/vectorstores/azure_cosmos_db.py +1 -1
- langchain/vectorstores/clickhouse.py +1 -1
- langchain/vectorstores/elastic_vector_search.py +1 -1
- langchain/vectorstores/elasticsearch.py +2 -2
- langchain/vectorstores/myscale.py +1 -1
- langchain/vectorstores/neo4j_vector.py +1 -1
- langchain/vectorstores/pgembedding.py +1 -1
- langchain/vectorstores/qdrant.py +1 -1
- langchain/vectorstores/redis/__init__.py +1 -1
- langchain/vectorstores/redis/base.py +1 -1
- langchain/vectorstores/redis/filters.py +4 -4
- langchain/vectorstores/redis/schema.py +6 -6
- langchain/vectorstores/sklearn.py +2 -2
- langchain/vectorstores/starrocks.py +1 -1
- langchain/vectorstores/utils.py +1 -1
- {langchain-0.3.26.dist-info → langchain-0.4.0.dev0.dist-info}/METADATA +4 -14
- {langchain-0.3.26.dist-info → langchain-0.4.0.dev0.dist-info}/RECORD +590 -591
- {langchain-0.3.26.dist-info → langchain-0.4.0.dev0.dist-info}/WHEEL +1 -1
- langchain/smith/evaluation/utils.py +0 -0
- {langchain-0.3.26.dist-info → langchain-0.4.0.dev0.dist-info}/entry_points.txt +0 -0
- {langchain-0.3.26.dist-info → langchain-0.4.0.dev0.dist-info}/licenses/LICENSE +0 -0
langchain/evaluation/loading.py
CHANGED
|
@@ -58,21 +58,24 @@ def load_dataset(uri: str) -> list[dict]:
|
|
|
58
58
|
|
|
59
59
|
from langchain.evaluation import load_dataset
|
|
60
60
|
ds = load_dataset("llm-math")
|
|
61
|
+
|
|
61
62
|
"""
|
|
62
63
|
try:
|
|
63
64
|
from datasets import load_dataset
|
|
64
|
-
except ImportError:
|
|
65
|
-
|
|
65
|
+
except ImportError as e:
|
|
66
|
+
msg = (
|
|
66
67
|
"load_dataset requires the `datasets` package."
|
|
67
68
|
" Please install with `pip install datasets`"
|
|
68
69
|
)
|
|
70
|
+
raise ImportError(msg) from e
|
|
69
71
|
|
|
70
72
|
dataset = load_dataset(f"LangChainDatasets/{uri}")
|
|
71
|
-
return
|
|
73
|
+
return list(dataset["train"])
|
|
72
74
|
|
|
73
75
|
|
|
74
76
|
_EVALUATOR_MAP: dict[
|
|
75
|
-
EvaluatorType,
|
|
77
|
+
EvaluatorType,
|
|
78
|
+
Union[type[LLMEvalChain], type[Chain], type[StringEvaluator]],
|
|
76
79
|
] = {
|
|
77
80
|
EvaluatorType.QA: QAEvalChain,
|
|
78
81
|
EvaluatorType.COT_QA: CotQAEvalChain,
|
|
@@ -125,10 +128,11 @@ def load_evaluator(
|
|
|
125
128
|
>>> evaluator = load_evaluator(EvaluatorType.QA)
|
|
126
129
|
"""
|
|
127
130
|
if evaluator not in _EVALUATOR_MAP:
|
|
128
|
-
|
|
131
|
+
msg = (
|
|
129
132
|
f"Unknown evaluator type: {evaluator}"
|
|
130
133
|
f"\nValid types are: {list(_EVALUATOR_MAP.keys())}"
|
|
131
134
|
)
|
|
135
|
+
raise ValueError(msg)
|
|
132
136
|
evaluator_cls = _EVALUATOR_MAP[evaluator]
|
|
133
137
|
if issubclass(evaluator_cls, LLMEvalChain):
|
|
134
138
|
try:
|
|
@@ -139,27 +143,28 @@ def load_evaluator(
|
|
|
139
143
|
from langchain_community.chat_models.openai import ( # type: ignore[no-redef]
|
|
140
144
|
ChatOpenAI,
|
|
141
145
|
)
|
|
142
|
-
except ImportError:
|
|
143
|
-
|
|
146
|
+
except ImportError as e:
|
|
147
|
+
msg = (
|
|
144
148
|
"Could not import langchain_openai or fallback onto "
|
|
145
149
|
"langchain_community. Please install langchain_openai "
|
|
146
150
|
"or specify a language model explicitly. "
|
|
147
151
|
"It's recommended to install langchain_openai AND "
|
|
148
152
|
"specify a language model explicitly."
|
|
149
153
|
)
|
|
154
|
+
raise ImportError(msg) from e
|
|
150
155
|
|
|
151
156
|
llm = llm or ChatOpenAI(model="gpt-4", seed=42, temperature=0)
|
|
152
157
|
except Exception as e:
|
|
153
|
-
|
|
158
|
+
msg = (
|
|
154
159
|
f"Evaluation with the {evaluator_cls} requires a "
|
|
155
160
|
"language model to function."
|
|
156
161
|
" Failed to create the default 'gpt-4' model."
|
|
157
162
|
" Please manually provide an evaluation LLM"
|
|
158
163
|
" or check your openai credentials."
|
|
159
|
-
)
|
|
164
|
+
)
|
|
165
|
+
raise ValueError(msg) from e
|
|
160
166
|
return evaluator_cls.from_llm(llm=llm, **kwargs)
|
|
161
|
-
|
|
162
|
-
return evaluator_cls(**kwargs)
|
|
167
|
+
return evaluator_cls(**kwargs)
|
|
163
168
|
|
|
164
169
|
|
|
165
170
|
def load_evaluators(
|
|
@@ -5,6 +5,7 @@ from operator import eq
|
|
|
5
5
|
from typing import Any, Callable, Optional, Union, cast
|
|
6
6
|
|
|
7
7
|
from langchain_core.utils.json import parse_json_markdown
|
|
8
|
+
from typing_extensions import override
|
|
8
9
|
|
|
9
10
|
from langchain.evaluation.schema import StringEvaluator
|
|
10
11
|
|
|
@@ -34,21 +35,26 @@ class JsonValidityEvaluator(StringEvaluator):
|
|
|
34
35
|
{'score': 0, 'reasoning': 'Expecting property name enclosed in double quotes'}
|
|
35
36
|
"""
|
|
36
37
|
|
|
37
|
-
def __init__(self, **
|
|
38
|
+
def __init__(self, **_: Any) -> None:
|
|
39
|
+
"""Initialize the JsonValidityEvaluator."""
|
|
38
40
|
super().__init__()
|
|
39
41
|
|
|
40
42
|
@property
|
|
43
|
+
@override
|
|
41
44
|
def requires_input(self) -> bool:
|
|
42
45
|
return False
|
|
43
46
|
|
|
44
47
|
@property
|
|
48
|
+
@override
|
|
45
49
|
def requires_reference(self) -> bool:
|
|
46
50
|
return False
|
|
47
51
|
|
|
48
52
|
@property
|
|
53
|
+
@override
|
|
49
54
|
def evaluation_name(self) -> str:
|
|
50
55
|
return "json_validity"
|
|
51
56
|
|
|
57
|
+
@override
|
|
52
58
|
def _evaluate_strings(
|
|
53
59
|
self,
|
|
54
60
|
prediction: str,
|
|
@@ -72,9 +78,9 @@ class JsonValidityEvaluator(StringEvaluator):
|
|
|
72
78
|
"""
|
|
73
79
|
try:
|
|
74
80
|
parse_json_markdown(prediction, parser=json.loads)
|
|
75
|
-
return {"score": 1}
|
|
76
81
|
except Exception as e:
|
|
77
82
|
return {"score": 0, "reasoning": str(e)}
|
|
83
|
+
return {"score": 1}
|
|
78
84
|
|
|
79
85
|
|
|
80
86
|
class JsonEqualityEvaluator(StringEvaluator):
|
|
@@ -108,19 +114,28 @@ class JsonEqualityEvaluator(StringEvaluator):
|
|
|
108
114
|
|
|
109
115
|
"""
|
|
110
116
|
|
|
111
|
-
def __init__(self, operator: Optional[Callable] = None, **
|
|
117
|
+
def __init__(self, operator: Optional[Callable] = None, **_: Any) -> None:
|
|
118
|
+
"""Initialize the JsonEqualityEvaluator.
|
|
119
|
+
|
|
120
|
+
Args:
|
|
121
|
+
operator: A custom operator to compare the parsed JSON objects.
|
|
122
|
+
Defaults to equality (`eq`).
|
|
123
|
+
"""
|
|
112
124
|
super().__init__()
|
|
113
125
|
self.operator = operator or eq
|
|
114
126
|
|
|
115
127
|
@property
|
|
128
|
+
@override
|
|
116
129
|
def requires_input(self) -> bool:
|
|
117
130
|
return False
|
|
118
131
|
|
|
119
132
|
@property
|
|
133
|
+
@override
|
|
120
134
|
def requires_reference(self) -> bool:
|
|
121
135
|
return True
|
|
122
136
|
|
|
123
137
|
@property
|
|
138
|
+
@override
|
|
124
139
|
def evaluation_name(self) -> str:
|
|
125
140
|
return "json_equality"
|
|
126
141
|
|
|
@@ -132,6 +147,7 @@ class JsonEqualityEvaluator(StringEvaluator):
|
|
|
132
147
|
return parse_json_markdown(string)
|
|
133
148
|
return string
|
|
134
149
|
|
|
150
|
+
@override
|
|
135
151
|
def _evaluate_strings(
|
|
136
152
|
self,
|
|
137
153
|
prediction: str,
|
|
@@ -150,7 +166,7 @@ class JsonEqualityEvaluator(StringEvaluator):
|
|
|
150
166
|
dict: A dictionary containing the evaluation score.
|
|
151
167
|
"""
|
|
152
168
|
parsed = self._parse_json(prediction)
|
|
153
|
-
label = self._parse_json(cast(str, reference))
|
|
169
|
+
label = self._parse_json(cast("str", reference))
|
|
154
170
|
if isinstance(label, list):
|
|
155
171
|
if not isinstance(parsed, list):
|
|
156
172
|
return {"score": 0}
|
|
@@ -2,6 +2,7 @@ import json
|
|
|
2
2
|
from typing import Any, Callable, Optional, Union
|
|
3
3
|
|
|
4
4
|
from langchain_core.utils.json import parse_json_markdown
|
|
5
|
+
from typing_extensions import override
|
|
5
6
|
|
|
6
7
|
from langchain.evaluation.schema import StringEvaluator
|
|
7
8
|
|
|
@@ -14,13 +15,6 @@ class JsonEditDistanceEvaluator(StringEvaluator):
|
|
|
14
15
|
after parsing them and converting them to a canonical format (i.e., whitespace and key order are normalized).
|
|
15
16
|
It can be customized with alternative distance and canonicalization functions.
|
|
16
17
|
|
|
17
|
-
Args:
|
|
18
|
-
string_distance (Optional[Callable[[str, str], float]]): A callable that computes the distance between two strings.
|
|
19
|
-
If not provided, a Damerau-Levenshtein distance from the `rapidfuzz` package will be used.
|
|
20
|
-
canonicalize (Optional[Callable[[Any], Any]]): A callable that converts a parsed JSON object into its canonical string form.
|
|
21
|
-
If not provided, the default behavior is to serialize the JSON with sorted keys and no extra whitespace.
|
|
22
|
-
**kwargs (Any): Additional keyword arguments.
|
|
23
|
-
|
|
24
18
|
Attributes:
|
|
25
19
|
_string_distance (Callable[[str, str], float]): The internal distance computation function.
|
|
26
20
|
_canonicalize (Callable[[Any], Any]): The internal canonicalization function.
|
|
@@ -39,21 +33,37 @@ class JsonEditDistanceEvaluator(StringEvaluator):
|
|
|
39
33
|
self,
|
|
40
34
|
string_distance: Optional[Callable[[str, str], float]] = None,
|
|
41
35
|
canonicalize: Optional[Callable[[Any], Any]] = None,
|
|
42
|
-
**
|
|
36
|
+
**_: Any,
|
|
43
37
|
) -> None:
|
|
38
|
+
"""Initialize the JsonEditDistanceEvaluator.
|
|
39
|
+
|
|
40
|
+
Args:
|
|
41
|
+
string_distance: A callable that computes the distance between two strings.
|
|
42
|
+
If not provided, a Damerau-Levenshtein distance from the `rapidfuzz`
|
|
43
|
+
package will be used.
|
|
44
|
+
canonicalize: A callable that converts a parsed JSON object into its
|
|
45
|
+
canonical string form.
|
|
46
|
+
If not provided, the default behavior is to serialize the JSON with
|
|
47
|
+
sorted keys and no extra whitespace.
|
|
48
|
+
|
|
49
|
+
Raises:
|
|
50
|
+
ImportError: If the `rapidfuzz` package is not installed and no
|
|
51
|
+
`string_distance` function is provided.
|
|
52
|
+
"""
|
|
44
53
|
super().__init__()
|
|
45
54
|
if string_distance is not None:
|
|
46
55
|
self._string_distance = string_distance
|
|
47
56
|
else:
|
|
48
57
|
try:
|
|
49
58
|
from rapidfuzz import distance as rfd
|
|
50
|
-
except ImportError:
|
|
51
|
-
|
|
59
|
+
except ImportError as e:
|
|
60
|
+
msg = (
|
|
52
61
|
"The default string_distance operator for the "
|
|
53
62
|
" JsonEditDistanceEvaluator requires installation of "
|
|
54
63
|
"the rapidfuzz package. "
|
|
55
64
|
"Please install it with `pip install rapidfuzz`."
|
|
56
65
|
)
|
|
66
|
+
raise ImportError(msg) from e
|
|
57
67
|
self._string_distance = rfd.DamerauLevenshtein.normalized_distance
|
|
58
68
|
if canonicalize is not None:
|
|
59
69
|
self._canonicalize = canonicalize
|
|
@@ -65,14 +75,17 @@ class JsonEditDistanceEvaluator(StringEvaluator):
|
|
|
65
75
|
)
|
|
66
76
|
|
|
67
77
|
@property
|
|
78
|
+
@override
|
|
68
79
|
def requires_input(self) -> bool:
|
|
69
80
|
return False
|
|
70
81
|
|
|
71
82
|
@property
|
|
83
|
+
@override
|
|
72
84
|
def requires_reference(self) -> bool:
|
|
73
85
|
return True
|
|
74
86
|
|
|
75
87
|
@property
|
|
88
|
+
@override
|
|
76
89
|
def evaluation_name(self) -> str:
|
|
77
90
|
return "json_edit_distance"
|
|
78
91
|
|
|
@@ -81,6 +94,7 @@ class JsonEditDistanceEvaluator(StringEvaluator):
|
|
|
81
94
|
return parse_json_markdown(node)
|
|
82
95
|
return node
|
|
83
96
|
|
|
97
|
+
@override
|
|
84
98
|
def _evaluate_strings(
|
|
85
99
|
self,
|
|
86
100
|
prediction: str,
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
from typing import Any, Union
|
|
2
2
|
|
|
3
3
|
from langchain_core.utils.json import parse_json_markdown
|
|
4
|
+
from typing_extensions import override
|
|
4
5
|
|
|
5
6
|
from langchain.evaluation.schema import StringEvaluator
|
|
6
7
|
|
|
@@ -32,23 +33,21 @@ class JsonSchemaEvaluator(StringEvaluator):
|
|
|
32
33
|
|
|
33
34
|
""" # noqa: E501
|
|
34
35
|
|
|
35
|
-
def __init__(self, **
|
|
36
|
+
def __init__(self, **_: Any) -> None:
|
|
36
37
|
"""Initializes the JsonSchemaEvaluator.
|
|
37
38
|
|
|
38
|
-
Args:
|
|
39
|
-
kwargs: Additional keyword arguments.
|
|
40
|
-
|
|
41
39
|
Raises:
|
|
42
40
|
ImportError: If the jsonschema package is not installed.
|
|
43
41
|
"""
|
|
44
42
|
super().__init__()
|
|
45
43
|
try:
|
|
46
44
|
import jsonschema # noqa: F401
|
|
47
|
-
except ImportError:
|
|
48
|
-
|
|
45
|
+
except ImportError as e:
|
|
46
|
+
msg = (
|
|
49
47
|
"The JsonSchemaEvaluator requires the jsonschema package."
|
|
50
48
|
" Please install it with `pip install jsonschema`."
|
|
51
49
|
)
|
|
50
|
+
raise ImportError(msg) from e
|
|
52
51
|
|
|
53
52
|
@property
|
|
54
53
|
def requires_input(self) -> bool:
|
|
@@ -68,9 +67,12 @@ class JsonSchemaEvaluator(StringEvaluator):
|
|
|
68
67
|
def _parse_json(self, node: Any) -> Union[dict, list, None, float, bool, int, str]:
|
|
69
68
|
if isinstance(node, str):
|
|
70
69
|
return parse_json_markdown(node)
|
|
71
|
-
|
|
72
|
-
# Pydantic model
|
|
73
|
-
return
|
|
70
|
+
if hasattr(node, "model_json_schema") and callable(node.model_json_schema):
|
|
71
|
+
# Pydantic v2 model
|
|
72
|
+
return node.model_json_schema()
|
|
73
|
+
if hasattr(node, "schema") and callable(node.schema):
|
|
74
|
+
# Pydantic v1 model
|
|
75
|
+
return node.schema()
|
|
74
76
|
return node
|
|
75
77
|
|
|
76
78
|
def _validate(self, prediction: Any, schema: Any) -> dict:
|
|
@@ -78,12 +80,11 @@ class JsonSchemaEvaluator(StringEvaluator):
|
|
|
78
80
|
|
|
79
81
|
try:
|
|
80
82
|
validate(instance=prediction, schema=schema)
|
|
81
|
-
return {
|
|
82
|
-
"score": True,
|
|
83
|
-
}
|
|
84
83
|
except ValidationError as e:
|
|
85
84
|
return {"score": False, "reasoning": repr(e)}
|
|
85
|
+
return {"score": True}
|
|
86
86
|
|
|
87
|
+
@override
|
|
87
88
|
def _evaluate_strings(
|
|
88
89
|
self,
|
|
89
90
|
prediction: Union[str, Any],
|
|
@@ -7,4 +7,4 @@ from langchain.evaluation.qa.eval_chain import (
|
|
|
7
7
|
)
|
|
8
8
|
from langchain.evaluation.qa.generate_chain import QAGenerateChain
|
|
9
9
|
|
|
10
|
-
__all__ = ["
|
|
10
|
+
__all__ = ["ContextQAEvalChain", "CotQAEvalChain", "QAEvalChain", "QAGenerateChain"]
|
|
@@ -11,6 +11,7 @@ from langchain_core.callbacks import Callbacks
|
|
|
11
11
|
from langchain_core.language_models import BaseLanguageModel
|
|
12
12
|
from langchain_core.prompts import PromptTemplate
|
|
13
13
|
from pydantic import ConfigDict
|
|
14
|
+
from typing_extensions import override
|
|
14
15
|
|
|
15
16
|
from langchain.chains.llm import LLMChain
|
|
16
17
|
from langchain.evaluation.qa.eval_prompt import CONTEXT_PROMPT, COT_PROMPT, PROMPT
|
|
@@ -23,7 +24,7 @@ def _get_score(text: str) -> Optional[tuple[str, int]]:
|
|
|
23
24
|
if match:
|
|
24
25
|
if match.group(1).upper() == "CORRECT":
|
|
25
26
|
return "CORRECT", 1
|
|
26
|
-
|
|
27
|
+
if match.group(1).upper() == "INCORRECT":
|
|
27
28
|
return "INCORRECT", 0
|
|
28
29
|
try:
|
|
29
30
|
first_word = (
|
|
@@ -31,7 +32,7 @@ def _get_score(text: str) -> Optional[tuple[str, int]]:
|
|
|
31
32
|
)
|
|
32
33
|
if first_word.upper() == "CORRECT":
|
|
33
34
|
return "CORRECT", 1
|
|
34
|
-
|
|
35
|
+
if first_word.upper() == "INCORRECT":
|
|
35
36
|
return "INCORRECT", 0
|
|
36
37
|
last_word = (
|
|
37
38
|
text.strip()
|
|
@@ -40,7 +41,7 @@ def _get_score(text: str) -> Optional[tuple[str, int]]:
|
|
|
40
41
|
)
|
|
41
42
|
if last_word.upper() == "CORRECT":
|
|
42
43
|
return "CORRECT", 1
|
|
43
|
-
|
|
44
|
+
if last_word.upper() == "INCORRECT":
|
|
44
45
|
return "INCORRECT", 0
|
|
45
46
|
except IndexError:
|
|
46
47
|
pass
|
|
@@ -79,18 +80,22 @@ class QAEvalChain(LLMChain, StringEvaluator, LLMEvalChain):
|
|
|
79
80
|
)
|
|
80
81
|
|
|
81
82
|
@classmethod
|
|
83
|
+
@override
|
|
82
84
|
def is_lc_serializable(cls) -> bool:
|
|
83
85
|
return False
|
|
84
86
|
|
|
85
87
|
@property
|
|
88
|
+
@override
|
|
86
89
|
def evaluation_name(self) -> str:
|
|
87
90
|
return "correctness"
|
|
88
91
|
|
|
89
92
|
@property
|
|
93
|
+
@override
|
|
90
94
|
def requires_reference(self) -> bool:
|
|
91
95
|
return True
|
|
92
96
|
|
|
93
97
|
@property
|
|
98
|
+
@override
|
|
94
99
|
def requires_input(self) -> bool:
|
|
95
100
|
return True
|
|
96
101
|
|
|
@@ -119,10 +124,11 @@ class QAEvalChain(LLMChain, StringEvaluator, LLMEvalChain):
|
|
|
119
124
|
prompt = prompt or PROMPT
|
|
120
125
|
expected_input_vars = {"query", "answer", "result"}
|
|
121
126
|
if expected_input_vars != set(prompt.input_variables):
|
|
122
|
-
|
|
127
|
+
msg = (
|
|
123
128
|
f"Input variables should be {expected_input_vars}, "
|
|
124
129
|
f"but got {prompt.input_variables}"
|
|
125
130
|
)
|
|
131
|
+
raise ValueError(msg)
|
|
126
132
|
return cls(llm=llm, prompt=prompt, **kwargs)
|
|
127
133
|
|
|
128
134
|
def evaluate(
|
|
@@ -153,6 +159,7 @@ class QAEvalChain(LLMChain, StringEvaluator, LLMEvalChain):
|
|
|
153
159
|
parsed_result[RUN_KEY] = result[RUN_KEY]
|
|
154
160
|
return parsed_result
|
|
155
161
|
|
|
162
|
+
@override
|
|
156
163
|
def _evaluate_strings(
|
|
157
164
|
self,
|
|
158
165
|
*,
|
|
@@ -188,6 +195,7 @@ class QAEvalChain(LLMChain, StringEvaluator, LLMEvalChain):
|
|
|
188
195
|
)
|
|
189
196
|
return self._prepare_output(result)
|
|
190
197
|
|
|
198
|
+
@override
|
|
191
199
|
async def _aevaluate_strings(
|
|
192
200
|
self,
|
|
193
201
|
*,
|
|
@@ -210,6 +218,7 @@ class ContextQAEvalChain(LLMChain, StringEvaluator, LLMEvalChain):
|
|
|
210
218
|
"""LLM Chain for evaluating QA w/o GT based on context"""
|
|
211
219
|
|
|
212
220
|
@classmethod
|
|
221
|
+
@override
|
|
213
222
|
def is_lc_serializable(cls) -> bool:
|
|
214
223
|
return False
|
|
215
224
|
|
|
@@ -231,12 +240,14 @@ class ContextQAEvalChain(LLMChain, StringEvaluator, LLMEvalChain):
|
|
|
231
240
|
def _validate_input_vars(cls, prompt: PromptTemplate) -> None:
|
|
232
241
|
expected_input_vars = {"query", "context", "result"}
|
|
233
242
|
if expected_input_vars != set(prompt.input_variables):
|
|
234
|
-
|
|
243
|
+
msg = (
|
|
235
244
|
f"Input variables should be {expected_input_vars}, "
|
|
236
245
|
f"but got {prompt.input_variables}"
|
|
237
246
|
)
|
|
247
|
+
raise ValueError(msg)
|
|
238
248
|
|
|
239
249
|
@property
|
|
250
|
+
@override
|
|
240
251
|
def evaluation_name(self) -> str:
|
|
241
252
|
return "Contextual Accuracy"
|
|
242
253
|
|
|
@@ -294,6 +305,7 @@ class ContextQAEvalChain(LLMChain, StringEvaluator, LLMEvalChain):
|
|
|
294
305
|
parsed_result[RUN_KEY] = result[RUN_KEY]
|
|
295
306
|
return parsed_result
|
|
296
307
|
|
|
308
|
+
@override
|
|
297
309
|
def _evaluate_strings(
|
|
298
310
|
self,
|
|
299
311
|
*,
|
|
@@ -315,6 +327,7 @@ class ContextQAEvalChain(LLMChain, StringEvaluator, LLMEvalChain):
|
|
|
315
327
|
)
|
|
316
328
|
return self._prepare_output(result)
|
|
317
329
|
|
|
330
|
+
@override
|
|
318
331
|
async def _aevaluate_strings(
|
|
319
332
|
self,
|
|
320
333
|
*,
|
|
@@ -337,10 +350,12 @@ class CotQAEvalChain(ContextQAEvalChain):
|
|
|
337
350
|
"""LLM Chain for evaluating QA using chain of thought reasoning."""
|
|
338
351
|
|
|
339
352
|
@classmethod
|
|
353
|
+
@override
|
|
340
354
|
def is_lc_serializable(cls) -> bool:
|
|
341
355
|
return False
|
|
342
356
|
|
|
343
357
|
@property
|
|
358
|
+
@override
|
|
344
359
|
def evaluation_name(self) -> str:
|
|
345
360
|
return "COT Contextual Accuracy"
|
|
346
361
|
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
# flake8: noqa
|
|
2
1
|
from langchain_core.prompts import PromptTemplate
|
|
3
2
|
|
|
4
3
|
template = """You are a teacher grading a quiz.
|
|
@@ -10,12 +9,12 @@ STUDENT ANSWER: student's answer here
|
|
|
10
9
|
TRUE ANSWER: true answer here
|
|
11
10
|
GRADE: CORRECT or INCORRECT here
|
|
12
11
|
|
|
13
|
-
Grade the student answers based ONLY on their factual accuracy. Ignore differences in punctuation and phrasing between the student answer and true answer. It is OK if the student answer contains more information than the true answer, as long as it does not contain any conflicting statements. Begin!
|
|
12
|
+
Grade the student answers based ONLY on their factual accuracy. Ignore differences in punctuation and phrasing between the student answer and true answer. It is OK if the student answer contains more information than the true answer, as long as it does not contain any conflicting statements. Begin!
|
|
14
13
|
|
|
15
14
|
QUESTION: {query}
|
|
16
15
|
STUDENT ANSWER: {result}
|
|
17
16
|
TRUE ANSWER: {answer}
|
|
18
|
-
GRADE:"""
|
|
17
|
+
GRADE:""" # noqa: E501
|
|
19
18
|
PROMPT = PromptTemplate(
|
|
20
19
|
input_variables=["query", "result", "answer"], template=template
|
|
21
20
|
)
|
|
@@ -29,12 +28,12 @@ CONTEXT: context the question is about here
|
|
|
29
28
|
STUDENT ANSWER: student's answer here
|
|
30
29
|
GRADE: CORRECT or INCORRECT here
|
|
31
30
|
|
|
32
|
-
Grade the student answers based ONLY on their factual accuracy. Ignore differences in punctuation and phrasing between the student answer and true answer. It is OK if the student answer contains more information than the true answer, as long as it does not contain any conflicting statements. Begin!
|
|
31
|
+
Grade the student answers based ONLY on their factual accuracy. Ignore differences in punctuation and phrasing between the student answer and true answer. It is OK if the student answer contains more information than the true answer, as long as it does not contain any conflicting statements. Begin!
|
|
33
32
|
|
|
34
33
|
QUESTION: {query}
|
|
35
34
|
CONTEXT: {context}
|
|
36
35
|
STUDENT ANSWER: {result}
|
|
37
|
-
GRADE:"""
|
|
36
|
+
GRADE:""" # noqa: E501
|
|
38
37
|
CONTEXT_PROMPT = PromptTemplate(
|
|
39
38
|
input_variables=["query", "context", "result"], template=context_template
|
|
40
39
|
)
|
|
@@ -51,12 +50,12 @@ STUDENT ANSWER: student's answer here
|
|
|
51
50
|
EXPLANATION: step by step reasoning here
|
|
52
51
|
GRADE: CORRECT or INCORRECT here
|
|
53
52
|
|
|
54
|
-
Grade the student answers based ONLY on their factual accuracy. Ignore differences in punctuation and phrasing between the student answer and true answer. It is OK if the student answer contains more information than the true answer, as long as it does not contain any conflicting statements. Begin!
|
|
53
|
+
Grade the student answers based ONLY on their factual accuracy. Ignore differences in punctuation and phrasing between the student answer and true answer. It is OK if the student answer contains more information than the true answer, as long as it does not contain any conflicting statements. Begin!
|
|
55
54
|
|
|
56
55
|
QUESTION: {query}
|
|
57
56
|
CONTEXT: {context}
|
|
58
57
|
STUDENT ANSWER: {result}
|
|
59
|
-
EXPLANATION:"""
|
|
58
|
+
EXPLANATION:""" # noqa: E501
|
|
60
59
|
COT_PROMPT = PromptTemplate(
|
|
61
60
|
input_variables=["query", "context", "result"], template=cot_template
|
|
62
61
|
)
|
|
@@ -72,7 +71,7 @@ template = """You are comparing a submitted answer to an expert answer on a give
|
|
|
72
71
|
[Submission]: {result}
|
|
73
72
|
***
|
|
74
73
|
[END DATA]
|
|
75
|
-
Compare the content and correctness of the submitted SQL with the expert answer. Ignore any differences in whitespace, style, or output column names. The submitted answer may either be correct or incorrect. Determine which case applies. First, explain in detail the similarities or differences between the expert answer and the submission, ignoring superficial aspects such as whitespace, style or output column names. Do not state the final answer in your initial explanation. Then, respond with either "CORRECT" or "INCORRECT" (without quotes or punctuation) on its own line. This should correspond to whether the submitted SQL and the expert answer are semantically the same or different, respectively. Then, repeat your final answer on a new line."""
|
|
74
|
+
Compare the content and correctness of the submitted SQL with the expert answer. Ignore any differences in whitespace, style, or output column names. The submitted answer may either be correct or incorrect. Determine which case applies. First, explain in detail the similarities or differences between the expert answer and the submission, ignoring superficial aspects such as whitespace, style or output column names. Do not state the final answer in your initial explanation. Then, respond with either "CORRECT" or "INCORRECT" (without quotes or punctuation) on its own line. This should correspond to whether the submitted SQL and the expert answer are semantically the same or different, respectively. Then, repeat your final answer on a new line.""" # noqa: E501
|
|
76
75
|
|
|
77
76
|
SQL_PROMPT = PromptTemplate(
|
|
78
77
|
input_variables=["query", "answer", "result"], template=template
|
|
@@ -7,13 +7,15 @@ from typing import Any
|
|
|
7
7
|
from langchain_core.language_models import BaseLanguageModel
|
|
8
8
|
from langchain_core.output_parsers import BaseLLMOutputParser
|
|
9
9
|
from pydantic import Field
|
|
10
|
+
from typing_extensions import override
|
|
10
11
|
|
|
11
12
|
from langchain.chains.llm import LLMChain
|
|
12
13
|
from langchain.evaluation.qa.generate_prompt import PROMPT
|
|
13
14
|
from langchain.output_parsers.regex import RegexParser
|
|
14
15
|
|
|
15
16
|
_QA_OUTPUT_PARSER = RegexParser(
|
|
16
|
-
regex=r"QUESTION: (.*?)\n+ANSWER: (.*)",
|
|
17
|
+
regex=r"QUESTION: (.*?)\n+ANSWER: (.*)",
|
|
18
|
+
output_keys=["query", "answer"],
|
|
17
19
|
)
|
|
18
20
|
|
|
19
21
|
|
|
@@ -24,6 +26,7 @@ class QAGenerateChain(LLMChain):
|
|
|
24
26
|
output_key: str = "qa_pairs"
|
|
25
27
|
|
|
26
28
|
@classmethod
|
|
29
|
+
@override
|
|
27
30
|
def is_lc_serializable(cls) -> bool:
|
|
28
31
|
return False
|
|
29
32
|
|
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
# flake8: noqa
|
|
2
|
-
from langchain.output_parsers.regex import RegexParser
|
|
3
1
|
from langchain_core.prompts import PromptTemplate
|
|
4
2
|
|
|
5
|
-
template = """You are a teacher coming up with questions to ask on a quiz.
|
|
3
|
+
template = """You are a teacher coming up with questions to ask on a quiz.
|
|
6
4
|
Given the following document, please generate a question and answer based on that document.
|
|
7
5
|
|
|
8
6
|
Example Format:
|
|
@@ -16,7 +14,7 @@ These questions should be detailed and be based explicitly on information in the
|
|
|
16
14
|
|
|
17
15
|
<Begin Document>
|
|
18
16
|
{doc}
|
|
19
|
-
<End Document>"""
|
|
17
|
+
<End Document>""" # noqa: E501
|
|
20
18
|
PROMPT = PromptTemplate(
|
|
21
19
|
input_variables=["doc"],
|
|
22
20
|
template=template,
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import re
|
|
2
2
|
from typing import Any
|
|
3
3
|
|
|
4
|
+
from typing_extensions import override
|
|
5
|
+
|
|
4
6
|
from langchain.evaluation.schema import StringEvaluator
|
|
5
7
|
|
|
6
8
|
|
|
@@ -27,7 +29,12 @@ class RegexMatchStringEvaluator(StringEvaluator):
|
|
|
27
29
|
) # This will return {'score': 1.0} as the prediction matches the second pattern in the union
|
|
28
30
|
""" # noqa: E501
|
|
29
31
|
|
|
30
|
-
def __init__(self, *, flags: int = 0, **
|
|
32
|
+
def __init__(self, *, flags: int = 0, **_: Any): # Default is no flags
|
|
33
|
+
"""Initialize the RegexMatchStringEvaluator.
|
|
34
|
+
|
|
35
|
+
Args:
|
|
36
|
+
flags: Flags to use for the regex match. Defaults to 0 (no flags).
|
|
37
|
+
"""
|
|
31
38
|
super().__init__()
|
|
32
39
|
self.flags = flags
|
|
33
40
|
|
|
@@ -65,6 +72,7 @@ class RegexMatchStringEvaluator(StringEvaluator):
|
|
|
65
72
|
"""
|
|
66
73
|
return "regex_match"
|
|
67
74
|
|
|
75
|
+
@override
|
|
68
76
|
def _evaluate_strings( # type: ignore[override]
|
|
69
77
|
self,
|
|
70
78
|
*,
|