Tony20100/langchain_full huggingface dataset:
langchain_core.utils.formatting.StrictFormatter¶
class langchain_core.utils.formatting.StrictFormatter[source]¶
Formatter that checks for extra keys.
Methods
__init__()
check_unused_args(used_args, args, kwargs)
convert_field(value, conversion)
format(format_string, /, *args, **kwargs)
format_field(value, format_spec)
get_field(field_name, args, kwargs)
get_value(key, args, kwargs)
parse(format_string)
validate_input_variables(format_string, ...)
vformat(format_string, args, kwargs)
Check that no arguments are provided.
__init__()¶
check_unused_args(used_args, args, kwargs)¶
convert_field(value, conversion)¶
format(format_string, /, *args, **kwargs)¶
format_field(value, format_spec)¶
get_field(field_name, args, kwargs)¶
get_value(key, args, kwargs)¶
parse(format_string)¶
validate_input_variables(format_string: str, input_variables: List[str]) → None[source]¶
Parameters
format_string (str) – 
input_variables (List[str]) – 
Return type
None
vformat(format_string: str, args: Sequence, kwargs: Mapping[str, Any]) → str[source]¶
Check that no arguments are provided.
Parameters
format_string (str) – 
args (Sequence) – 
kwargs (Mapping[str, Any]) – 
Return type
str
langchain_core.utils.utils.check_package_version¶
langchain_core.utils.utils.check_package_version(package: str, lt_version: Optional[str] = None, lte_version: Optional[str] = None, gt_version: Optional[str] = None, gte_version: Optional[str] = None) → None[source]¶
Check the version of a package.
Parameters
package (str) – 
lt_version (Optional[str]) – 
lte_version (Optional[str]) – 
gt_version (Optional[str]) – 
gte_version (Optional[str]) – 
Return type
None
langchain_core.utils.aiter.py_anext¶
langchain_core.utils.aiter.py_anext(iterator: ~typing.AsyncIterator[~langchain_core.utils.aiter.T], default: ~typing.Union[~langchain_core.utils.aiter.T, ~typing.Any] = <object object>) → Awaitable[Union[T, None, Any]][source]¶
Pure-Python implementation of anext() for testing purposes.
Closely matches the builtin anext() C implementation.
Can be used to compare the built-in implementation of the inner
coroutines machinery to C-implementation of __anext__() and send()
or throw() on the returned generator.
Parameters
iterator (AsyncIterator[T]) – 
default (Union[T, Any]) – 
Return type
Awaitable[Union[T, None, Any]]
langchain_astradb.utils.mmr.cosine_similarity¶
langchain_astradb.utils.mmr.cosine_similarity(X: Union[List[List[float]], List[ndarray], ndarray], Y: Union[List[List[float]], List[ndarray], ndarray]) → ndarray[source]¶
Row-wise cosine similarity between two equal-width matrices.
Parameters
X (Union[List[List[float]], List[ndarray], ndarray]) – 
Y (Union[List[List[float]], List[ndarray], ndarray]) – 
Return type
ndarray
Examples using cosine_similarity¶
How to route between sub-chains
langchain_core.utils.utils.mock_now¶
langchain_core.utils.utils.mock_now(dt_value)[source]¶
Context manager for mocking out datetime.now() in unit tests.
Example:
with mock_now(datetime.datetime(2011, 2, 3, 10, 11)):
assert datetime.datetime.now() == datetime.datetime(2011, 2, 3, 10, 11)
Examples using mock_now¶
How to use a time-weighted vector store retriever
langchain_core.utils.env.get_from_env¶
langchain_core.utils.env.get_from_env(key: str, env_key: str, default: Optional[str] = None) → str[source]¶
Get a value from a dictionary or an environment variable.
Parameters
key (str) – 
env_key (str) – 
default (Optional[str]) – 
Return type
str
Examples using get_from_env¶
Passio NutritionAI
langchain_core.utils.json.parse_and_check_json_markdown¶
langchain_core.utils.json.parse_and_check_json_markdown(text: str, expected_keys: List[str]) → dict[source]¶
Parse a JSON string from a Markdown string and check that it
contains the expected keys.
Parameters
text (str) – The Markdown string.
expected_keys (List[str]) – The expected keys in the JSON string.
Returns
The parsed JSON object as a Python dictionary.
Return type
dict
langchain_core.utils.json.parse_json_markdown¶
langchain_core.utils.json.parse_json_markdown(json_string: str, *, parser: ~typing.Callable[[str], ~typing.Any] = <function parse_partial_json>) → dict[source]¶
Parse a JSON string from a Markdown string.
Parameters
json_string (str) – The Markdown string.
parser (Callable[[str], Any]) – 
Returns
The parsed JSON object as a Python dictionary.
Return type
dict
langchain_core.utils.function_calling.format_tool_to_openai_function¶
langchain_core.utils.function_calling.format_tool_to_openai_function(tool: BaseTool) → FunctionDescription[source]¶
[Deprecated] Format tool into the OpenAI function API.
Notes
Deprecated since version 0.1.16: Use langchain_core.utils.function_calling.convert_to_openai_function() instead.
Parameters
tool (BaseTool) – 
Return type
FunctionDescription
langchain_core.utils.function_calling.tool_example_to_messages¶
langchain_core.utils.function_calling.tool_example_to_messages(input: str, tool_calls: List[BaseModel], tool_outputs: Optional[List[str]] = None) → List[BaseMessage][source]¶
Convert an example into a list of messages that can be fed into an LLM.
This code is an adapter that converts a single example to a list of messages
that can be fed into a chat model.
The list of messages per example corresponds to:
HumanMessage: contains the content from which content should be extracted.
AIMessage: contains the extracted information from the model
ToolMessage: contains confirmation to the model that the model requested a toolcorrectly.
The ToolMessage is required because some chat models are hyper-optimized for agents
rather than for an extraction use case.
Parameters
input (str) – string, the user input
tool_calls (List[BaseModel]) – List[BaseModel], a list of tool calls represented as Pydantic
BaseModels
tool_outputs (Optional[List[str]]) – Optional[List[str]], a list of tool call outputs.
Does not need to be provided. If not provided, a placeholder value
will be inserted.
Returns
A list of messages
Return type
List[BaseMessage]
Examples
from typing import List, Optional
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_openai import ChatOpenAI
class Person(BaseModel):
    '''Information about a person.'''
    name: Optional[str] = Field(..., description="The name of the person")
    hair_color: Optional[str] = Field(
        ..., description="The color of the person's hair if known"
    )
    height_in_meters: Optional[str] = Field(
        ..., description="Height in METERs"
    )
..., description="Height in METERs"
    )
examples = [
    (
        "The ocean is vast and blue. It's more than 20,000 feet deep.",
        Person(name=None, height_in_meters=None, hair_color=None),
    ),
    (
        "Fiona traveled far from France to Spain.",
        Person(name="Fiona", height_in_meters=None, hair_color=None),
    ),
]
messages = []
for txt, tool_call in examples:
    messages.extend(
        tool_example_to_messages(txt, [tool_call])
    )
langchain_core.utils.input.print_text¶
langchain_core.utils.input.print_text(text: str, color: Optional[str] = None, end: str = '', file: Optional[TextIO] = None) → None[source]¶
Print text with highlighting and no end characters.
Parameters
text (str) – 
color (Optional[str]) – 
end (str) – 
file (Optional[TextIO]) – 
Return type
None
langchain_core.utils.mustache.tokenize¶
langchain_core.utils.mustache.tokenize(template: str, def_ldel: str = '{{', def_rdel: str = '}}') → Iterator[Tuple[str, str]][source]¶
Tokenize a mustache template.
Tokenizes a mustache template in a generator fashion,
using file-like objects. It also accepts a string containing
the template.
Arguments:
template – a file-like object, or a string of a mustache template
def_ldel – The default left delimiter(“{{” by default, as in spec compliant mustache)
def_rdel – The default right delimiter(“}}” by default, as in spec compliant mustache)
Returns:
A generator of mustache tags in the form of a tuple
– (tag_type, tag_key)
Where tag_type is one of:
literal
section
inverted section
end
partial
no escape
And tag_key is either the key or in the case of a literal tag,
the literal itself.
Parameters
template (str) – 
def_ldel (str) – 
def_rdel (str) – 
Return type
Iterator[Tuple[str, str]]
and access is automatically synchronised.
Methods
__init__(iterable[, n, lock])
close()
Parameters
iterable (Iterator[T]) – 
n (int) – 
lock (Optional[ContextManager[Any]]) – 
__init__(iterable: Iterator[T], n: int = 2, *, lock: Optional[ContextManager[Any]] = None)[source]¶
Parameters
iterable (Iterator[T]) – 
n (int) – 
lock (Optional[ContextManager[Any]]) – 
close() → None[source]¶
Return type
None
langchain_prompty.utils.execute¶
langchain_prompty.utils.execute(prompt: Union[str, Prompty], configuration: Dict[str, Any] = {}, parameters: Dict[str, Any] = {}, inputs: Dict[str, Any] = {}, raw: bool = False, connection: str = 'default') → Any[source]¶
Parameters
prompt (Union[str, Prompty]) – 
configuration (Dict[str, Any]) – 
parameters (Dict[str, Any]) – 
inputs (Dict[str, Any]) – 
raw (bool) – 
connection (str) – 
Return type
Any
langchain_community.utils.ernie_functions.convert_pydantic_to_ernie_tool¶
langchain_community.utils.ernie_functions.convert_pydantic_to_ernie_tool(model: Type[BaseModel], *, name: Optional[str] = None, description: Optional[str] = None) → ToolDescription[source]¶
Convert a Pydantic model to a function description for the Ernie API.
Parameters
model (Type[BaseModel]) – 
name (Optional[str]) – 
description (Optional[str]) – 
Return type
ToolDescription
langchain_cohere.rag_retrievers.CohereRagRetriever¶
class langchain_cohere.rag_retrievers.CohereRagRetriever[source]¶
Bases: BaseRetriever
Cohere Chat API with RAG.
Create a new model by parsing and validating input data from keyword arguments.
Raises ValidationError if the input data cannot be parsed to form a valid model.
param connectors: List[Dict] [Optional]¶
When specified, the model’s reply will be enriched with information found by
querying each of the connectors (RAG). These will be returned as langchain
documents.
Currently only accepts {“id”: “web-search”}.
param llm: BaseChatModel [Required]¶
Cohere ChatModel to use.
param metadata: Optional[Dict[str, Any]] = None¶
Optional metadata associated with the retriever. Defaults to None
This metadata will be associated with each call to this retriever,
and passed as arguments to the handlers defined in callbacks.
You can use these to eg identify a specific instance of a retriever with its
use case.
param tags: Optional[List[str]] = None¶
Optional list of tags associated with the retriever. Defaults to None
These tags will be associated with each call to this retriever,
and passed as arguments to the handlers defined in callbacks.
You can use these to eg identify a specific instance of a retriever with its
use case.
async abatch(inputs: List[Input], config: Optional[Union[RunnableConfig, List[RunnableConfig]]] = None, *, return_exceptions: bool = False, **kwargs: Optional[Any]) → List[Output]¶
Default implementation runs ainvoke in parallel using asyncio.gather.
The default implementation of batch works well for IO bound runnables.
The default implementation of batch works well for IO bound runnables.
Subclasses should override this method if they can batch more efficiently;
e.g., if the underlying runnable uses an API which supports a batch mode.
Parameters
inputs (List[Input]) – 
config (Optional[Union[RunnableConfig, List[RunnableConfig]]]) – 
return_exceptions (bool) – 
kwargs (Optional[Any]) – 
Return type
List[Output]
async abatch_as_completed(inputs: Sequence[Input], config: Optional[Union[RunnableConfig, Sequence[RunnableConfig]]] = None, *, return_exceptions: bool = False, **kwargs: Optional[Any]) → AsyncIterator[Tuple[int, Union[Output, Exception]]]¶
Run ainvoke in parallel on a list of inputs,
yielding results as they complete.
Parameters
inputs (Sequence[Input]) – 
config (Optional[Union[RunnableConfig, Sequence[RunnableConfig]]]) – 
return_exceptions (bool) – 
kwargs (Optional[Any]) – 
Return type
AsyncIterator[Tuple[int, Union[Output, Exception]]]
async aget_relevant_documents(query: str, *, callbacks: Callbacks = None, tags: Optional[List[str]] = None, metadata: Optional[Dict[str, Any]] = None, run_name: Optional[str] = None, **kwargs: Any) → List[Document]¶
[Deprecated] Asynchronously get documents relevant to a query.
Users should favor using .ainvoke or .abatch rather than
aget_relevant_documents directly.
Parameters
query (str) – string to find relevant documents for
callbacks (Callbacks) – Callback manager or list of callbacks
tags (Optional[List[str]]) – Optional list of tags associated with the retriever. Defaults to None
These tags will be associated with each call to this retriever,
These tags will be associated with each call to this retriever,
and passed as arguments to the handlers defined in callbacks.
metadata (Optional[Dict[str, Any]]) – Optional metadata associated with the retriever. Defaults to None
This metadata will be associated with each call to this retriever,
and passed as arguments to the handlers defined in callbacks.
run_name (Optional[str]) – Optional name for the run.
kwargs (Any) – 
Returns
List of relevant documents
Return type
List[Document]
Notes
Deprecated since version langchain-core==0.1.46: Use ainvoke instead.
async ainvoke(input: str, config: Optional[RunnableConfig] = None, **kwargs: Any) → List[Document]¶
Asynchronously invoke the retriever to get relevant documents.
Main entry point for asynchronous retriever invocations.
Parameters
input (str) – The query string
config (Optional[RunnableConfig]) – Configuration for the retriever
**kwargs (Any) – Additional arguments to pass to the retriever
Returns
List of relevant documents
Return type
List[Document]
Examples:
await retriever.ainvoke("query")
assign(**kwargs: Union[Runnable[Dict[str, Any], Any], Callable[[Dict[str, Any]], Any], Mapping[str, Union[Runnable[Dict[str, Any], Any], Callable[[Dict[str, Any]], Any]]]]) → RunnableSerializable[Any, Any]¶
Assigns new fields to the dict output of this runnable.
Returns a new runnable.
from langchain_community.llms.fake import FakeStreamingListLLM
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import SystemMessagePromptTemplate
from langchain_core.runnables import Runnable
from operator import itemgetter
prompt = (
    SystemMessagePromptTemplate.from_template("You are a nice assistant.")
prompt = (
    SystemMessagePromptTemplate.from_template("You are a nice assistant.")
    + "{question}"
)
llm = FakeStreamingListLLM(responses=["foo-lish"])
chain: Runnable = prompt | llm | {"str": StrOutputParser()}
chain_with_assign = chain.assign(hello=itemgetter("str") | llm)
print(chain_with_assign.input_schema.schema())
# {'title': 'PromptInput', 'type': 'object', 'properties':
{'question': {'title': 'Question', 'type': 'string'}}}
print(chain_with_assign.output_schema.schema()) #
{'title': 'RunnableSequenceOutput', 'type': 'object', 'properties':
{'str': {'title': 'Str',
'type': 'string'}, 'hello': {'title': 'Hello', 'type': 'string'}}}
Parameters
kwargs (Union[Runnable[Dict[str, Any], Any], Callable[[Dict[str, Any]], Any], Mapping[str, Union[Runnable[Dict[str, Any], Any], Callable[[Dict[str, Any]], Any]]]]) – 
Return type
RunnableSerializable[Any, Any]
async astream(input: Input, config: Optional[RunnableConfig] = None, **kwargs: Optional[Any]) → AsyncIterator[Output]¶
Default implementation of astream, which calls ainvoke.
Subclasses should override this method if they support streaming output.
Parameters
input (Input) – 
config (Optional[RunnableConfig]) – 
kwargs (Optional[Any]) – 
Return type
AsyncIterator[Output]
Return type
Runnable[Input, Output]
with_types(*, input_type: Optional[Type[Input]] = None, output_type: Optional[Type[Output]] = None) → Runnable[Input, Output]¶
Bind input and output types to a Runnable, returning a new Runnable.
Parameters
input_type (Optional[Type[Input]]) – 
output_type (Optional[Type[Output]]) – 
Return type
Runnable[Input, Output]
property InputType: Type[Input]¶
The type of input this runnable accepts specified as a type annotation.
property OutputType: Type[Output]¶
The type of output this runnable produces specified as a type annotation.
property config_specs: List[ConfigurableFieldSpec]¶
List configurable fields for this runnable.
property input_schema: Type[BaseModel]¶
The type of input this runnable accepts specified as a pydantic model.
property lc_attributes: Dict¶
List of attribute names that should be included in the serialized kwargs.
These attributes must be accepted by the constructor.
property lc_secrets: Dict[str, str]¶
A map of constructor argument names to secret ids.
For example,{“openai_api_key”: “OPENAI_API_KEY”}
name: Optional[str] = None¶
The name of the runnable. Used for debugging and tracing.
property output_schema: Type[BaseModel]¶
The type of output this runnable produces specified as a pydantic model.
Examples using CohereRagRetriever¶
Cohere
Cohere RAG
langchain_core.beta.runnables.context.ContextSet¶
class langchain_core.beta.runnables.context.ContextSet[source]¶
Bases: RunnableSerializable
[Beta] Set a context value.
Notes
Create a new model by parsing and validating input data from keyword arguments.
Raises ValidationError if the input data cannot be parsed to form a valid model.
param keys: Mapping[str, Optional[Runnable]] [Required]¶
param prefix: str = ''¶
async abatch(inputs: List[Input], config: Optional[Union[RunnableConfig, List[RunnableConfig]]] = None, *, return_exceptions: bool = False, **kwargs: Optional[Any]) → List[Output]¶
Default implementation runs ainvoke in parallel using asyncio.gather.
The default implementation of batch works well for IO bound runnables.
Subclasses should override this method if they can batch more efficiently;
e.g., if the underlying runnable uses an API which supports a batch mode.
Parameters
inputs (List[Input]) – 
config (Optional[Union[RunnableConfig, List[RunnableConfig]]]) – 
return_exceptions (bool) – 
kwargs (Optional[Any]) – 
Return type
List[Output]
async abatch_as_completed(inputs: Sequence[Input], config: Optional[Union[RunnableConfig, Sequence[RunnableConfig]]] = None, *, return_exceptions: bool = False, **kwargs: Optional[Any]) → AsyncIterator[Tuple[int, Union[Output, Exception]]]¶
Run ainvoke in parallel on a list of inputs,
yielding results as they complete.
Parameters
inputs (Sequence[Input]) – 
config (Optional[Union[RunnableConfig, Sequence[RunnableConfig]]]) – 
return_exceptions (bool) – 
kwargs (Optional[Any]) – 
Return type
AsyncIterator[Tuple[int, Union[Output, Exception]]]
Return type
AsyncIterator[Tuple[int, Union[Output, Exception]]]
async ainvoke(input: Any, config: Optional[RunnableConfig] = None, **kwargs: Any) → Any[source]¶
Default implementation of ainvoke, calls invoke from a thread.
The default implementation allows usage of async code even if
the runnable did not implement a native async version of invoke.
Subclasses should override this method if they can run asynchronously.
Parameters
input (Any) – 
config (Optional[RunnableConfig]) – 
kwargs (Any) – 
Return type
Any
assign(**kwargs: Union[Runnable[Dict[str, Any], Any], Callable[[Dict[str, Any]], Any], Mapping[str, Union[Runnable[Dict[str, Any], Any], Callable[[Dict[str, Any]], Any]]]]) → RunnableSerializable[Any, Any]¶
Assigns new fields to the dict output of this runnable.
Returns a new runnable.
from langchain_community.llms.fake import FakeStreamingListLLM
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import SystemMessagePromptTemplate
from langchain_core.runnables import Runnable
from operator import itemgetter
prompt = (
    SystemMessagePromptTemplate.from_template("You are a nice assistant.")
    + "{question}"
)
llm = FakeStreamingListLLM(responses=["foo-lish"])
chain: Runnable = prompt | llm | {"str": StrOutputParser()}
chain_with_assign = chain.assign(hello=itemgetter("str") | llm)
print(chain_with_assign.input_schema.schema())
# {'title': 'PromptInput', 'type': 'object', 'properties':
{'question': {'title': 'Question', 'type': 'string'}}}
print(chain_with_assign.output_schema.schema()) #
Runnables that leverage the configurable_fields and configurable_alternatives
methods will have a dynamic output schema that depends on which
configuration the runnable is invoked with.
This method allows to get an output schema for a specific configuration.
Parameters
config (Optional[RunnableConfig]) – A config to use when generating the schema.
Returns
A pydantic model that can be used to validate output.
Return type
Type[BaseModel]
get_prompts(config: Optional[RunnableConfig] = None) → List[BasePromptTemplate]¶
Parameters
config (Optional[RunnableConfig]) – 
Return type
List[BasePromptTemplate]
invoke(input: Any, config: Optional[RunnableConfig] = None) → Any[source]¶
Transform a single input into an output. Override to implement.
Parameters
input (Any) – The input to the runnable.
config (Optional[RunnableConfig]) – A config to use when invoking the runnable.
The config supports standard keys like ‘tags’, ‘metadata’ for tracing
purposes, ‘max_concurrency’ for controlling how much work to do
in parallel, and other keys. Please refer to the RunnableConfig
for more details.
Returns
The output of the runnable.
Return type
Any
classmethod is_lc_serializable() → bool¶
Is this class serializable?
Return type
bool
json(*, include: Optional[Union[AbstractSetIntStr, MappingIntStrAny]] = None, exclude: Optional[Union[AbstractSetIntStr, MappingIntStrAny]] = None, by_alias: bool = False, skip_defaults: Optional[bool] = None, exclude_unset: bool = False, exclude_defaults: bool = False, exclude_none: bool = False, encoder: Optional[Callable[[Any], Any]] = None, models_as_dict: bool = True, **dumps_kwargs: Any) → unicode¶
output_type (Optional[Type[Output]]) – 
Return type
Runnable[Input, Output]
property InputType: Type[Input]¶
The type of input this runnable accepts specified as a type annotation.
property OutputType: Type[Output]¶
The type of output this runnable produces specified as a type annotation.
property config_specs: List[ConfigurableFieldSpec]¶
List configurable fields for this runnable.
property ids: List[str]¶
property input_schema: Type[BaseModel]¶
The type of input this runnable accepts specified as a pydantic model.
property lc_attributes: Dict¶
List of attribute names that should be included in the serialized kwargs.
These attributes must be accepted by the constructor.
property lc_secrets: Dict[str, str]¶
A map of constructor argument names to secret ids.
For example,{“openai_api_key”: “OPENAI_API_KEY”}
name: Optional[str] = None¶
The name of the runnable. Used for debugging and tracing.
property output_schema: Type[BaseModel]¶
The type of output this runnable produces specified as a pydantic model.
langchain_core.beta.runnables.context.config_with_context¶
langchain_core.beta.runnables.context.config_with_context(config: RunnableConfig, steps: List[Runnable]) → RunnableConfig[source]¶
Patch a runnable config with context getters and setters.
Parameters
config (RunnableConfig) – The runnable config.
steps (List[Runnable]) – The runnable steps.
Returns
The patched runnable config.
Return type
RunnableConfig
langchain_core.beta.runnables.context.aconfig_with_context¶
langchain_core.beta.runnables.context.aconfig_with_context(config: RunnableConfig, steps: List[Runnable]) → RunnableConfig[source]¶
Asynchronously patch a runnable config with context getters and setters.
Parameters
config (RunnableConfig) – The runnable config.
steps (List[Runnable]) – The runnable steps.
Returns
The patched runnable config.
Return type
RunnableConfig