Tony20100/langchain_full huggingface dataset:
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[source]¶
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][source]¶
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
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 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[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
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 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
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
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.zhipuai.aconnect_sse¶
langchain_community.chat_models.zhipuai.aconnect_sse(client: Any, method: str, url: str, **kwargs: Any) → AsyncIterator[source]¶
Parameters
client (Any) – 
method (str) – 
url (str) – 
kwargs (Any) – 
Return type
AsyncIterator
langchain_cohere.chat_models.get_cohere_chat_request¶
langchain_cohere.chat_models.get_cohere_chat_request(messages: List[BaseMessage], *, documents: Optional[List[Document]] = None, connectors: Optional[List[Dict[str, str]]] = None, stop_sequences: Optional[List[str]] = None, **kwargs: Any) → Dict[str, Any][source]¶
Get the request for the Cohere chat API.
Parameters
messages (List[BaseMessage]) – The messages.
connectors (Optional[List[Dict[str, str]]]) – The connectors.
**kwargs (Any) – The keyword arguments.
documents (Optional[List[Document]]) – 
stop_sequences (Optional[List[str]]) – 
**kwargs – 
Returns
The request for the Cohere chat API.
Return type
Dict[str, Any]
langchain_experimental.chat_models.llm_wrapper.Llama2Chat¶
class langchain_experimental.chat_models.llm_wrapper.Llama2Chat[source]¶
Bases: ChatWrapper
Wrapper for Llama-2-chat model.
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 ai_n_beg: str = ' '¶
param ai_n_end: str = ' 
'¶
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 llm: LLM [Required]¶
param metadata: Optional[Dict[str, Any]] = None¶
Metadata to add to the run trace.
param sys_beg: str = '

 
\n'¶
param sys_end: str = '\n
\n\n'¶
param sys_end: str = '\n
\n\n'¶
param system_message: SystemMessage = SystemMessage(content="You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.\n\nIf a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.")¶
param tags: Optional[List[str]] = None¶
Tags to add to the run trace.
param usr_0_beg: str = ''¶
param usr_0_end: str = ' 
'¶
param usr_n_beg: str = '

 '¶
param usr_n_end: str = ' 
'¶
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
messages (List[BaseMessage]) – 
stop (Optional[List[str]]) – 
callbacks (Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]]) – 
kwargs (Any) – 
Return type
BaseMessage
async abatch(inputs: List[Input], config: Optional[Union[RunnableConfig, List[RunnableConfig]]] = None, *, return_exceptions: bool = False, **kwargs: Optional[Any]) → List[Output]¶
Not implemented on this class.
Parameters
schema (Union[Dict, Type[BaseModel]]) – 
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 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 Llama2Chat¶
Llama2Chat
langchain_community.chat_models.mlflow_ai_gateway.ChatMLflowAIGateway¶
class langchain_community.chat_models.mlflow_ai_gateway.ChatMLflowAIGateway[source]¶
Bases: BaseChatModel
MLflow AI Gateway chat models API.
To use, you should have the mlflow[gateway] python package installed.
For more information, see https://mlflow.org/docs/latest/gateway/index.html.
Example
from langchain_community.chat_models import ChatMLflowAIGateway
chat = ChatMLflowAIGateway(
    gateway_uri="<your-mlflow-ai-gateway-uri>",
    route="<your-mlflow-ai-gateway-chat-route>",
    params={
        "temperature": 0.1
    }
)
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 gateway_uri: Optional[str] = None¶
param metadata: Optional[Dict[str, Any]] = None¶
Metadata to add to the run trace.
param params: Optional[ChatParams] = None¶
param params: Optional[ChatParams] = None¶
param route: str [Required]¶
param tags: Optional[List[str]] = None¶
Tags to add to the run trace.
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
messages (List[BaseMessage]) – 
stop (Optional[List[str]]) – 
callbacks (Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]]) – 
kwargs (Any) – 
Return type
BaseMessage
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]]]¶
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 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 ChatMLflowAIGateway¶
MLflow AI Gateway
langchain_openai.chat_models.base.ChatOpenAI¶
class langchain_openai.chat_models.base.ChatOpenAI[source]¶
Bases: BaseChatOpenAI
OpenAI chat model integration.
Setup:Install langchain-openai and set environment variable OPENAI_API_KEY.
pip install -U langchain-openai
export OPENAI_API_KEY="your-api-key"
Key init args — completion params:
model: strName of OpenAI model to use.
temperature: floatSampling temperature.
max_tokens: Optional[int]Max number of tokens to generate.
logprobs: Optional[bool]Whether to return logprobs.
stream_options: DictConfigure streaming outputs, like whether to return token usage when
streaming ({"include_usage": True}).
Key init args — client params:
timeout: Union[float, Tuple[float, float], Any, None]Timeout for requests.
max_retries: intMax number of retries.
api_key: Optional[str]OpenAI API key. If not passed in will be read from env var OPENAI_API_KEY.
base_url: Optional[str]Base URL for API requests. Only specify if using a proxy or service
emulator.
organization: Optional[str]OpenAI organization ID. If not passed in will be read from env
var OPENAI_ORG_ID.
See full list of supported init args and their descriptions in the params section.
Instantiate:from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
    model="gpt-4o",
    temperature=0,
    max_tokens=None,
    timeout=None,
    max_retries=2,
    # api_key="...",
    # base_url="...",
    # organization="...",
    # other params...
)
NOTE: Any param which is not explicitly supported will be passed directly to the
)
NOTE: Any param which is not explicitly supported will be passed directly to the
openai.OpenAI.chat.completions.create(...) API every time to the model is
invoked. For example:
from langchain_openai import ChatOpenAI
import openai
ChatOpenAI(..., frequency_penalty=0.2).invoke(...)
# results in underlying API call of:
openai.OpenAI(..).chat.completions.create(..., frequency_penalty=0.2)
# which is also equivalent to:
ChatOpenAI(...).invoke(..., frequency_penalty=0.2)
Invoke:messages = [
    ("system", "You are a helpful translator. Translate the user sentence to French."),
    ("human", "I love programming."),
]
llm.invoke(messages)
AIMessage(content="J'adore la programmation.", response_metadata={'token_usage': {'completion_tokens': 5, 'prompt_tokens': 31, 'total_tokens': 36}, 'model_name': 'gpt-4o', 'system_fingerprint': 'fp_43dfabdef1', 'finish_reason': 'stop', 'logprobs': None}, id='run-012cffe2-5d3d-424d-83b5-51c6d4a593d1-0', usage_metadata={'input_tokens': 31, 'output_tokens': 5, 'total_tokens': 36})
Stream:for chunk in llm.stream(messages):
    print(chunk)
AIMessageChunk(content='', id='run-9e1517e3-12bf-48f2-bb1b-2e824f7cd7b0')
AIMessageChunk(content='J', id='run-9e1517e3-12bf-48f2-bb1b-2e824f7cd7b0')
AIMessageChunk(content="'adore", id='run-9e1517e3-12bf-48f2-bb1b-2e824f7cd7b0')
AIMessageChunk(content=' la', id='run-9e1517e3-12bf-48f2-bb1b-2e824f7cd7b0')
AIMessageChunk(content=' programmation', id='run-9e1517e3-12bf-48f2-bb1b-2e824f7cd7b0')
AIMessageChunk(content='.', id='run-9e1517e3-12bf-48f2-bb1b-2e824f7cd7b0')
AIMessageChunk(content='', response_metadata={'finish_reason': 'stop'}, id='run-9e1517e3-12bf-48f2-bb1b-2e824f7cd7b0')
stream = llm.stream(messages)
full = next(stream)
for chunk in stream:
    full += chunk
full
AIMessageChunk(content="J'adore la programmation.", response_metadata={'finish_reason': 'stop'}, id='run-bf917526-7f58-4683-84f7-36a6b671d140')
Async:await llm.ainvoke(messages)
# stream:
# async for chunk in (await llm.astream(messages))
# batch:
# await llm.abatch([messages])
# batch:
# await llm.abatch([messages])
AIMessage(content="J'adore la programmation.", response_metadata={'token_usage': {'completion_tokens': 5, 'prompt_tokens': 31, 'total_tokens': 36}, 'model_name': 'gpt-4o', 'system_fingerprint': 'fp_43dfabdef1', 'finish_reason': 'stop', 'logprobs': None}, id='run-012cffe2-5d3d-424d-83b5-51c6d4a593d1-0', usage_metadata={'input_tokens': 31, 'output_tokens': 5, 'total_tokens': 36})
Tool calling:from langchain_core.pydantic_v1 import BaseModel, Field
class GetWeather(BaseModel):
    '''Get the current weather in a given location'''
    location: str = Field(..., description="The city and state, e.g. San Francisco, CA")
class GetPopulation(BaseModel):
    '''Get the current population in a given location'''
    location: str = Field(..., description="The city and state, e.g. San Francisco, CA")
llm_with_tools = llm.bind_tools([GetWeather, GetPopulation])
ai_msg = llm_with_tools.invoke("Which city is hotter today and which is bigger: LA or NY?")
ai_msg.tool_calls
[{'name': 'GetWeather',
  'args': {'location': 'Los Angeles, CA'},
  'id': 'call_6XswGD5Pqk8Tt5atYr7tfenU'},
 {'name': 'GetWeather',
  'args': {'location': 'New York, NY'},
  'id': 'call_ZVL15vA8Y7kXqOy3dtmQgeCi'},
{'name': 'GetPopulation',
  'args': {'location': 'Los Angeles, CA'},
  'id': 'call_49CFW8zqC9W7mh7hbMLSIrXw'},
 {'name': 'GetPopulation',
  'args': {'location': 'New York, NY'},
  'id': 'call_6ghfKxV264jEfe1mRIkS3PE7'}]
See ChatOpenAI.bind_tools() method for more.
Structured output:from typing import Optional
from langchain_core.pydantic_v1 import BaseModel, Field
class Joke(BaseModel):
    '''Joke to tell user.'''
    setup: str = Field(description="The setup of the joke")
    punchline: str = Field(description="The punchline to the joke")
    rating: Optional[int] = Field(description="How funny the joke is, from 1 to 10")
structured_llm = llm.with_structured_output(Joke)
structured_llm.invoke("Tell me a joke about cats")
Joke(setup='Why was the cat sitting on the computer?', punchline='To keep an eye on the mouse!', rating=None)
See ChatOpenAI.with_structured_output() for more.
JSON mode:json_llm = llm.bind(response_format={"type": "json_object"})
ai_msg = json_llm.invoke("Return a JSON object with key 'random_ints' and a value of 10 random ints in [0-99]")
ai_msg.content
'\n{\n  "random_ints": [23, 87, 45, 12, 78, 34, 56, 90, 11, 67]\n}'
Image input:import base64
import httpx
from langchain_core.messages import HumanMessage