Tony20100/langchain_full huggingface dataset:
Image input:import base64
import httpx
from langchain_core.messages import HumanMessage
image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
image_data = base64.b64encode(httpx.get(image_url).content).decode("utf-8")
message = HumanMessage(
    content=[
        {"type": "text", "text": "describe the weather in this image"},
        {
            "type": "image_url",
            "image_url": {"url": f"data:image/jpeg;base64,{image_data}"},
        },
    ],
)
ai_msg = llm.invoke([message])
ai_msg.content
"The weather in the image appears to be clear and pleasant. The sky is mostly blue with scattered, light clouds, suggesting a sunny day with minimal cloud cover. There is no indication of rain or strong winds, and the overall scene looks bright and calm. The lush green grass and clear visibility further indicate good weather conditions."
Token usage:ai_msg = llm.invoke(messages)
ai_msg.usage_metadata
{'input_tokens': 28, 'output_tokens': 5, 'total_tokens': 33}
When streaming, set the stream_options model kwarg:
stream = llm.stream(messages, stream_options={"include_usage": True})
full = next(stream)
for chunk in stream:
    full += chunk
full.usage_metadata
{'input_tokens': 28, 'output_tokens': 5, 'total_tokens': 33}
Alternatively, setting stream_options when instantiating the model can be
useful when incorporating ChatOpenAI into LCEL chains– or when using
methods like .with_structured_output, which generate chains under the
hood.
methods like .with_structured_output, which generate chains under the
hood.
llm = ChatOpenAI(
    model="gpt-4o",
    model_kwargs={"stream_options": {"include_usage": True}},
)
structured_llm = llm.with_structured_output(...)
Logprobs:logprobs_llm = llm.bind(logprobs=True)
ai_msg = logprobs_llm.invoke(messages)
ai_msg.response_metadata["logprobs"]
{'content': [{'token': 'J', 'bytes': [74], 'logprob': -4.9617593e-06, 'top_logprobs': []},
  {'token': "'adore", 'bytes': [39, 97, 100, 111, 114, 101], 'logprob': -0.25202933, 'top_logprobs': []},
  {'token': ' la', 'bytes': [32, 108, 97], 'logprob': -0.20141791, 'top_logprobs': []},
  {'token': ' programmation', 'bytes': [32, 112, 114, 111, 103, 114, 97, 109, 109, 97, 116, 105, 111, 110], 'logprob': -1.9361265e-07, 'top_logprobs': []},
  {'token': '.', 'bytes': [46], 'logprob': -1.2233183e-05, 'top_logprobs': []}]}
Response metadataai_msg = llm.invoke(messages)
ai_msg.response_metadata
{'token_usage': {'completion_tokens': 5,
  'prompt_tokens': 28,
  'total_tokens': 33},
 'model_name': 'gpt-4o',
'model_name': 'gpt-4o',
 'system_fingerprint': 'fp_319be4768e',
 'finish_reason': 'stop',
 'logprobs': None}
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 cache: Union[BaseCache, bool, None] = None¶
Whether to cache the response.
If true, will use the global cache.
If false, will not use a cache
If None, will use the global cache if it’s set, otherwise no cache.
If instance of BaseCache, will use the provided cache.
Caching is not currently supported for streaming methods of models.
param callback_manager: Optional[BaseCallbackManager] = None¶
[DEPRECATED] Callback manager to add to the run trace.
param callbacks: Callbacks = None¶
Callbacks to add to the run trace.
param custom_get_token_ids: Optional[Callable[[str], List[int]]] = None¶
Optional encoder to use for counting tokens.
param default_headers: Union[Mapping[str, str], None] = None¶
param default_query: Union[Mapping[str, object], None] = None¶
param http_async_client: Union[Any, None] = None¶
Optional httpx.AsyncClient. Only used for async invocations. Must specify
http_client as well if you’d like a custom client for sync invocations.
param http_client: Union[Any, None] = None¶
Optional httpx.Client. Only used for sync invocations. Must specify
http_async_client as well if you’d like a custom client for async invocations.
param max_retries: int = 2¶
Maximum number of retries to make when generating.
param max_tokens: Optional[int] = None¶
Get a pydantic model that can be used to validate input to the runnable.
Runnables that leverage the configurable_fields and configurable_alternatives
methods will have a dynamic input schema that depends on which
configuration the runnable is invoked with.
This method allows to get an input 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 input.
Return type
Type[BaseModel]
classmethod get_lc_namespace() → List[str][source]¶
Get the namespace of the langchain object.
Return type
List[str]
get_name(suffix: Optional[str] = None, *, name: Optional[str] = None) → str¶
Get the name of the runnable.
Parameters
suffix (Optional[str]) – 
name (Optional[str]) – 
Return type
str
get_num_tokens(text: str) → int¶
Get the number of tokens present in the text.
Useful for checking if an input will fit in a model’s context window.
Parameters
text (str) – The string input to tokenize.
Returns
The integer number of tokens in the text.
Return type
int
get_num_tokens_from_messages(messages: List[BaseMessage]) → int¶
Calculate num tokens for gpt-3.5-turbo and gpt-4 with tiktoken package.
Official documentation: https://github.com/openai/openai-cookbook/blob/
main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb
Parameters
messages (List[BaseMessage]) – 
Return type
int
get_output_schema(config: Optional[RunnableConfig] = None) → Type[BaseModel]¶
Get a pydantic model that can be used to validate output to the runnable.
Get a pydantic model that can be used to validate output to the runnable.
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]
get_token_ids(text: str) → List[int]¶
Get the tokens present in the text with tiktoken package.
Parameters
text (str) – 
Return type
List[int]
invoke(input: LanguageModelInput, config: Optional[RunnableConfig] = None, *, stop: Optional[List[str]] = None, **kwargs: Any) → BaseMessage¶
Transform a single input into an output. Override to implement.
Parameters
input (LanguageModelInput) – 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.
stop (Optional[List[str]]) – 
kwargs (Any) – 
Returns
The output of the runnable.
Return type
BaseMessage
classmethod is_lc_serializable() → bool[source]¶
Return whether this model can be serialized by Langchain.
Return type
bool
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[str, Any]¶
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 ChatOpenAI¶
# Cogniswitch Tools
AINetwork
AWS DynamoDB
Activeloop Deep Memory
Airbyte Question Answering
Amadeus
Amazon Neptune with Cypher
Apache AGE
Apache Cassandra
ArXiv
ArangoDB
Arthur
Arxiv
AskNews
Astra DB
Azure Container Apps dynamic sessions
Bearly Code Interpreter
Browserbase
Build a Query Analysis System
Build a Question Answering application over a Graph Database
Build a Simple LLM Application
CSV
Cassandra Database
ChatGPT Plugins
ChatOpenAI
Classify Text into Labels
CnosDB
Connery Action Tool
Connery Toolkit
Context
Conversational RAG
Dataherald
Diffbot
Discord
Document Comparison
E2B Data Analysis
Exa Search
Facebook Messenger
FalkorDB
Figma
FlashRank reranker
Fleet AI Context
Flyte
Generate Synthetic Data
Github
Gmail
Hippo
How deal with high cardinality categoricals when doing query analysis
How to add a semantic layer over graph database
How to add chat history
How to add a semantic layer over graph database
How to add chat history
How to add default invocation args to a Runnable
How to add examples to the prompt for query analysis
How to add fallbacks to a runnable
How to add memory to chatbots
How to add retrieval to chatbots
How to add scores to retriever results
How to add tools to chatbots
How to add values to a chain’s state
How to best prompt for Graph-RAG
How to configure runtime chain internals
How to construct knowledge graphs
How to convert tools to OpenAI Functions
How to do “self-querying” retrieval
How to get log probabilities
How to handle cases where no queries are generated
How to handle multiple queries when doing query analysis
How to handle multiple retrievers when doing query analysis
How to inspect runnables
How to invoke runnables in parallel
How to map values to a graph database
How to migrate from legacy LangChain agents to LangGraph
How to parse JSON output
How to parse YAML output
How to pass multimodal data directly to models
How to pass through arguments from one step to the next
How to retry when a parsing error occurs
How to run custom functions
How to track token usage in ChatModels
How to use a model to call tools
How to use few shot examples in chat models
How to use multimodal prompts
How to use reference examples when doing extraction
How to use the MultiQueryRetriever
How to use the output-fixing parser
HugeGraph
Human as a tool
Hybrid Search
Infino
Infobip
Jaguar Vector Database
Jina Reranker
KDB.AI
Kay.ai
Kuzu
LLMLingua Document Compressor
LLMonitor
Label Studio
LangSmith Chat Datasets
LangSmith LLM Runs
Log, Trace, and Monitor
Log10
Memgraph
LangSmith LLM Runs
Log, Trace, and Monitor
Log10
Memgraph
Milvus Hybrid Search
Momento Vector Index (MVI)
MongoDB
MultiOn
MyScale
NebulaGraph
Neo4j
Neo4j Vector Index
Ontotext GraphDB
OpenAI
OpenAI metadata tagger
OpenAPI
Outline
Pandas Dataframe
Passio NutritionAI
Polygon IO Toolkit
Portkey
PowerBI Dataset
PromptLayer
Python
RAGatouille
RDFLib
RankLLM Reranker
RePhraseQuery
Reddit Search 
Redis
Rememberizer
Remembrall
Response metadata
Robocorp
SAP HANA Cloud Vector Engine
SEC filing
SQL (SQLAlchemy)
SQL Database
SQLite
Semantic Scholar API Tool
Shell (bash)
Slack
Spark SQL
Streamlit
Summarize Text
Tavily Search
Tavily Search API
Telegram
Tencent Cloud VectorDB
TiDB
Timescale Vector (Postgres)
Trubrics
UpTrain
Vectara
WeChat
Weaviate
WhatsApp
Wikipedia
Xata
Yahoo Finance News
Yellowbrick
You.com
You.com Search
YouTube audio
ZepCloudChatMessageHistory
iMessage
vLLM Chat
🦜️🏓 LangServe
langchain_community.chat_models.fireworks.conditional_decorator¶
langchain_community.chat_models.fireworks.conditional_decorator(condition: bool, decorator: Callable[[Any], Any]) → Callable[[Any], Any][source]¶
Define conditional decorator.
Parameters
condition (bool) – The condition.
decorator (Callable[[Any], Any]) – The decorator.
Returns
The decorated function.
Return type
Callable[[Any], Any]
langchain_anthropic.chat_models.convert_to_anthropic_tool¶
langchain_anthropic.chat_models.convert_to_anthropic_tool(tool: Union[Dict[str, Any], Type[BaseModel], Callable, BaseTool]) → AnthropicTool[source]¶
Parameters
tool (Union[Dict[str, Any], Type[BaseModel], Callable, BaseTool]) – 
Return type
AnthropicTool
langchain_nvidia_ai_endpoints.chat_models.ChatNVIDIA¶
class langchain_nvidia_ai_endpoints.chat_models.ChatNVIDIA[source]¶
Bases: BaseChatModel
NVIDIA chat model.
Example
from langchain_nvidia_ai_endpoints import ChatNVIDIA
model = ChatNVIDIA(model="meta/llama2-70b")
response = model.invoke("Hello")
Create a new NVIDIAChat chat model.
This class provides access to a NVIDIA NIM for chat. By default, it
connects to a hosted NIM, but can be configured to connect to a local NIM
using the base_url parameter. An API key is required to connect to the
hosted NIM.
Parameters
model (str) – The model to use for chat.
nvidia_api_key (str) – The API key to use for connecting to the hosted NIM.
api_key (str) – Alternative to nvidia_api_key.
base_url (str) – The base URL of the NIM to connect to.
temperature (float) – Sampling temperature in [0, 1].
max_tokens (int) – Maximum number of tokens to generate.
top_p (float) – Top-p for distribution sampling.
seed (int) – A seed for deterministic results.
stop (list[str]) – A list of cased stop words.
API Key:
- The recommended way to provide the API key is through the NVIDIA_API_KEY
environment variable.
param base_url: str = 'https://integrate.api.nvidia.com/v1'¶
Base url for model listing an invocation
param cache: Union[BaseCache, bool, None] = None¶
Whether to cache the response.
If true, will use the global cache.
If false, will not use a cache
If true, will use the global cache.
If false, will not use a cache
If None, will use the global cache if it’s set, otherwise no cache.
If instance of BaseCache, will use the provided cache.
Caching is not currently supported for streaming methods of models.
param callback_manager: Optional[BaseCallbackManager] = None¶
[DEPRECATED] Callback manager to add to the run trace.
param callbacks: Callbacks = None¶
Callbacks to add to the run trace.
param custom_get_token_ids: Optional[Callable[[str], List[int]]] = None¶
Optional encoder to use for counting tokens.
param max_tokens: Optional[int] = 1024¶
Maximum # of tokens to generate
param metadata: Optional[Dict[str, Any]] = None¶
Metadata to add to the run trace.
param model: str = 'mistralai/mixtral-8x7b-instruct-v0.1'¶
Name of the model to invoke
param seed: Optional[int] = None¶
The seed for deterministic results
param stop: Optional[Sequence[str]] = None¶
Stop words (cased)
param tags: Optional[List[str]] = None¶
Tags to add to the run trace.
param temperature: Optional[float] = None¶
Sampling temperature in [0, 1]
param top_p: Optional[float] = None¶
Top-p for distribution sampling
param verbose: bool [Optional]¶
Whether to print out response text.
__call__(messages: List[BaseMessage], stop: Optional[List[str]] = None, callbacks: Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]] = None, **kwargs: Any) → BaseMessage¶
[Deprecated]
Notes
Deprecated since version langchain-core==0.1.7: Use invoke instead.
Parameters
yielding results as they complete.
Parameters
inputs (Sequence[Input]) – 
config (Optional[Union[RunnableConfig, Sequence[RunnableConfig]]]) – 
return_exceptions (bool) – 
kwargs (Optional[Any]) – 
Return type
Iterator[Tuple[int, Union[Output, Exception]]]
bind(**kwargs: Any) → Runnable[Input, Output]¶
Bind arguments to a Runnable, returning a new Runnable.
Useful when a runnable in a chain requires an argument that is not
in the output of the previous runnable or included in the user input.
Example:
from langchain_community.chat_models import ChatOllama
from langchain_core.output_parsers import StrOutputParser
llm = ChatOllama(model='llama2')
# Without bind.
chain = (
    llm
    | StrOutputParser()
)
chain.invoke("Repeat quoted words exactly: 'One two three four five.'")
# Output is 'One two three four five.'
# With bind.
chain = (
    llm.bind(stop=["three"])
    | StrOutputParser()
)
chain.invoke("Repeat quoted words exactly: 'One two three four five.'")
# Output is 'One two'
Parameters
kwargs (Any) – 
Return type
Runnable[Input, Output]
bind_functions(functions: Sequence[Union[Dict[str, Any], Type[BaseModel], Callable]], function_call: Optional[str] = None, **kwargs: Any) → Runnable[Union[PromptValue, str, Sequence[Union[BaseMessage, List[str], Tuple[str, str], str, Dict[str, Any]]]], BaseMessage][source]¶
Parameters
functions (Sequence[Union[Dict[str, Any], Type[BaseModel], Callable]]) – 
function_call (Optional[str]) –
function_call (Optional[str]) – 
kwargs (Any) – 
Return type
Runnable[Union[PromptValue, str, Sequence[Union[BaseMessage, List[str], Tuple[str, str], str, Dict[str, Any]]]], BaseMessage]
bind_tools(tools: Sequence[Union[Dict[str, Any], Type[BaseModel], Callable, BaseTool]], *, tool_choice: Optional[Union[dict, str, Literal['auto', 'none'], bool]] = None, **kwargs: Any) → Runnable[Union[PromptValue, str, Sequence[Union[BaseMessage, List[str], Tuple[str, str], str, Dict[str, Any]]]], BaseMessage][source]¶
Parameters
tools (Sequence[Union[Dict[str, Any], Type[BaseModel], Callable, BaseTool]]) – 
tool_choice (Optional[Union[dict, str, Literal['auto', 'none'], bool]]) – 
kwargs (Any) – 
Return type
Runnable[Union[PromptValue, str, Sequence[Union[BaseMessage, List[str], Tuple[str, str], str, Dict[str, Any]]]], BaseMessage]
call_as_llm(message: str, stop: Optional[List[str]] = None, **kwargs: Any) → str¶
[Deprecated]
Notes
Deprecated since version langchain-core==0.1.7: Use invoke instead.
Parameters
message (str) – 
stop (Optional[List[str]]) – 
kwargs (Any) – 
Return type
str
config_schema(*, include: Optional[Sequence[str]] = None) → Type[BaseModel]¶
The type of config this runnable accepts specified as a pydantic model.
To mark a field as configurable, see the configurable_fields
and configurable_alternatives methods.
Parameters
text generation models and BaseMessages for chat models).
stop (Optional[List[str]]) – Stop words to use when generating. Model output is cut off at the
first occurrence of any of these substrings.
callbacks (Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]]) – Callbacks to pass through. Used for executing additional
functionality, such as logging or streaming, throughout generation.
**kwargs (Any) – Arbitrary additional keyword arguments. These are usually passed
to the model provider API call.
Returns
An LLMResult, which contains a list of candidate Generations for each inputprompt and additional model provider-specific output.
Return type
LLMResult
classmethod get_available_models(**kwargs: Any) → List[Model][source]¶
Get a list of available models that work with ChatNVIDIA.
Parameters
kwargs (Any) – 
Return type
List[Model]
get_graph(config: Optional[RunnableConfig] = None) → Graph¶
Return a graph representation of this runnable.
Parameters
config (Optional[RunnableConfig]) – 
Return type
Graph
get_input_schema(config: Optional[RunnableConfig] = None) → Type[BaseModel]¶
Get a pydantic model that can be used to validate input to the runnable.
Runnables that leverage the configurable_fields and configurable_alternatives
methods will have a dynamic input schema that depends on which
configuration the runnable is invoked with.
This method allows to get an input 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 input.
Return type
Type[BaseModel]
classmethod get_lc_namespace() → List[str]¶
Get the namespace of the langchain object.
Return type
Type[BaseModel]
get_prompts(config: Optional[RunnableConfig] = None) → List[BasePromptTemplate]¶
Parameters
config (Optional[RunnableConfig]) – 
Return type
List[BasePromptTemplate]
get_token_ids(text: str) → List[int]¶
Return the ordered ids of the tokens in a text.
Parameters
text (str) – The string input to tokenize.
Returns
A list of ids corresponding to the tokens in the text, in order they occurin the text.
Return type
List[int]
invoke(input: LanguageModelInput, config: Optional[RunnableConfig] = None, *, stop: Optional[List[str]] = None, **kwargs: Any) → BaseMessage¶
Transform a single input into an output. Override to implement.
Parameters
input (LanguageModelInput) – 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.
stop (Optional[List[str]]) – 
kwargs (Any) – 
Returns
The output of the runnable.
Return type
BaseMessage
classmethod is_lc_serializable() → bool¶
Is this class serializable?
Return type
bool
count = 0
def _lambda(x: int) -> None:
    global count
    count = count + 1
    if x == 1:
        raise ValueError("x is 1")
    else:
         pass
runnable = RunnableLambda(_lambda)
try:
    runnable.with_retry(
        stop_after_attempt=2,
        retry_if_exception_type=(ValueError,),
    ).invoke(1)
except ValueError:
    pass
assert (count == 2)
Parameters
retry_if_exception_type (Tuple[Type[BaseException], ...]) – A tuple of exception types to retry on
wait_exponential_jitter (bool) – Whether to add jitter to the wait time
between retries
stop_after_attempt (int) – The maximum number of attempts to make before giving up
Returns
A new Runnable that retries the original runnable on exceptions.
Return type
Runnable[Input, Output]
with_structured_output(schema: Union[Dict[str, Any], Type[BaseModel]], *, method: Literal['function_calling', 'json_mode'] = 'function_calling', return_type: Literal['parsed', 'all'] = 'parsed', **kwargs: Any) → Runnable[Union[PromptValue, str, Sequence[Union[BaseMessage, List[str], Tuple[str, str], str, Dict[str, Any]]]], Union[Dict, BaseModel]][source]¶
Not implemented on this class.
Parameters
schema (Union[Dict[str, Any], Type[BaseModel]]) – 
method (Literal['function_calling', 'json_mode']) – 
return_type (Literal['parsed', 'all']) – 
kwargs (Any) – 
Return type
kwargs (Any) – 
Return type
Runnable[Union[PromptValue, str, Sequence[Union[BaseMessage, List[str], Tuple[str, str], str, Dict[str, Any]]]], Union[Dict, BaseModel]]
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: TypeAlias¶
Get the input type for this runnable.
property OutputType: Any¶
Get the output type for this runnable.
property available_models: List[Model]¶
Get a list of available models that work with ChatNVIDIA.
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.
langchain_community.chat_models.premai.create_prem_retry_decorator¶
langchain_community.chat_models.premai.create_prem_retry_decorator(llm: ChatPremAI, *, max_retries: int = 1, run_manager: Optional[CallbackManagerForLLMRun] = None) → Callable[[Any], Any][source]¶
Create a retry decorator for PremAI API errors.
Parameters
llm (ChatPremAI) – 
max_retries (int) – 
run_manager (Optional[CallbackManagerForLLMRun]) – 
Return type
Callable[[Any], Any]