langchain
API Reference¶
langchain.agents
¶
Agent is a class that uses an LLM to choose a sequence of actions to take.
In Chains, a sequence of actions is hardcoded. In Agents, a language model is used as a reasoning engine to determine which actions to take and in which order.
Agents select and use Tools and Toolkits for actions.
Class hierarchy:
BaseSingleActionAgent --> LLMSingleActionAgent
OpenAIFunctionsAgent
XMLAgent
Agent --> <name>Agent # Examples: ZeroShotAgent, ChatAgent
BaseMultiActionAgent --> OpenAIMultiFunctionsAgent
Main helpers:
AgentType, AgentExecutor, AgentOutputParser, AgentExecutorIterator,
AgentAction, AgentFinish
Classes¶
Agent that calls the language model and deciding the action. |
|
Agent that is using tools. |
|
Base class for parsing agent output into agent action/finish. |
|
Base Multi Action Agent class. |
|
Base Single Action Agent class. |
|
Tool that just returns the query. |
|
Base class for single action agents. |
|
Base class for parsing agent output into agent actions/finish. |
|
Agent powered by runnables. |
|
Agent powered by runnables. |
|
Iterator for AgentExecutor. |
|
Information about a VectorStore. |
|
|
Toolkit for routing between Vector Stores. |
|
Toolkit for interacting with a Vector Store. |
|
An enum for agent types. |
Chat Agent. |
|
Output parser for the chat agent. |
|
An agent that holds a conversation in addition to using tools. |
|
Output parser for the conversational agent. |
|
An agent designed to hold a conversation in addition to using tools. |
|
Output parser for the conversational agent. |
|
|
Configuration for chain to use in MRKL system. |
[Deprecated] Chain that implements the MRKL system. |
|
Agent for the MRKL chain. |
|
MRKL Output parser for the chat agent. |
|
AgentAction with info needed to submit custom tool output to existing run. |
|
AgentFinish with run and thread metadata. |
|
Run an OpenAI Assistant. |
|
|
Memory used to save agent output AND intermediate steps. |
An Agent driven by OpenAIs function powered API. |
|
|
An Agent driven by OpenAIs function powered API. |
Parses tool invocations and final answers in JSON format. |
|
|
Parses a message into agent action/finish. |
Override init to support instantiation by position for backward compat. |
|
|
Parses a message into agent actions/finish. |
|
Parses ReAct-style LLM calls that have a single tool input in json format. |
|
Parses ReAct-style LLM calls that have a single tool input. |
Parses self-ask style LLM calls. |
|
Parses tool invocations and final answers in XML format. |
|
|
Class to assist with exploration of a document store. |
[Deprecated] Chain that implements the ReAct paper. |
|
Agent for the ReAct chain. |
|
Agent for the ReAct TextWorld chain. |
|
Output parser for the ReAct agent. |
|
Chat prompt template for the agent scratchpad. |
|
Agent for the self-ask-with-search paper. |
|
[Deprecated] Chain that does self-ask with search. |
|
Structured Chat Agent. |
|
|
Output parser for the structured chat agent. |
|
Output parser with retries for the structured chat agent. |
Tool that is run when invalid tool name is encountered by agent. |
|
Agent that uses XML tags. |
Functions¶
A convenience method for creating a conversational retrieval agent. |
|
|
Construct a VectorStore agent from an LLM and tools. |
|
Construct a VectorStore router agent from an LLM and tools. |
Construct the scratchpad that lets the agent continue its thought process. |
|
|
Construct the scratchpad that lets the agent continue its thought process. |
|
Convert (AgentAction, tool output) tuples into FunctionMessages. |
|
Convert (AgentAction, tool output) tuples into FunctionMessages. |
|
Convert (AgentAction, tool output) tuples into FunctionMessages. |
Format the intermediate steps as XML. |
|
|
Load an agent executor given tools and LLM. |
Get a list of all possible tool names. |
|
Loads a tool from the HuggingFace Hub. |
|
|
Load tools based on their name. |
|
Unified method for loading an agent from LangChainHub or local fs. |
Load agent from Config Dict. |
|
|
Parse an AI message potentially containing tool_calls. |
Validate tools for single input. |
langchain.callbacks
¶
Callback handlers allow listening to events in LangChain.
Class hierarchy:
BaseCallbackHandler --> <name>CallbackHandler # Example: AimCallbackHandler
Classes¶
|
Callback Handler that writes to a file. |
Callback handler that returns an async iterator. |
|
|
Callback handler that returns an async iterator. |
|
Callback handler for streaming in agents. |
Tracer that logs via the input Logger. |
langchain.chains
¶
Chains are easily reusable components linked together.
Chains encode a sequence of calls to components like models, document retrievers, other Chains, etc., and provide a simple interface to this sequence.
The Chain interface makes it easy to create apps that are:
Stateful: add Memory to any Chain to give it state,
Observable: pass Callbacks to a Chain to execute additional functionality, like logging, outside the main sequence of component calls,
Composable: combine Chains with other components, including other Chains.
Class hierarchy:
Chain --> <name>Chain # Examples: LLMChain, MapReduceChain, RouterChain
Classes¶
Chain that makes API calls and summarizes the responses to answer a question. |
|
Chain interacts with an OpenAPI endpoint using natural language. |
|
Get the request parser. |
|
Parse the request and error tags. |
|
Get the response parser. |
|
Parse the response and error tags. |
|
Abstract base class for creating structured sequences of calls to components. |
|
Chain that splits documents, then analyzes it in pieces. |
|
Base interface for chains combining documents. |
|
Combining documents by mapping a chain over them, then combining results. |
|
Combining documents by mapping a chain over them, then reranking results. |
|
|
Interface for the combine_docs method. |
Interface for the combine_docs method. |
|
Combine documents by recursively reducing them. |
|
Combine documents by doing a first pass and then refining on more documents. |
|
Chain that combines documents by stuffing into context. |
|
Chain for applying constitutional principles. |
|
Class for a constitutional principle. |
|
Chain to have a conversation and load context from memory. |
|
|
Chain for chatting with an index. |
Chain for chatting with a vector database. |
|
|
Chain for having a conversation based on retrieved documents. |
Create a new model by parsing and validating input data from keyword arguments. |
|
|
Chain for interacting with Elasticsearch Database. |
Chain that combines a retriever, a question generator, and a response generator. |
|
Chain that generates questions from uncertain spans. |
|
Output parser that checks if the output is finished. |
|
Chain for question-answering against a graph by generating AQL statements. |
|
Chain for question-answering against a graph. |
|
Chain for question-answering against a graph by generating Cypher statements. |
|
Used to correct relationship direction in generated Cypher statements. |
|
Create new instance of Schema(left_node, relation, right_node) |
|
Chain for question-answering against a graph by generating Cypher statements. |
|
Chain for question-answering against a graph by generating gremlin statements. |
|
Question-answering against a graph by generating Cypher statements for Kùzu. |
|
Chain for question-answering against a graph by generating nGQL statements. |
|
Chain for question-answering against a Neptune graph by generating openCypher statements. |
|
Question-answering against an RDF or OWL graph by generating SPARQL statements. |
|
Generate hypothetical document for query, and then embed that. |
|
Chain to run queries against LLMs. |
|
Chain for question-answering with self-verification. |
|
Chain that interprets a prompt and executes python code to do math. |
|
Chain that requests a URL and then uses an LLM to parse results. |
|
|
Chain for question-answering with self-verification. |
Map-reduce chain. |
|
Pass input through a moderation endpoint. |
|
Implement an LLM driven browser. |
|
A crawler for web pages. |
|
A typed dictionary containing information about elements in the viewport. |
|
|
Class representing a single statement. |
A question and its answer as a list of facts each one should have a source. |
|
Chain for making a simple request to an API endpoint. |
|
An answer to the question, with sources. |
|
Base class for prompt selectors. |
|
Prompt collection that goes through conditionals. |
|
Base class for question-answer generation chains. |
|
Question answering chain with sources over documents. |
|
Question answering with sources over documents. |
|
Interface for loading the combine documents chain. |
|
|
Question-answering with sources over an index. |
Question-answering with sources over a vector database. |
|
Output parser that parses a structured query. |
|
Enumerator of the comparison operators. |
|
A comparison to a value. |
|
Base class for all expressions. |
|
A filtering expression. |
|
A logical operation over other directives. |
|
Enumerator of the operations. |
|
A structured query. |
|
Defines interface for IR translation using visitor pattern. |
|
A date in ISO 8601 format (YYYY-MM-DD). |
|
Information about a data source attribute. |
|
Base class for question-answering chains. |
|
Chain for question-answering against an index. |
|
Chain for question-answering against a vector database. |
|
Use a single chain to route an input to one of multiple candidate chains. |
|
|
Create new instance of Route(destination, next_inputs) |
Chain that outputs the name of a destination chain and the inputs to it. |
|
Chain that uses embeddings to route between options. |
|
A router chain that uses an LLM chain to perform routing. |
|
Parser for output of router chain in the multi-prompt chain. |
|
A multi-route chain that uses an LLM router chain to choose amongst prompts. |
|
A multi-route chain that uses an LLM router chain to choose amongst retrieval qa chains. |
|
Chain where the outputs of one chain feed directly into next. |
|
Simple chain where the outputs of one step feed directly into next. |
|
Input for a SQL Chain. |
|
Input for a SQL Chain. |
|
Chain that transforms the chain output. |
Functions¶
Execute a collapse function on a set of documents and merge their metadatas. |
|
Execute a collapse function on a set of documents and merge their metadatas. |
|
Split Documents into subsets that each meet a cumulative length constraint. |
|
|
Convert a Python function to an Ernie function-calling API compatible dict. |
Convert a raw function/class to an Ernie function. |
|
[Legacy] Create an LLM chain that uses Ernie functions. |
|
Create a runnable sequence that uses Ernie functions. |
|
|
[Legacy] Create an LLMChain that uses an Ernie function to get a structured output. |
|
Create a runnable that uses an Ernie function to get a structured output. |
Get the appropriate function output parser given the user functions. |
|
Return another example given a list of examples for a prompt. |
|
Filter the schema based on included or excluded types |
|
Extract Cypher code from a text. |
|
Extract Cypher code from a text. |
|
Extract Cypher code from text using Regex. |
|
Trim the query to only include Cypher keywords. |
|
Decides whether to use the simple prompt |
|
|
Unified method for loading a chain from LangChainHub or local fs. |
Load chain from Config Dict. |
|
|
Convert a Python function to an OpenAI function-calling API compatible dict. |
|
Convert a raw function/class to an OpenAI function. |
[Legacy] Create an LLM chain that uses OpenAI functions. |
|
Create a runnable sequence that uses OpenAI functions. |
|
|
[Legacy] Create an LLMChain that uses an OpenAI function to get a structured output. |
|
Create a runnable that uses an OpenAI function to get a structured output. |
Get the appropriate function output parser given the user functions. |
|
|
Create a citation fuzzy match chain. |
|
Creates a chain that extracts information from a passage. |
|
Creates a chain that extracts information from a passage using pydantic schema. |
Create a chain for querying an API from a OpenAPI spec. |
|
|
Convert a valid OpenAPI spec to the JSON Schema format expected for OpenAI |
|
Create a question answering chain that returns an answer with sources. |
|
Create a question answering chain that returns an answer with sources |
Creates a chain that extracts information from a passage |
|
|
Creates a chain that extracts information from a passage |
Returns the kwargs for the LLMChain constructor. |
|
|
|
Check if the language model is a chat model. |
|
Check if the language model is a LLM. |
|
|
Load a question answering with sources chain. |
Construct examples from input-output pairs. |
|
Fix invalid filter directive. |
|
|
Create query construction prompt. |
|
Load a query constructor chain. |
|
Load a query constructor runnable chain. |
Returns a parser for the query language. |
|
Dummy decorator for when lark is not installed. |
|
Create a chain that generates SQL queries. |
langchain.embeddings
¶
Embedding models are wrappers around embedding models from different APIs and services.
Embedding models can be LLMs or not.
Class hierarchy:
Embeddings --> <name>Embeddings # Examples: OpenAIEmbeddings, HuggingFaceEmbeddings
Classes¶
Interface for caching results from embedding models. |
Functions¶
langchain.evaluation
¶
Evaluation chains for grading LLM and Chain outputs.
This module contains off-the-shelf evaluation chains for grading the output of LangChain primitives such as language models and chains.
Loading an evaluator
To load an evaluator, you can use the load_evaluators
or
load_evaluator
functions with the
names of the evaluators to load.
from langchain.evaluation import load_evaluator
evaluator = load_evaluator("qa")
evaluator.evaluate_strings(
prediction="We sold more than 40,000 units last week",
input="How many units did we sell last week?",
reference="We sold 32,378 units",
)
The evaluator must be one of EvaluatorType
.
Datasets
To load one of the LangChain HuggingFace datasets, you can use the load_dataset
function with the
name of the dataset to load.
from langchain.evaluation import load_dataset
ds = load_dataset("llm-math")
Some common use cases for evaluation include:
Grading the accuracy of a response against ground truth answers:
QAEvalChain
Comparing the output of two models:
PairwiseStringEvalChain
orLabeledPairwiseStringEvalChain
when there is additionally a reference label.Judging the efficacy of an agent’s tool usage:
TrajectoryEvalChain
Checking whether an output complies with a set of criteria:
CriteriaEvalChain
orLabeledCriteriaEvalChain
when there is additionally a reference label.Computing semantic difference between a prediction and reference:
EmbeddingDistanceEvalChain
or between two predictions:PairwiseEmbeddingDistanceEvalChain
Measuring the string distance between a prediction and reference
StringDistanceEvalChain
or between two predictionsPairwiseStringDistanceEvalChain
Low-level API
These evaluators implement one of the following interfaces:
StringEvaluator
: Evaluate a prediction string against a reference label and/or input context.PairwiseStringEvaluator
: Evaluate two prediction strings against each other. Useful for scoring preferences, measuring similarity between two chain or llm agents, or comparing outputs on similar inputs.AgentTrajectoryEvaluator
Evaluate the full sequence of actions taken by an agent.
These interfaces enable easier composability and usage within a higher level evaluation framework.
Classes¶
A named tuple containing the score and reasoning for a trajectory. |
|
A chain for evaluating ReAct style agents. |
|
|
Trajectory output parser. |
|
A chain for comparing two outputs, such as the outputs |
A chain for comparing two outputs, such as the outputs |
|
|
A parser for the output of the PairwiseStringEvalChain. |
A Criteria to evaluate. |
|
LLM Chain for evaluating runs against criteria. |
|
A parser for the output of the CriteriaEvalChain. |
|
Criteria evaluation chain that requires references. |
|
Embedding Distance Metric. |
|
|
Use embedding distances to score semantic difference between a prediction and reference. |
|
Use embedding distances to score semantic difference between two predictions. |
Compute an exact match between the prediction and the reference. |
|
Evaluates whether the prediction is equal to the reference after |
|
Evaluates whether the prediction is valid JSON. |
|
|
An evaluator that calculates the edit distance between JSON strings. |
An evaluator that validates a JSON prediction against a JSON schema reference. |
|
LLM Chain for evaluating QA w/o GT based on context |
|
LLM Chain for evaluating QA using chain of thought reasoning. |
|
LLM Chain for evaluating question answering. |
|
LLM Chain for generating examples for question answering. |
|
Compute a regex match between the prediction and the reference. |
|
Interface for evaluating agent trajectories. |
|
|
The types of the evaluators. |
A base class for evaluators that use an LLM. |
|
Compare the output of two models (or two outputs of the same model). |
|
Grade, tag, or otherwise evaluate predictions relative to their inputs and/or reference labels. |
|
A chain for scoring the output of a model on a scale of 1-10. |
|
A chain for scoring on a scale of 1-10 the output of a model. |
|
A parser for the output of the ScoreStringEvalChain. |
|
|
Compute string edit distances between two predictions. |
Distance metric to use. |
|
Compute string distances between the prediction and the reference. |
Functions¶
|
Resolve the criteria for the pairwise evaluator. |
Resolve the criteria to evaluate. |
|
Load a dataset from the LangChainDatasets on HuggingFace. |
|
|
Load the requested evaluation chain specified by a string. |
|
Load evaluators specified by a list of evaluator types. |
Resolve the criteria for the pairwise evaluator. |
langchain.hub
¶
Interface with the LangChain Hub.
Functions¶
|
Pulls an object from the hub and returns it as a LangChain object. |
|
Pushes an object to the hub and returns the URL it can be viewed at in a browser. |
langchain.indexes
¶
Code to support various indexing workflows.
Provides code to:
Create knowledge graphs from data.
Support indexing workflows from LangChain data loaders to vectorstores.
For indexing workflows, this code is used to avoid writing duplicated content into the vectostore and to avoid over-writing content if it’s unchanged.
Importantly, this keeps on working even if the content being written is derived via a set of transformations from some source content (e.g., indexing children documents that were derived from parent documents by chunking.)
Classes¶
|
An abstract base class representing the interface for a record manager. |
Functionality to create graph index. |
|
Wrapper around a vectorstore for easy access. |
|
Logic for creating indexes. |
Functions¶
langchain.memory
¶
Memory maintains Chain state, incorporating context from past runs.
Class hierarchy for Memory:
BaseMemory --> BaseChatMemory --> <name>Memory # Examples: ZepMemory, MotorheadMemory
Main helpers:
BaseChatMessageHistory
Chat Message History stores the chat message history in different stores.
Class hierarchy for ChatMessageHistory:
BaseChatMessageHistory --> <name>ChatMessageHistory # Example: ZepChatMessageHistory
Main helpers:
AIMessage, BaseMessage, HumanMessage
Classes¶
Buffer for storing conversation memory. |
|
Buffer for storing conversation memory. |
|
Buffer for storing conversation memory inside a limited size window. |
|
Abstract base class for chat memory. |
|
Combining multiple memories' data together. |
|
Abstract base class for Entity store. |
|
Entity extractor & summarizer memory. |
|
In-memory Entity store. |
|
Redis-backed Entity store. |
|
SQLite-backed Entity store |
|
Upstash Redis backed Entity store. |
|
Knowledge graph conversation memory. |
|
Chat message memory backed by Motorhead service. |
|
A memory wrapper that is read-only and cannot be changed. |
|
Simple memory for storing context or other information that shouldn't ever change between prompts. |
|
Conversation summarizer to chat memory. |
|
Mixin for summarizer. |
|
Buffer with summarizer for storing conversation memory. |
|
Conversation chat memory with token limit. |
|
VectorStoreRetriever-backed memory. |
|
Persist your chain history to the Zep MemoryStore. |
Functions¶
|
Get the prompt input key. |
langchain.model_laboratory
¶
Experiment with different models.
Classes¶
|
Experiment with different models. |
langchain.output_parsers
¶
OutputParser classes parse the output of an LLM call.
Class hierarchy:
BaseLLMOutputParser --> BaseOutputParser --> <name>OutputParser # ListOutputParser, PydanticOutputParser
Main helpers:
Serializable, Generation, PromptValue
Classes¶
Parse the output of an LLM call to a boolean. |
|
Combine multiple output parsers into one. |
|
Parse the output of an LLM call to a datetime. |
|
Parse an output that is one of a set of values. |
|
Parse an output as the element of the Json object. |
|
Parse an output as the Json object. |
|
Parse an output that is one of sets of values. |
|
|
Parse an output as an attribute of a pydantic object. |
|
Parse an output as a pydantic object. |
Wraps a parser and tries to fix parsing errors. |
|
Parse the output of an LLM call to a JSON object. |
|
|
Parse an output as the element of the Json object. |
Parse an output as the Json object. |
|
Parse an output that is one of sets of values. |
|
|
Parse an output as an attribute of a pydantic object. |
|
Parse an output as a pydantic object. |
Parse tools from OpenAI response. |
|
Parse tools from OpenAI response. |
|
Parse tools from OpenAI response. |
|
Parse an output using Pandas DataFrame format. |
|
Parse an output using a pydantic model. |
|
Parse the output of an LLM call using Guardrails. |
|
Parse the output of an LLM call using a regex. |
|
Parse the output of an LLM call into a Dictionary using a regex. |
|
Wraps a parser and tries to fix parsing errors. |
|
Wraps a parser and tries to fix parsing errors. |
|
A schema for a response from a structured output parser. |
|
Parse the output of an LLM call to a structured output. |
|
Parse an output using xml format. |
Functions¶
Parse a JSON string from a Markdown string and check that it contains the expected keys. |
|
Parse a JSON string from a Markdown string. |
|
Parse a JSON string that may be missing closing braces. |
|
Load an output parser. |
langchain.prompts
¶
Prompt is the input to the model.
Prompt is often constructed from multiple components. Prompt classes and functions make constructing
and working with prompts easy.
Class hierarchy:
BasePromptTemplate --> PipelinePromptTemplate
StringPromptTemplate --> PromptTemplate
FewShotPromptTemplate
FewShotPromptWithTemplates
BaseChatPromptTemplate --> AutoGPTPrompt
ChatPromptTemplate --> AgentScratchPadChatPromptTemplate
BaseMessagePromptTemplate --> MessagesPlaceholder
BaseStringMessagePromptTemplate --> ChatMessagePromptTemplate
HumanMessagePromptTemplate
AIMessagePromptTemplate
SystemMessagePromptTemplate
PromptValue --> StringPromptValue
ChatPromptValue
Classes¶
|
Select and order examples based on ngram overlap score (sentence_bleu score). |
Functions¶
|
Compute ngram overlap score of source and example as sentence_bleu score. |
langchain.retrievers
¶
Retriever class returns Documents given a text query.
It is more general than a vector store. A retriever does not need to be able to store documents, only to return (or retrieve) it. Vector stores can be used as the backbone of a retriever, but there are other types of retrievers as well.
Class hierarchy:
BaseRetriever --> <name>Retriever # Examples: ArxivRetriever, MergerRetriever
Main helpers:
Document, Serializable, Callbacks,
CallbackManagerForRetrieverRun, AsyncCallbackManagerForRetrieverRun
Classes¶
|
Retriever that wraps a base retriever and compresses the results. |
Base class for document compressors. |
|
|
Document compressor that uses a pipeline of Transformers. |
|
Document compressor that uses an LLM chain to extract the relevant parts of documents. |
|
Parse outputs that could return a null string of some sort. |
Filter that drops documents that aren't relevant to the query. |
|
Document compressor that uses Cohere Rerank API. |
|
|
Document compressor that uses embeddings to drop documents unrelated to the query. |
Retriever that ensembles the multiple retrievers. |
|
Retriever that merges the results of multiple retrievers. |
|
List of lines. |
|
Output parser for a list of lines. |
|
Given a query, use an LLM to write a set of queries. |
|
Retrieve from a set of multiple embeddings for the same document. |
|
|
Enumerator of the types of search to perform. |
|
Retrieve small chunks then retrieve their parent documents. |
Given a query, use an LLM to re-phrase it. |
|
Retriever that uses a vector store and an LLM to generate the vector store queries. |
|
Translate Chroma internal query language elements to valid filters. |
|
Logic for converting internal query language elements to valid filters. |
|
Translate DeepLake internal query language elements to valid filters. |
|
|
Translate Elasticsearch internal query language elements to valid filters. |
Translate Milvus internal query language elements to valid filters. |
|
|
Translate Mongo internal query language elements to valid filters. |
Translate MyScale internal query language elements to valid filters. |
|
Translate OpenSearch internal query domain-specific language elements to valid filters. |
|
Translate Pinecone internal query language elements to valid filters. |
|
Translate Qdrant internal query language elements to valid filters. |
|
Translate |
|
Translate Langchain filters to Supabase PostgREST filters. |
|
|
Translate the internal query language elements to valid filters. |
Translate Vectara internal query language elements to valid filters. |
|
Translate Weaviate internal query language elements to valid filters. |
|
|
Retriever that combines embedding similarity with recency in retrieving values. |
List of questions. |
|
Output parser for a list of numbered questions. |
|
Search queries to research for the user's goal. |
|
Google Search API retriever. |
Functions¶
|
Return the compression chain input. |
|
Return the compression chain input. |
Check if a string can be cast to a float. |
|
Convert a value to a string and add double quotes if it is a string. |
|
Convert a value to a string and add single quotes if it is a string. |
langchain.runnables
¶
Classes¶
An instance of a runnable stored in the LangChain Hub. |
|
A function description for ChatOpenAI |
|
A runnable that routes to the selected function. |
langchain.smith
¶
LangSmith utilities.
This module provides utilities for connecting to LangSmith. For more information on LangSmith, see the LangSmith documentation.
Evaluation
LangSmith helps you evaluate Chains and other language model application components using a number of LangChain evaluators.
An example of this is shown below, assuming you’ve created a LangSmith dataset called <my_dataset_name>
:
from langsmith import Client
from langchain.chat_models import ChatOpenAI
from langchain.chains import LLMChain
from langchain.smith import RunEvalConfig, run_on_dataset
# Chains may have memory. Passing in a constructor function lets the
# evaluation framework avoid cross-contamination between runs.
def construct_chain():
llm = ChatOpenAI(temperature=0)
chain = LLMChain.from_string(
llm,
"What's the answer to {your_input_key}"
)
return chain
# Load off-the-shelf evaluators via config or the EvaluatorType (string or enum)
evaluation_config = RunEvalConfig(
evaluators=[
"qa", # "Correctness" against a reference answer
"embedding_distance",
RunEvalConfig.Criteria("helpfulness"),
RunEvalConfig.Criteria({
"fifth-grader-score": "Do you have to be smarter than a fifth grader to answer this question?"
}),
]
)
client = Client()
run_on_dataset(
client,
"<my_dataset_name>",
construct_chain,
evaluation=evaluation_config,
)
You can also create custom evaluators by subclassing the
StringEvaluator
or LangSmith’s RunEvaluator classes.
from typing import Optional
from langchain.evaluation import StringEvaluator
class MyStringEvaluator(StringEvaluator):
@property
def requires_input(self) -> bool:
return False
@property
def requires_reference(self) -> bool:
return True
@property
def evaluation_name(self) -> str:
return "exact_match"
def _evaluate_strings(self, prediction, reference=None, input=None, **kwargs) -> dict:
return {"score": prediction == reference}
evaluation_config = RunEvalConfig(
custom_evaluators = [MyStringEvaluator()],
)
run_on_dataset(
client,
"<my_dataset_name>",
construct_chain,
evaluation=evaluation_config,
)
Primary Functions
arun_on_dataset
: Asynchronous function to evaluate a chain, agent, or other LangChain component over a dataset.run_on_dataset
: Function to evaluate a chain, agent, or other LangChain component over a dataset.RunEvalConfig
: Class representing the configuration for running evaluation. You can select evaluators byEvaluatorType
or config, or you can pass in custom_evaluators
Classes¶
Configuration for a given run evaluator. |
|
Configuration for a run evaluation. |
|
Create a new model by parsing and validating input data from keyword arguments. |
|
A simple progress bar for the console. |
|
Your architecture raised an error. |
|
Raised when the input format is invalid. |
|
A dictionary of the results of a single test run. |
|
Extract items to evaluate from the run object from a chain. |
|
Extract items to evaluate from the run object. |
|
Map an example, or row in the dataset, to the inputs of an evaluation. |
|
|
Evaluate Run and optional examples. |
Extract items to evaluate from the run object. |
|
Map an input to the tool. |
Functions¶
Generate a random name. |
|
Run the Chain or language model on a dataset and store traces to the specified project name. |
|
Run the Chain or language model on a dataset and store traces to the specified project name. |
langchain.storage
¶
Implementations of key-value stores and storage helpers.
Module provides implementations of various key-value stores that conform to a simple key-value interface.
The primary goal of these storages is to support implementation of caching.
Classes¶
Wraps a store with key and value encoders/decoders. |
|
|
BaseStore interface that works on the local file system. |
In-memory implementation of the BaseStore using a dictionary. |
langchain.text_splitter
¶
Text Splitters are classes for splitting text.
Class hierarchy:
BaseDocumentTransformer --> TextSplitter --> <name>TextSplitter # Example: CharacterTextSplitter
RecursiveCharacterTextSplitter --> <name>TextSplitter
Note: MarkdownHeaderTextSplitter and **HTMLHeaderTextSplitter do not derive from TextSplitter.
Main helpers:
Document, Tokenizer, Language, LineType, HeaderType
Classes¶
Splitting text that looks at characters. |
|
Element type as typed dict. |
|
|
Splitting HTML files based on specified headers. |
Header type as typed dict. |
|
|
Enum of the programming languages. |
|
Attempts to split the text along Latex-formatted layout elements. |
Line type as typed dict. |
|
Splitting markdown files based on specified headers. |
|
|
Attempts to split the text along Markdown-formatted headings. |
|
Splitting text using NLTK package. |
|
Attempts to split the text along Python syntax. |
Splitting text by recursively look at characters. |
|
Splitting text to tokens using sentence model tokenizer. |
|
|
Splitting text using Spacy package. |
|
Interface for splitting text into chunks. |
Splitting text to tokens using model tokenizer. |
|
|
Tokenizer data class. |
Functions¶
|
Split incoming text and return chunks using tokenizer. |
langchain.tools
¶
Tools are classes that an Agent uses to interact with the world.
Each tool has a description. Agent uses the description to choose the right tool for the job.
Class hierarchy:
ToolMetaclass --> BaseTool --> <name>Tool # Examples: AIPluginTool, BaseGraphQLTool
<name> # Examples: BraveSearch, HumanInputRun
Main helpers:
CallbackManagerForToolRun, AsyncCallbackManagerForToolRun
Classes¶
Create a new model by parsing and validating input data from keyword arguments. |
Functions¶
Create a tool to do retrieval of documents. |
langchain.utils
¶
Utility functions for LangChain.
These functions do not depend on any other LangChain module.
Classes¶
Representation of a callable function to the Ernie API. |
|
Representation of a callable function to the Ernie API. |
Functions¶
|
Converts a Pydantic model to a function description for the Ernie API. |
Converts a Pydantic model to a function description for the Ernie API. |