langchain.cache
langchain.callbacks.aim_callback
langchain.callbacks.argilla_callback
langchain.callbacks.arize_callback
langchain.callbacks.arthur_callback
langchain.callbacks.base
langchain.callbacks.clearml_callback
langchain.callbacks.comet_ml_callback
langchain.callbacks.confident_callback
langchain.callbacks.context_callback
langchain.callbacks.file
langchain.callbacks.flyte_callback
langchain.callbacks.human
langchain.callbacks.infino_callback
langchain.callbacks.labelstudio_callback
langchain.callbacks.llmonitor_callback
langchain.callbacks.manager
langchain.callbacks.mlflow_callback
langchain.callbacks.openai_info
langchain.callbacks.promptlayer_callback
langchain.callbacks.sagemaker_callback
langchain.callbacks.stdout
langchain.callbacks.streaming_aiter
langchain.callbacks.streaming_aiter_final_only
langchain.callbacks.streaming_stdout
langchain.callbacks.streaming_stdout_final_only
langchain.callbacks.streamlit.mutable_expander
langchain.callbacks.streamlit.streamlit_callback_handler
langchain.callbacks.tracers.base
langchain.callbacks.tracers.evaluation
langchain.callbacks.tracers.langchain
langchain.callbacks.tracers.langchain_v1
langchain.callbacks.tracers.log_stream
langchain.callbacks.tracers.run_collector
langchain.callbacks.tracers.schemas
langchain.callbacks.tracers.stdout
langchain.callbacks.tracers.wandb
langchain.callbacks.trubrics_callback
langchain.callbacks.utils
langchain.callbacks.wandb_callback
langchain.callbacks.whylabs_callback
langchain.chains.api.base
langchain.chains.api.openapi.chain
langchain.chains.api.openapi.requests_chain
langchain.chains.api.openapi.response_chain
langchain.chains.base
langchain.chains.combine_documents.base
langchain.chains.combine_documents.map_reduce
langchain.chains.combine_documents.map_rerank
langchain.chains.combine_documents.reduce
langchain.chains.combine_documents.refine
langchain.chains.combine_documents.stuff


langchain.chains.combine_documents.refine
langchain.chains.combine_documents.stuff
langchain.chains.constitutional_ai.base
langchain.chains.constitutional_ai.models
langchain.chains.conversation.base
langchain.chains.conversational_retrieval.base
langchain.chains.elasticsearch_database.base
langchain.chains.example_generator
langchain.chains.flare.base
langchain.chains.flare.prompts
langchain.chains.graph_qa.arangodb
langchain.chains.graph_qa.base
langchain.chains.graph_qa.cypher
langchain.chains.graph_qa.falkordb
langchain.chains.graph_qa.hugegraph
langchain.chains.graph_qa.kuzu
langchain.chains.graph_qa.nebulagraph
langchain.chains.graph_qa.neptune_cypher
langchain.chains.graph_qa.sparql
langchain.chains.hyde.base
langchain.chains.llm
langchain.chains.llm_bash.base
langchain.chains.llm_bash.prompt
langchain.chains.llm_checker.base
langchain.chains.llm_math.base
langchain.chains.llm_requests
langchain.chains.llm_summarization_checker.base
langchain.chains.llm_symbolic_math.base
langchain.chains.loading
langchain.chains.mapreduce
langchain.chains.moderation
langchain.chains.natbot.base
langchain.chains.natbot.crawler
langchain.chains.openai_functions.base
langchain.chains.openai_functions.citation_fuzzy_match
langchain.chains.openai_functions.extraction
langchain.chains.openai_functions.openapi
langchain.chains.openai_functions.qa_with_structure
langchain.chains.openai_functions.tagging
langchain.chains.openai_functions.utils
langchain.chains.prompt_selector
langchain.chains.qa_generation.base


langchain.chains.prompt_selector
langchain.chains.qa_generation.base
langchain.chains.qa_with_sources.base
langchain.chains.qa_with_sources.loading
langchain.chains.qa_with_sources.retrieval
langchain.chains.qa_with_sources.vector_db
langchain.chains.query_constructor.base
langchain.chains.query_constructor.ir
langchain.chains.query_constructor.parser
langchain.chains.query_constructor.schema
langchain.chains.retrieval_qa.base
langchain.chains.router.base
langchain.chains.router.embedding_router
langchain.chains.router.llm_router
langchain.chains.router.multi_prompt
langchain.chains.router.multi_retrieval_qa
langchain.chains.sequential
langchain.chains.sql_database.query
langchain.chains.transform
langchain.chat_loaders.base
langchain.chat_loaders.facebook_messenger
langchain.chat_loaders.gmail
langchain.chat_loaders.imessage
langchain.chat_loaders.slack
langchain.chat_loaders.telegram
langchain.chat_loaders.utils
langchain.chat_loaders.whatsapp
langchain.chat_models.anthropic
langchain.chat_models.anyscale
langchain.chat_models.azure_openai
langchain.chat_models.azureml_endpoint
langchain.chat_models.baidu_qianfan_endpoint
langchain.chat_models.base
langchain.chat_models.bedrock
langchain.chat_models.ernie
langchain.chat_models.fake
langchain.chat_models.fireworks
langchain.chat_models.google_palm
langchain.chat_models.human
langchain.chat_models.javelin_ai_gateway
langchain.chat_models.jinachat
langchain.chat_models.konko
langchain.chat_models.litellm
langchain.chat_models.minimax
langchain.chat_models.mlflow_ai_gateway
langchain.chat_models.ollama
langchain.chat_models.openai
langchain.chat_models.promptlayer_openai
langchain.chat_models.vertexai


langchain.chat_models.promptlayer_openai
langchain.chat_models.vertexai
langchain.docstore.arbitrary_fn
langchain.docstore.base
langchain.docstore.in_memory
langchain.docstore.wikipedia
langchain.document_loaders.acreom
langchain.document_loaders.airbyte
langchain.document_loaders.airbyte_json
langchain.document_loaders.airtable
langchain.document_loaders.apify_dataset
langchain.document_loaders.arcgis_loader
langchain.document_loaders.arxiv
langchain.document_loaders.assemblyai
langchain.document_loaders.async_html
langchain.document_loaders.azlyrics
langchain.document_loaders.azure_blob_storage_container
langchain.document_loaders.azure_blob_storage_file
langchain.document_loaders.base
langchain.document_loaders.base_o365
langchain.document_loaders.bibtex
langchain.document_loaders.bigquery
langchain.document_loaders.bilibili
langchain.document_loaders.blackboard
langchain.document_loaders.blob_loaders.file_system
langchain.document_loaders.blob_loaders.schema
langchain.document_loaders.blob_loaders.youtube_audio
langchain.document_loaders.blockchain
langchain.document_loaders.brave_search
langchain.document_loaders.browserless
langchain.document_loaders.chatgpt
langchain.document_loaders.chromium
langchain.document_loaders.college_confidential
langchain.document_loaders.concurrent
langchain.document_loaders.confluence
langchain.document_loaders.conllu
langchain.document_loaders.csv_loader
langchain.document_loaders.cube_semantic
langchain.document_loaders.datadog_logs
langchain.document_loaders.dataframe
langchain.document_loaders.diffbot
langchain.document_loaders.directory
langchain.document_loaders.discord
langchain.document_loaders.docugami
langchain.document_loaders.dropbox
langchain.document_loaders.duckdb_loader
langchain.document_loaders.email


langchain.document_loaders.duckdb_loader
langchain.document_loaders.email
langchain.document_loaders.embaas
langchain.document_loaders.epub
langchain.document_loaders.etherscan
langchain.document_loaders.evernote
langchain.document_loaders.excel
langchain.document_loaders.facebook_chat
langchain.document_loaders.fauna
langchain.document_loaders.figma
langchain.document_loaders.gcs_directory
langchain.document_loaders.gcs_file
langchain.document_loaders.generic
langchain.document_loaders.geodataframe
langchain.document_loaders.git
langchain.document_loaders.gitbook
langchain.document_loaders.github
langchain.document_loaders.googledrive
langchain.document_loaders.gutenberg
langchain.document_loaders.helpers
langchain.document_loaders.hn
langchain.document_loaders.html
langchain.document_loaders.html_bs
langchain.document_loaders.hugging_face_dataset
langchain.document_loaders.ifixit
langchain.document_loaders.image
langchain.document_loaders.image_captions
langchain.document_loaders.imsdb
langchain.document_loaders.iugu
langchain.document_loaders.joplin
langchain.document_loaders.json_loader
langchain.document_loaders.larksuite
langchain.document_loaders.markdown
langchain.document_loaders.mastodon
langchain.document_loaders.max_compute
langchain.document_loaders.mediawikidump
langchain.document_loaders.merge
langchain.document_loaders.mhtml
langchain.document_loaders.modern_treasury
langchain.document_loaders.mongodb
langchain.document_loaders.news
langchain.document_loaders.notebook
langchain.document_loaders.notion
langchain.document_loaders.notiondb
langchain.document_loaders.nuclia
langchain.document_loaders.obs_directory
langchain.document_loaders.obs_file


langchain.document_loaders.obs_directory
langchain.document_loaders.obs_file
langchain.document_loaders.obsidian
langchain.document_loaders.odt
langchain.document_loaders.onedrive
langchain.document_loaders.onedrive_file
langchain.document_loaders.open_city_data
langchain.document_loaders.org_mode
langchain.document_loaders.parsers.audio
langchain.document_loaders.parsers.docai
langchain.document_loaders.parsers.generic
langchain.document_loaders.parsers.grobid
langchain.document_loaders.parsers.html.bs4
langchain.document_loaders.parsers.language.code_segmenter
langchain.document_loaders.parsers.language.javascript
langchain.document_loaders.parsers.language.language_parser
langchain.document_loaders.parsers.language.python
langchain.document_loaders.parsers.msword
langchain.document_loaders.parsers.pdf
langchain.document_loaders.parsers.registry
langchain.document_loaders.parsers.txt
langchain.document_loaders.pdf
langchain.document_loaders.polars_dataframe
langchain.document_loaders.powerpoint
langchain.document_loaders.psychic
langchain.document_loaders.pubmed
langchain.document_loaders.pyspark_dataframe
langchain.document_loaders.python
langchain.document_loaders.readthedocs
langchain.document_loaders.recursive_url_loader
langchain.document_loaders.reddit
langchain.document_loaders.roam
langchain.document_loaders.rocksetdb
langchain.document_loaders.rss
langchain.document_loaders.rst
langchain.document_loaders.rtf
langchain.document_loaders.s3_directory
langchain.document_loaders.s3_file
langchain.document_loaders.sharepoint
langchain.document_loaders.sitemap
langchain.document_loaders.slack_directory
langchain.document_loaders.snowflake_loader
langchain.document_loaders.spreedly
langchain.document_loaders.srt
langchain.document_loaders.stripe
langchain.document_loaders.telegram


langchain.document_loaders.stripe
langchain.document_loaders.telegram
langchain.document_loaders.tencent_cos_directory
langchain.document_loaders.tencent_cos_file
langchain.document_loaders.tensorflow_datasets
langchain.document_loaders.text
langchain.document_loaders.tomarkdown
langchain.document_loaders.toml
langchain.document_loaders.trello
langchain.document_loaders.tsv
langchain.document_loaders.twitter
langchain.document_loaders.unstructured
langchain.document_loaders.url
langchain.document_loaders.url_playwright
langchain.document_loaders.url_selenium
langchain.document_loaders.weather
langchain.document_loaders.web_base
langchain.document_loaders.whatsapp_chat
langchain.document_loaders.wikipedia
langchain.document_loaders.word_document
langchain.document_loaders.xml
langchain.document_loaders.xorbits
langchain.document_loaders.youtube
langchain.document_transformers.beautiful_soup_transformer
langchain.document_transformers.doctran_text_extract
langchain.document_transformers.doctran_text_qa
langchain.document_transformers.doctran_text_translate
langchain.document_transformers.embeddings_redundant_filter
langchain.document_transformers.html2text
langchain.document_transformers.long_context_reorder
langchain.document_transformers.nuclia_text_transform
langchain.document_transformers.openai_functions
langchain.embeddings.aleph_alpha
langchain.embeddings.awa
langchain.embeddings.baidu_qianfan_endpoint
langchain.embeddings.bedrock
langchain.embeddings.cache
langchain.embeddings.clarifai
langchain.embeddings.cohere
langchain.embeddings.dashscope
langchain.embeddings.deepinfra
langchain.embeddings.edenai
langchain.embeddings.elasticsearch
langchain.embeddings.embaas
langchain.embeddings.ernie
langchain.embeddings.fake
langchain.embeddings.google_palm


langchain.embeddings.fake
langchain.embeddings.google_palm
langchain.embeddings.gpt4all
langchain.embeddings.gradient_ai
langchain.embeddings.huggingface
langchain.embeddings.huggingface_hub
langchain.embeddings.javelin_ai_gateway
langchain.embeddings.jina
langchain.embeddings.llamacpp
langchain.embeddings.llm_rails
langchain.embeddings.localai
langchain.embeddings.minimax
langchain.embeddings.mlflow_gateway
langchain.embeddings.modelscope_hub
langchain.embeddings.mosaicml
langchain.embeddings.nlpcloud
langchain.embeddings.octoai_embeddings
langchain.embeddings.ollama
langchain.embeddings.openai
langchain.embeddings.sagemaker_endpoint
langchain.embeddings.self_hosted
langchain.embeddings.self_hosted_hugging_face
langchain.embeddings.spacy_embeddings
langchain.embeddings.tensorflow_hub
langchain.embeddings.vertexai
langchain.embeddings.xinference
langchain.evaluation.agents.trajectory_eval_chain
langchain.evaluation.comparison.eval_chain
langchain.evaluation.criteria.eval_chain
langchain.evaluation.embedding_distance.base
langchain.evaluation.exact_match.base
langchain.evaluation.loading
langchain.evaluation.parsing.base
langchain.evaluation.qa.eval_chain
langchain.evaluation.qa.generate_chain
langchain.evaluation.regex_match.base
langchain.evaluation.schema
langchain.evaluation.string_distance.base
langchain.graphs.arangodb_graph
langchain.graphs.falkordb_graph
langchain.graphs.graph_document
langchain.graphs.hugegraph
langchain.graphs.kuzu_graph
langchain.graphs.memgraph_graph
langchain.graphs.nebula_graph
langchain.graphs.neo4j_graph
langchain.graphs.neptune_graph
langchain.graphs.networkx_graph


langchain.graphs.neptune_graph
langchain.graphs.networkx_graph
langchain.graphs.rdf_graph
langchain.hub
langchain.indexes.base
langchain.indexes.graph
langchain.indexes.vectorstore
langchain.llms.ai21
langchain.llms.aleph_alpha
langchain.llms.amazon_api_gateway
langchain.llms.anthropic
langchain.llms.anyscale
langchain.llms.aviary
langchain.llms.azureml_endpoint
langchain.llms.baidu_qianfan_endpoint
langchain.llms.bananadev
langchain.llms.base
langchain.llms.baseten
langchain.llms.beam
langchain.llms.bedrock
langchain.llms.bittensor
langchain.llms.cerebriumai
langchain.llms.chatglm
langchain.llms.clarifai
langchain.llms.cohere
langchain.llms.ctransformers
langchain.llms.ctranslate2
langchain.llms.databricks
langchain.llms.deepinfra
langchain.llms.deepsparse
langchain.llms.edenai
langchain.llms.fake
langchain.llms.fireworks
langchain.llms.forefrontai
langchain.llms.google_palm
langchain.llms.gooseai
langchain.llms.gpt4all
langchain.llms.gradient_ai
langchain.llms.huggingface_endpoint
langchain.llms.huggingface_hub
langchain.llms.huggingface_pipeline
langchain.llms.huggingface_text_gen_inference
langchain.llms.human
langchain.llms.javelin_ai_gateway
langchain.llms.koboldai
langchain.llms.llamacpp
langchain.llms.loading
langchain.llms.manifest
langchain.llms.minimax
langchain.llms.mlflow_ai_gateway


langchain.llms.minimax
langchain.llms.mlflow_ai_gateway
langchain.llms.modal
langchain.llms.mosaicml
langchain.llms.nlpcloud
langchain.llms.octoai_endpoint
langchain.llms.ollama
langchain.llms.opaqueprompts
langchain.llms.openai
langchain.llms.openllm
langchain.llms.openlm
langchain.llms.petals
langchain.llms.pipelineai
langchain.llms.predibase
langchain.llms.predictionguard
langchain.llms.promptlayer_openai
langchain.llms.replicate
langchain.llms.rwkv
langchain.llms.sagemaker_endpoint
langchain.llms.self_hosted
langchain.llms.self_hosted_hugging_face
langchain.llms.stochasticai
langchain.llms.symblai_nebula
langchain.llms.textgen
langchain.llms.titan_takeoff
langchain.llms.tongyi
langchain.llms.utils
langchain.llms.vertexai
langchain.llms.vllm
langchain.llms.writer
langchain.llms.xinference
langchain.load.dump
langchain.load.load
langchain.load.serializable
langchain.memory.buffer
langchain.memory.buffer_window
langchain.memory.chat_memory
langchain.memory.chat_message_histories.cassandra
langchain.memory.chat_message_histories.cosmos_db
langchain.memory.chat_message_histories.dynamodb
langchain.memory.chat_message_histories.file
langchain.memory.chat_message_histories.firestore
langchain.memory.chat_message_histories.in_memory
langchain.memory.chat_message_histories.momento
langchain.memory.chat_message_histories.mongodb
langchain.memory.chat_message_histories.postgres
langchain.memory.chat_message_histories.redis
langchain.memory.chat_message_histories.rocksetdb
langchain.memory.chat_message_histories.sql


langchain.memory.chat_message_histories.sql
langchain.memory.chat_message_histories.streamlit
langchain.memory.chat_message_histories.xata
langchain.memory.chat_message_histories.zep
langchain.memory.combined
langchain.memory.entity
langchain.memory.kg
langchain.memory.motorhead_memory
langchain.memory.readonly
langchain.memory.simple
langchain.memory.summary
langchain.memory.summary_buffer
langchain.memory.token_buffer
langchain.memory.utils
langchain.memory.vectorstore
langchain.memory.zep_memory
langchain.model_laboratory
langchain.output_parsers.boolean
langchain.output_parsers.combining
langchain.output_parsers.datetime
langchain.output_parsers.enum
langchain.output_parsers.fix
langchain.output_parsers.json
langchain.output_parsers.list
langchain.output_parsers.loading
langchain.output_parsers.openai_functions
langchain.output_parsers.pydantic
langchain.output_parsers.rail_parser
langchain.output_parsers.regex
langchain.output_parsers.regex_dict
langchain.output_parsers.retry
langchain.output_parsers.structured
langchain.output_parsers.xml
langchain.prompts.base
langchain.prompts.chat
langchain.prompts.example_selector.base
langchain.prompts.example_selector.length_based
langchain.prompts.example_selector.ngram_overlap
langchain.prompts.example_selector.semantic_similarity
langchain.prompts.few_shot
langchain.prompts.few_shot_with_templates
langchain.prompts.loading
langchain.prompts.pipeline
langchain.prompts.prompt
langchain.retrievers.arxiv
langchain.retrievers.azure_cognitive_search
langchain.retrievers.bm25
langchain.retrievers.chaindesk
langchain.retrievers.chatgpt_plugin_retriever
langchain.retrievers.contextual_compression
langchain.retrievers.databerry
langchain.retrievers.docarray


langchain.retrievers.databerry
langchain.retrievers.docarray
langchain.retrievers.document_compressors.base
langchain.retrievers.document_compressors.chain_extract
langchain.retrievers.document_compressors.chain_filter
langchain.retrievers.document_compressors.cohere_rerank
langchain.retrievers.document_compressors.embeddings_filter
langchain.retrievers.elastic_search_bm25
langchain.retrievers.ensemble
langchain.retrievers.google_cloud_enterprise_search
langchain.retrievers.kay
langchain.retrievers.kendra
langchain.retrievers.knn
langchain.retrievers.llama_index
langchain.retrievers.merger_retriever
langchain.retrievers.metal
langchain.retrievers.milvus
langchain.retrievers.multi_query
langchain.retrievers.multi_vector
langchain.retrievers.parent_document_retriever
langchain.retrievers.pinecone_hybrid_search
langchain.retrievers.pubmed
langchain.retrievers.re_phraser
langchain.retrievers.remote_retriever
langchain.retrievers.self_query.base
langchain.retrievers.self_query.chroma
langchain.retrievers.self_query.dashvector
langchain.retrievers.self_query.deeplake
langchain.retrievers.self_query.elasticsearch
langchain.retrievers.self_query.milvus
langchain.retrievers.self_query.myscale
langchain.retrievers.self_query.opensearch
langchain.retrievers.self_query.pinecone
langchain.retrievers.self_query.qdrant
langchain.retrievers.self_query.redis
langchain.retrievers.self_query.supabase
langchain.retrievers.self_query.timescalevector
langchain.retrievers.self_query.vectara
langchain.retrievers.self_query.weaviate


langchain.retrievers.self_query.weaviate
langchain.retrievers.svm
langchain.retrievers.tfidf
langchain.retrievers.time_weighted_retriever
langchain.retrievers.vespa_retriever
langchain.retrievers.weaviate_hybrid_search
langchain.retrievers.web_research
langchain.retrievers.wikipedia
langchain.retrievers.zep
langchain.retrievers.zilliz
langchain.runnables.openai_functions
langchain.schema.agent
langchain.schema.cache
langchain.schema.chat
langchain.schema.chat_history
langchain.schema.document
langchain.schema.embeddings
langchain.schema.exceptions
langchain.schema.language_model
langchain.schema.memory
langchain.schema.messages
langchain.schema.output
langchain.schema.output_parser
langchain.schema.prompt
langchain.schema.prompt_template
langchain.schema.retriever
langchain.schema.runnable.base
langchain.schema.runnable.config
langchain.schema.runnable.passthrough
langchain.schema.runnable.retry
langchain.schema.runnable.router
langchain.schema.runnable.utils
langchain.schema.storage
langchain.schema.vectorstore
langchain.smith.evaluation.config
langchain.smith.evaluation.name_generation
langchain.smith.evaluation.progress
langchain.smith.evaluation.runner_utils
langchain.smith.evaluation.string_run_evaluator
langchain.storage.encoder_backed
langchain.storage.exceptions
langchain.storage.file_system
langchain.storage.in_memory
langchain.storage.redis
langchain.text_splitter
langchain.tools.ainetwork.app
langchain.tools.ainetwork.base
langchain.tools.ainetwork.owner
langchain.tools.ainetwork.rule
langchain.tools.ainetwork.transfer
langchain.tools.ainetwork.utils
langchain.tools.ainetwork.value
langchain.tools.amadeus.base
langchain.tools.amadeus.closest_airport


langchain.tools.amadeus.base
langchain.tools.amadeus.closest_airport
langchain.tools.amadeus.flight_search
langchain.tools.amadeus.utils
langchain.tools.arxiv.tool
langchain.tools.azure_cognitive_services.form_recognizer
langchain.tools.azure_cognitive_services.image_analysis
langchain.tools.azure_cognitive_services.speech2text
langchain.tools.azure_cognitive_services.text2speech
langchain.tools.azure_cognitive_services.utils
langchain.tools.base
langchain.tools.bing_search.tool
langchain.tools.brave_search.tool
langchain.tools.dataforseo_api_search.tool
langchain.tools.ddg_search.tool
langchain.tools.edenai.audio_speech_to_text
langchain.tools.edenai.audio_text_to_speech
langchain.tools.edenai.edenai_base_tool
langchain.tools.edenai.image_explicitcontent
langchain.tools.edenai.image_objectdetection
langchain.tools.edenai.ocr_identityparser
langchain.tools.edenai.ocr_invoiceparser
langchain.tools.edenai.text_moderation
langchain.tools.eleven_labs.models
langchain.tools.eleven_labs.text2speech
langchain.tools.file_management.copy
langchain.tools.file_management.delete
langchain.tools.file_management.file_search
langchain.tools.file_management.list_dir
langchain.tools.file_management.move
langchain.tools.file_management.read
langchain.tools.file_management.utils
langchain.tools.file_management.write
langchain.tools.github.tool
langchain.tools.gitlab.tool
langchain.tools.gmail.base
langchain.tools.gmail.create_draft
langchain.tools.gmail.get_message
langchain.tools.gmail.get_thread
langchain.tools.gmail.search
langchain.tools.gmail.send_message
langchain.tools.gmail.utils
langchain.tools.golden_query.tool
langchain.tools.google_places.tool
langchain.tools.google_search.tool
langchain.tools.google_serper.tool


langchain.tools.google_search.tool
langchain.tools.google_serper.tool
langchain.tools.graphql.tool
langchain.tools.human.tool
langchain.tools.ifttt
langchain.tools.interaction.tool
langchain.tools.jira.tool
langchain.tools.json.tool
langchain.tools.metaphor_search.tool
langchain.tools.multion.create_session
langchain.tools.multion.update_session
langchain.tools.nuclia.tool
langchain.tools.office365.base
langchain.tools.office365.create_draft_message
langchain.tools.office365.events_search
langchain.tools.office365.messages_search
langchain.tools.office365.send_event
langchain.tools.office365.send_message
langchain.tools.office365.utils
langchain.tools.openapi.utils.api_models
langchain.tools.openweathermap.tool
langchain.tools.playwright.base
langchain.tools.playwright.click
langchain.tools.playwright.current_page
langchain.tools.playwright.extract_hyperlinks
langchain.tools.playwright.extract_text
langchain.tools.playwright.get_elements
langchain.tools.playwright.navigate
langchain.tools.playwright.navigate_back
langchain.tools.playwright.utils
langchain.tools.plugin
langchain.tools.powerbi.tool
langchain.tools.pubmed.tool
langchain.tools.python.tool
langchain.tools.render
langchain.tools.requests.tool
langchain.tools.scenexplain.tool
langchain.tools.searchapi.tool
langchain.tools.searx_search.tool
langchain.tools.shell.tool
langchain.tools.sleep.tool
langchain.tools.spark_sql.tool
langchain.tools.sql_database.tool
langchain.tools.steamship_image_generation.tool
langchain.tools.steamship_image_generation.utils
langchain.tools.vectorstore.tool
langchain.tools.wikipedia.tool
langchain.tools.wolfram_alpha.tool
langchain.tools.yahoo_finance_news
langchain.tools.youtube.search
langchain.tools.zapier.tool
langchain.utilities.alpha_vantage
langchain.utilities.apify
langchain.utilities.arxiv


langchain.utilities.alpha_vantage
langchain.utilities.apify
langchain.utilities.arxiv
langchain.utilities.awslambda
langchain.utilities.bash
langchain.utilities.bibtex
langchain.utilities.bing_search
langchain.utilities.brave_search
langchain.utilities.dalle_image_generator
langchain.utilities.dataforseo_api_search
langchain.utilities.duckduckgo_search
langchain.utilities.github
langchain.utilities.gitlab
langchain.utilities.golden_query
langchain.utilities.google_places_api
langchain.utilities.google_search
langchain.utilities.google_serper
langchain.utilities.graphql
langchain.utilities.jira
langchain.utilities.max_compute
langchain.utilities.metaphor_search
langchain.utilities.opaqueprompts
langchain.utilities.openapi
langchain.utilities.openweathermap
langchain.utilities.portkey
langchain.utilities.powerbi
langchain.utilities.pubmed
langchain.utilities.python
langchain.utilities.redis
langchain.utilities.requests
langchain.utilities.scenexplain
langchain.utilities.searchapi
langchain.utilities.searx_search
langchain.utilities.serpapi
langchain.utilities.spark_sql
langchain.utilities.sql_database
langchain.utilities.tensorflow_datasets
langchain.utilities.twilio
langchain.utilities.vertexai
langchain.utilities.wikipedia
langchain.utilities.wolfram_alpha
langchain.utilities.zapier
langchain.utils.aiter
langchain.utils.env
langchain.utils.formatting
langchain.utils.html
langchain.utils.input
langchain.utils.iter
langchain.utils.json_schema
langchain.utils.loading
langchain.utils.math
langchain.utils.openai_functions
langchain.utils.pydantic
langchain.utils.strings
langchain.utils.utils
langchain.vectorstores.alibabacloud_opensearch
langchain.vectorstores.analyticdb
langchain.vectorstores.annoy
langchain.vectorstores.atlas
langchain.vectorstores.awadb
langchain.vectorstores.azuresearch


langchain.vectorstores.awadb
langchain.vectorstores.azuresearch
langchain.vectorstores.bageldb
langchain.vectorstores.cassandra
langchain.vectorstores.chroma
langchain.vectorstores.clarifai
langchain.vectorstores.clickhouse
langchain.vectorstores.dashvector
langchain.vectorstores.deeplake
langchain.vectorstores.dingo
langchain.vectorstores.docarray.base
langchain.vectorstores.docarray.hnsw
langchain.vectorstores.docarray.in_memory
langchain.vectorstores.elastic_vector_search
langchain.vectorstores.elasticsearch
langchain.vectorstores.epsilla
langchain.vectorstores.faiss
langchain.vectorstores.hologres
langchain.vectorstores.lancedb
langchain.vectorstores.llm_rails
langchain.vectorstores.marqo
langchain.vectorstores.matching_engine
langchain.vectorstores.meilisearch
langchain.vectorstores.milvus
langchain.vectorstores.mongodb_atlas
langchain.vectorstores.myscale
langchain.vectorstores.neo4j_vector
langchain.vectorstores.nucliadb
langchain.vectorstores.opensearch_vector_search
langchain.vectorstores.pgembedding
langchain.vectorstores.pgvector
langchain.vectorstores.pinecone
langchain.vectorstores.qdrant
langchain.vectorstores.redis.base
langchain.vectorstores.redis.filters
langchain.vectorstores.redis.schema
langchain.vectorstores.rocksetdb
langchain.vectorstores.scann
langchain.vectorstores.singlestoredb
langchain.vectorstores.sklearn
langchain.vectorstores.sqlitevss
langchain.vectorstores.starrocks
langchain.vectorstores.supabase
langchain.vectorstores.tair
langchain.vectorstores.tencentvectordb
langchain.vectorstores.tigris
langchain.vectorstores.timescalevector
langchain.vectorstores.typesense
langchain.vectorstores.usearch
langchain.vectorstores.utils
langchain.vectorstores.vald


langchain.vectorstores.usearch
langchain.vectorstores.utils
langchain.vectorstores.vald
langchain.vectorstores.vearch
langchain.vectorstores.vectara
langchain.vectorstores.weaviate
langchain.vectorstores.xata
langchain.vectorstores.zep
langchain.vectorstores.zilliz
langchain_experimental.autonomous_agents.autogpt.agent
langchain_experimental.autonomous_agents.autogpt.memory
langchain_experimental.autonomous_agents.autogpt.output_parser
langchain_experimental.autonomous_agents.autogpt.prompt
langchain_experimental.autonomous_agents.autogpt.prompt_generator
langchain_experimental.autonomous_agents.baby_agi.baby_agi
langchain_experimental.autonomous_agents.baby_agi.task_creation
langchain_experimental.autonomous_agents.baby_agi.task_execution
langchain_experimental.autonomous_agents.baby_agi.task_prioritization
langchain_experimental.autonomous_agents.hugginggpt.hugginggpt
langchain_experimental.autonomous_agents.hugginggpt.repsonse_generator
langchain_experimental.autonomous_agents.hugginggpt.task_executor
langchain_experimental.autonomous_agents.hugginggpt.task_planner
langchain_experimental.comprehend_moderation.amazon_comprehend_moderation
langchain_experimental.comprehend_moderation.base_moderation
langchain_experimental.comprehend_moderation.base_moderation_callbacks
langchain_experimental.comprehend_moderation.base_moderation_config
langchain_experimental.comprehend_moderation.base_moderation_exceptions
langchain_experimental.comprehend_moderation.intent
langchain_experimental.comprehend_moderation.pii
langchain_experimental.comprehend_moderation.toxicity
langchain_experimental.cpal.constants
langchain_experimental.data_anonymizer.base


langchain_experimental.cpal.constants
langchain_experimental.data_anonymizer.base
langchain_experimental.data_anonymizer.deanonymizer_mapping
langchain_experimental.data_anonymizer.faker_presidio_mapping
langchain_experimental.fallacy_removal.base
langchain_experimental.fallacy_removal.models
langchain_experimental.generative_agents.generative_agent
langchain_experimental.generative_agents.memory
langchain_experimental.graph_transformers.diffbot
langchain_experimental.llms.anthropic_functions
langchain_experimental.llms.jsonformer_decoder
langchain_experimental.llms.llamaapi
langchain_experimental.llms.rellm_decoder
langchain_experimental.pal_chain.base
langchain_experimental.plan_and_execute.agent_executor
langchain_experimental.plan_and_execute.executors.agent_executor
langchain_experimental.plan_and_execute.executors.base
langchain_experimental.plan_and_execute.planners.base
langchain_experimental.plan_and_execute.planners.chat_planner
langchain_experimental.plan_and_execute.schema
langchain_experimental.prompt_injection_identifier.hugging_face_identifier
langchain_experimental.retrievers.vector_sql_database
langchain_experimental.smart_llm.base
langchain_experimental.sql.base
langchain_experimental.sql.vector_sql
langchain_experimental.tabular_synthetic_data.base
langchain_experimental.tabular_synthetic_data.openai
langchain_experimental.tot.base
langchain_experimental.tot.checker
langchain_experimental.tot.controller
langchain_experimental.tot.memory
langchain_experimental.tot.prompts
langchain_experimental.tot.thought
langchain_experimental.tot.thought_generation
pydantic.main


Source code for langchain_experimental.tot.checker
from abc import ABC, abstractmethod
from typing import Any, Dict, List, Optional, Tuple
from langchain.callbacks.manager import CallbackManagerForChainRun
from langchain.chains.base import Chain
from langchain_experimental.tot.thought import ThoughtValidity
[docs]class ToTChecker(Chain, ABC):
    """
    Tree of Thought (ToT) checker.
    This is an abstract ToT checker that must be implemented by the user. You
    can implement a simple rule-based checker or a more sophisticated
    neural network based classifier.
    """
    output_key: str = "validity"  #: :meta private:
    @property
    def input_keys(self) -> List[str]:
        """The checker input keys.
        :meta private:
        """
        return ["problem_description", "thoughts"]
    @property
    def output_keys(self) -> List[str]:
        """The checker output keys.
        :meta private:
        """
        return [self.output_key]
[docs]    @abstractmethod
    def evaluate(
        self,
        problem_description: str,
        thoughts: Tuple[str, ...] = (),
    ) -> ThoughtValidity:
        """
        Evaluate the response to the problem description and return the solution type.
        """
    def _call(
        self,
        inputs: Dict[str, Any],
        run_manager: Optional[CallbackManagerForChainRun] = None,
    ) -> Dict[str, ThoughtValidity]:
        return {self.output_key: self.evaluate(**inputs)}


Source code for langchain_experimental.tot.memory
from __future__ import annotations
from typing import List, Optional
from langchain_experimental.tot.thought import Thought
[docs]class ToTDFSMemory:
    """
    Memory for the Tree of Thought (ToT) chain. Implemented as a stack of
    thoughts. This allows for a depth first search (DFS) of the ToT.
    """
[docs]    def __init__(self, stack: Optional[List[Thought]] = None):
        self.stack: list[Thought] = stack or []
[docs]    def top(self) -> Optional[Thought]:
        "Get the top of the stack without popping it."
        return self.stack[-1] if len(self.stack) > 0 else None
[docs]    def pop(self, n: int = 1) -> Optional[Thought]:
        "Pop the top n elements of the stack and return the last one."
        if len(self.stack) < n:
            return None
        for _ in range(n):
            node = self.stack.pop()
        return node
[docs]    def top_parent(self) -> Optional[Thought]:
        "Get the parent of the top of the stack without popping it."
        return self.stack[-2] if len(self.stack) > 1 else None
[docs]    def store(self, node: Thought) -> None:
        "Add a node on the top of the stack."
        if len(self.stack) > 0:
            self.stack[-1].children.add(node)
        self.stack.append(node)
    @property
    def level(self) -> int:
        "Return the current level of the stack."
        return len(self.stack)
[docs]    def current_path(self) -> List[Thought]:


[docs]    def current_path(self) -> List[Thought]:
        "Return the thoughts path."
        return self.stack[:]


Source code for langchain_experimental.tot.base
"""
This a Tree of Thought (ToT) chain based on the paper "Large Language Model
Guided Tree-of-Thought"
https://arxiv.org/pdf/2305.08291.pdf
The Tree of Thought (ToT) chain uses a tree structure to explore the space of
possible solutions to a problem.
"""
from __future__ import annotations
from textwrap import indent
from typing import Any, Dict, List, Optional, Type
from langchain.base_language import BaseLanguageModel
from langchain.callbacks.manager import (
    AsyncCallbackManagerForChainRun,
    CallbackManagerForChainRun,
)
from langchain.chains.base import Chain
from langchain_experimental.pydantic_v1 import Extra
from langchain_experimental.tot.checker import ToTChecker
from langchain_experimental.tot.controller import ToTController
from langchain_experimental.tot.memory import ToTDFSMemory
from langchain_experimental.tot.thought import Thought, ThoughtValidity
from langchain_experimental.tot.thought_generation import (
    BaseThoughtGenerationStrategy,
    ProposePromptStrategy,
)
[docs]class ToTChain(Chain):
    """
    A Chain implementing the Tree of Thought (ToT).
    """
    llm: BaseLanguageModel
    """
    Language model to use. It must be set to produce different variations for
    the same prompt.
    """
    checker: ToTChecker
    """ToT Checker to use."""
    output_key: str = "response"  #: :meta private:
    k: int = 10
    """The maximum number of conversation rounds"""
    c: int = 3
    """The number of children to explore at each node"""


"""The number of children to explore at each node"""
    tot_memory: ToTDFSMemory = ToTDFSMemory()
    tot_controller: ToTController = ToTController()
    tot_strategy_class: Type[BaseThoughtGenerationStrategy] = ProposePromptStrategy
    verbose_llm: bool = False
    class Config:
        """Configuration for this pydantic object."""
        extra = Extra.forbid
        arbitrary_types_allowed = True
[docs]    @classmethod
    def from_llm(cls, llm: BaseLanguageModel, **kwargs: Any) -> ToTChain:
        """
        Create a ToTChain from a language model.
        :param llm: The language model to use.
        :param kwargs: Additional arguments to pass to the ToTChain constructor.
        """
        return cls(llm=llm, **kwargs)
    def __init__(self, **kwargs: Any):
        super().__init__(**kwargs)
        self.tot_controller.c = self.c
    @property
    def input_keys(self) -> List[str]:
        """Will be whatever keys the prompt expects.
        :meta private:
        """
        return ["problem_description"]
    @property
    def output_keys(self) -> List[str]:
        """Will always return text key.
        :meta private:
        """
        return [self.output_key]
[docs]    def log_thought(
        self,
        thought: Thought,
        level: int,
        run_manager: Optional[CallbackManagerForChainRun] = None,
    ) -> None:
        if run_manager:
            colors = {
                ThoughtValidity.VALID_FINAL: "green",
                ThoughtValidity.VALID_INTERMEDIATE: "yellow",
                ThoughtValidity.INVALID: "red",


ThoughtValidity.INVALID: "red",
            }
            text = indent(f"Thought: {thought.text}\n", prefix="    " * level)
            run_manager.on_text(
                text=text, color=colors[thought.validity], verbose=self.verbose
            )
    def _call(
        self,
        inputs: Dict[str, Any],
        run_manager: Optional[CallbackManagerForChainRun] = None,
    ) -> Dict[str, str]:
        _run_manager = run_manager or CallbackManagerForChainRun.get_noop_manager()
        if run_manager:
            run_manager.on_text(text="Starting the ToT solve procedure.\n")
        problem_description = inputs["problem_description"]
        checker_inputs = {"problem_description": problem_description}
        thoughts_path: tuple[str, ...] = ()
        thought_generator = self.tot_strategy_class(
            llm=self.llm, c=self.c, verbose=self.verbose_llm
        )
        level = 0
        for _ in range(self.k):
            level = self.tot_memory.level
            thought_text = thought_generator.next_thought(
                problem_description, thoughts_path, callbacks=_run_manager.get_child()
            )
            checker_inputs["thoughts"] = thoughts_path + (thought_text,)
            thought_validity = self.checker(
                checker_inputs, callbacks=_run_manager.get_child()
            )["validity"]
            thought = Thought(text=thought_text, validity=thought_validity)
            if thought.validity == ThoughtValidity.VALID_FINAL:
                self.log_thought(thought, level, run_manager)
                return {self.output_key: thought.text}
            self.tot_memory.store(thought)
            self.log_thought(thought, level, run_manager)


self.log_thought(thought, level, run_manager)
            thoughts_path = self.tot_controller(self.tot_memory)
        return {self.output_key: "No solution found"}
    async def _acall(
        self,
        inputs: Dict[str, Any],
        run_manager: Optional[AsyncCallbackManagerForChainRun] = None,
    ) -> Dict[str, str]:
        raise NotImplementedError("Async not implemented yet")
    @property
    def _chain_type(self) -> str:
        return "tot"


Source code for langchain_experimental.tot.thought
from __future__ import annotations
from enum import Enum
from typing import Set
from langchain_experimental.pydantic_v1 import BaseModel, Field
[docs]class ThoughtValidity(Enum):
    VALID_INTERMEDIATE = 0
    VALID_FINAL = 1
    INVALID = 2
[docs]class Thought(BaseModel):
    text: str
    validity: ThoughtValidity
    children: Set[Thought] = Field(default_factory=set)
    def __hash__(self) -> int:
        return id(self)


Source code for langchain_experimental.tot.controller
from typing import Tuple
from langchain_experimental.tot.memory import ToTDFSMemory
from langchain_experimental.tot.thought import ThoughtValidity
[docs]class ToTController:
    """
    Tree of Thought (ToT) controller.
    This is a version of a ToT controller, dubbed in the paper as a "Simple
    Controller".
    It has one parameter `c` which is the number of children to explore for each
    thought.
    """
[docs]    def __init__(self, c: int = 3):
        """
        Initialize the controller.
        Args:
            c: The number of children to explore at each node.
        """
        self.c = c
    def __call__(self, memory: ToTDFSMemory) -> Tuple[str, ...]:
        next_thought = memory.top()
        parent_thought = memory.top_parent()
        validity = (
            ThoughtValidity.VALID_INTERMEDIATE
            if next_thought is None
            else next_thought.validity
        )
        # 1 if the current partial solution is invalid, backtrack to the parent
        # thought.
        if validity == ThoughtValidity.INVALID:
            memory.pop()
            next_thought = memory.top()
            if next_thought and len(next_thought.children) >= self.c:
                memory.pop()
        # 2 if the current partial solution is valid but C children were
        # explored and yet failed to find a final solution, backtrack to the
        # parent thought.
        elif (
            validity == ThoughtValidity.VALID_INTERMEDIATE
            and parent_thought
            and len(parent_thought.children) >= self.c
        ):
            memory.pop(2)


):
            memory.pop(2)
        return tuple(thought.text for thought in memory.current_path())


Source code for langchain_experimental.tot.prompts
import json
from textwrap import dedent
from typing import List
from langchain.prompts import PromptTemplate
from langchain.schema import BaseOutputParser
from langchain_experimental.tot.thought import ThoughtValidity
COT_PROMPT = PromptTemplate(
    template_format="jinja2",
    input_variables=["problem_description", "thoughts"],
    template=dedent(
        """
        You are an intelligent agent that is generating one thought at a time in
        a tree of thoughts setting.
        PROBLEM

        {{problem_description}}

        {% if thoughts %}
        THOUGHTS

        {% for thought in thoughts %}
        {{ thought }}
        {% endfor %}
        {% endif %}

        Let's think step by step.
        """
    ).strip(),
)
[docs]class JSONListOutputParser(BaseOutputParser):
    """Class to parse the output of a PROPOSE_PROMPT response."""
    @property
    def _type(self) -> str:
        return "json_list"
[docs]    def parse(self, text: str) -> List[str]:
        """Parse the output of an LLM call."""
        json_string = text.split("```json")[1].strip().strip("```").strip()
        try:
            return json.loads(json_string)
        except json.JSONDecodeError:
            return []
PROPOSE_PROMPT = PromptTemplate(
    template_format="jinja2",
    input_variables=["problem_description", "thoughts", "n"],
    output_parser=JSONListOutputParser(),
    template=dedent(
        """
        You are an intelligent agent that is generating thoughts in a tree of
        thoughts setting.


You are an intelligent agent that is generating thoughts in a tree of
        thoughts setting.
        The output should be a markdown code snippet formatted as a JSON list of
        strings, including the leading and trailing "```json" and "```":
        ```json
        [
            "<thought-1>",
            "<thought-2>",
            "<thought-3>"
        ]
        ```
        PROBLEM
        {{ problem_description }}
        {% if thoughts %}
        VALID THOUGHTS
        {% for thought in thoughts %}
        {{ thought }}
        {% endfor %}
        Possible next {{ n }} valid thoughts based on the last valid thought:
        {% else %}
        Possible next {{ n }} valid thoughts based on the PROBLEM:
        {%- endif -%}
        """
    ).strip(),
)
[docs]class CheckerOutputParser(BaseOutputParser):
[docs]    def parse(self, text: str) -> ThoughtValidity:
        """Parse the output of the language model."""
        text = text.upper()
        if "INVALID" in text:
            return ThoughtValidity.INVALID
        elif "INTERMEDIATE" in text:
            return ThoughtValidity.VALID_INTERMEDIATE
        elif "VALID" in text:
            return ThoughtValidity.VALID_FINAL
        else:
            return ThoughtValidity.INVALID
    @property
    def _type(self) -> str:
        return "tot_llm_checker_output"
CHECKER_PROMPT = PromptTemplate(
    input_variables=["problem_description", "thoughts"],
    template=dedent(
        """
        You are an intelligent agent, validating thoughts of another intelligent agent.
        PROBLEM

        {problem_description}
        THOUGHTS

        {thoughts}


{problem_description}
        THOUGHTS

        {thoughts}
        Evaluate the thoughts and respond with one word.
        - Respond VALID if the last thought is a valid final solution to the
        problem.
        - Respond INVALID if the last thought is invalid.
        - Respond INTERMEDIATE if the last thought is valid but not the final
        solution to the problem.
        This chain of thoughts is"""
    ).strip(),
    output_parser=CheckerOutputParser(),
)


Source code for langchain_experimental.tot.thought_generation
"""
We provide two strategies for generating thoughts in the Tree of Thoughts (ToT)
framework to avoid repetition:
These strategies ensure that the language model generates diverse and
non-repeating thoughts, which are crucial for problem-solving tasks that require
exploration.
"""
from abc import abstractmethod
from typing import Any, Dict, List, Tuple
from langchain.chains.llm import LLMChain
from langchain.prompts.base import BasePromptTemplate
from langchain_experimental.pydantic_v1 import Field
from langchain_experimental.tot.prompts import COT_PROMPT, PROPOSE_PROMPT
[docs]class BaseThoughtGenerationStrategy(LLMChain):
    """
    Base class for a thought generation strategy.
    """
    c: int = 3
    """The number of children thoughts to propose at each step."""
[docs]    @abstractmethod
    def next_thought(
        self,
        problem_description: str,
        thoughts_path: Tuple[str, ...] = (),
        **kwargs: Any
    ) -> str:
        """
        Generate the next thought given the problem description and the thoughts
        generated so far.
        """
[docs]class SampleCoTStrategy(BaseThoughtGenerationStrategy):
    """
    Sample thoughts from a Chain-of-Thought (CoT) prompt.
    This strategy works better when the thought space is rich, such as when each
    thought is a paragraph. Independent and identically distributed samples
    lead to diversity, which helps to avoid repetition.
    """
    prompt: BasePromptTemplate = COT_PROMPT
[docs]    def next_thought(
        self,
        problem_description: str,
        thoughts_path: Tuple[str, ...] = (),
        **kwargs: Any
    ) -> str:


**kwargs: Any
    ) -> str:
        response_text = self.predict_and_parse(
            problem_description=problem_description, thoughts=thoughts_path, **kwargs
        )
        return response_text if isinstance(response_text, str) else ""
[docs]class ProposePromptStrategy(BaseThoughtGenerationStrategy):
    """
    Propose thoughts sequentially using a "propose prompt".
    This strategy works better when the thought space is more constrained, such
    as when each thought is just a word or a line. Proposing different thoughts
    in the same prompt completion helps to avoid duplication.
    """
    prompt: BasePromptTemplate = PROPOSE_PROMPT
    tot_memory: Dict[Tuple[str, ...], List[str]] = Field(default_factory=dict)
[docs]    def next_thought(
        self,
        problem_description: str,
        thoughts_path: Tuple[str, ...] = (),
        **kwargs: Any
    ) -> str:
        if thoughts_path not in self.tot_memory or not self.tot_memory[thoughts_path]:
            new_thoughts = self.predict_and_parse(
                problem_description=problem_description,
                thoughts=thoughts_path,
                n=self.c,
                **kwargs
            )
            if not new_thoughts:
                return ""
            if isinstance(new_thoughts, list):
                self.tot_memory[thoughts_path] = new_thoughts[::-1]
            else:
                return ""
        return self.tot_memory[thoughts_path].pop()


Source code for langchain_experimental.fallacy_removal.models
"""Models for the Logical Fallacy Chain"""
from langchain_experimental.pydantic_v1 import BaseModel
[docs]class LogicalFallacy(BaseModel):
    """Class for a logical fallacy."""
    fallacy_critique_request: str
    fallacy_revision_request: str
    name: str = "Logical Fallacy"


Source code for langchain_experimental.fallacy_removal.base
"""Chain for applying removals of logical fallacies."""
from __future__ import annotations
from typing import Any, Dict, List, Optional
from langchain.callbacks.manager import CallbackManagerForChainRun
from langchain.chains.base import Chain
from langchain.chains.llm import LLMChain
from langchain.schema import BasePromptTemplate
from langchain.schema.language_model import BaseLanguageModel
from langchain_experimental.fallacy_removal.fallacies import FALLACIES
from langchain_experimental.fallacy_removal.models import LogicalFallacy
from langchain_experimental.fallacy_removal.prompts import (
    FALLACY_CRITIQUE_PROMPT,
    FALLACY_REVISION_PROMPT,
)
[docs]class FallacyChain(Chain):
    """Chain for applying logical fallacy evaluations, modeled after Constitutional AI \
    and in same format, but applying logical fallacies as generalized rules to remove \
    in output
    Example:
        .. code-block:: python
            from langchain.llms import OpenAI
            from langchain.chains import LLMChain
            from langchain_experimental.fallacy import FallacyChain
            from langchain_experimental.fallacy_removal.models import LogicalFallacy
            llm = OpenAI()
            qa_prompt = PromptTemplate(
                template="Q: {question} A:",
                input_variables=["question"],
            )
            qa_chain = LLMChain(llm=llm, prompt=qa_prompt)
            fallacy_chain = FallacyChain.from_llm(
                llm=llm,
                chain=qa_chain,
                logical_fallacies=[
                    LogicalFallacy(
                        fallacy_critique_request="Tell if this answer meets criteria.",


fallacy_critique_request="Tell if this answer meets criteria.",
                        fallacy_revision_request=\
                        "Give an answer that meets better criteria.",
                    )
                ],
            )
            fallacy_chain.run(question="How do I know if the earth is round?")
    """
    chain: LLMChain
    logical_fallacies: List[LogicalFallacy]
    fallacy_critique_chain: LLMChain
    fallacy_revision_chain: LLMChain
    return_intermediate_steps: bool = False
[docs]    @classmethod
    def get_fallacies(cls, names: Optional[List[str]] = None) -> List[LogicalFallacy]:
        if names is None:
            return list(FALLACIES.values())
        else:
            return [FALLACIES[name] for name in names]
[docs]    @classmethod
    def from_llm(
        cls,
        llm: BaseLanguageModel,
        chain: LLMChain,
        fallacy_critique_prompt: BasePromptTemplate = FALLACY_CRITIQUE_PROMPT,
        fallacy_revision_prompt: BasePromptTemplate = FALLACY_REVISION_PROMPT,
        **kwargs: Any,
    ) -> "FallacyChain":
        """Create a chain from an LLM."""
        fallacy_critique_chain = LLMChain(llm=llm, prompt=fallacy_critique_prompt)
        fallacy_revision_chain = LLMChain(llm=llm, prompt=fallacy_revision_prompt)
        return cls(
            chain=chain,
            fallacy_critique_chain=fallacy_critique_chain,
            fallacy_revision_chain=fallacy_revision_chain,
            **kwargs,
        )
    @property
    def input_keys(self) -> List[str]:
        """Input keys."""


def input_keys(self) -> List[str]:
        """Input keys."""
        return self.chain.input_keys
    @property
    def output_keys(self) -> List[str]:
        """Output keys."""
        if self.return_intermediate_steps:
            return ["output", "fallacy_critiques_and_revisions", "initial_output"]
        return ["output"]
    def _call(
        self,
        inputs: Dict[str, Any],
        run_manager: Optional[CallbackManagerForChainRun] = None,
    ) -> Dict[str, Any]:
        _run_manager = run_manager or CallbackManagerForChainRun.get_noop_manager()
        response = self.chain.run(
            **inputs,
            callbacks=_run_manager.get_child("original"),
        )
        initial_response = response
        input_prompt = self.chain.prompt.format(**inputs)
        _run_manager.on_text(
            text="Initial response: " + response + "\n\n",
            verbose=self.verbose,
            color="yellow",
        )
        fallacy_critiques_and_revisions = []
        for logical_fallacy in self.logical_fallacies:
            # Fallacy critique below
            fallacy_raw_critique = self.fallacy_critique_chain.run(
                input_prompt=input_prompt,
                output_from_model=response,
                fallacy_critique_request=logical_fallacy.fallacy_critique_request,
                callbacks=_run_manager.get_child("fallacy_critique"),
            )
            fallacy_critique = self._parse_critique(
                output_string=fallacy_raw_critique,
            ).strip()
            # if fallacy critique contains "No fallacy critique needed" then done
            if "no fallacy critique needed" in fallacy_critique.lower():


if "no fallacy critique needed" in fallacy_critique.lower():
                fallacy_critiques_and_revisions.append((fallacy_critique, ""))
                continue
            fallacy_revision = self.fallacy_revision_chain.run(
                input_prompt=input_prompt,
                output_from_model=response,
                fallacy_critique_request=logical_fallacy.fallacy_critique_request,
                fallacy_critique=fallacy_critique,
                revision_request=logical_fallacy.fallacy_revision_request,
                callbacks=_run_manager.get_child("fallacy_revision"),
            ).strip()
            response = fallacy_revision
            fallacy_critiques_and_revisions.append((fallacy_critique, fallacy_revision))
            _run_manager.on_text(
                text=f"Applying {logical_fallacy.name}..." + "\n\n",
                verbose=self.verbose,
                color="green",
            )
            _run_manager.on_text(
                text="Logical Fallacy: " + fallacy_critique + "\n\n",
                verbose=self.verbose,
                color="blue",
            )
            _run_manager.on_text(
                text="Updated response: " + fallacy_revision + "\n\n",
                verbose=self.verbose,
                color="yellow",
            )
        final_output: Dict[str, Any] = {"output": response}
        if self.return_intermediate_steps:
            final_output["initial_output"] = initial_response
            final_output[
                "fallacy_critiques_and_revisions"
            ] = fallacy_critiques_and_revisions
        return final_output
    @staticmethod
    def _parse_critique(output_string: str) -> str:
        if "Fallacy Revision request:" not in output_string:
            return output_string


if "Fallacy Revision request:" not in output_string:
            return output_string
        output_string = output_string.split("Fallacy Revision request:")[0]
        if "\n\n" in output_string:
            output_string = output_string.split("\n\n")[0]
        return output_string


Source code for langchain_experimental.generative_agents.generative_agent
import re
from datetime import datetime
from typing import Any, Dict, List, Optional, Tuple
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain.schema.language_model import BaseLanguageModel
from langchain_experimental.generative_agents.memory import GenerativeAgentMemory
from langchain_experimental.pydantic_v1 import BaseModel, Field
[docs]class GenerativeAgent(BaseModel):
    """An Agent as a character with memory and innate characteristics."""
    name: str
    """The character's name."""
    age: Optional[int] = None
    """The optional age of the character."""
    traits: str = "N/A"
    """Permanent traits to ascribe to the character."""
    status: str
    """The traits of the character you wish not to change."""
    memory: GenerativeAgentMemory
    """The memory object that combines relevance, recency, and 'importance'."""
    llm: BaseLanguageModel
    """The underlying language model."""
    verbose: bool = False
    summary: str = ""  #: :meta private:
    """Stateful self-summary generated via reflection on the character's memory."""
    summary_refresh_seconds: int = 3600  #: :meta private:
    """How frequently to re-generate the summary."""
    last_refreshed: datetime = Field(default_factory=datetime.now)  # : :meta private:
    """The last time the character's summary was regenerated."""
    daily_summaries: List[str] = Field(default_factory=list)  # : :meta private:
    """Summary of the events in the plan that the agent took."""
    class Config:
        """Configuration for this pydantic object."""
        arbitrary_types_allowed = True


"""Configuration for this pydantic object."""
        arbitrary_types_allowed = True
    # LLM-related methods
    @staticmethod
    def _parse_list(text: str) -> List[str]:
        """Parse a newline-separated string into a list of strings."""
        lines = re.split(r"\n", text.strip())
        return [re.sub(r"^\s*\d+\.\s*", "", line).strip() for line in lines]
[docs]    def chain(self, prompt: PromptTemplate) -> LLMChain:
        return LLMChain(
            llm=self.llm, prompt=prompt, verbose=self.verbose, memory=self.memory
        )
    def _get_entity_from_observation(self, observation: str) -> str:
        prompt = PromptTemplate.from_template(
            "What is the observed entity in the following observation? {observation}"
            + "\nEntity="
        )
        return self.chain(prompt).run(observation=observation).strip()
    def _get_entity_action(self, observation: str, entity_name: str) -> str:
        prompt = PromptTemplate.from_template(
            "What is the {entity} doing in the following observation? {observation}"
            + "\nThe {entity} is"
        )
        return (
            self.chain(prompt).run(entity=entity_name, observation=observation).strip()
        )
[docs]    def summarize_related_memories(self, observation: str) -> str:
        """Summarize memories that are most relevant to an observation."""
        prompt = PromptTemplate.from_template(
            """
{q1}?
Context from memory:
{relevant_memories}
Relevant context:
"""
        )
        entity_name = self._get_entity_from_observation(observation)


"""
        )
        entity_name = self._get_entity_from_observation(observation)
        entity_action = self._get_entity_action(observation, entity_name)
        q1 = f"What is the relationship between {self.name} and {entity_name}"
        q2 = f"{entity_name} is {entity_action}"
        return self.chain(prompt=prompt).run(q1=q1, queries=[q1, q2]).strip()
    def _generate_reaction(
        self, observation: str, suffix: str, now: Optional[datetime] = None
    ) -> str:
        """React to a given observation or dialogue act."""
        prompt = PromptTemplate.from_template(
            "{agent_summary_description}"
            + "\nIt is {current_time}."
            + "\n{agent_name}'s status: {agent_status}"
            + "\nSummary of relevant context from {agent_name}'s memory:"
            + "\n{relevant_memories}"
            + "\nMost recent observations: {most_recent_memories}"
            + "\nObservation: {observation}"
            + "\n\n"
            + suffix
        )
        agent_summary_description = self.get_summary(now=now)
        relevant_memories_str = self.summarize_related_memories(observation)
        current_time_str = (
            datetime.now().strftime("%B %d, %Y, %I:%M %p")
            if now is None
            else now.strftime("%B %d, %Y, %I:%M %p")
        )
        kwargs: Dict[str, Any] = dict(
            agent_summary_description=agent_summary_description,
            current_time=current_time_str,
            relevant_memories=relevant_memories_str,
            agent_name=self.name,
            observation=observation,
            agent_status=self.status,


agent_name=self.name,
            observation=observation,
            agent_status=self.status,
        )
        consumed_tokens = self.llm.get_num_tokens(
            prompt.format(most_recent_memories="", **kwargs)
        )
        kwargs[self.memory.most_recent_memories_token_key] = consumed_tokens
        return self.chain(prompt=prompt).run(**kwargs).strip()
    def _clean_response(self, text: str) -> str:
        return re.sub(f"^{self.name} ", "", text.strip()).strip()
[docs]    def generate_reaction(
        self, observation: str, now: Optional[datetime] = None
    ) -> Tuple[bool, str]:
        """React to a given observation."""
        call_to_action_template = (
            "Should {agent_name} react to the observation, and if so,"
            + " what would be an appropriate reaction? Respond in one line."
            + ' If the action is to engage in dialogue, write:\nSAY: "what to say"'
            + "\notherwise, write:\nREACT: {agent_name}'s reaction (if anything)."
            + "\nEither do nothing, react, or say something but not both.\n\n"
        )
        full_result = self._generate_reaction(
            observation, call_to_action_template, now=now
        )
        result = full_result.strip().split("\n")[0]
        # AAA
        self.memory.save_context(
            {},
            {
                self.memory.add_memory_key: f"{self.name} observed "
                f"{observation} and reacted by {result}",
                self.memory.now_key: now,
            },
        )
        if "REACT:" in result:
            reaction = self._clean_response(result.split("REACT:")[-1])


reaction = self._clean_response(result.split("REACT:")[-1])
            return False, f"{self.name} {reaction}"
        if "SAY:" in result:
            said_value = self._clean_response(result.split("SAY:")[-1])
            return True, f"{self.name} said {said_value}"
        else:
            return False, result
[docs]    def generate_dialogue_response(
        self, observation: str, now: Optional[datetime] = None
    ) -> Tuple[bool, str]:
        """React to a given observation."""
        call_to_action_template = (
            "What would {agent_name} say? To end the conversation, write:"
            ' GOODBYE: "what to say". Otherwise to continue the conversation,'
            ' write: SAY: "what to say next"\n\n'
        )
        full_result = self._generate_reaction(
            observation, call_to_action_template, now=now
        )
        result = full_result.strip().split("\n")[0]
        if "GOODBYE:" in result:
            farewell = self._clean_response(result.split("GOODBYE:")[-1])
            self.memory.save_context(
                {},
                {
                    self.memory.add_memory_key: f"{self.name} observed "
                    f"{observation} and said {farewell}",
                    self.memory.now_key: now,
                },
            )
            return False, f"{self.name} said {farewell}"
        if "SAY:" in result:
            response_text = self._clean_response(result.split("SAY:")[-1])
            self.memory.save_context(
                {},
                {
                    self.memory.add_memory_key: f"{self.name} observed "
                    f"{observation} and said {response_text}",


f"{observation} and said {response_text}",
                    self.memory.now_key: now,
                },
            )
            return True, f"{self.name} said {response_text}"
        else:
            return False, result
    ######################################################
    # Agent stateful' summary methods.                   #
    # Each dialog or response prompt includes a header   #
    # summarizing the agent's self-description. This is  #
    # updated periodically through probing its memories  #
    ######################################################
    def _compute_agent_summary(self) -> str:
        """"""
        prompt = PromptTemplate.from_template(
            "How would you summarize {name}'s core characteristics given the"
            + " following statements:\n"
            + "{relevant_memories}"
            + "Do not embellish."
            + "\n\nSummary: "
        )
        # The agent seeks to think about their core characteristics.
        return (
            self.chain(prompt)
            .run(name=self.name, queries=[f"{self.name}'s core characteristics"])
            .strip()
        )
[docs]    def get_summary(
        self, force_refresh: bool = False, now: Optional[datetime] = None
    ) -> str:
        """Return a descriptive summary of the agent."""
        current_time = datetime.now() if now is None else now
        since_refresh = (current_time - self.last_refreshed).seconds
        if (
            not self.summary
            or since_refresh >= self.summary_refresh_seconds
            or force_refresh
        ):
            self.summary = self._compute_agent_summary()
            self.last_refreshed = current_time
        age = self.age if self.age is not None else "N/A"
        return (
            f"Name: {self.name} (age: {age})"


f"Name: {self.name} (age: {age})"
            + f"\nInnate traits: {self.traits}"
            + f"\n{self.summary}"
        )
[docs]    def get_full_header(
        self, force_refresh: bool = False, now: Optional[datetime] = None
    ) -> str:
        """Return a full header of the agent's status, summary, and current time."""
        now = datetime.now() if now is None else now
        summary = self.get_summary(force_refresh=force_refresh, now=now)
        current_time_str = now.strftime("%B %d, %Y, %I:%M %p")
        return (
            f"{summary}\nIt is {current_time_str}.\n{self.name}'s status: {self.status}"
        )


Source code for langchain_experimental.generative_agents.memory
import logging
import re
from datetime import datetime
from typing import Any, Dict, List, Optional
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain.retrievers import TimeWeightedVectorStoreRetriever
from langchain.schema import BaseMemory, Document
from langchain.schema.language_model import BaseLanguageModel
from langchain.utils import mock_now
logger = logging.getLogger(__name__)
[docs]class GenerativeAgentMemory(BaseMemory):
    """Memory for the generative agent."""
    llm: BaseLanguageModel
    """The core language model."""
    memory_retriever: TimeWeightedVectorStoreRetriever
    """The retriever to fetch related memories."""
    verbose: bool = False
    reflection_threshold: Optional[float] = None
    """When aggregate_importance exceeds reflection_threshold, stop to reflect."""
    current_plan: List[str] = []
    """The current plan of the agent."""
    # A weight of 0.15 makes this less important than it
    # would be otherwise, relative to salience and time
    importance_weight: float = 0.15
    """How much weight to assign the memory importance."""
    aggregate_importance: float = 0.0  # : :meta private:
    """Track the sum of the 'importance' of recent memories.
    Triggers reflection when it reaches reflection_threshold."""
    max_tokens_limit: int = 1200  # : :meta private:
    # input keys
    queries_key: str = "queries"
    most_recent_memories_token_key: str = "recent_memories_token"
    add_memory_key: str = "add_memory"
    # output keys
    relevant_memories_key: str = "relevant_memories"


# output keys
    relevant_memories_key: str = "relevant_memories"
    relevant_memories_simple_key: str = "relevant_memories_simple"
    most_recent_memories_key: str = "most_recent_memories"
    now_key: str = "now"
    reflecting: bool = False
[docs]    def chain(self, prompt: PromptTemplate) -> LLMChain:
        return LLMChain(llm=self.llm, prompt=prompt, verbose=self.verbose)
    @staticmethod
    def _parse_list(text: str) -> List[str]:
        """Parse a newline-separated string into a list of strings."""
        lines = re.split(r"\n", text.strip())
        lines = [line for line in lines if line.strip()]  # remove empty lines
        return [re.sub(r"^\s*\d+\.\s*", "", line).strip() for line in lines]
    def _get_topics_of_reflection(self, last_k: int = 50) -> List[str]:
        """Return the 3 most salient high-level questions about recent observations."""
        prompt = PromptTemplate.from_template(
            "{observations}\n\n"
            "Given only the information above, what are the 3 most salient "
            "high-level questions we can answer about the subjects in the statements?\n"
            "Provide each question on a new line."
        )
        observations = self.memory_retriever.memory_stream[-last_k:]
        observation_str = "\n".join(
            [self._format_memory_detail(o) for o in observations]
        )
        result = self.chain(prompt).run(observations=observation_str)
        return self._parse_list(result)
    def _get_insights_on_topic(
        self, topic: str, now: Optional[datetime] = None


self, topic: str, now: Optional[datetime] = None
    ) -> List[str]:
        """Generate 'insights' on a topic of reflection, based on pertinent memories."""
        prompt = PromptTemplate.from_template(
            "Statements relevant to: '{topic}'\n"
            "---\n"
            "{related_statements}\n"
            "---\n"
            "What 5 high-level novel insights can you infer from the above statements "
            "that are relevant for answering the following question?\n"
            "Do not include any insights that are not relevant to the question.\n"
            "Do not repeat any insights that have already been made.\n\n"
            "Question: {topic}\n\n"
            "(example format: insight (because of 1, 5, 3))\n"
        )
        related_memories = self.fetch_memories(topic, now=now)
        related_statements = "\n".join(
            [
                self._format_memory_detail(memory, prefix=f"{i+1}. ")
                for i, memory in enumerate(related_memories)
            ]
        )
        result = self.chain(prompt).run(
            topic=topic, related_statements=related_statements
        )
        # TODO: Parse the connections between memories and insights
        return self._parse_list(result)
[docs]    def pause_to_reflect(self, now: Optional[datetime] = None) -> List[str]:
        """Reflect on recent observations and generate 'insights'."""
        if self.verbose:
            logger.info("Character is reflecting")
        new_insights = []
        topics = self._get_topics_of_reflection()
        for topic in topics:
            insights = self._get_insights_on_topic(topic, now=now)


insights = self._get_insights_on_topic(topic, now=now)
            for insight in insights:
                self.add_memory(insight, now=now)
            new_insights.extend(insights)
        return new_insights
    def _score_memory_importance(self, memory_content: str) -> float:
        """Score the absolute importance of the given memory."""
        prompt = PromptTemplate.from_template(
            "On the scale of 1 to 10, where 1 is purely mundane"
            + " (e.g., brushing teeth, making bed) and 10 is"
            + " extremely poignant (e.g., a break up, college"
            + " acceptance), rate the likely poignancy of the"
            + " following piece of memory. Respond with a single integer."
            + "\nMemory: {memory_content}"
            + "\nRating: "
        )
        score = self.chain(prompt).run(memory_content=memory_content).strip()
        if self.verbose:
            logger.info(f"Importance score: {score}")
        match = re.search(r"^\D*(\d+)", score)
        if match:
            return (float(match.group(1)) / 10) * self.importance_weight
        else:
            return 0.0
    def _score_memories_importance(self, memory_content: str) -> List[float]:
        """Score the absolute importance of the given memory."""
        prompt = PromptTemplate.from_template(
            "On the scale of 1 to 10, where 1 is purely mundane"
            + " (e.g., brushing teeth, making bed) and 10 is"
            + " extremely poignant (e.g., a break up, college"
            + " acceptance), rate the likely poignancy of the"


+ " acceptance), rate the likely poignancy of the"
            + " following piece of memory. Always answer with only a list of numbers."
            + " If just given one memory still respond in a list."
            + " Memories are separated by semi colans (;)"
            + "\Memories: {memory_content}"
            + "\nRating: "
        )
        scores = self.chain(prompt).run(memory_content=memory_content).strip()
        if self.verbose:
            logger.info(f"Importance scores: {scores}")
        # Split into list of strings and convert to floats
        scores_list = [float(x) for x in scores.split(";")]
        return scores_list
[docs]    def add_memories(
        self, memory_content: str, now: Optional[datetime] = None
    ) -> List[str]:
        """Add an observations or memories to the agent's memory."""
        importance_scores = self._score_memories_importance(memory_content)
        self.aggregate_importance += max(importance_scores)
        memory_list = memory_content.split(";")
        documents = []
        for i in range(len(memory_list)):
            documents.append(
                Document(
                    page_content=memory_list[i],
                    metadata={"importance": importance_scores[i]},
                )
            )
        result = self.memory_retriever.add_documents(documents, current_time=now)
        # After an agent has processed a certain amount of memories (as measured by
        # aggregate importance), it is time to reflect on recent events to add
        # more synthesized memories to the agent's memory stream.
        if (
            self.reflection_threshold is not None
            and self.aggregate_importance > self.reflection_threshold
            and not self.reflecting
        ):
            self.reflecting = True


and not self.reflecting
        ):
            self.reflecting = True
            self.pause_to_reflect(now=now)
            # Hack to clear the importance from reflection
            self.aggregate_importance = 0.0
            self.reflecting = False
        return result
[docs]    def add_memory(
        self, memory_content: str, now: Optional[datetime] = None
    ) -> List[str]:
        """Add an observation or memory to the agent's memory."""
        importance_score = self._score_memory_importance(memory_content)
        self.aggregate_importance += importance_score
        document = Document(
            page_content=memory_content, metadata={"importance": importance_score}
        )
        result = self.memory_retriever.add_documents([document], current_time=now)
        # After an agent has processed a certain amount of memories (as measured by
        # aggregate importance), it is time to reflect on recent events to add
        # more synthesized memories to the agent's memory stream.
        if (
            self.reflection_threshold is not None
            and self.aggregate_importance > self.reflection_threshold
            and not self.reflecting
        ):
            self.reflecting = True
            self.pause_to_reflect(now=now)
            # Hack to clear the importance from reflection
            self.aggregate_importance = 0.0
            self.reflecting = False
        return result
[docs]    def fetch_memories(
        self, observation: str, now: Optional[datetime] = None
    ) -> List[Document]:
        """Fetch related memories."""
        if now is not None:
            with mock_now(now):
                return self.memory_retriever.get_relevant_documents(observation)
        else:
            return self.memory_retriever.get_relevant_documents(observation)


else:
            return self.memory_retriever.get_relevant_documents(observation)
[docs]    def format_memories_detail(self, relevant_memories: List[Document]) -> str:
        content = []
        for mem in relevant_memories:
            content.append(self._format_memory_detail(mem, prefix="- "))
        return "\n".join([f"{mem}" for mem in content])
    def _format_memory_detail(self, memory: Document, prefix: str = "") -> str:
        created_time = memory.metadata["created_at"].strftime("%B %d, %Y, %I:%M %p")
        return f"{prefix}[{created_time}] {memory.page_content.strip()}"
[docs]    def format_memories_simple(self, relevant_memories: List[Document]) -> str:
        return "; ".join([f"{mem.page_content}" for mem in relevant_memories])
    def _get_memories_until_limit(self, consumed_tokens: int) -> str:
        """Reduce the number of tokens in the documents."""
        result = []
        for doc in self.memory_retriever.memory_stream[::-1]:
            if consumed_tokens >= self.max_tokens_limit:
                break
            consumed_tokens += self.llm.get_num_tokens(doc.page_content)
            if consumed_tokens < self.max_tokens_limit:
                result.append(doc)
        return self.format_memories_simple(result)
    @property
    def memory_variables(self) -> List[str]:
        """Input keys this memory class will load dynamically."""
        return []
[docs]    def load_memory_variables(self, inputs: Dict[str, Any]) -> Dict[str, str]:
        """Return key-value pairs given the text input to the chain."""
        queries = inputs.get(self.queries_key)
        now = inputs.get(self.now_key)


now = inputs.get(self.now_key)
        if queries is not None:
            relevant_memories = [
                mem for query in queries for mem in self.fetch_memories(query, now=now)
            ]
            return {
                self.relevant_memories_key: self.format_memories_detail(
                    relevant_memories
                ),
                self.relevant_memories_simple_key: self.format_memories_simple(
                    relevant_memories
                ),
            }
        most_recent_memories_token = inputs.get(self.most_recent_memories_token_key)
        if most_recent_memories_token is not None:
            return {
                self.most_recent_memories_key: self._get_memories_until_limit(
                    most_recent_memories_token
                )
            }
        return {}
[docs]    def save_context(self, inputs: Dict[str, Any], outputs: Dict[str, Any]) -> None:
        """Save the context of this model run to memory."""
        # TODO: fix the save memory key
        mem = outputs.get(self.add_memory_key)
        now = outputs.get(self.now_key)
        if mem:
            self.add_memory(mem, now=now)
[docs]    def clear(self) -> None:
        """Clear memory contents."""
        # TODO


Source code for langchain_experimental.llms.anthropic_functions
import json
from collections import defaultdict
from html.parser import HTMLParser
from typing import Any, DefaultDict, Dict, List, Optional
from langchain.callbacks.manager import (
    CallbackManagerForLLMRun,
    Callbacks,
)
from langchain.chat_models.anthropic import ChatAnthropic
from langchain.chat_models.base import BaseChatModel
from langchain.schema import (
    ChatGeneration,
    ChatResult,
    LLMResult,
)
from langchain.schema.messages import (
    AIMessage,
    BaseMessage,
    SystemMessage,
)
from langchain_experimental.pydantic_v1 import root_validator
prompt = """In addition to responding, you can use tools. \
You have access to the following tools.
{tools}
In order to use a tool, you can use <tool></tool> to specify the name, \
and the <tool_input></tool_input> tags to specify the parameters. \
Each parameter should be passed in as <$param_name>$value</$param_name>, \
Where $param_name is the name of the specific parameter, and $value \
is the value for that parameter.
You will then get back a response in the form <observation></observation>
For example, if you have a tool called 'search' that accepts a single \
parameter 'query' that could run a google search, in order to search \
for the weather in SF you would respond:
<tool>search</tool><tool_input><query>weather in SF</query></tool_input>
<observation>64 degrees</observation>"""
[docs]class TagParser(HTMLParser):
[docs]    def __init__(self) -> None:
        """A heavy-handed solution, but it's fast for prototyping.


"""A heavy-handed solution, but it's fast for prototyping.
        Might be re-implemented later to restrict scope to the limited grammar, and
        more efficiency.
        Uses an HTML parser to parse a limited grammar that allows
        for syntax of the form:
            INPUT -> JUNK? VALUE*
            JUNK -> JUNK_CHARACTER+
            JUNK_CHARACTER -> whitespace | ,
            VALUE -> <IDENTIFIER>DATA</IDENTIFIER> | OBJECT
            OBJECT -> <IDENTIFIER>VALUE+</IDENTIFIER>
            IDENTIFIER -> [a-Z][a-Z0-9_]*
            DATA -> .*
        Interprets the data to allow repetition of tags and recursion
        to support representation of complex types.
        ^ Just another approximately wrong grammar specification.
        """
        super().__init__()
        self.parse_data: DefaultDict[str, List[Any]] = defaultdict(list)
        self.stack: List[DefaultDict[str, List[str]]] = [self.parse_data]
        self.success = True
        self.depth = 0
        self.data: Optional[str] = None
[docs]    def handle_starttag(self, tag: str, attrs: Any) -> None:
        """Hook when a new tag is encountered."""
        self.depth += 1
        self.stack.append(defaultdict(list))
        self.data = None
[docs]    def handle_endtag(self, tag: str) -> None:
        """Hook when a tag is closed."""
        self.depth -= 1
        top_of_stack = dict(self.stack.pop(-1))  # Pop the dictionary we don't need it
        # If a lead node
        is_leaf = self.data is not None
        # Annoying to type here, code is tested, hopefully OK
        value = self.data if is_leaf else top_of_stack


value = self.data if is_leaf else top_of_stack
        # Difficult to type this correctly with mypy (maybe impossible?)
        # Can be nested indefinitely, so requires self referencing type
        self.stack[-1][tag].append(value)  # type: ignore
        # Reset the data so we if we encounter a sequence of end tags, we
        # don't confuse an outer end tag for belonging to a leaf node.
        self.data = None
[docs]    def handle_data(self, data: str) -> None:
        """Hook when handling data."""
        stripped_data = data.strip()
        # The only data that's allowed is whitespace or a comma surrounded by whitespace
        if self.depth == 0 and stripped_data not in (",", ""):
            # If this is triggered the parse should be considered invalid.
            self.success = False
        if stripped_data:  # ignore whitespace-only strings
            self.data = stripped_data
def _destrip(tool_input: Any) -> Any:
    if isinstance(tool_input, dict):
        return {k: _destrip(v) for k, v in tool_input.items()}
    elif isinstance(tool_input, list):
        if isinstance(tool_input[0], str):
            if len(tool_input) == 1:
                return tool_input[0]
            else:
                raise ValueError
        elif isinstance(tool_input[0], dict):
            return [_destrip(v) for v in tool_input]
        else:
            raise ValueError
    else:
        raise ValueError
[docs]class AnthropicFunctions(BaseChatModel):
    model: ChatAnthropic
    @root_validator(pre=True)
    def validate_environment(cls, values: Dict) -> Dict:
        return {"model": ChatAnthropic(**values)}
    def _generate(
        self,


def _generate(
        self,
        messages: List[BaseMessage],
        stop: Optional[List[str]] = None,
        run_manager: Optional[CallbackManagerForLLMRun] = None,
        **kwargs: Any,
    ) -> ChatResult:
        forced = False
        function_call = ""
        if "functions" in kwargs:
            content = prompt.format(tools=json.dumps(kwargs["functions"], indent=2))
            system = SystemMessage(content=content)
            messages = [system] + messages
            del kwargs["functions"]
            if stop is None:
                stop = ["</tool_input>"]
            else:
                stop.append("</tool_input>")
            if "function_call" in kwargs:
                forced = True
                function_call = kwargs["function_call"]["name"]
                AIMessage(content=f"<tool>{function_call}</tool>")
                del kwargs["function_call"]
        else:
            if "function_call" in kwargs:
                raise ValueError(
                    "if `function_call` provided, `functions` must also be"
                )
        response = self.model.predict_messages(
            messages, stop=stop, callbacks=run_manager, **kwargs
        )
        completion = response.content
        if forced:
            tag_parser = TagParser()
            tag_parser.feed(completion.strip() + "</tool_input>")
            v1 = tag_parser.parse_data["tool_input"][0]
            kwargs = {
                "function_call": {
                    "name": function_call,
                    "arguments": json.dumps(_destrip(v1)),
                }
            }
            message = AIMessage(content="", additional_kwargs=kwargs)
            return ChatResult(generations=[ChatGeneration(message=message)])
        elif "<tool>" in completion:
            tag_parser = TagParser()


elif "<tool>" in completion:
            tag_parser = TagParser()
            tag_parser.feed(completion.strip() + "</tool_input>")
            msg = completion.split("<tool>")[0]
            v1 = tag_parser.parse_data["tool_input"][0]
            kwargs = {
                "function_call": {
                    "name": tag_parser.parse_data["tool"][0],
                    "arguments": json.dumps(_destrip(v1)),
                }
            }
            message = AIMessage(content=msg, additional_kwargs=kwargs)
            return ChatResult(generations=[ChatGeneration(message=message)])
        else:
            return ChatResult(generations=[ChatGeneration(message=response)])
[docs]    async def agenerate(
        self,
        messages: List[List[BaseMessage]],
        stop: Optional[List[str]] = None,
        callbacks: Callbacks = None,
        *,
        tags: Optional[List[str]] = None,
        metadata: Optional[Dict[str, Any]] = None,
        **kwargs: Any,
    ) -> LLMResult:
        raise NotImplementedError
    @property
    def _llm_type(self) -> str:
        return "anthropic_functions"


Source code for langchain_experimental.llms.llamaapi
import json
import logging
from typing import (
    Any,
    Dict,
    List,
    Mapping,
    Optional,
    Tuple,
)
from langchain.callbacks.manager import CallbackManagerForLLMRun
from langchain.chat_models.base import BaseChatModel
from langchain.schema import (
    ChatGeneration,
    ChatResult,
)
from langchain.schema.messages import (
    AIMessage,
    BaseMessage,
    ChatMessage,
    FunctionMessage,
    HumanMessage,
    SystemMessage,
)
logger = logging.getLogger(__name__)
def _convert_dict_to_message(_dict: Mapping[str, Any]) -> BaseMessage:
    role = _dict["role"]
    if role == "user":
        return HumanMessage(content=_dict["content"])
    elif role == "assistant":
        # Fix for azure
        # Also OpenAI returns None for tool invocations
        content = _dict.get("content") or ""
        if _dict.get("function_call"):
            _dict["function_call"]["arguments"] = json.dumps(
                _dict["function_call"]["arguments"]
            )
            additional_kwargs = {"function_call": dict(_dict["function_call"])}
        else:
            additional_kwargs = {}
        return AIMessage(content=content, additional_kwargs=additional_kwargs)
    elif role == "system":
        return SystemMessage(content=_dict["content"])
    elif role == "function":
        return FunctionMessage(content=_dict["content"], name=_dict["name"])
    else:
        return ChatMessage(content=_dict["content"], role=role)
def _convert_message_to_dict(message: BaseMessage) -> dict:
    if isinstance(message, ChatMessage):


if isinstance(message, ChatMessage):
        message_dict = {"role": message.role, "content": message.content}
    elif isinstance(message, HumanMessage):
        message_dict = {"role": "user", "content": message.content}
    elif isinstance(message, AIMessage):
        message_dict = {"role": "assistant", "content": message.content}
        if "function_call" in message.additional_kwargs:
            message_dict["function_call"] = message.additional_kwargs["function_call"]
    elif isinstance(message, SystemMessage):
        message_dict = {"role": "system", "content": message.content}
    elif isinstance(message, FunctionMessage):
        message_dict = {
            "role": "function",
            "content": message.content,
            "name": message.name,
        }
    else:
        raise ValueError(f"Got unknown type {message}")
    if "name" in message.additional_kwargs:
        message_dict["name"] = message.additional_kwargs["name"]
    return message_dict
[docs]class ChatLlamaAPI(BaseChatModel):
    client: Any  #: :meta private:
    def _generate(
        self,
        messages: List[BaseMessage],
        stop: Optional[List[str]] = None,
        run_manager: Optional[CallbackManagerForLLMRun] = None,
        **kwargs: Any,
    ) -> ChatResult:
        message_dicts, params = self._create_message_dicts(messages, stop)
        _params = {"messages": message_dicts}
        final_params = {**params, **kwargs, **_params}
        response = self.client.run(final_params).json()
        return self._create_chat_result(response)
    def _create_message_dicts(
        self, messages: List[BaseMessage], stop: Optional[List[str]]


self, messages: List[BaseMessage], stop: Optional[List[str]]
    ) -> Tuple[List[Dict[str, Any]], Dict[str, Any]]:
        params = dict(self._client_params)
        if stop is not None:
            if "stop" in params:
                raise ValueError("`stop` found in both the input and default params.")
            params["stop"] = stop
        message_dicts = [_convert_message_to_dict(m) for m in messages]
        return message_dicts, params
    def _create_chat_result(self, response: Mapping[str, Any]) -> ChatResult:
        generations = []
        for res in response["choices"]:
            message = _convert_dict_to_message(res["message"])
            gen = ChatGeneration(
                message=message,
                generation_info=dict(finish_reason=res.get("finish_reason")),
            )
            generations.append(gen)
        return ChatResult(generations=generations)
    @property
    def _client_params(self) -> Mapping[str, Any]:
        """Get the parameters used for the client."""
        return {}
    @property
    def _llm_type(self) -> str:
        """Return type of chat model."""
        return "llama-api"


Source code for langchain_experimental.llms.rellm_decoder
"""Experimental implementation of RELLM wrapped LLM."""
from __future__ import annotations
from typing import TYPE_CHECKING, Any, List, Optional, cast
from langchain.callbacks.manager import CallbackManagerForLLMRun
from langchain.llms.huggingface_pipeline import HuggingFacePipeline
from langchain.llms.utils import enforce_stop_tokens
from langchain_experimental.pydantic_v1 import Field, root_validator
if TYPE_CHECKING:
    import rellm
    from regex import Pattern as RegexPattern
else:
    try:
        from regex import Pattern as RegexPattern
    except ImportError:
        pass
[docs]def import_rellm() -> rellm:
    """Lazily import rellm."""
    try:
        import rellm
    except ImportError:
        raise ImportError(
            "Could not import rellm python package. "
            "Please install it with `pip install rellm`."
        )
    return rellm
[docs]class RELLM(HuggingFacePipeline):
    """RELLM wrapped LLM using HuggingFace Pipeline API."""
    regex: RegexPattern = Field(..., description="The structured format to complete.")
    max_new_tokens: int = Field(
        default=200, description="Maximum number of new tokens to generate."
    )
    @root_validator
    def check_rellm_installation(cls, values: dict) -> dict:
        import_rellm()
        return values
    def _call(
        self,
        prompt: str,
        stop: Optional[List[str]] = None,
        run_manager: Optional[CallbackManagerForLLMRun] = None,
        **kwargs: Any,
    ) -> str:


**kwargs: Any,
    ) -> str:
        rellm = import_rellm()
        from transformers import Text2TextGenerationPipeline
        pipeline = cast(Text2TextGenerationPipeline, self.pipeline)
        text = rellm.complete_re(
            prompt,
            self.regex,
            tokenizer=pipeline.tokenizer,
            model=pipeline.model,
            max_new_tokens=self.max_new_tokens,
        )
        if stop is not None:
            # This is a bit hacky, but I can't figure out a better way to enforce
            # stop tokens when making calls to huggingface_hub.
            text = enforce_stop_tokens(text, stop)
        return text


Source code for langchain_experimental.llms.jsonformer_decoder
"""Experimental implementation of jsonformer wrapped LLM."""
from __future__ import annotations
import json
from typing import TYPE_CHECKING, Any, List, Optional, cast
from langchain.callbacks.manager import CallbackManagerForLLMRun
from langchain.llms.huggingface_pipeline import HuggingFacePipeline
from langchain_experimental.pydantic_v1 import Field, root_validator
if TYPE_CHECKING:
    import jsonformer
[docs]def import_jsonformer() -> jsonformer:
    """Lazily import jsonformer."""
    try:
        import jsonformer
    except ImportError:
        raise ImportError(
            "Could not import jsonformer python package. "
            "Please install it with `pip install jsonformer`."
        )
    return jsonformer
[docs]class JsonFormer(HuggingFacePipeline):
    """Jsonformer wrapped LLM using HuggingFace Pipeline API.
    This pipeline is experimental and not yet stable.
    """
    json_schema: dict = Field(..., description="The JSON Schema to complete.")
    max_new_tokens: int = Field(
        default=200, description="Maximum number of new tokens to generate."
    )
    debug: bool = Field(default=False, description="Debug mode.")
    @root_validator
    def check_jsonformer_installation(cls, values: dict) -> dict:
        import_jsonformer()
        return values
    def _call(
        self,
        prompt: str,
        stop: Optional[List[str]] = None,
        run_manager: Optional[CallbackManagerForLLMRun] = None,
        **kwargs: Any,
    ) -> str:
        jsonformer = import_jsonformer()
        from transformers import Text2TextGenerationPipeline


jsonformer = import_jsonformer()
        from transformers import Text2TextGenerationPipeline
        pipeline = cast(Text2TextGenerationPipeline, self.pipeline)
        model = jsonformer.Jsonformer(
            model=pipeline.model,
            tokenizer=pipeline.tokenizer,
            json_schema=self.json_schema,
            prompt=prompt,
            max_number_tokens=self.max_new_tokens,
            debug=self.debug,
        )
        text = model()
        return json.dumps(text)


Source code for langchain_experimental.sql.base
"""Chain for interacting with SQL Database."""
from __future__ import annotations
import warnings
from typing import Any, Dict, List, Optional
from langchain.callbacks.manager import CallbackManagerForChainRun
from langchain.chains.base import Chain
from langchain.chains.llm import LLMChain
from langchain.chains.sql_database.prompt import DECIDER_PROMPT, PROMPT, SQL_PROMPTS
from langchain.prompts.prompt import PromptTemplate
from langchain.schema import BasePromptTemplate
from langchain.schema.language_model import BaseLanguageModel
from langchain.tools.sql_database.prompt import QUERY_CHECKER
from langchain.utilities.sql_database import SQLDatabase
from langchain_experimental.pydantic_v1 import Extra, Field, root_validator
INTERMEDIATE_STEPS_KEY = "intermediate_steps"
[docs]class SQLDatabaseChain(Chain):
    """Chain for interacting with SQL Database.
    Example:
        .. code-block:: python
            from langchain_experimental.sql import SQLDatabaseChain
            from langchain.llms import OpenAI, SQLDatabase
            db = SQLDatabase(...)
            db_chain = SQLDatabaseChain.from_llm(OpenAI(), db)
    *Security note*: Make sure that the database connection uses credentials
        that are narrowly-scoped to only include the permissions this chain needs.
        Failure to do so may result in data corruption or loss, since this chain may
        attempt commands like `DROP TABLE` or `INSERT` if appropriately prompted.
        The best way to guard against such negative outcomes is to (as appropriate)
        limit the permissions granted to the credentials used with this chain.
        This issue shows an example negative outcome if these steps are not taken:
        https://github.com/langchain-ai/langchain/issues/5923
    """
    llm_chain: LLMChain