From 1d2273597a5aa8dda7d1785bd44030e00d78539c Mon Sep 17 00:00:00 2001 From: Mason Daugherty Date: Thu, 16 Oct 2025 22:54:16 -0400 Subject: [PATCH] docs: more fixes for refs (#33554) --- .../integration_template/chat_models.py | 12 +- .../integration_template/vectorstores.py | 8 +- libs/core/langchain_core/indexing/base.py | 8 +- .../langchain_core/language_models/base.py | 11 +- .../language_models/chat_models.py | 164 +++--- .../langchain_core/language_models/llms.py | 12 +- .../block_translators/google_genai.py | 2 +- libs/core/langchain_core/messages/utils.py | 4 +- libs/core/langchain_core/runnables/base.py | 23 +- .../langchain_core/runnables/fallbacks.py | 2 +- libs/core/langchain_core/runnables/retry.py | 2 +- libs/core/langchain_core/runnables/schema.py | 2 +- libs/core/langchain_core/tools/base.py | 2 +- libs/core/langchain_core/vectorstores/base.py | 118 ++--- .../core/langchain_core/vectorstores/utils.py | 2 +- .../agent_toolkits/vectorstore/toolkit.py | 4 +- .../retrievers/parent_document_retriever.py | 4 +- .../retrievers/time_weighted_retriever.py | 2 +- .../langchain_anthropic/chat_models.py | 466 +++++++++--------- .../chroma/langchain_chroma/vectorstores.py | 38 +- .../langchain_deepseek/chat_models.py | 53 +- .../langchain_fireworks/chat_models.py | 324 ++++++------ .../langchain_fireworks/embeddings.py | 4 +- .../groq/langchain_groq/chat_models.py | 303 ++++++------ .../chat_models/huggingface.py | 44 +- .../langchain_mistralai/chat_models.py | 286 +++++------ .../langchain_mistralai/embeddings.py | 14 +- .../nomic/langchain_nomic/embeddings.py | 2 +- .../ollama/langchain_ollama/chat_models.py | 37 +- libs/partners/ollama/langchain_ollama/llms.py | 6 +- .../langchain_openai/chat_models/azure.py | 56 ++- .../langchain_openai/chat_models/base.py | 72 +-- .../langchain_openai/embeddings/azure.py | 13 +- .../langchain_openai/embeddings/base.py | 12 +- .../openai/langchain_openai/llms/base.py | 56 +-- .../langchain_perplexity/chat_models.py | 39 +- .../langchain_qdrant/fastembed_sparse.py | 2 +- .../qdrant/langchain_qdrant/qdrant.py | 30 +- .../qdrant/langchain_qdrant/vectorstores.py | 64 +-- .../partners/xai/langchain_xai/chat_models.py | 236 ++++----- .../integration_tests/vectorstores.py | 4 +- 41 files changed, 1305 insertions(+), 1238 deletions(-) diff --git a/libs/cli/langchain_cli/integration_template/integration_template/chat_models.py b/libs/cli/langchain_cli/integration_template/integration_template/chat_models.py index fedf46fb95d..740da85137e 100644 --- a/libs/cli/langchain_cli/integration_template/integration_template/chat_models.py +++ b/libs/cli/langchain_cli/integration_template/integration_template/chat_models.py @@ -36,20 +36,20 @@ class Chat__ModuleName__(BaseChatModel): # TODO: Populate with relevant params. Key init args — completion params: - model: str + model: Name of __ModuleName__ model to use. - temperature: float + temperature: Sampling temperature. - max_tokens: int | None + max_tokens: Max number of tokens to generate. # TODO: Populate with relevant params. Key init args — client params: - timeout: float | None + timeout: Timeout for requests. - max_retries: int + max_retries: Max number of retries. - api_key: str | None + api_key: __ModuleName__ API key. If not passed in will be read from env var __MODULE_NAME___API_KEY. diff --git a/libs/cli/langchain_cli/integration_template/integration_template/vectorstores.py b/libs/cli/langchain_cli/integration_template/integration_template/vectorstores.py index 9969932f8da..48dc5980102 100644 --- a/libs/cli/langchain_cli/integration_template/integration_template/vectorstores.py +++ b/libs/cli/langchain_cli/integration_template/integration_template/vectorstores.py @@ -37,16 +37,16 @@ class __ModuleName__VectorStore(VectorStore): # TODO: Populate with relevant params. Key init args — indexing params: - collection_name: str + collection_name: Name of the collection. - embedding_function: Embeddings + embedding_function: Embedding function to use. # TODO: Populate with relevant params. Key init args — client params: - client: Client | None + client: Client to use. - connection_args: dict | None + connection_args: Connection arguments. # TODO: Replace with relevant init params. diff --git a/libs/core/langchain_core/indexing/base.py b/libs/core/langchain_core/indexing/base.py index aa4f991686e..810858d7a8b 100644 --- a/libs/core/langchain_core/indexing/base.py +++ b/libs/core/langchain_core/indexing/base.py @@ -522,10 +522,10 @@ class DocumentIndex(BaseRetriever): When an ID is specified and the content already exists in the vectorstore, the upsert method should update the content with the new data. If the content - does not exist, the upsert method should add the item to the vectorstore. + does not exist, the upsert method should add the item to the `VectorStore`. Args: - items: Sequence of documents to add to the vectorstore. + items: Sequence of documents to add to the `VectorStore`. **kwargs: Additional keyword arguments. Returns: @@ -545,10 +545,10 @@ class DocumentIndex(BaseRetriever): When an ID is specified and the item already exists in the vectorstore, the upsert method should update the item with the new data. If the item - does not exist, the upsert method should add the item to the vectorstore. + does not exist, the upsert method should add the item to the `VectorStore`. Args: - items: Sequence of documents to add to the vectorstore. + items: Sequence of documents to add to the `VectorStore`. **kwargs: Additional keyword arguments. Returns: diff --git a/libs/core/langchain_core/language_models/base.py b/libs/core/langchain_core/language_models/base.py index 7091ccc5217..621f1da5b7f 100644 --- a/libs/core/langchain_core/language_models/base.py +++ b/libs/core/langchain_core/language_models/base.py @@ -200,14 +200,14 @@ class BaseLanguageModel( pure text generation models and `BaseMessage` objects for chat models). stop: Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings. - callbacks: Callbacks to pass through. Used for executing additional + callbacks: `Callbacks` to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation. **kwargs: Arbitrary additional keyword arguments. These are usually passed to the model provider API call. Returns: An `LLMResult`, which contains a list of candidate `Generation` objects for - each input prompt and additional model provider-specific output. + each input prompt and additional model provider-specific output. """ @@ -237,14 +237,14 @@ class BaseLanguageModel( pure text generation models and `BaseMessage` objects for chat models). stop: Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings. - callbacks: Callbacks to pass through. Used for executing additional + callbacks: `Callbacks` to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation. **kwargs: Arbitrary additional keyword arguments. These are usually passed to the model provider API call. Returns: An `LLMResult`, which contains a list of candidate `Generation` objects for - each input prompt and additional model provider-specific output. + each input prompt and additional model provider-specific output. """ @@ -269,8 +269,7 @@ class BaseLanguageModel( Returns: A list of ids corresponding to the tokens in the text, in order they occur - in the text. - + in the text. """ if self.custom_get_token_ids is not None: return self.custom_get_token_ids(text) diff --git a/libs/core/langchain_core/language_models/chat_models.py b/libs/core/langchain_core/language_models/chat_models.py index 291bd833f2d..76fb4e8a753 100644 --- a/libs/core/langchain_core/language_models/chat_models.py +++ b/libs/core/langchain_core/language_models/chat_models.py @@ -838,13 +838,13 @@ class BaseChatModel(BaseLanguageModel[AIMessage], ABC): 1. Take advantage of batched calls, 2. Need more output from the model than just the top generated value, 3. Are building chains that are agnostic to the underlying language model - type (e.g., pure text completion models vs chat models). + type (e.g., pure text completion models vs chat models). Args: messages: List of list of messages. stop: Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings. - callbacks: Callbacks to pass through. Used for executing additional + callbacks: `Callbacks` to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation. tags: The tags to apply. metadata: The metadata to apply. @@ -854,8 +854,8 @@ class BaseChatModel(BaseLanguageModel[AIMessage], ABC): to the model provider API call. Returns: - An LLMResult, which contains a list of candidate Generations for each input - prompt and additional model provider-specific output. + An `LLMResult`, which contains a list of candidate `Generations` for each + input prompt and additional model provider-specific output. """ ls_structured_output_format = kwargs.pop( @@ -956,13 +956,13 @@ class BaseChatModel(BaseLanguageModel[AIMessage], ABC): 1. Take advantage of batched calls, 2. Need more output from the model than just the top generated value, 3. Are building chains that are agnostic to the underlying language model - type (e.g., pure text completion models vs chat models). + type (e.g., pure text completion models vs chat models). Args: messages: List of list of messages. stop: Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings. - callbacks: Callbacks to pass through. Used for executing additional + callbacks: `Callbacks` to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation. tags: The tags to apply. metadata: The metadata to apply. @@ -972,8 +972,8 @@ class BaseChatModel(BaseLanguageModel[AIMessage], ABC): to the model provider API call. Returns: - An LLMResult, which contains a list of candidate Generations for each input - prompt and additional model provider-specific output. + An `LLMResult`, which contains a list of candidate `Generations` for each + input prompt and additional model provider-specific output. """ ls_structured_output_format = kwargs.pop( @@ -1510,17 +1510,21 @@ class BaseChatModel(BaseLanguageModel[AIMessage], ABC): If `schema` is a Pydantic class then the model output will be a Pydantic instance of that class, and the model-generated fields will be validated by the Pydantic class. Otherwise the model output will be a - dict and will not be validated. See `langchain_core.utils.function_calling.convert_to_openai_tool` - for more on how to properly specify types and descriptions of - schema fields when specifying a Pydantic or `TypedDict` class. + dict and will not be validated. + + See `langchain_core.utils.function_calling.convert_to_openai_tool` for + more on how to properly specify types and descriptions of schema fields + when specifying a Pydantic or `TypedDict` class. include_raw: If `False` then only the parsed structured output is returned. If an error occurs during model output parsing it will be raised. If `True` then both the raw model response (a `BaseMessage`) and the parsed model response will be returned. If an error occurs during output parsing it - will be caught and returned as well. The final output is always a dict - with keys `'raw'`, `'parsed'`, and `'parsing_error'`. + will be caught and returned as well. + + The final output is always a `dict` with keys `'raw'`, `'parsed'`, and + `'parsing_error'`. Raises: ValueError: If there are any unsupported `kwargs`. @@ -1528,97 +1532,99 @@ class BaseChatModel(BaseLanguageModel[AIMessage], ABC): `with_structured_output()`. Returns: - A Runnable that takes same inputs as a `langchain_core.language_models.chat.BaseChatModel`. + A `Runnable` that takes same inputs as a + `langchain_core.language_models.chat.BaseChatModel`. If `include_raw` is + `False` and `schema` is a Pydantic class, `Runnable` outputs an instance + of `schema` (i.e., a Pydantic object). Otherwise, if `include_raw` is + `False` then `Runnable` outputs a `dict`. - If `include_raw` is False and `schema` is a Pydantic class, Runnable outputs - an instance of `schema` (i.e., a Pydantic object). + If `include_raw` is `True`, then `Runnable` outputs a `dict` with keys: - Otherwise, if `include_raw` is False then Runnable outputs a dict. - - If `include_raw` is True, then Runnable outputs a dict with keys: - - - `'raw'`: BaseMessage - - `'parsed'`: None if there was a parsing error, otherwise the type + - `'raw'`: `BaseMessage` + - `'parsed'`: `None` if there was a parsing error, otherwise the type depends on the `schema` as described above. - - `'parsing_error'`: BaseException | None + - `'parsing_error'`: `BaseException | None` - Example: Pydantic schema (include_raw=False): - ```python - from pydantic import BaseModel + Example: Pydantic schema (`include_raw=False`): + + ```python + from pydantic import BaseModel - class AnswerWithJustification(BaseModel): - '''An answer to the user question along with justification for the answer.''' + class AnswerWithJustification(BaseModel): + '''An answer to the user question along with justification for the answer.''' - answer: str - justification: str + answer: str + justification: str - model = ChatModel(model="model-name", temperature=0) - structured_model = model.with_structured_output(AnswerWithJustification) + model = ChatModel(model="model-name", temperature=0) + structured_model = model.with_structured_output(AnswerWithJustification) - structured_model.invoke( - "What weighs more a pound of bricks or a pound of feathers" - ) + structured_model.invoke( + "What weighs more a pound of bricks or a pound of feathers" + ) - # -> AnswerWithJustification( - # answer='They weigh the same', - # justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.' - # ) - ``` + # -> AnswerWithJustification( + # answer='They weigh the same', + # justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.' + # ) + ``` - Example: Pydantic schema (include_raw=True): - ```python - from pydantic import BaseModel + Example: Pydantic schema (`include_raw=True`): + + ```python + from pydantic import BaseModel - class AnswerWithJustification(BaseModel): - '''An answer to the user question along with justification for the answer.''' + class AnswerWithJustification(BaseModel): + '''An answer to the user question along with justification for the answer.''' - answer: str - justification: str + answer: str + justification: str - model = ChatModel(model="model-name", temperature=0) - structured_model = model.with_structured_output( - AnswerWithJustification, include_raw=True - ) + model = ChatModel(model="model-name", temperature=0) + structured_model = model.with_structured_output( + AnswerWithJustification, include_raw=True + ) - structured_model.invoke( - "What weighs more a pound of bricks or a pound of feathers" - ) - # -> { - # 'raw': AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_Ao02pnFYXD6GN1yzc0uXPsvF', 'function': {'arguments': '{"answer":"They weigh the same.","justification":"Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ."}', 'name': 'AnswerWithJustification'}, 'type': 'function'}]}), - # 'parsed': AnswerWithJustification(answer='They weigh the same.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'), - # 'parsing_error': None - # } - ``` + structured_model.invoke( + "What weighs more a pound of bricks or a pound of feathers" + ) + # -> { + # 'raw': AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_Ao02pnFYXD6GN1yzc0uXPsvF', 'function': {'arguments': '{"answer":"They weigh the same.","justification":"Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ."}', 'name': 'AnswerWithJustification'}, 'type': 'function'}]}), + # 'parsed': AnswerWithJustification(answer='They weigh the same.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'), + # 'parsing_error': None + # } + ``` - Example: Dict schema (include_raw=False): - ```python - from pydantic import BaseModel - from langchain_core.utils.function_calling import convert_to_openai_tool + Example: `dict` schema (`include_raw=False`): + + ```python + from pydantic import BaseModel + from langchain_core.utils.function_calling import convert_to_openai_tool - class AnswerWithJustification(BaseModel): - '''An answer to the user question along with justification for the answer.''' + class AnswerWithJustification(BaseModel): + '''An answer to the user question along with justification for the answer.''' - answer: str - justification: str + answer: str + justification: str - dict_schema = convert_to_openai_tool(AnswerWithJustification) - model = ChatModel(model="model-name", temperature=0) - structured_model = model.with_structured_output(dict_schema) + dict_schema = convert_to_openai_tool(AnswerWithJustification) + model = ChatModel(model="model-name", temperature=0) + structured_model = model.with_structured_output(dict_schema) - structured_model.invoke( - "What weighs more a pound of bricks or a pound of feathers" - ) - # -> { - # 'answer': 'They weigh the same', - # 'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.' - # } - ``` + structured_model.invoke( + "What weighs more a pound of bricks or a pound of feathers" + ) + # -> { + # 'answer': 'They weigh the same', + # 'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.' + # } + ``` !!! warning "Behavior changed in 0.2.26" Added support for TypedDict class. diff --git a/libs/core/langchain_core/language_models/llms.py b/libs/core/langchain_core/language_models/llms.py index bd30493c9a4..81a390c4069 100644 --- a/libs/core/langchain_core/language_models/llms.py +++ b/libs/core/langchain_core/language_models/llms.py @@ -841,7 +841,7 @@ class BaseLLM(BaseLanguageModel[str], ABC): prompts: List of string prompts. stop: Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings. - callbacks: Callbacks to pass through. Used for executing additional + callbacks: `Callbacks` to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation. tags: List of tags to associate with each prompt. If provided, the length of the list must match the length of the prompts list. @@ -861,8 +861,8 @@ class BaseLLM(BaseLanguageModel[str], ABC): `run_name` (if provided) does not match the length of prompts. Returns: - An LLMResult, which contains a list of candidate Generations for each input - prompt and additional model provider-specific output. + An `LLMResult`, which contains a list of candidate `Generations` for each + input prompt and additional model provider-specific output. """ if not isinstance(prompts, list): msg = ( @@ -1111,7 +1111,7 @@ class BaseLLM(BaseLanguageModel[str], ABC): prompts: List of string prompts. stop: Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings. - callbacks: Callbacks to pass through. Used for executing additional + callbacks: `Callbacks` to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation. tags: List of tags to associate with each prompt. If provided, the length of the list must match the length of the prompts list. @@ -1130,8 +1130,8 @@ class BaseLLM(BaseLanguageModel[str], ABC): `run_name` (if provided) does not match the length of prompts. Returns: - An LLMResult, which contains a list of candidate Generations for each input - prompt and additional model provider-specific output. + An `LLMResult`, which contains a list of candidate `Generations` for each + input prompt and additional model provider-specific output. """ if isinstance(metadata, list): metadata = [ diff --git a/libs/core/langchain_core/messages/block_translators/google_genai.py b/libs/core/langchain_core/messages/block_translators/google_genai.py index aff955100b5..d57e8dc1a15 100644 --- a/libs/core/langchain_core/messages/block_translators/google_genai.py +++ b/libs/core/langchain_core/messages/block_translators/google_genai.py @@ -282,7 +282,7 @@ def _convert_to_v1_from_genai(message: AIMessage) -> list[types.ContentBlock]: standard content blocks for returning. Args: - message: The AIMessage or AIMessageChunk to convert. + message: The `AIMessage` or `AIMessageChunk` to convert. Returns: List of standard content blocks derived from the message content. diff --git a/libs/core/langchain_core/messages/utils.py b/libs/core/langchain_core/messages/utils.py index c5bdf2fe6b9..c9eab08da15 100644 --- a/libs/core/langchain_core/messages/utils.py +++ b/libs/core/langchain_core/messages/utils.py @@ -1678,11 +1678,11 @@ def count_tokens_approximately( messages: List of messages to count tokens for. chars_per_token: Number of characters per token to use for the approximation. One token corresponds to ~4 chars for common English text. - You can also specify float values for more fine-grained control. + You can also specify `float` values for more fine-grained control. [See more here](https://platform.openai.com/tokenizer). extra_tokens_per_message: Number of extra tokens to add per message, e.g. special tokens, including beginning/end of message. - You can also specify float values for more fine-grained control. + You can also specify `float` values for more fine-grained control. [See more here](https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb). count_name: Whether to include message names in the count. Enabled by default. diff --git a/libs/core/langchain_core/runnables/base.py b/libs/core/langchain_core/runnables/base.py index d56f663c80d..a99845232c0 100644 --- a/libs/core/langchain_core/runnables/base.py +++ b/libs/core/langchain_core/runnables/base.py @@ -1367,8 +1367,8 @@ class Runnable(ABC, Generic[Input, Output]): events = [event async for event in chain.astream_events("hello", version="v2")] - # will produce the following events (run_id, and parent_ids - # has been omitted for brevity): + # Will produce the following events + # (run_id, and parent_ids has been omitted for brevity): [ { "data": {"input": "hello"}, @@ -1423,7 +1423,7 @@ class Runnable(ABC, Generic[Input, Output]): async for event in slow_thing.astream_events("some_input", version="v2"): print(event) - `` + ``` Args: input: The input to the `Runnable`. @@ -1813,7 +1813,7 @@ class Runnable(ABC, Generic[Input, Output]): output_type: The output type to bind to the `Runnable`. Returns: - A new Runnable with the types bound. + A new `Runnable` with the types bound. """ return RunnableBinding( bound=self, @@ -1840,7 +1840,7 @@ class Runnable(ABC, Generic[Input, Output]): giving up. exponential_jitter_params: Parameters for `tenacity.wait_exponential_jitter`. Namely: `initial`, `max`, - `exp_base`, and `jitter` (all float values). + `exp_base`, and `jitter` (all `float` values). Returns: A new Runnable that retries the original Runnable on exceptions. @@ -1925,15 +1925,15 @@ class Runnable(ABC, Generic[Input, Output]): fallbacks: A sequence of runnables to try if the original `Runnable` fails. exceptions_to_handle: A tuple of exception types to handle. - exception_key: If string is specified then handled exceptions will be passed - to fallbacks as part of the input under the specified key. + exception_key: If `string` is specified then handled exceptions will be + passed to fallbacks as part of the input under the specified key. If `None`, exceptions will not be passed to fallbacks. If used, the base `Runnable` and its fallbacks must accept a dictionary as input. Returns: A new `Runnable` that will try the original `Runnable`, and then each - Fallback in order, upon failures. + Fallback in order, upon failures. Example: ```python @@ -1961,16 +1961,15 @@ class Runnable(ABC, Generic[Input, Output]): fallbacks: A sequence of runnables to try if the original `Runnable` fails. exceptions_to_handle: A tuple of exception types to handle. - exception_key: If string is specified then handled exceptions will be passed - to fallbacks as part of the input under the specified key. + exception_key: If `string` is specified then handled exceptions will be + passed to fallbacks as part of the input under the specified key. If `None`, exceptions will not be passed to fallbacks. If used, the base `Runnable` and its fallbacks must accept a dictionary as input. Returns: A new `Runnable` that will try the original `Runnable`, and then each - Fallback in order, upon failures. - + Fallback in order, upon failures. """ # Import locally to prevent circular import from langchain_core.runnables.fallbacks import ( # noqa: PLC0415 diff --git a/libs/core/langchain_core/runnables/fallbacks.py b/libs/core/langchain_core/runnables/fallbacks.py index 32f05846950..2e76a9b81f6 100644 --- a/libs/core/langchain_core/runnables/fallbacks.py +++ b/libs/core/langchain_core/runnables/fallbacks.py @@ -96,7 +96,7 @@ class RunnableWithFallbacks(RunnableSerializable[Input, Output]): Any exception that is not a subclass of these exceptions will be raised immediately. """ exception_key: str | None = None - """If string is specified then handled exceptions will be passed to fallbacks as + """If `string` is specified then handled exceptions will be passed to fallbacks as part of the input under the specified key. If `None`, exceptions will not be passed to fallbacks. If used, the base Runnable and its fallbacks must accept a dictionary as input.""" diff --git a/libs/core/langchain_core/runnables/retry.py b/libs/core/langchain_core/runnables/retry.py index ed0adf8eaf7..03466718274 100644 --- a/libs/core/langchain_core/runnables/retry.py +++ b/libs/core/langchain_core/runnables/retry.py @@ -126,7 +126,7 @@ class RunnableRetry(RunnableBindingBase[Input, Output]): # type: ignore[no-rede exponential_jitter_params: ExponentialJitterParams | None = None """Parameters for `tenacity.wait_exponential_jitter`. Namely: `initial`, - `max`, `exp_base`, and `jitter` (all float values). + `max`, `exp_base`, and `jitter` (all `float` values). """ max_attempt_number: int = 3 diff --git a/libs/core/langchain_core/runnables/schema.py b/libs/core/langchain_core/runnables/schema.py index 53de98df284..4545650af83 100644 --- a/libs/core/langchain_core/runnables/schema.py +++ b/libs/core/langchain_core/runnables/schema.py @@ -65,7 +65,7 @@ class BaseStreamEvent(TypedDict): events = [event async for event in chain.astream_events("hello")] - # will produce the following events + # Will produce the following events # (where some fields have been omitted for brevity): [ { diff --git a/libs/core/langchain_core/tools/base.py b/libs/core/langchain_core/tools/base.py index 3849acfefa8..845b4e38578 100644 --- a/libs/core/langchain_core/tools/base.py +++ b/libs/core/langchain_core/tools/base.py @@ -615,7 +615,7 @@ class ChildTool(BaseTool): The parsed and validated input. Raises: - ValueError: If string input is provided with JSON schema `args_schema`. + ValueError: If `string` input is provided with JSON schema `args_schema`. ValueError: If InjectedToolCallId is required but `tool_call_id` is not provided. TypeError: If args_schema is not a Pydantic `BaseModel` or dict. diff --git a/libs/core/langchain_core/vectorstores/base.py b/libs/core/langchain_core/vectorstores/base.py index 0ce79dfa813..a64f61cd6a4 100644 --- a/libs/core/langchain_core/vectorstores/base.py +++ b/libs/core/langchain_core/vectorstores/base.py @@ -52,10 +52,10 @@ class VectorStore(ABC): ids: list[str] | None = None, **kwargs: Any, ) -> list[str]: - """Run more texts through the embeddings and add to the vectorstore. + """Run more texts through the embeddings and add to the `VectorStore`. Args: - texts: Iterable of strings to add to the vectorstore. + texts: Iterable of strings to add to the `VectorStore`. metadatas: Optional list of metadatas associated with the texts. ids: Optional list of IDs associated with the texts. **kwargs: vectorstore specific parameters. @@ -63,7 +63,7 @@ class VectorStore(ABC): associated with the texts. Returns: - List of ids from adding the texts into the vectorstore. + List of ids from adding the texts into the `VectorStore`. Raises: ValueError: If the number of metadatas does not match the number of texts. @@ -192,16 +192,16 @@ class VectorStore(ABC): ids: list[str] | None = None, **kwargs: Any, ) -> list[str]: - """Async run more texts through the embeddings and add to the vectorstore. + """Async run more texts through the embeddings and add to the `VectorStore`. Args: - texts: Iterable of strings to add to the vectorstore. + texts: Iterable of strings to add to the `VectorStore`. metadatas: Optional list of metadatas associated with the texts. ids: Optional list **kwargs: vectorstore specific parameters. Returns: - List of ids from adding the texts into the vectorstore. + List of ids from adding the texts into the `VectorStore`. Raises: ValueError: If the number of metadatas does not match the number of texts. @@ -237,7 +237,7 @@ class VectorStore(ABC): """Add or update documents in the vectorstore. Args: - documents: Documents to add to the vectorstore. + documents: Documents to add to the `VectorStore`. **kwargs: Additional keyword arguments. if kwargs contains ids and documents contain ids, the ids in the kwargs will receive precedence. @@ -266,10 +266,10 @@ class VectorStore(ABC): async def aadd_documents( self, documents: list[Document], **kwargs: Any ) -> list[str]: - """Async run more documents through the embeddings and add to the vectorstore. + """Async run more documents through the embeddings and add to the `VectorStore`. Args: - documents: Documents to add to the vectorstore. + documents: Documents to add to the `VectorStore`. **kwargs: Additional keyword arguments. Returns: @@ -301,7 +301,7 @@ class VectorStore(ABC): **kwargs: Arguments to pass to the search method. Returns: - List of Documents most similar to the query. + List of `Document` objects most similar to the query. Raises: ValueError: If search_type is not one of "similarity", @@ -335,7 +335,7 @@ class VectorStore(ABC): **kwargs: Arguments to pass to the search method. Returns: - List of Documents most similar to the query. + List of `Document` objects most similar to the query. Raises: ValueError: If search_type is not one of "similarity", @@ -368,7 +368,7 @@ class VectorStore(ABC): **kwargs: Arguments to pass to the search method. Returns: - List of Documents most similar to the query. + List of `Document` objects most similar to the query. """ @staticmethod @@ -423,7 +423,7 @@ class VectorStore(ABC): **kwargs: Arguments to pass to the search method. Returns: - List of Tuples of (doc, similarity_score). + List of Tuples of `(doc, similarity_score)`. """ raise NotImplementedError @@ -437,7 +437,7 @@ class VectorStore(ABC): **kwargs: Arguments to pass to the search method. Returns: - List of Tuples of (doc, similarity_score). + List of Tuples of `(doc, similarity_score)`. """ # This is a temporary workaround to make the similarity search # asynchronous. The proper solution is to make the similarity search @@ -455,19 +455,19 @@ class VectorStore(ABC): """Default similarity search with relevance scores. Modify if necessary in subclass. - Return docs and relevance scores in the range [0, 1]. + Return docs and relevance scores in the range `[0, 1]`. - 0 is dissimilar, 1 is most similar. + `0` is dissimilar, `1` is most similar. Args: query: Input text. k: Number of Documents to return. - **kwargs: kwargs to be passed to similarity search. Should include: - score_threshold: Optional, a floating point value between 0 to 1 to - filter the resulting set of retrieved docs + **kwargs: kwargs to be passed to similarity search. Should include + `score_threshold`, An optional floating point value between `0` to `1` + to filter the resulting set of retrieved docs Returns: - List of Tuples of (doc, similarity_score) + List of Tuples of `(doc, similarity_score)` """ relevance_score_fn = self._select_relevance_score_fn() docs_and_scores = self.similarity_search_with_score(query, k, **kwargs) @@ -482,19 +482,19 @@ class VectorStore(ABC): """Default similarity search with relevance scores. Modify if necessary in subclass. - Return docs and relevance scores in the range [0, 1]. + Return docs and relevance scores in the range `[0, 1]`. - 0 is dissimilar, 1 is most similar. + `0` is dissimilar, `1` is most similar. Args: query: Input text. k: Number of Documents to return. - **kwargs: kwargs to be passed to similarity search. Should include: - score_threshold: Optional, a floating point value between 0 to 1 to - filter the resulting set of retrieved docs + **kwargs: kwargs to be passed to similarity search. Should include + `score_threshold`, An optional floating point value between `0` to `1` + to filter the resulting set of retrieved docs Returns: - List of Tuples of (doc, similarity_score) + List of Tuples of `(doc, similarity_score)` """ relevance_score_fn = self._select_relevance_score_fn() docs_and_scores = await self.asimilarity_search_with_score(query, k, **kwargs) @@ -506,19 +506,19 @@ class VectorStore(ABC): k: int = 4, **kwargs: Any, ) -> list[tuple[Document, float]]: - """Return docs and relevance scores in the range [0, 1]. + """Return docs and relevance scores in the range `[0, 1]`. - 0 is dissimilar, 1 is most similar. + `0` is dissimilar, `1` is most similar. Args: query: Input text. k: Number of Documents to return. - **kwargs: kwargs to be passed to similarity search. Should include: - score_threshold: Optional, a floating point value between 0 to 1 to - filter the resulting set of retrieved docs. + **kwargs: kwargs to be passed to similarity search. Should include + `score_threshold`, An optional floating point value between `0` to `1` + to filter the resulting set of retrieved docs Returns: - List of Tuples of (doc, similarity_score). + List of Tuples of `(doc, similarity_score)`. """ score_threshold = kwargs.pop("score_threshold", None) @@ -555,19 +555,19 @@ class VectorStore(ABC): k: int = 4, **kwargs: Any, ) -> list[tuple[Document, float]]: - """Async return docs and relevance scores in the range [0, 1]. + """Async return docs and relevance scores in the range `[0, 1]`. - 0 is dissimilar, 1 is most similar. + `0` is dissimilar, `1` is most similar. Args: query: Input text. k: Number of Documents to return. - **kwargs: kwargs to be passed to similarity search. Should include: - score_threshold: Optional, a floating point value between 0 to 1 to - filter the resulting set of retrieved docs + **kwargs: kwargs to be passed to similarity search. Should include + `score_threshold`, An optional floating point value between `0` to `1` + to filter the resulting set of retrieved docs Returns: - List of Tuples of (doc, similarity_score) + List of Tuples of `(doc, similarity_score)` """ score_threshold = kwargs.pop("score_threshold", None) @@ -609,7 +609,7 @@ class VectorStore(ABC): **kwargs: Arguments to pass to the search method. Returns: - List of Documents most similar to the query. + List of `Document` objects most similar to the query. """ # This is a temporary workaround to make the similarity search # asynchronous. The proper solution is to make the similarity search @@ -627,7 +627,7 @@ class VectorStore(ABC): **kwargs: Arguments to pass to the search method. Returns: - List of Documents most similar to the query vector. + List of `Document` objects most similar to the query vector. """ raise NotImplementedError @@ -642,7 +642,7 @@ class VectorStore(ABC): **kwargs: Arguments to pass to the search method. Returns: - List of Documents most similar to the query vector. + List of `Document` objects most similar to the query vector. """ # This is a temporary workaround to make the similarity search # asynchronous. The proper solution is to make the similarity search @@ -674,7 +674,7 @@ class VectorStore(ABC): **kwargs: Arguments to pass to the search method. Returns: - List of Documents selected by maximal marginal relevance. + List of `Document` objects selected by maximal marginal relevance. """ raise NotImplementedError @@ -701,7 +701,7 @@ class VectorStore(ABC): **kwargs: Arguments to pass to the search method. Returns: - List of Documents selected by maximal marginal relevance. + List of `Document` objects selected by maximal marginal relevance. """ # This is a temporary workaround to make the similarity search # asynchronous. The proper solution is to make the similarity search @@ -739,7 +739,7 @@ class VectorStore(ABC): **kwargs: Arguments to pass to the search method. Returns: - List of Documents selected by maximal marginal relevance. + List of `Document` objects selected by maximal marginal relevance. """ raise NotImplementedError @@ -766,7 +766,7 @@ class VectorStore(ABC): **kwargs: Arguments to pass to the search method. Returns: - List of Documents selected by maximal marginal relevance. + List of `Document` objects selected by maximal marginal relevance. """ return await run_in_executor( None, @@ -785,15 +785,15 @@ class VectorStore(ABC): embedding: Embeddings, **kwargs: Any, ) -> Self: - """Return VectorStore initialized from documents and embeddings. + """Return `VectorStore` initialized from documents and embeddings. Args: - documents: List of Documents to add to the vectorstore. + documents: List of `Document` objects to add to the `VectorStore`. embedding: Embedding function to use. **kwargs: Additional keyword arguments. Returns: - VectorStore initialized from documents and embeddings. + `VectorStore` initialized from documents and embeddings. """ texts = [d.page_content for d in documents] metadatas = [d.metadata for d in documents] @@ -815,15 +815,15 @@ class VectorStore(ABC): embedding: Embeddings, **kwargs: Any, ) -> Self: - """Async return VectorStore initialized from documents and embeddings. + """Async return `VectorStore` initialized from documents and embeddings. Args: - documents: List of Documents to add to the vectorstore. + documents: List of `Document` objects to add to the `VectorStore`. embedding: Embedding function to use. **kwargs: Additional keyword arguments. Returns: - VectorStore initialized from documents and embeddings. + `VectorStore` initialized from documents and embeddings. """ texts = [d.page_content for d in documents] metadatas = [d.metadata for d in documents] @@ -852,7 +852,7 @@ class VectorStore(ABC): """Return VectorStore initialized from texts and embeddings. Args: - texts: Texts to add to the vectorstore. + texts: Texts to add to the `VectorStore`. embedding: Embedding function to use. metadatas: Optional list of metadatas associated with the texts. ids: Optional list of IDs associated with the texts. @@ -875,7 +875,7 @@ class VectorStore(ABC): """Async return VectorStore initialized from texts and embeddings. Args: - texts: Texts to add to the vectorstore. + texts: Texts to add to the `VectorStore`. embedding: Embedding function to use. metadatas: Optional list of metadatas associated with the texts. ids: Optional list of IDs associated with the texts. @@ -898,7 +898,7 @@ class VectorStore(ABC): return tags def as_retriever(self, **kwargs: Any) -> VectorStoreRetriever: - """Return VectorStoreRetriever initialized from this VectorStore. + """Return `VectorStoreRetriever` initialized from this `VectorStore`. Args: **kwargs: Keyword arguments to pass to the search function. @@ -918,7 +918,7 @@ class VectorStore(ABC): filter: Filter by document metadata Returns: - Retriever class for VectorStore. + Retriever class for `VectorStore`. Examples: ```python @@ -1072,10 +1072,10 @@ class VectorStoreRetriever(BaseRetriever): return docs def add_documents(self, documents: list[Document], **kwargs: Any) -> list[str]: - """Add documents to the vectorstore. + """Add documents to the `VectorStore`. Args: - documents: Documents to add to the vectorstore. + documents: Documents to add to the `VectorStore`. **kwargs: Other keyword arguments that subclasses might use. Returns: @@ -1086,10 +1086,10 @@ class VectorStoreRetriever(BaseRetriever): async def aadd_documents( self, documents: list[Document], **kwargs: Any ) -> list[str]: - """Async add documents to the vectorstore. + """Async add documents to the `VectorStore`. Args: - documents: Documents to add to the vectorstore. + documents: Documents to add to the `VectorStore`. **kwargs: Other keyword arguments that subclasses might use. Returns: diff --git a/libs/core/langchain_core/vectorstores/utils.py b/libs/core/langchain_core/vectorstores/utils.py index c51c3302e68..855af9211e7 100644 --- a/libs/core/langchain_core/vectorstores/utils.py +++ b/libs/core/langchain_core/vectorstores/utils.py @@ -1,4 +1,4 @@ -"""Internal utilities for the in memory implementation of VectorStore. +"""Internal utilities for the in memory implementation of `VectorStore`. These are part of a private API, and users should not use them directly as they can change without notice. diff --git a/libs/langchain/langchain_classic/agents/agent_toolkits/vectorstore/toolkit.py b/libs/langchain/langchain_classic/agents/agent_toolkits/vectorstore/toolkit.py index 3d2df64ffcc..56940d8564e 100644 --- a/libs/langchain/langchain_classic/agents/agent_toolkits/vectorstore/toolkit.py +++ b/libs/langchain/langchain_classic/agents/agent_toolkits/vectorstore/toolkit.py @@ -8,7 +8,7 @@ from pydantic import BaseModel, ConfigDict, Field class VectorStoreInfo(BaseModel): - """Information about a VectorStore.""" + """Information about a `VectorStore`.""" vectorstore: VectorStore = Field(exclude=True) name: str @@ -20,7 +20,7 @@ class VectorStoreInfo(BaseModel): class VectorStoreToolkit(BaseToolkit): - """Toolkit for interacting with a Vector Store.""" + """Toolkit for interacting with a `VectorStore`.""" vectorstore_info: VectorStoreInfo = Field(exclude=True) llm: BaseLanguageModel diff --git a/libs/langchain/langchain_classic/retrievers/parent_document_retriever.py b/libs/langchain/langchain_classic/retrievers/parent_document_retriever.py index 9d4e47d2bae..c7d57961ae8 100644 --- a/libs/langchain/langchain_classic/retrievers/parent_document_retriever.py +++ b/libs/langchain/langchain_classic/retrievers/parent_document_retriever.py @@ -133,7 +133,7 @@ class ParentDocumentRetriever(MultiVectorRetriever): This can be false if and only if `ids` are provided. You may want to set this to False if the documents are already in the docstore and you don't want to re-add them. - **kwargs: additional keyword arguments passed to the vectorstore. + **kwargs: additional keyword arguments passed to the `VectorStore`. """ docs, full_docs = self._split_docs_for_adding( documents, @@ -164,7 +164,7 @@ class ParentDocumentRetriever(MultiVectorRetriever): This can be false if and only if `ids` are provided. You may want to set this to False if the documents are already in the docstore and you don't want to re-add them. - **kwargs: additional keyword arguments passed to the vectorstore. + **kwargs: additional keyword arguments passed to the `VectorStore`. """ docs, full_docs = self._split_docs_for_adding( documents, diff --git a/libs/langchain/langchain_classic/retrievers/time_weighted_retriever.py b/libs/langchain/langchain_classic/retrievers/time_weighted_retriever.py index 9823318baa8..ffed2d17e4e 100644 --- a/libs/langchain/langchain_classic/retrievers/time_weighted_retriever.py +++ b/libs/langchain/langchain_classic/retrievers/time_weighted_retriever.py @@ -28,7 +28,7 @@ class TimeWeightedVectorStoreRetriever(BaseRetriever): """The vectorstore to store documents and determine salience.""" search_kwargs: dict = Field(default_factory=lambda: {"k": 100}) - """Keyword arguments to pass to the vectorstore similarity search.""" + """Keyword arguments to pass to the `VectorStore` similarity search.""" # TODO: abstract as a queue memory_stream: list[Document] = Field(default_factory=list) diff --git a/libs/partners/anthropic/langchain_anthropic/chat_models.py b/libs/partners/anthropic/langchain_anthropic/chat_models.py index a57c2499e1e..348a0fb3364 100644 --- a/libs/partners/anthropic/langchain_anthropic/chat_models.py +++ b/libs/partners/anthropic/langchain_anthropic/chat_models.py @@ -581,25 +581,25 @@ class ChatAnthropic(BaseChatModel): ``` Key init args — completion params: - model: str + model: Name of Anthropic model to use. e.g. `'claude-3-7-sonnet-20250219'`. - temperature: float + temperature: Sampling temperature. Ranges from `0.0` to `1.0`. - max_tokens: int + max_tokens: Max number of tokens to generate. Key init args — client params: - timeout: float | None + timeout: Timeout for requests. - anthropic_proxy: str | None + anthropic_proxy: Proxy to use for the Anthropic clients, will be used for every API call. If not passed in will be read from env var `ANTHROPIC_PROXY`. - max_retries: int + max_retries: Max number of retries if a request fails. - api_key: str | None + api_key: Anthropic API key. If not passed in will be read from env var `ANTHROPIC_API_KEY`. - base_url: str | None + base_url: Base URL for API requests. Only specify if using a proxy or service emulator. @@ -1984,156 +1984,156 @@ class ChatAnthropic(BaseChatModel): ) ``` - Example — force specific tool call with tool_choice `''`: + Example — force specific tool call with `tool_choice` `''`: - ```python - from langchain_anthropic import ChatAnthropic - from pydantic import BaseModel, Field + ```python + from langchain_anthropic import ChatAnthropic + from pydantic import BaseModel, Field - class GetWeather(BaseModel): - '''Get the current weather in a given location''' + class GetWeather(BaseModel): + '''Get the current weather in a given location''' - location: str = Field(..., description="The city and state, e.g. San Francisco, CA") + location: str = Field(..., description="The city and state, e.g. San Francisco, CA") - class GetPrice(BaseModel): - '''Get the price of a specific product.''' + class GetPrice(BaseModel): + '''Get the price of a specific product.''' - product: str = Field(..., description="The product to look up.") + product: str = Field(..., description="The product to look up.") - model = ChatAnthropic(model="claude-3-5-sonnet-latest", temperature=0) - model_with_tools = model.bind_tools([GetWeather, GetPrice], tool_choice="GetWeather") - model_with_tools.invoke("What is the weather like in San Francisco") - ``` + model = ChatAnthropic(model="claude-3-5-sonnet-latest", temperature=0) + model_with_tools = model.bind_tools([GetWeather, GetPrice], tool_choice="GetWeather") + model_with_tools.invoke("What is the weather like in San Francisco") + ``` Example — cache specific tools: - ```python - from langchain_anthropic import ChatAnthropic, convert_to_anthropic_tool - from pydantic import BaseModel, Field + ```python + from langchain_anthropic import ChatAnthropic, convert_to_anthropic_tool + from pydantic import BaseModel, Field - class GetWeather(BaseModel): - '''Get the current weather in a given location''' + class GetWeather(BaseModel): + '''Get the current weather in a given location''' - location: str = Field(..., description="The city and state, e.g. San Francisco, CA") + location: str = Field(..., description="The city and state, e.g. San Francisco, CA") - class GetPrice(BaseModel): - '''Get the price of a specific product.''' + class GetPrice(BaseModel): + '''Get the price of a specific product.''' - product: str = Field(..., description="The product to look up.") + product: str = Field(..., description="The product to look up.") - # We'll convert our pydantic class to the anthropic tool format - # before passing to bind_tools so that we can set the 'cache_control' - # field on our tool. - cached_price_tool = convert_to_anthropic_tool(GetPrice) - # Currently the only supported "cache_control" value is - # {"type": "ephemeral"}. - cached_price_tool["cache_control"] = {"type": "ephemeral"} + # We'll convert our pydantic class to the anthropic tool format + # before passing to bind_tools so that we can set the 'cache_control' + # field on our tool. + cached_price_tool = convert_to_anthropic_tool(GetPrice) + # Currently the only supported "cache_control" value is + # {"type": "ephemeral"}. + cached_price_tool["cache_control"] = {"type": "ephemeral"} - # We need to pass in extra headers to enable use of the beta cache - # control API. - model = ChatAnthropic( - model="claude-3-5-sonnet-latest", - temperature=0, - ) - model_with_tools = model.bind_tools([GetWeather, cached_price_tool]) - model_with_tools.invoke("What is the weather like in San Francisco") - ``` + # We need to pass in extra headers to enable use of the beta cache + # control API. + model = ChatAnthropic( + model="claude-3-5-sonnet-latest", + temperature=0, + ) + model_with_tools = model.bind_tools([GetWeather, cached_price_tool]) + model_with_tools.invoke("What is the weather like in San Francisco") + ``` - This outputs: + This outputs: - ```python - AIMessage( - content=[ - { - "text": "Certainly! I can help you find out the current weather in San Francisco. To get this information, I'll use the GetWeather function. Let me fetch that data for you right away.", - "type": "text", - }, - { - "id": "toolu_01TS5h8LNo7p5imcG7yRiaUM", - "input": {"location": "San Francisco, CA"}, - "name": "GetWeather", - "type": "tool_use", - }, - ], - response_metadata={ - "id": "msg_01Xg7Wr5inFWgBxE5jH9rpRo", - "model": "claude-3-5-sonnet-latest", - "stop_reason": "tool_use", - "stop_sequence": None, - "usage": { - "input_tokens": 171, - "output_tokens": 96, - "cache_creation_input_tokens": 1470, - "cache_read_input_tokens": 0, - }, + ```python + AIMessage( + content=[ + { + "text": "Certainly! I can help you find out the current weather in San Francisco. To get this information, I'll use the GetWeather function. Let me fetch that data for you right away.", + "type": "text", }, - id="run-b36a5b54-5d69-470e-a1b0-b932d00b089e-0", - tool_calls=[ - { - "name": "GetWeather", - "args": {"location": "San Francisco, CA"}, - "id": "toolu_01TS5h8LNo7p5imcG7yRiaUM", - "type": "tool_call", - } - ], - usage_metadata={ + { + "id": "toolu_01TS5h8LNo7p5imcG7yRiaUM", + "input": {"location": "San Francisco, CA"}, + "name": "GetWeather", + "type": "tool_use", + }, + ], + response_metadata={ + "id": "msg_01Xg7Wr5inFWgBxE5jH9rpRo", + "model": "claude-3-5-sonnet-latest", + "stop_reason": "tool_use", + "stop_sequence": None, + "usage": { "input_tokens": 171, "output_tokens": 96, - "total_tokens": 267, + "cache_creation_input_tokens": 1470, + "cache_read_input_tokens": 0, }, - ) - ``` + }, + id="run-b36a5b54-5d69-470e-a1b0-b932d00b089e-0", + tool_calls=[ + { + "name": "GetWeather", + "args": {"location": "San Francisco, CA"}, + "id": "toolu_01TS5h8LNo7p5imcG7yRiaUM", + "type": "tool_call", + } + ], + usage_metadata={ + "input_tokens": 171, + "output_tokens": 96, + "total_tokens": 267, + }, + ) + ``` - If we invoke the tool again, we can see that the "usage" information in the AIMessage.response_metadata shows that we had a cache hit: + If we invoke the tool again, we can see that the "usage" information in the AIMessage.response_metadata shows that we had a cache hit: - ```python - AIMessage( - content=[ - { - "text": "To get the current weather in San Francisco, I can use the GetWeather function. Let me check that for you.", - "type": "text", - }, - { - "id": "toolu_01HtVtY1qhMFdPprx42qU2eA", - "input": {"location": "San Francisco, CA"}, - "name": "GetWeather", - "type": "tool_use", - }, - ], - response_metadata={ - "id": "msg_016RfWHrRvW6DAGCdwB6Ac64", - "model": "claude-3-5-sonnet-latest", - "stop_reason": "tool_use", - "stop_sequence": None, - "usage": { - "input_tokens": 171, - "output_tokens": 82, - "cache_creation_input_tokens": 0, - "cache_read_input_tokens": 1470, - }, + ```python + AIMessage( + content=[ + { + "text": "To get the current weather in San Francisco, I can use the GetWeather function. Let me check that for you.", + "type": "text", }, - id="run-88b1f825-dcb7-4277-ac27-53df55d22001-0", - tool_calls=[ - { - "name": "GetWeather", - "args": {"location": "San Francisco, CA"}, - "id": "toolu_01HtVtY1qhMFdPprx42qU2eA", - "type": "tool_call", - } - ], - usage_metadata={ + { + "id": "toolu_01HtVtY1qhMFdPprx42qU2eA", + "input": {"location": "San Francisco, CA"}, + "name": "GetWeather", + "type": "tool_use", + }, + ], + response_metadata={ + "id": "msg_016RfWHrRvW6DAGCdwB6Ac64", + "model": "claude-3-5-sonnet-latest", + "stop_reason": "tool_use", + "stop_sequence": None, + "usage": { "input_tokens": 171, "output_tokens": 82, - "total_tokens": 253, + "cache_creation_input_tokens": 0, + "cache_read_input_tokens": 1470, }, - ) - ``` + }, + id="run-88b1f825-dcb7-4277-ac27-53df55d22001-0", + tool_calls=[ + { + "name": "GetWeather", + "args": {"location": "San Francisco, CA"}, + "id": "toolu_01HtVtY1qhMFdPprx42qU2eA", + "type": "tool_call", + } + ], + usage_metadata={ + "input_tokens": 171, + "output_tokens": 82, + "total_tokens": 253, + }, + ) + ``` """ # noqa: E501 formatted_tools = [ tool if _is_builtin_tool(tool) else convert_to_anthropic_tool(tool) @@ -2191,108 +2191,112 @@ class ChatAnthropic(BaseChatModel): If `schema` is a Pydantic class then the model output will be a Pydantic instance of that class, and the model-generated fields will be validated by the Pydantic class. Otherwise the model output will be a - dict and will not be validated. See `langchain_core.utils.function_calling.convert_to_openai_tool` - for more on how to properly specify types and descriptions of - schema fields when specifying a Pydantic or `TypedDict` class. + dict and will not be validated. + + See `langchain_core.utils.function_calling.convert_to_openai_tool` for + more on how to properly specify types and descriptions of schema fields + when specifying a Pydantic or `TypedDict` class. include_raw: If `False` then only the parsed structured output is returned. If an error occurs during model output parsing it will be raised. If `True` then both the raw model response (a `BaseMessage`) and the parsed model response will be returned. If an error occurs during output parsing it - will be caught and returned as well. The final output is always a dict - with keys `raw`, `parsed`, and `parsing_error`. + will be caught and returned as well. + + The final output is always a `dict` with keys `'raw'`, `'parsed'`, and + `'parsing_error'`. kwargs: Additional keyword arguments are ignored. Returns: - A Runnable that takes same inputs as a `langchain_core.language_models.chat.BaseChatModel`. + A `Runnable` that takes same inputs as a + `langchain_core.language_models.chat.BaseChatModel`. If `include_raw` is + `False` and `schema` is a Pydantic class, `Runnable` outputs an instance + of `schema` (i.e., a Pydantic object). Otherwise, if `include_raw` is + `False` then `Runnable` outputs a `dict`. - If `include_raw` is `False` and `schema` is a Pydantic class, Runnable outputs - an instance of `schema` (i.e., a Pydantic object). + If `include_raw` is `True`, then `Runnable` outputs a `dict` with keys: - Otherwise, if `include_raw` is `False` then Runnable outputs a dict. + - `'raw'`: `BaseMessage` + - `'parsed'`: `None` if there was a parsing error, otherwise the type + depends on the `schema` as described above. + - `'parsing_error'`: `BaseException | None` - If `include_raw` is True, then Runnable outputs a dict with keys: + Example: Pydantic schema (`include_raw=False`): - - `'raw'`: BaseMessage - - `'parsed'`: None if there was a parsing error, otherwise the type depends on the `schema` as described above. - - `'parsing_error'`: BaseException | None - - Example: Pydantic schema (include_raw=False): - - ```python - from langchain_anthropic import ChatAnthropic - from pydantic import BaseModel + ```python + from langchain_anthropic import ChatAnthropic + from pydantic import BaseModel - class AnswerWithJustification(BaseModel): - '''An answer to the user question along with justification for the answer.''' + class AnswerWithJustification(BaseModel): + '''An answer to the user question along with justification for the answer.''' - answer: str - justification: str + answer: str + justification: str - model = ChatAnthropic(model="claude-3-5-sonnet-latest", temperature=0) - structured_model = model.with_structured_output(AnswerWithJustification) + model = ChatAnthropic(model="claude-3-5-sonnet-latest", temperature=0) + structured_model = model.with_structured_output(AnswerWithJustification) - structured_model.invoke("What weighs more a pound of bricks or a pound of feathers") + structured_model.invoke("What weighs more a pound of bricks or a pound of feathers") - # -> AnswerWithJustification( - # answer='They weigh the same', - # justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.' - # ) - ``` + # -> AnswerWithJustification( + # answer='They weigh the same', + # justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.' + # ) + ``` - Example: Pydantic schema (include_raw=True): + Example: Pydantic schema (`include_raw=True`): - ```python - from langchain_anthropic import ChatAnthropic - from pydantic import BaseModel + ```python + from langchain_anthropic import ChatAnthropic + from pydantic import BaseModel - class AnswerWithJustification(BaseModel): - '''An answer to the user question along with justification for the answer.''' + class AnswerWithJustification(BaseModel): + '''An answer to the user question along with justification for the answer.''' - answer: str - justification: str + answer: str + justification: str - model = ChatAnthropic(model="claude-3-5-sonnet-latest", temperature=0) - structured_model = model.with_structured_output(AnswerWithJustification, include_raw=True) + model = ChatAnthropic(model="claude-3-5-sonnet-latest", temperature=0) + structured_model = model.with_structured_output(AnswerWithJustification, include_raw=True) - structured_model.invoke("What weighs more a pound of bricks or a pound of feathers") - # -> { - # 'raw': AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_Ao02pnFYXD6GN1yzc0uXPsvF', 'function': {'arguments': '{"answer":"They weigh the same.","justification":"Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ."}', 'name': 'AnswerWithJustification'}, 'type': 'function'}]}), - # 'parsed': AnswerWithJustification(answer='They weigh the same.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'), - # 'parsing_error': None - # } - ``` + structured_model.invoke("What weighs more a pound of bricks or a pound of feathers") + # -> { + # 'raw': AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_Ao02pnFYXD6GN1yzc0uXPsvF', 'function': {'arguments': '{"answer":"They weigh the same.","justification":"Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ."}', 'name': 'AnswerWithJustification'}, 'type': 'function'}]}), + # 'parsed': AnswerWithJustification(answer='They weigh the same.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'), + # 'parsing_error': None + # } + ``` - Example: Dict schema (include_raw=False): + Example: `dict` schema (`include_raw=False`): - ```python - from langchain_anthropic import ChatAnthropic + ```python + from langchain_anthropic import ChatAnthropic - schema = { - "name": "AnswerWithJustification", - "description": "An answer to the user question along with justification for the answer.", - "input_schema": { - "type": "object", - "properties": { - "answer": {"type": "string"}, - "justification": {"type": "string"}, - }, - "required": ["answer", "justification"], + schema = { + "name": "AnswerWithJustification", + "description": "An answer to the user question along with justification for the answer.", + "input_schema": { + "type": "object", + "properties": { + "answer": {"type": "string"}, + "justification": {"type": "string"}, }, - } - model = ChatAnthropic(model="claude-3-5-sonnet-latest", temperature=0) - structured_model = model.with_structured_output(schema) + "required": ["answer", "justification"], + }, + } + model = ChatAnthropic(model="claude-3-5-sonnet-latest", temperature=0) + structured_model = model.with_structured_output(schema) - structured_model.invoke("What weighs more a pound of bricks or a pound of feathers") - # -> { - # 'answer': 'They weigh the same', - # 'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.' - # } - ``` + structured_model.invoke("What weighs more a pound of bricks or a pound of feathers") + # -> { + # 'answer': 'They weigh the same', + # 'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.' + # } + ``` """ # noqa: E501 formatted_tool = convert_to_anthropic_tool(schema) tool_name = formatted_tool["name"] @@ -2345,60 +2349,60 @@ class ChatAnthropic(BaseChatModel): Args: messages: The message inputs to tokenize. - tools: If provided, sequence of dict, BaseModel, function, or BaseTools - to be converted to tool schemas. + tools: If provided, sequence of `dict`, `BaseModel`, function, or `BaseTool` + objects to be converted to tool schemas. kwargs: Additional keyword arguments are passed to the Anthropic `messages.count_tokens` method. Basic usage: - ```python - from langchain_anthropic import ChatAnthropic - from langchain_core.messages import HumanMessage, SystemMessage + ```python + from langchain_anthropic import ChatAnthropic + from langchain_core.messages import HumanMessage, SystemMessage - model = ChatAnthropic(model="claude-3-5-sonnet-20241022") + model = ChatAnthropic(model="claude-3-5-sonnet-20241022") - messages = [ - SystemMessage(content="You are a scientist"), - HumanMessage(content="Hello, Claude"), - ] - model.get_num_tokens_from_messages(messages) - ``` + messages = [ + SystemMessage(content="You are a scientist"), + HumanMessage(content="Hello, Claude"), + ] + model.get_num_tokens_from_messages(messages) + ``` - ```txt - 14 - ``` + ```txt + 14 + ``` Pass tool schemas: - ```python - from langchain_anthropic import ChatAnthropic - from langchain_core.messages import HumanMessage - from langchain_core.tools import tool + ```python + from langchain_anthropic import ChatAnthropic + from langchain_core.messages import HumanMessage + from langchain_core.tools import tool - model = ChatAnthropic(model="claude-3-5-sonnet-20241022") + model = ChatAnthropic(model="claude-3-5-sonnet-20241022") - @tool(parse_docstring=True) - def get_weather(location: str) -> str: - \"\"\"Get the current weather in a given location + @tool(parse_docstring=True) + def get_weather(location: str) -> str: + \"\"\"Get the current weather in a given location - Args: - location: The city and state, e.g. San Francisco, CA - \"\"\" - return "Sunny" + Args: + location: The city and state, e.g. San Francisco, CA + \"\"\" + return "Sunny" - messages = [ - HumanMessage(content="What's the weather like in San Francisco?"), - ] - model.get_num_tokens_from_messages(messages, tools=[get_weather]) - ``` + messages = [ + HumanMessage(content="What's the weather like in San Francisco?"), + ] + model.get_num_tokens_from_messages(messages, tools=[get_weather]) + ``` - ```txt - 403 - ``` + ```txt + 403 + ``` !!! warning "Behavior changed in 0.3.0" - Uses Anthropic's [token counting API](https://docs.claude.com/en/docs/build-with-claude/token-counting) to count tokens in messages. + Uses Anthropic's [token counting API](https://docs.claude.com/en/docs/build-with-claude/token-counting) to count tokens in messages. """ # noqa: D214,E501 formatted_system, formatted_messages = _format_messages(messages) diff --git a/libs/partners/chroma/langchain_chroma/vectorstores.py b/libs/partners/chroma/langchain_chroma/vectorstores.py index f95dac1194d..e100743a7f5 100644 --- a/libs/partners/chroma/langchain_chroma/vectorstores.py +++ b/libs/partners/chroma/langchain_chroma/vectorstores.py @@ -156,31 +156,31 @@ class Chroma(VectorStore): ``` Key init args — indexing params: - collection_name: str + collection_name: Name of the collection. - embedding_function: Embeddings + embedding_function: Embedding function to use. Key init args — client params: - client: Client | None + client: Chroma client to use. - client_settings: chromadb.config.Settings | None + client_settings: Chroma client settings. - persist_directory: str | None + persist_directory: Directory to persist the collection. - host: str | None + host: Hostname of a deployed Chroma server. - port: int | None + port: Connection port for a deployed Chroma server. Default is 8000. - ssl: bool | None + ssl: Whether to establish an SSL connection with a deployed Chroma server. Default is False. - headers: dict[str, str] | None + headers: HTTP headers to send to a deployed Chroma server. - chroma_cloud_api_key: str | None + chroma_cloud_api_key: Chroma Cloud API key. - tenant: str | None + tenant: Tenant ID. Required for Chroma Cloud connections. Default is 'default_tenant' for local Chroma servers. - database: str | None + database: Database name. Required for Chroma Cloud connections. Default is 'default_database'. Instantiate: @@ -505,7 +505,7 @@ class Chroma(VectorStore): metadatas: list[dict] | None = None, ids: list[str] | None = None, ) -> list[str]: - """Run more images through the embeddings and add to the vectorstore. + """Run more images through the embeddings and add to the `VectorStore`. Args: uris: File path to the image. @@ -594,10 +594,10 @@ class Chroma(VectorStore): ids: list[str] | None = None, **kwargs: Any, ) -> list[str]: - """Run more texts through the embeddings and add to the vectorstore. + """Run more texts through the embeddings and add to the `VectorStore`. Args: - texts: Texts to add to the vectorstore. + texts: Texts to add to the `VectorStore`. metadatas: Optional list of metadatas. When querying, you can filter on this metadata. ids: Optional list of IDs. (Items without IDs will be assigned UUIDs) @@ -719,7 +719,7 @@ class Chroma(VectorStore): kwargs: Additional keyword arguments to pass to Chroma collection query. Returns: - List of Documents most similar to the query vector. + List of `Document` objects most similar to the query vector. """ results = self.__query_collection( query_embeddings=[embedding], @@ -998,7 +998,7 @@ class Chroma(VectorStore): kwargs: Additional keyword arguments to pass to Chroma collection query. Returns: - List of Documents selected by maximal marginal relevance. + List of `Document` objects selected by maximal marginal relevance. """ results = self.__query_collection( query_embeddings=[embedding], @@ -1047,7 +1047,7 @@ class Chroma(VectorStore): kwargs: Additional keyword arguments to pass to Chroma collection query. Returns: - List of Documents selected by maximal marginal relevance. + List of `Document` objects selected by maximal marginal relevance. Raises: ValueError: If the embedding function is not provided. @@ -1357,7 +1357,7 @@ class Chroma(VectorStore): database: Database name. Required for Chroma Cloud connections. Default is 'default_database'. ids: List of document IDs. - documents: List of documents to add to the vectorstore. + documents: List of documents to add to the `VectorStore`. embedding: Embedding function. client_settings: Chroma client settings. client: Chroma client. Documentation: diff --git a/libs/partners/deepseek/langchain_deepseek/chat_models.py b/libs/partners/deepseek/langchain_deepseek/chat_models.py index ea692a1ce54..4229f23e165 100644 --- a/libs/partners/deepseek/langchain_deepseek/chat_models.py +++ b/libs/partners/deepseek/langchain_deepseek/chat_models.py @@ -38,20 +38,20 @@ class ChatDeepSeek(BaseChatOpenAI): ``` Key init args — completion params: - model: str - Name of DeepSeek model to use, e.g. "deepseek-chat". - temperature: float + model: + Name of DeepSeek model to use, e.g. `"deepseek-chat"`. + temperature: Sampling temperature. - max_tokens: int | None + max_tokens: Max number of tokens to generate. Key init args — client params: - timeout: float | None + timeout: Timeout for requests. - max_retries: int + max_retries: Max number of retries. - api_key: str | None - DeepSeek API key. If not passed in will be read from env var DEEPSEEK_API_KEY. + api_key: + DeepSeek API key. If not passed in will be read from env var `DEEPSEEK_API_KEY`. See full list of supported init args and their descriptions in the params section. @@ -378,15 +378,17 @@ class ChatDeepSeek(BaseChatOpenAI): - an OpenAI function/tool schema, - a JSON Schema, - - a `TypedDict` class (support added in 0.1.20), + - a `TypedDict` class, - or a Pydantic class. If `schema` is a Pydantic class then the model output will be a Pydantic instance of that class, and the model-generated fields will be validated by the Pydantic class. Otherwise the model output will be a - dict and will not be validated. See `langchain_core.utils.function_calling.convert_to_openai_tool` - for more on how to properly specify types and descriptions of - schema fields when specifying a Pydantic or `TypedDict` class. + dict and will not be validated. + + See `langchain_core.utils.function_calling.convert_to_openai_tool` for + more on how to properly specify types and descriptions of schema fields + when specifying a Pydantic or `TypedDict` class. method: The method for steering model generation, one of: @@ -400,8 +402,10 @@ class ChatDeepSeek(BaseChatOpenAI): an error occurs during model output parsing it will be raised. If `True` then both the raw model response (a `BaseMessage`) and the parsed model response will be returned. If an error occurs during output parsing it - will be caught and returned as well. The final output is always a dict - with keys `'raw'`, `'parsed'`, and `'parsing_error'`. + will be caught and returned as well. + + The final output is always a `dict` with keys `'raw'`, `'parsed'`, and + `'parsing_error'`. strict: Whether to enable strict schema adherence when generating the function @@ -413,18 +417,19 @@ class ChatDeepSeek(BaseChatOpenAI): kwargs: Additional keyword args aren't supported. Returns: - A Runnable that takes same inputs as a `langchain_core.language_models.chat.BaseChatModel`. + A `Runnable` that takes same inputs as a + `langchain_core.language_models.chat.BaseChatModel`. If `include_raw` is + `False` and `schema` is a Pydantic class, `Runnable` outputs an instance + of `schema` (i.e., a Pydantic object). Otherwise, if `include_raw` is + `False` then `Runnable` outputs a `dict`. - If `include_raw` is False and `schema` is a Pydantic class, Runnable outputs - an instance of `schema` (i.e., a Pydantic object). Otherwise, if `include_raw` is False then Runnable outputs a dict. + If `include_raw` is `True`, then `Runnable` outputs a `dict` with keys: - If `include_raw` is True, then Runnable outputs a dict with keys: - - - `'raw'`: BaseMessage - - `'parsed'`: None if there was a parsing error, otherwise the type depends on the `schema` as described above. - - `'parsing_error'`: BaseException | None - - """ # noqa: E501 + - `'raw'`: `BaseMessage` + - `'parsed'`: `None` if there was a parsing error, otherwise the type + depends on the `schema` as described above. + - `'parsing_error'`: `BaseException | None` + """ # Some applications require that incompatible parameters (e.g., unsupported # methods) be handled. if method == "json_schema": diff --git a/libs/partners/fireworks/langchain_fireworks/chat_models.py b/libs/partners/fireworks/langchain_fireworks/chat_models.py index df4c3420732..38343c188ed 100644 --- a/libs/partners/fireworks/langchain_fireworks/chat_models.py +++ b/libs/partners/fireworks/langchain_fireworks/chat_models.py @@ -683,15 +683,17 @@ class ChatFireworks(BaseChatModel): - an OpenAI function/tool schema, - a JSON Schema, - - a `TypedDict` class (support added in 0.1.7), + - a `TypedDict` class, - or a Pydantic class. If `schema` is a Pydantic class then the model output will be a Pydantic instance of that class, and the model-generated fields will be validated by the Pydantic class. Otherwise the model output will be a - dict and will not be validated. See `langchain_core.utils.function_calling.convert_to_openai_tool` - for more on how to properly specify types and descriptions of - schema fields when specifying a Pydantic or `TypedDict` class. + dict and will not be validated. + + See `langchain_core.utils.function_calling.convert_to_openai_tool` for + more on how to properly specify types and descriptions of schema fields + when specifying a Pydantic or `TypedDict` class. method: The method for steering model generation, one of: @@ -710,216 +712,218 @@ class ChatFireworks(BaseChatModel): an error occurs during model output parsing it will be raised. If `True` then both the raw model response (a `BaseMessage`) and the parsed model response will be returned. If an error occurs during output parsing it - will be caught and returned as well. The final output is always a dict - with keys `'raw'`, `'parsed'`, and `'parsing_error'`. + will be caught and returned as well. + + The final output is always a `dict` with keys `'raw'`, `'parsed'`, and + `'parsing_error'`. kwargs: - Any additional parameters to pass to the - `langchain.runnable.Runnable` constructor. + Any additional parameters to pass to the `langchain.runnable.Runnable` + constructor. Returns: - A Runnable that takes same inputs as a `langchain_core.language_models.chat.BaseChatModel`. + A `Runnable` that takes same inputs as a + `langchain_core.language_models.chat.BaseChatModel`. If `include_raw` is + `False` and `schema` is a Pydantic class, `Runnable` outputs an instance + of `schema` (i.e., a Pydantic object). Otherwise, if `include_raw` is + `False` then `Runnable` outputs a `dict`. - If `include_raw` is False and `schema` is a Pydantic class, Runnable outputs - an instance of `schema` (i.e., a Pydantic object). + If `include_raw` is `True`, then `Runnable` outputs a `dict` with keys: - Otherwise, if `include_raw` is False then Runnable outputs a dict. - - If `include_raw` is True, then Runnable outputs a dict with keys: - - - `'raw'`: BaseMessage - - `'parsed'`: None if there was a parsing error, otherwise the type depends on the `schema` as described above. - - `'parsing_error'`: BaseException | None + - `'raw'`: `BaseMessage` + - `'parsed'`: `None` if there was a parsing error, otherwise the type + depends on the `schema` as described above. + - `'parsing_error'`: `BaseException | None` Example: schema=Pydantic class, method="function_calling", include_raw=False: - ```python - from typing import Optional + ```python + from typing import Optional - from langchain_fireworks import ChatFireworks - from pydantic import BaseModel, Field + from langchain_fireworks import ChatFireworks + from pydantic import BaseModel, Field - class AnswerWithJustification(BaseModel): - '''An answer to the user question along with justification for the answer.''' + class AnswerWithJustification(BaseModel): + '''An answer to the user question along with justification for the answer.''' - answer: str - # If we provide default values and/or descriptions for fields, these will be passed - # to the model. This is an important part of improving a model's ability to - # correctly return structured outputs. - justification: str | None = Field( - default=None, description="A justification for the answer." - ) - - - model = ChatFireworks( - model="accounts/fireworks/models/firefunction-v1", - temperature=0, - ) - structured_model = model.with_structured_output(AnswerWithJustification) - - structured_model.invoke( - "What weighs more a pound of bricks or a pound of feathers" + answer: str + # If we provide default values and/or descriptions for fields, these will be passed + # to the model. This is an important part of improving a model's ability to + # correctly return structured outputs. + justification: str | None = Field( + default=None, description="A justification for the answer." ) - # -> AnswerWithJustification( - # answer='They weigh the same', - # justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.' - # ) - ``` + + model = ChatFireworks( + model="accounts/fireworks/models/firefunction-v1", + temperature=0, + ) + structured_model = model.with_structured_output(AnswerWithJustification) + + structured_model.invoke( + "What weighs more a pound of bricks or a pound of feathers" + ) + + # -> AnswerWithJustification( + # answer='They weigh the same', + # justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.' + # ) + ``` Example: schema=Pydantic class, method="function_calling", include_raw=True: - ```python - from langchain_fireworks import ChatFireworks - from pydantic import BaseModel + ```python + from langchain_fireworks import ChatFireworks + from pydantic import BaseModel - class AnswerWithJustification(BaseModel): - '''An answer to the user question along with justification for the answer.''' + class AnswerWithJustification(BaseModel): + '''An answer to the user question along with justification for the answer.''' - answer: str - justification: str + answer: str + justification: str - model = ChatFireworks( - model="accounts/fireworks/models/firefunction-v1", - temperature=0, - ) - structured_model = model.with_structured_output( - AnswerWithJustification, include_raw=True - ) + model = ChatFireworks( + model="accounts/fireworks/models/firefunction-v1", + temperature=0, + ) + structured_model = model.with_structured_output( + AnswerWithJustification, include_raw=True + ) - structured_model.invoke( - "What weighs more a pound of bricks or a pound of feathers" - ) - # -> { - # 'raw': AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_Ao02pnFYXD6GN1yzc0uXPsvF', 'function': {'arguments': '{"answer":"They weigh the same.","justification":"Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ."}', 'name': 'AnswerWithJustification'}, 'type': 'function'}]}), - # 'parsed': AnswerWithJustification(answer='They weigh the same.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'), - # 'parsing_error': None - # } - ``` + structured_model.invoke( + "What weighs more a pound of bricks or a pound of feathers" + ) + # -> { + # 'raw': AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_Ao02pnFYXD6GN1yzc0uXPsvF', 'function': {'arguments': '{"answer":"They weigh the same.","justification":"Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ."}', 'name': 'AnswerWithJustification'}, 'type': 'function'}]}), + # 'parsed': AnswerWithJustification(answer='They weigh the same.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'), + # 'parsing_error': None + # } + ``` Example: schema=TypedDict class, method="function_calling", include_raw=False: - ```python - from typing_extensions import Annotated, TypedDict + ```python + from typing_extensions import Annotated, TypedDict - from langchain_fireworks import ChatFireworks + from langchain_fireworks import ChatFireworks - class AnswerWithJustification(TypedDict): - '''An answer to the user question along with justification for the answer.''' + class AnswerWithJustification(TypedDict): + '''An answer to the user question along with justification for the answer.''' - answer: str - justification: Annotated[ - str | None, None, "A justification for the answer." - ] + answer: str + justification: Annotated[ + str | None, None, "A justification for the answer." + ] - model = ChatFireworks( - model="accounts/fireworks/models/firefunction-v1", - temperature=0, - ) - structured_model = model.with_structured_output(AnswerWithJustification) + model = ChatFireworks( + model="accounts/fireworks/models/firefunction-v1", + temperature=0, + ) + structured_model = model.with_structured_output(AnswerWithJustification) - structured_model.invoke( - "What weighs more a pound of bricks or a pound of feathers" - ) - # -> { - # 'answer': 'They weigh the same', - # 'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.' - # } - ``` + structured_model.invoke( + "What weighs more a pound of bricks or a pound of feathers" + ) + # -> { + # 'answer': 'They weigh the same', + # 'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.' + # } + ``` Example: schema=OpenAI function schema, method="function_calling", include_raw=False: - ```python - from langchain_fireworks import ChatFireworks + ```python + from langchain_fireworks import ChatFireworks - oai_schema = { - "name": "AnswerWithJustification", - "description": "An answer to the user question along with justification for the answer.", - "parameters": { - "type": "object", - "properties": { - "answer": {"type": "string"}, - "justification": { - "description": "A justification for the answer.", - "type": "string", - }, + oai_schema = { + "name": "AnswerWithJustification", + "description": "An answer to the user question along with justification for the answer.", + "parameters": { + "type": "object", + "properties": { + "answer": {"type": "string"}, + "justification": { + "description": "A justification for the answer.", + "type": "string", }, - "required": ["answer"], }, - } + "required": ["answer"], + }, + } - model = ChatFireworks( - model="accounts/fireworks/models/firefunction-v1", - temperature=0, - ) - structured_model = model.with_structured_output(oai_schema) + model = ChatFireworks( + model="accounts/fireworks/models/firefunction-v1", + temperature=0, + ) + structured_model = model.with_structured_output(oai_schema) - structured_model.invoke( - "What weighs more a pound of bricks or a pound of feathers" - ) - # -> { - # 'answer': 'They weigh the same', - # 'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.' - # } - ``` + structured_model.invoke( + "What weighs more a pound of bricks or a pound of feathers" + ) + # -> { + # 'answer': 'They weigh the same', + # 'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.' + # } + ``` Example: schema=Pydantic class, method="json_mode", include_raw=True: - ```python - from langchain_fireworks import ChatFireworks - from pydantic import BaseModel + ```python + from langchain_fireworks import ChatFireworks + from pydantic import BaseModel - class AnswerWithJustification(BaseModel): - answer: str - justification: str + class AnswerWithJustification(BaseModel): + answer: str + justification: str - model = ChatFireworks( - model="accounts/fireworks/models/firefunction-v1", temperature=0 - ) - structured_model = model.with_structured_output( - AnswerWithJustification, method="json_mode", include_raw=True - ) + model = ChatFireworks( + model="accounts/fireworks/models/firefunction-v1", temperature=0 + ) + structured_model = model.with_structured_output( + AnswerWithJustification, method="json_mode", include_raw=True + ) - structured_model.invoke( - "Answer the following question. " - "Make sure to return a JSON blob with keys 'answer' and 'justification'. " - "What's heavier a pound of bricks or a pound of feathers?" - ) - # -> { - # 'raw': AIMessage(content='{"answer": "They are both the same weight.", "justification": "Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight."}'), - # 'parsed': AnswerWithJustification(answer='They are both the same weight.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight.'), - # 'parsing_error': None - # } - ``` + structured_model.invoke( + "Answer the following question. " + "Make sure to return a JSON blob with keys 'answer' and 'justification'. " + "What's heavier a pound of bricks or a pound of feathers?" + ) + # -> { + # 'raw': AIMessage(content='{"answer": "They are both the same weight.", "justification": "Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight."}'), + # 'parsed': AnswerWithJustification(answer='They are both the same weight.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight.'), + # 'parsing_error': None + # } + ``` Example: schema=None, method="json_mode", include_raw=True: - ```python - structured_model = model.with_structured_output( - method="json_mode", include_raw=True - ) + ```python + structured_model = model.with_structured_output( + method="json_mode", include_raw=True + ) - structured_model.invoke( - "Answer the following question. " - "Make sure to return a JSON blob with keys 'answer' and 'justification'. " - "What's heavier a pound of bricks or a pound of feathers?" - ) - # -> { - # 'raw': AIMessage(content='{"answer": "They are both the same weight.", "justification": "Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight."}'), - # 'parsed': { - # 'answer': 'They are both the same weight.', - # 'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight.' - # }, - # 'parsing_error': None - # } - ``` + structured_model.invoke( + "Answer the following question. " + "Make sure to return a JSON blob with keys 'answer' and 'justification'. " + "What's heavier a pound of bricks or a pound of feathers?" + ) + # -> { + # 'raw': AIMessage(content='{"answer": "They are both the same weight.", "justification": "Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight."}'), + # 'parsed': { + # 'answer': 'They are both the same weight.', + # 'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight.' + # }, + # 'parsing_error': None + # } + ``` """ # noqa: E501 _ = kwargs.pop("strict", None) diff --git a/libs/partners/fireworks/langchain_fireworks/embeddings.py b/libs/partners/fireworks/langchain_fireworks/embeddings.py index a445d5a7432..c8eacbe6150 100644 --- a/libs/partners/fireworks/langchain_fireworks/embeddings.py +++ b/libs/partners/fireworks/langchain_fireworks/embeddings.py @@ -19,11 +19,11 @@ class FireworksEmbeddings(BaseModel, Embeddings): ``` Key init args — completion params: - model: str + model: Name of Fireworks model to use. Key init args — client params: - fireworks_api_key: SecretStr + fireworks_api_key: Fireworks API key. See full list of supported init args and their descriptions in the params section. diff --git a/libs/partners/groq/langchain_groq/chat_models.py b/libs/partners/groq/langchain_groq/chat_models.py index 30889468f51..bbbe94dad06 100644 --- a/libs/partners/groq/langchain_groq/chat_models.py +++ b/libs/partners/groq/langchain_groq/chat_models.py @@ -80,13 +80,13 @@ class ChatGroq(BaseChatModel): ``` Key init args — completion params: - model: str + model: Name of Groq model to use, e.g. `llama-3.1-8b-instant`. - temperature: float + temperature: Sampling temperature. Ranges from `0.0` to `1.0`. - max_tokens: int | None + max_tokens: Max number of tokens to generate. - reasoning_format: Literal["parsed", "raw", "hidden] | None + reasoning_format: The format for reasoning output. Groq will default to `raw` if left undefined. @@ -101,21 +101,21 @@ class ChatGroq(BaseChatModel): See the [Groq documentation](https://console.groq.com/docs/reasoning#reasoning) for more details and a list of supported models. - model_kwargs: Dict[str, Any] + model_kwargs: Holds any model parameters valid for create call not explicitly specified. Key init args — client params: - timeout: Union[float, Tuple[float, float], Any, None] + timeout: Timeout for requests. - max_retries: int + max_retries: Max number of retries. - api_key: str | None + api_key: Groq API key. If not passed in will be read from env var `GROQ_API_KEY`. - base_url: str | None + base_url: Base URL path for API requests, leave blank if not using a proxy or service emulator. - custom_get_token_ids: Callable[[str], list[int]] | None + custom_get_token_ids: Optional encoder to use for counting tokens. See full list of supported init args and their descriptions in the params @@ -828,15 +828,17 @@ class ChatGroq(BaseChatModel): - an OpenAI function/tool schema, - a JSON Schema, - - a `TypedDict` class (supported added in 0.1.9), + - a `TypedDict` class, - or a Pydantic class. If `schema` is a Pydantic class then the model output will be a Pydantic instance of that class, and the model-generated fields will be validated by the Pydantic class. Otherwise the model output will be a - dict and will not be validated. See `langchain_core.utils.function_calling.convert_to_openai_tool` - for more on how to properly specify types and descriptions of - schema fields when specifying a Pydantic or `TypedDict` class. + dict and will not be validated. + + See `langchain_core.utils.function_calling.convert_to_openai_tool` for + more on how to properly specify types and descriptions of schema fields + when specifying a Pydantic or `TypedDict` class. !!! warning "Behavior changed in 0.3.8" Added support for Groq's dedicated structured output feature via @@ -879,195 +881,202 @@ class ChatGroq(BaseChatModel): an error occurs during model output parsing it will be raised. If `True` then both the raw model response (a `BaseMessage`) and the parsed model response will be returned. If an error occurs during output parsing it - will be caught and returned as well. The final output is always a dict - with keys `'raw'`, `'parsed'`, and `'parsing_error'`. + will be caught and returned as well. + + The final output is always a `dict` with keys `'raw'`, `'parsed'`, and + `'parsing_error'`. kwargs: - Any additional parameters to pass to the - `langchain.runnable.Runnable` constructor. + Any additional parameters to pass to the `langchain.runnable.Runnable` + constructor. Returns: - A Runnable that takes same inputs as a `langchain_core.language_models.chat.BaseChatModel`. + A `Runnable` that takes same inputs as a + `langchain_core.language_models.chat.BaseChatModel`. If `include_raw` is + `False` and `schema` is a Pydantic class, `Runnable` outputs an instance + of `schema` (i.e., a Pydantic object). Otherwise, if `include_raw` is + `False` then `Runnable` outputs a `dict`. - If `include_raw` is False and `schema` is a Pydantic class, Runnable outputs - an instance of `schema` (i.e., a Pydantic object). + If `include_raw` is `True`, then `Runnable` outputs a `dict` with keys: - Otherwise, if `include_raw` is False then Runnable outputs a dict. - - If `include_raw` is True, then Runnable outputs a dict with keys: - - - `'raw'`: BaseMessage - - `'parsed'`: None if there was a parsing error, otherwise the type depends on the `schema` as described above. - - `'parsing_error'`: BaseException | None + - `'raw'`: `BaseMessage` + - `'parsed'`: `None` if there was a parsing error, otherwise the type + depends on the `schema` as described above. + - `'parsing_error'`: `BaseException | None` Example: schema=Pydantic class, method="function_calling", include_raw=False: - ```python - from typing import Optional + ```python + from typing import Optional - from langchain_groq import ChatGroq - from pydantic import BaseModel, Field + from langchain_groq import ChatGroq + from pydantic import BaseModel, Field - class AnswerWithJustification(BaseModel): - '''An answer to the user question along with justification for the answer.''' + class AnswerWithJustification(BaseModel): + '''An answer to the user question along with justification for the answer.''' - answer: str - # If we provide default values and/or descriptions for fields, these will be passed - # to the model. This is an important part of improving a model's ability to - # correctly return structured outputs. - justification: str | None = Field(default=None, description="A justification for the answer.") + answer: str + # If we provide default values and/or descriptions for fields, these will be passed + # to the model. This is an important part of improving a model's ability to + # correctly return structured outputs. + justification: str | None = Field(default=None, description="A justification for the answer.") - model = ChatGroq(model="openai/gpt-oss-120b", temperature=0) - structured_model = model.with_structured_output(AnswerWithJustification) + model = ChatGroq(model="openai/gpt-oss-120b", temperature=0) + structured_model = model.with_structured_output(AnswerWithJustification) - structured_model.invoke("What weighs more a pound of bricks or a pound of feathers") + structured_model.invoke("What weighs more a pound of bricks or a pound of feathers") - # -> AnswerWithJustification( - # answer='They weigh the same', - # justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.' - # ) - ``` + # -> AnswerWithJustification( + # answer='They weigh the same', + # justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.' + # ) + ``` Example: schema=Pydantic class, method="function_calling", include_raw=True: - ```python - from langchain_groq import ChatGroq - from pydantic import BaseModel + + ```python + from langchain_groq import ChatGroq + from pydantic import BaseModel - class AnswerWithJustification(BaseModel): - '''An answer to the user question along with justification for the answer.''' + class AnswerWithJustification(BaseModel): + '''An answer to the user question along with justification for the answer.''' - answer: str - justification: str + answer: str + justification: str - model = ChatGroq(model="openai/gpt-oss-120b", temperature=0) - structured_model = model.with_structured_output( - AnswerWithJustification, - include_raw=True, - ) + model = ChatGroq(model="openai/gpt-oss-120b", temperature=0) + structured_model = model.with_structured_output( + AnswerWithJustification, + include_raw=True, + ) - structured_model.invoke("What weighs more a pound of bricks or a pound of feathers") - # -> { - # 'raw': AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_Ao02pnFYXD6GN1yzc0uXPsvF', 'function': {'arguments': '{"answer":"They weigh the same.","justification":"Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ."}', 'name': 'AnswerWithJustification'}, 'type': 'function'}]}), - # 'parsed': AnswerWithJustification(answer='They weigh the same.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'), - # 'parsing_error': None - # } - ``` + structured_model.invoke("What weighs more a pound of bricks or a pound of feathers") + # -> { + # 'raw': AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_Ao02pnFYXD6GN1yzc0uXPsvF', 'function': {'arguments': '{"answer":"They weigh the same.","justification":"Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ."}', 'name': 'AnswerWithJustification'}, 'type': 'function'}]}), + # 'parsed': AnswerWithJustification(answer='They weigh the same.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'), + # 'parsing_error': None + # } + ``` Example: schema=TypedDict class, method="function_calling", include_raw=False: - ```python - from typing_extensions import Annotated, TypedDict - from langchain_groq import ChatGroq + ```python + from typing_extensions import Annotated, TypedDict + + from langchain_groq import ChatGroq - class AnswerWithJustification(TypedDict): - '''An answer to the user question along with justification for the answer.''' + class AnswerWithJustification(TypedDict): + '''An answer to the user question along with justification for the answer.''' - answer: str - justification: Annotated[str | None, None, "A justification for the answer."] + answer: str + justification: Annotated[str | None, None, "A justification for the answer."] + model = ChatGroq(model="openai/gpt-oss-120b", temperature=0) + structured_model = model.with_structured_output(AnswerWithJustification) + + structured_model.invoke("What weighs more a pound of bricks or a pound of feathers") + # -> { + # 'answer': 'They weigh the same', + # 'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.' + # } + ``` + + Example: schema=OpenAI function schema, method="function_calling", include_raw=False: + + ```python + from langchain_groq import ChatGroq + + oai_schema = { + 'name': 'AnswerWithJustification', + 'description': 'An answer to the user question along with justification for the answer.', + 'parameters': { + 'type': 'object', + 'properties': { + 'answer': {'type': 'string'}, + 'justification': {'description': 'A justification for the answer.', 'type': 'string'} + }, + 'required': ['answer'] + } + model = ChatGroq(model="openai/gpt-oss-120b", temperature=0) - structured_model = model.with_structured_output(AnswerWithJustification) + structured_model = model.with_structured_output(oai_schema) - structured_model.invoke("What weighs more a pound of bricks or a pound of feathers") + structured_model.invoke( + "What weighs more a pound of bricks or a pound of feathers" + ) # -> { # 'answer': 'They weigh the same', # 'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.' # } - ``` - - Example: schema=OpenAI function schema, method="function_calling", include_raw=False: - ```python - from langchain_groq import ChatGroq - - oai_schema = { - 'name': 'AnswerWithJustification', - 'description': 'An answer to the user question along with justification for the answer.', - 'parameters': { - 'type': 'object', - 'properties': { - 'answer': {'type': 'string'}, - 'justification': {'description': 'A justification for the answer.', 'type': 'string'} - }, - 'required': ['answer'] - } - - model = ChatGroq(model="openai/gpt-oss-120b", temperature=0) - structured_model = model.with_structured_output(oai_schema) - - structured_model.invoke( - "What weighs more a pound of bricks or a pound of feathers" - ) - # -> { - # 'answer': 'They weigh the same', - # 'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.' - # } - ``` + ``` Example: schema=Pydantic class, method="json_schema", include_raw=False: - ```python - from typing import Optional - from langchain_groq import ChatGroq - from pydantic import BaseModel, Field + ```python + from typing import Optional + + from langchain_groq import ChatGroq + from pydantic import BaseModel, Field - class AnswerWithJustification(BaseModel): - '''An answer to the user question along with justification for the answer.''' + class AnswerWithJustification(BaseModel): + '''An answer to the user question along with justification for the answer.''' - answer: str - # If we provide default values and/or descriptions for fields, these will be passed - # to the model. This is an important part of improving a model's ability to - # correctly return structured outputs. - justification: str | None = Field(default=None, description="A justification for the answer.") + answer: str + # If we provide default values and/or descriptions for fields, these will be passed + # to the model. This is an important part of improving a model's ability to + # correctly return structured outputs. + justification: str | None = Field(default=None, description="A justification for the answer.") - model = ChatGroq(model="openai/gpt-oss-120b", temperature=0) - structured_model = model.with_structured_output( - AnswerWithJustification, - method="json_schema", - ) + model = ChatGroq(model="openai/gpt-oss-120b", temperature=0) + structured_model = model.with_structured_output( + AnswerWithJustification, + method="json_schema", + ) - structured_model.invoke("What weighs more a pound of bricks or a pound of feathers") + structured_model.invoke("What weighs more a pound of bricks or a pound of feathers") - # -> AnswerWithJustification( - # answer='They weigh the same', - # justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.' - # ) - ``` + # -> AnswerWithJustification( + # answer='They weigh the same', + # justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.' + # ) + ``` Example: schema=Pydantic class, method="json_mode", include_raw=True: - ```python - from langchain_groq import ChatGroq - from pydantic import BaseModel + + ```python + from langchain_groq import ChatGroq + from pydantic import BaseModel - class AnswerWithJustification(BaseModel): - answer: str - justification: str + class AnswerWithJustification(BaseModel): + answer: str + justification: str - model = ChatGroq(model="openai/gpt-oss-120b", temperature=0) - structured_model = model.with_structured_output( - AnswerWithJustification, method="json_mode", include_raw=True - ) + model = ChatGroq(model="openai/gpt-oss-120b", temperature=0) + structured_model = model.with_structured_output( + AnswerWithJustification, method="json_mode", include_raw=True + ) - structured_model.invoke( - "Answer the following question. " - "Make sure to return a JSON blob with keys 'answer' and 'justification'.\n\n" - "What's heavier a pound of bricks or a pound of feathers?" - ) - # -> { - # 'raw': AIMessage(content='{\n "answer": "They are both the same weight.",\n "justification": "Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight." \n}'), - # 'parsed': AnswerWithJustification(answer='They are both the same weight.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight.'), - # 'parsing_error': None - # } - ``` + structured_model.invoke( + "Answer the following question. " + "Make sure to return a JSON blob with keys 'answer' and 'justification'.\n\n" + "What's heavier a pound of bricks or a pound of feathers?" + ) + # -> { + # 'raw': AIMessage(content='{\n "answer": "They are both the same weight.",\n "justification": "Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight." \n}'), + # 'parsed': AnswerWithJustification(answer='They are both the same weight.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight.'), + # 'parsing_error': None + # } + ``` """ # noqa: E501 _ = kwargs.pop("strict", None) diff --git a/libs/partners/huggingface/langchain_huggingface/chat_models/huggingface.py b/libs/partners/huggingface/langchain_huggingface/chat_models/huggingface.py index 967c2a6a61a..70e2f275e1f 100644 --- a/libs/partners/huggingface/langchain_huggingface/chat_models/huggingface.py +++ b/libs/partners/huggingface/langchain_huggingface/chat_models/huggingface.py @@ -330,18 +330,17 @@ class ChatHuggingFace(BaseChatModel): ``` Key init args — completion params: - llm: `HuggingFaceTextGenInference`, `HuggingFaceEndpoint`, `HuggingFaceHub`, or - 'HuggingFacePipeline' LLM to be used. + llm: + LLM to be used. Key init args — client params: - custom_get_token_ids: Callable[[str], list[int]] | None + custom_get_token_ids: Optional encoder to use for counting tokens. - metadata: dict[str, Any] | None + metadata: Metadata to add to the run trace. - tags: list[str] | None + tags: Tags to add to the run trace. - tokenizer: Any - verbose: bool + verbose: Whether to print out response text. See full list of supported init args and their descriptions in the params @@ -883,7 +882,7 @@ class ChatHuggingFace(BaseChatModel): - an OpenAI function/tool schema, - a JSON Schema, - - a typedDict class (support added in 0.1.7), + - a `TypedDict` class Pydantic class is currently supported. @@ -898,8 +897,10 @@ class ChatHuggingFace(BaseChatModel): an error occurs during model output parsing it will be raised. If `True` then both the raw model response (a `BaseMessage`) and the parsed model response will be returned. If an error occurs during output parsing it - will be caught and returned as well. The final output is always a dict - with keys `'raw'`, `'parsed'`, and `'parsing_error'`. + will be caught and returned as well. + + The final output is always a `dict` with keys `'raw'`, `'parsed'`, and + `'parsing_error'`. kwargs: Additional parameters to pass to the underlying LLM's @@ -907,20 +908,19 @@ class ChatHuggingFace(BaseChatModel): method, such as `response_format` or `ls_structured_output_format`. Returns: - A Runnable that takes same inputs as a `langchain_core.language_models.chat.BaseChatModel`. + A `Runnable` that takes same inputs as a + `langchain_core.language_models.chat.BaseChatModel`. If `include_raw` is + `False` and `schema` is a Pydantic class, `Runnable` outputs an instance + of `schema` (i.e., a Pydantic object). Otherwise, if `include_raw` is + `False` then `Runnable` outputs a `dict`. - If `include_raw` is False and `schema` is a Pydantic class, Runnable outputs - an instance of `schema` (i.e., a Pydantic object). + If `include_raw` is `True`, then `Runnable` outputs a `dict` with keys: - Otherwise, if `include_raw` is False then Runnable outputs a dict. - - If `include_raw` is True, then Runnable outputs a dict with keys: - - - `'raw'`: BaseMessage - - `'parsed'`: None if there was a parsing error, otherwise the type depends on the `schema` as described above. - - `'parsing_error'`: BaseException | None - - """ # noqa: E501 + - `'raw'`: `BaseMessage` + - `'parsed'`: `None` if there was a parsing error, otherwise the type + depends on the `schema` as described above. + - `'parsing_error'`: `BaseException | None` + """ _ = kwargs.pop("strict", None) if kwargs: msg = f"Received unsupported arguments {kwargs}" diff --git a/libs/partners/mistralai/langchain_mistralai/chat_models.py b/libs/partners/mistralai/langchain_mistralai/chat_models.py index 9c56f300a09..78cba61dc32 100644 --- a/libs/partners/mistralai/langchain_mistralai/chat_models.py +++ b/libs/partners/mistralai/langchain_mistralai/chat_models.py @@ -807,15 +807,17 @@ class ChatMistralAI(BaseChatModel): - an OpenAI function/tool schema, - a JSON Schema, - - a `TypedDict` class (support added in 0.1.12), + - a `TypedDict` class, - or a Pydantic class. If `schema` is a Pydantic class then the model output will be a Pydantic instance of that class, and the model-generated fields will be validated by the Pydantic class. Otherwise the model output will be a - dict and will not be validated. See `langchain_core.utils.function_calling.convert_to_openai_tool` - for more on how to properly specify types and descriptions of - schema fields when specifying a Pydantic or `TypedDict` class. + dict and will not be validated. + + See `langchain_core.utils.function_calling.convert_to_openai_tool` for + more on how to properly specify types and descriptions of schema fields + when specifying a Pydantic or `TypedDict` class. method: The method for steering model generation, one of: @@ -840,8 +842,10 @@ class ChatMistralAI(BaseChatModel): an error occurs during model output parsing it will be raised. If `True` then both the raw model response (a `BaseMessage`) and the parsed model response will be returned. If an error occurs during output parsing it - will be caught and returned as well. The final output is always a dict - with keys `'raw'`, `'parsed'`, and `'parsing_error'`. + will be caught and returned as well. + + The final output is always a `dict` with keys `'raw'`, `'parsed'`, and + `'parsing_error'`. kwargs: Any additional parameters are passed directly to `self.bind(**kwargs)`. This is useful for passing in @@ -850,97 +854,130 @@ class ChatMistralAI(BaseChatModel): `stop` to control when the model should stop generating output. Returns: - A Runnable that takes same inputs as a `langchain_core.language_models.chat.BaseChatModel`. + A `Runnable` that takes same inputs as a + `langchain_core.language_models.chat.BaseChatModel`. If `include_raw` is + `False` and `schema` is a Pydantic class, `Runnable` outputs an instance + of `schema` (i.e., a Pydantic object). Otherwise, if `include_raw` is + `False` then `Runnable` outputs a `dict`. - If `include_raw` is False and `schema` is a Pydantic class, Runnable outputs - an instance of `schema` (i.e., a Pydantic object). + If `include_raw` is `True`, then `Runnable` outputs a `dict` with keys: - Otherwise, if `include_raw` is False then Runnable outputs a dict. - - If `include_raw` is True, then Runnable outputs a dict with keys: - - `'raw'`: BaseMessage - - `'parsed'`: None if there was a parsing error, otherwise the type depends on the `schema` as described above. - - `'parsing_error'`: BaseException | None + - `'raw'`: `BaseMessage` + - `'parsed'`: `None` if there was a parsing error, otherwise the type + depends on the `schema` as described above. + - `'parsing_error'`: `BaseException | None` Example: schema=Pydantic class, method="function_calling", include_raw=False: - ```python - from typing import Optional - from langchain_mistralai import ChatMistralAI - from pydantic import BaseModel, Field + ```python + from typing import Optional + + from langchain_mistralai import ChatMistralAI + from pydantic import BaseModel, Field - class AnswerWithJustification(BaseModel): - '''An answer to the user question along with justification for the answer.''' + class AnswerWithJustification(BaseModel): + '''An answer to the user question along with justification for the answer.''' - answer: str - # If we provide default values and/or descriptions for fields, these will be passed - # to the model. This is an important part of improving a model's ability to - # correctly return structured outputs. - justification: str | None = Field( - default=None, description="A justification for the answer." - ) - - - model = ChatMistralAI(model="mistral-large-latest", temperature=0) - structured_model = model.with_structured_output(AnswerWithJustification) - - structured_model.invoke( - "What weighs more a pound of bricks or a pound of feathers" + answer: str + # If we provide default values and/or descriptions for fields, these will be passed + # to the model. This is an important part of improving a model's ability to + # correctly return structured outputs. + justification: str | None = Field( + default=None, description="A justification for the answer." ) - # -> AnswerWithJustification( - # answer='They weigh the same', - # justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.' - # ) - ``` + + model = ChatMistralAI(model="mistral-large-latest", temperature=0) + structured_model = model.with_structured_output(AnswerWithJustification) + + structured_model.invoke( + "What weighs more a pound of bricks or a pound of feathers" + ) + + # -> AnswerWithJustification( + # answer='They weigh the same', + # justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.' + # ) + ``` Example: schema=Pydantic class, method="function_calling", include_raw=True: - ```python - from langchain_mistralai import ChatMistralAI - from pydantic import BaseModel + + ```python + from langchain_mistralai import ChatMistralAI + from pydantic import BaseModel - class AnswerWithJustification(BaseModel): - '''An answer to the user question along with justification for the answer.''' + class AnswerWithJustification(BaseModel): + '''An answer to the user question along with justification for the answer.''' - answer: str - justification: str + answer: str + justification: str - model = ChatMistralAI(model="mistral-large-latest", temperature=0) - structured_model = model.with_structured_output( - AnswerWithJustification, include_raw=True - ) + model = ChatMistralAI(model="mistral-large-latest", temperature=0) + structured_model = model.with_structured_output( + AnswerWithJustification, include_raw=True + ) - structured_model.invoke( - "What weighs more a pound of bricks or a pound of feathers" - ) - # -> { - # 'raw': AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_Ao02pnFYXD6GN1yzc0uXPsvF', 'function': {'arguments': '{"answer":"They weigh the same.","justification":"Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ."}', 'name': 'AnswerWithJustification'}, 'type': 'function'}]}), - # 'parsed': AnswerWithJustification(answer='They weigh the same.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'), - # 'parsing_error': None - # } - ``` + structured_model.invoke( + "What weighs more a pound of bricks or a pound of feathers" + ) + # -> { + # 'raw': AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_Ao02pnFYXD6GN1yzc0uXPsvF', 'function': {'arguments': '{"answer":"They weigh the same.","justification":"Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ."}', 'name': 'AnswerWithJustification'}, 'type': 'function'}]}), + # 'parsed': AnswerWithJustification(answer='They weigh the same.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'), + # 'parsing_error': None + # } + ``` Example: schema=TypedDict class, method="function_calling", include_raw=False: - ```python - from typing_extensions import Annotated, TypedDict - from langchain_mistralai import ChatMistralAI + ```python + from typing_extensions import Annotated, TypedDict + + from langchain_mistralai import ChatMistralAI - class AnswerWithJustification(TypedDict): - '''An answer to the user question along with justification for the answer.''' + class AnswerWithJustification(TypedDict): + '''An answer to the user question along with justification for the answer.''' - answer: str - justification: Annotated[ - str | None, None, "A justification for the answer." - ] + answer: str + justification: Annotated[ + str | None, None, "A justification for the answer." + ] + model = ChatMistralAI(model="mistral-large-latest", temperature=0) + structured_model = model.with_structured_output(AnswerWithJustification) + + structured_model.invoke( + "What weighs more a pound of bricks or a pound of feathers" + ) + # -> { + # 'answer': 'They weigh the same', + # 'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.' + # } + ``` + + Example: schema=OpenAI function schema, method="function_calling", include_raw=False: + + ```python + from langchain_mistralai import ChatMistralAI + + oai_schema = { + 'name': 'AnswerWithJustification', + 'description': 'An answer to the user question along with justification for the answer.', + 'parameters': { + 'type': 'object', + 'properties': { + 'answer': {'type': 'string'}, + 'justification': {'description': 'A justification for the answer.', 'type': 'string'} + }, + 'required': ['answer'] + } + model = ChatMistralAI(model="mistral-large-latest", temperature=0) - structured_model = model.with_structured_output(AnswerWithJustification) + structured_model = model.with_structured_output(oai_schema) structured_model.invoke( "What weighs more a pound of bricks or a pound of feathers" @@ -949,85 +986,58 @@ class ChatMistralAI(BaseChatModel): # 'answer': 'They weigh the same', # 'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.' # } - ``` - - Example: schema=OpenAI function schema, method="function_calling", include_raw=False: - ```python - from langchain_mistralai import ChatMistralAI - - oai_schema = { - 'name': 'AnswerWithJustification', - 'description': 'An answer to the user question along with justification for the answer.', - 'parameters': { - 'type': 'object', - 'properties': { - 'answer': {'type': 'string'}, - 'justification': {'description': 'A justification for the answer.', 'type': 'string'} - }, - 'required': ['answer'] - } - - model = ChatMistralAI(model="mistral-large-latest", temperature=0) - structured_model = model.with_structured_output(oai_schema) - - structured_model.invoke( - "What weighs more a pound of bricks or a pound of feathers" - ) - # -> { - # 'answer': 'They weigh the same', - # 'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.' - # } - ``` + ``` Example: schema=Pydantic class, method="json_mode", include_raw=True: - ```python - from langchain_mistralai import ChatMistralAI - from pydantic import BaseModel + + ```python + from langchain_mistralai import ChatMistralAI + from pydantic import BaseModel - class AnswerWithJustification(BaseModel): - answer: str - justification: str + class AnswerWithJustification(BaseModel): + answer: str + justification: str - model = ChatMistralAI(model="mistral-large-latest", temperature=0) - structured_model = model.with_structured_output( - AnswerWithJustification, method="json_mode", include_raw=True - ) + model = ChatMistralAI(model="mistral-large-latest", temperature=0) + structured_model = model.with_structured_output( + AnswerWithJustification, method="json_mode", include_raw=True + ) - structured_model.invoke( - "Answer the following question. " - "Make sure to return a JSON blob with keys 'answer' and 'justification'.\\n\\n" - "What's heavier a pound of bricks or a pound of feathers?" - ) - # -> { - # 'raw': AIMessage(content='{\\n "answer": "They are both the same weight.",\\n "justification": "Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight." \\n}'), - # 'parsed': AnswerWithJustification(answer='They are both the same weight.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight.'), - # 'parsing_error': None - # } - ``` + structured_model.invoke( + "Answer the following question. " + "Make sure to return a JSON blob with keys 'answer' and 'justification'.\\n\\n" + "What's heavier a pound of bricks or a pound of feathers?" + ) + # -> { + # 'raw': AIMessage(content='{\\n "answer": "They are both the same weight.",\\n "justification": "Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight." \\n}'), + # 'parsed': AnswerWithJustification(answer='They are both the same weight.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight.'), + # 'parsing_error': None + # } + ``` Example: schema=None, method="json_mode", include_raw=True: - ```python - structured_model = model.with_structured_output( - method="json_mode", include_raw=True - ) - structured_model.invoke( - "Answer the following question. " - "Make sure to return a JSON blob with keys 'answer' and 'justification'.\\n\\n" - "What's heavier a pound of bricks or a pound of feathers?" - ) - # -> { - # 'raw': AIMessage(content='{\\n "answer": "They are both the same weight.",\\n "justification": "Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight." \\n}'), - # 'parsed': { - # 'answer': 'They are both the same weight.', - # 'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight.' - # }, - # 'parsing_error': None - # } - ``` + ```python + structured_model = model.with_structured_output( + method="json_mode", include_raw=True + ) + structured_model.invoke( + "Answer the following question. " + "Make sure to return a JSON blob with keys 'answer' and 'justification'.\\n\\n" + "What's heavier a pound of bricks or a pound of feathers?" + ) + # -> { + # 'raw': AIMessage(content='{\\n "answer": "They are both the same weight.",\\n "justification": "Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight." \\n}'), + # 'parsed': { + # 'answer': 'They are both the same weight.', + # 'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight.' + # }, + # 'parsing_error': None + # } + ``` """ # noqa: E501 _ = kwargs.pop("strict", None) if kwargs: diff --git a/libs/partners/mistralai/langchain_mistralai/embeddings.py b/libs/partners/mistralai/langchain_mistralai/embeddings.py index 26093d15c20..5fd0d6bb62c 100644 --- a/libs/partners/mistralai/langchain_mistralai/embeddings.py +++ b/libs/partners/mistralai/langchain_mistralai/embeddings.py @@ -50,21 +50,21 @@ class MistralAIEmbeddings(BaseModel, Embeddings): ``` Key init args — completion params: - model: str - Name of MistralAI model to use. + model: + Name of `MistralAI` model to use. Key init args — client params: - api_key: SecretStr | None + api_key: The API key for the MistralAI API. If not provided, it will be read from the environment variable `MISTRAL_API_KEY`. - max_retries: int + max_retries: The number of times to retry a request if it fails. - timeout: int + timeout: The number of seconds to wait for a response before timing out. - wait_time: int + wait_time: The number of seconds to wait before retrying a request in case of 429 error. - max_concurrent_requests: int + max_concurrent_requests: The maximum number of concurrent requests to make to the Mistral API. See full list of supported init args and their descriptions in the params section. diff --git a/libs/partners/nomic/langchain_nomic/embeddings.py b/libs/partners/nomic/langchain_nomic/embeddings.py index 1bcc6ce00e1..63571345767 100644 --- a/libs/partners/nomic/langchain_nomic/embeddings.py +++ b/libs/partners/nomic/langchain_nomic/embeddings.py @@ -63,7 +63,7 @@ class NomicEmbeddings(Embeddings): device: str | None = None, vision_model: str | None = None, ): - """Initialize NomicEmbeddings model. + """Initialize `NomicEmbeddings` model. Args: model: model name diff --git a/libs/partners/ollama/langchain_ollama/chat_models.py b/libs/partners/ollama/langchain_ollama/chat_models.py index 9eefcb04e3d..149a3977372 100644 --- a/libs/partners/ollama/langchain_ollama/chat_models.py +++ b/libs/partners/ollama/langchain_ollama/chat_models.py @@ -1258,17 +1258,19 @@ class ChatOllama(BaseChatModel): Args: schema: The output schema. Can be passed in as: - - a Pydantic class, - - a JSON schema - - a `TypedDict` class - an OpenAI function/tool schema. + - a JSON Schema, + - a `TypedDict` class, + - or a Pydantic class. If `schema` is a Pydantic class then the model output will be a Pydantic instance of that class, and the model-generated fields will be validated by the Pydantic class. Otherwise the model output will be a - dict and will not be validated. See `langchain_core.utils.function_calling.convert_to_openai_tool` - for more on how to properly specify types and descriptions of - schema fields when specifying a Pydantic or `TypedDict` class. + dict and will not be validated. + + See `langchain_core.utils.function_calling.convert_to_openai_tool` for + more on how to properly specify types and descriptions of schema fields + when specifying a Pydantic or `TypedDict` class. method: The method for steering model generation, one of: @@ -1286,21 +1288,26 @@ class ChatOllama(BaseChatModel): an error occurs during model output parsing it will be raised. If `True` then both the raw model response (a `BaseMessage`) and the parsed model response will be returned. If an error occurs during output parsing it - will be caught and returned as well. The final output is always a dict - with keys `'raw'`, `'parsed'`, and `'parsing_error'`. + will be caught and returned as well. + + The final output is always a `dict` with keys `'raw'`, `'parsed'`, and + `'parsing_error'`. kwargs: Additional keyword args aren't supported. Returns: - A Runnable that takes same inputs as a `langchain_core.language_models.chat.BaseChatModel`. + A `Runnable` that takes same inputs as a + `langchain_core.language_models.chat.BaseChatModel`. If `include_raw` is + `False` and `schema` is a Pydantic class, `Runnable` outputs an instance + of `schema` (i.e., a Pydantic object). Otherwise, if `include_raw` is + `False` then `Runnable` outputs a `dict`. - If `include_raw` is False and `schema` is a Pydantic class, Runnable outputs an instance of `schema` (i.e., a Pydantic object). Otherwise, if `include_raw` is False then Runnable outputs a dict. + If `include_raw` is `True`, then `Runnable` outputs a `dict` with keys: - If `include_raw` is True, then Runnable outputs a dict with keys: - - - `'raw'`: `BaseMessage` - - `'parsed'`: None if there was a parsing error, otherwise the type depends on the `schema` as described above. - - `'parsing_error'`: BaseException | None + - `'raw'`: `BaseMessage` + - `'parsed'`: `None` if there was a parsing error, otherwise the type + depends on the `schema` as described above. + - `'parsing_error'`: `BaseException | None` !!! warning "Behavior changed in 0.2.2" Added support for structured output API via `format` parameter. diff --git a/libs/partners/ollama/langchain_ollama/llms.py b/libs/partners/ollama/langchain_ollama/llms.py index 42b387a381c..898259361b1 100644 --- a/libs/partners/ollama/langchain_ollama/llms.py +++ b/libs/partners/ollama/langchain_ollama/llms.py @@ -53,11 +53,11 @@ class OllamaLLM(BaseLLM): Random number seed for generation reproducibility. Key init args — client params: - base_url: str | None + base_url: Base URL where Ollama server is hosted. - keep_alive: int | str | None + keep_alive: How long the model stays loaded into memory. - format: Literal["", "json"] + format: Specify the format of the output. See full list of supported init args and their descriptions in the params section. diff --git a/libs/partners/openai/langchain_openai/chat_models/azure.py b/libs/partners/openai/langchain_openai/chat_models/azure.py index 98e99ab09f7..429d599af91 100644 --- a/libs/partners/openai/langchain_openai/chat_models/azure.py +++ b/libs/partners/openai/langchain_openai/chat_models/azure.py @@ -49,30 +49,30 @@ class AzureChatOpenAI(BaseChatOpenAI): ``` Key init args — completion params: - azure_deployment: str + azure_deployment: Name of Azure OpenAI deployment to use. - temperature: float + temperature: Sampling temperature. - max_tokens: int | None + max_tokens: Max number of tokens to generate. - logprobs: bool | None + logprobs: Whether to return logprobs. Key init args — client params: - api_version: str + api_version: Azure OpenAI REST API version to use (distinct from the version of the underlying model). [See more on the different versions.](https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#rest-api-versioning) - timeout: Union[float, Tuple[float, float], Any, None] + timeout: Timeout for requests. - max_retries: int | None + max_retries: Max number of retries. - organization: str | None + organization: OpenAI organization ID. If not passed in will be read from env var `OPENAI_ORG_ID`. - model: str | None + model: The name of the underlying OpenAI model. Used for tracing and token counting. Does not affect completion. E.g. `'gpt-4'`, `'gpt-35-turbo'`, etc. - model_version: str | None + model_version: The version of the underlying OpenAI model. Used for tracing and token counting. Does not affect completion. E.g., `'0125'`, `'0125-preview'`, etc. @@ -835,15 +835,17 @@ class AzureChatOpenAI(BaseChatOpenAI): - a JSON Schema, - a `TypedDict` class, - - or a Pydantic class, - - an OpenAI function/tool schema. + - a Pydantic class, + - or an OpenAI function/tool schema. If `schema` is a Pydantic class then the model output will be a Pydantic instance of that class, and the model-generated fields will be validated by the Pydantic class. Otherwise the model output will be a - dict and will not be validated. See `langchain_core.utils.function_calling.convert_to_openai_tool` - for more on how to properly specify types and descriptions of - schema fields when specifying a Pydantic or `TypedDict` class. + dict and will not be validated. + + See `langchain_core.utils.function_calling.convert_to_openai_tool` for + more on how to properly specify types and descriptions of schema fields + when specifying a Pydantic or `TypedDict` class. method: The method for steering model generation, one of: @@ -867,8 +869,10 @@ class AzureChatOpenAI(BaseChatOpenAI): an error occurs during model output parsing it will be raised. If `True` then both the raw model response (a `BaseMessage`) and the parsed model response will be returned. If an error occurs during output parsing it - will be caught and returned as well. The final output is always a dict - with keys `'raw'`, `'parsed'`, and `'parsing_error'`. + will be caught and returned as well. + + The final output is always a `dict` with keys `'raw'`, `'parsed'`, and + `'parsing_error'`. strict: - True: @@ -934,16 +938,18 @@ class AzureChatOpenAI(BaseChatOpenAI): kwargs: Additional keyword args are passed through to the model. Returns: - A Runnable that takes same inputs as a `langchain_core.language_models.chat.BaseChatModel`. + A `Runnable` that takes same inputs as a + `langchain_core.language_models.chat.BaseChatModel`. If `include_raw` is + `False` and `schema` is a Pydantic class, `Runnable` outputs an instance + of `schema` (i.e., a Pydantic object). Otherwise, if `include_raw` is + `False` then `Runnable` outputs a `dict`. - If `include_raw` is False and `schema` is a Pydantic class, Runnable outputs - an instance of `schema` (i.e., a Pydantic object). Otherwise, if `include_raw` is False then Runnable outputs a dict. + If `include_raw` is `True`, then `Runnable` outputs a `dict` with keys: - If `include_raw` is True, then Runnable outputs a dict with keys: - - - `'raw'`: BaseMessage - - `'parsed'`: None if there was a parsing error, otherwise the type depends on the `schema` as described above. - - `'parsing_error'`: BaseException | None + - `'raw'`: `BaseMessage` + - `'parsed'`: `None` if there was a parsing error, otherwise the type + depends on the `schema` as described above. + - `'parsing_error'`: `BaseException | None` !!! warning "Behavior changed in 0.3.0" `method` default changed from "function_calling" to "json_schema". diff --git a/libs/partners/openai/langchain_openai/chat_models/base.py b/libs/partners/openai/langchain_openai/chat_models/base.py index 9be2f7c1a2c..a3eee17aa58 100644 --- a/libs/partners/openai/langchain_openai/chat_models/base.py +++ b/libs/partners/openai/langchain_openai/chat_models/base.py @@ -1744,9 +1744,11 @@ class BaseChatOpenAI(BaseChatModel): If `schema` is a Pydantic class then the model output will be a Pydantic instance of that class, and the model-generated fields will be validated by the Pydantic class. Otherwise the model output will be a - dict and will not be validated. See `langchain_core.utils.function_calling.convert_to_openai_tool` - for more on how to properly specify types and descriptions of - schema fields when specifying a Pydantic or `TypedDict` class. + dict and will not be validated. + + See `langchain_core.utils.function_calling.convert_to_openai_tool` for + more on how to properly specify types and descriptions of schema fields + when specifying a Pydantic or `TypedDict` class. method: The method for steering model generation, one of: @@ -1765,8 +1767,10 @@ class BaseChatOpenAI(BaseChatModel): an error occurs during model output parsing it will be raised. If `True` then both the raw model response (a `BaseMessage`) and the parsed model response will be returned. If an error occurs during output parsing it - will be caught and returned as well. The final output is always a dict - with keys `'raw'`, `'parsed'`, and `'parsing_error'`. + will be caught and returned as well. + + The final output is always a `dict` with keys `'raw'`, `'parsed'`, and + `'parsing_error'`. strict: - `True`: @@ -1827,25 +1831,25 @@ class BaseChatOpenAI(BaseChatModel): kwargs: Additional keyword args are passed through to the model. Returns: - A `Runnable` that takes same inputs as a `BaseChatModel`. + A `Runnable` that takes same inputs as a + `langchain_core.language_models.chat.BaseChatModel`. If `include_raw` is + `False` and `schema` is a Pydantic class, `Runnable` outputs an instance + of `schema` (i.e., a Pydantic object). Otherwise, if `include_raw` is + `False` then `Runnable` outputs a `dict`. - If `include_raw` is `False` and `schema` is a Pydantic class, `Runnable` - outputs an instance of `schema` (i.e., a Pydantic object). Otherwise, if - `include_raw` is `False` then `Runnable` outputs a `dict`. + If `include_raw` is `True`, then `Runnable` outputs a `dict` with keys: - If `include_raw` is `True`, then `Runnable` outputs a dict with keys: - - - `'raw'`: `BaseMessage` - - `'parsed'`: `None` if there was a parsing error, otherwise the type depends - on the `schema` as described above. - - `'parsing_error'`: `BaseException` or `None` + - `'raw'`: `BaseMessage` + - `'parsed'`: `None` if there was a parsing error, otherwise the type + depends on the `schema` as described above. + - `'parsing_error'`: `BaseException | None` !!! warning "Behavior changed in 0.3.12" Support for `tools` added. !!! warning "Behavior changed in 0.3.21" Pass `kwargs` through to the model. - """ # noqa: E501 + """ if strict is not None and method == "json_mode": msg = "Argument `strict` is not supported with `method`='json_mode'" raise ValueError(msg) @@ -2832,17 +2836,19 @@ class ChatOpenAI(BaseChatOpenAI): # type: ignore[override] Args: schema: The output schema. Can be passed in as: + - an OpenAI function/tool schema, - a JSON Schema, - a `TypedDict` class, - - or a Pydantic class, - - an OpenAI function/tool schema. + - or a Pydantic class. If `schema` is a Pydantic class then the model output will be a Pydantic instance of that class, and the model-generated fields will be validated by the Pydantic class. Otherwise the model output will be a - dict and will not be validated. See `langchain_core.utils.function_calling.convert_to_openai_tool` - for more on how to properly specify types and descriptions of - schema fields when specifying a Pydantic or `TypedDict` class. + dict and will not be validated. + + See `langchain_core.utils.function_calling.convert_to_openai_tool` for + more on how to properly specify types and descriptions of schema fields + when specifying a Pydantic or `TypedDict` class. method: The method for steering model generation, one of: @@ -2864,8 +2870,10 @@ class ChatOpenAI(BaseChatOpenAI): # type: ignore[override] an error occurs during model output parsing it will be raised. If `True` then both the raw model response (a `BaseMessage`) and the parsed model response will be returned. If an error occurs during output parsing it - will be caught and returned as well. The final output is always a dict - with keys `'raw'`, `'parsed'`, and `'parsing_error'`. + will be caught and returned as well. + + The final output is always a `dict` with keys `'raw'`, `'parsed'`, and + `'parsing_error'`. strict: - `True`: @@ -2931,16 +2939,18 @@ class ChatOpenAI(BaseChatOpenAI): # type: ignore[override] kwargs: Additional keyword args are passed through to the model. Returns: - A Runnable that takes same inputs as a `langchain_core.language_models.chat.BaseChatModel`. + A `Runnable` that takes same inputs as a + `langchain_core.language_models.chat.BaseChatModel`. If `include_raw` is + `False` and `schema` is a Pydantic class, `Runnable` outputs an instance + of `schema` (i.e., a Pydantic object). Otherwise, if `include_raw` is + `False` then `Runnable` outputs a `dict`. - If `include_raw` is `False` and `schema` is a Pydantic class, Runnable outputs - an instance of `schema` (i.e., a Pydantic object). Otherwise, if `include_raw` is `False` then `Runnable` outputs a `dict`. + If `include_raw` is `True`, then `Runnable` outputs a `dict` with keys: - If `include_raw` is `True`, then `Runnable` outputs a `dict` with keys: - - - `'raw'`: `BaseMessage` - - `'parsed'`: `None` if there was a parsing error, otherwise the type depends on the `schema` as described above. - - `'parsing_error'`: `BaseException` or `None` + - `'raw'`: `BaseMessage` + - `'parsed'`: `None` if there was a parsing error, otherwise the type + depends on the `schema` as described above. + - `'parsing_error'`: `BaseException | None` !!! warning "Behavior changed in 0.3.0" `method` default changed from `"function_calling"` to `"json_schema"`. diff --git a/libs/partners/openai/langchain_openai/embeddings/azure.py b/libs/partners/openai/langchain_openai/embeddings/azure.py index 3b1d03d8797..d9d13be528f 100644 --- a/libs/partners/openai/langchain_openai/embeddings/azure.py +++ b/libs/partners/openai/langchain_openai/embeddings/azure.py @@ -38,14 +38,11 @@ class AzureOpenAIEmbeddings(OpenAIEmbeddings): # type: ignore[override] ``` Key init args — completion params: - model: str - Name of AzureOpenAI model to use. - dimensions: int | None - Number of dimensions for the embeddings. Can be specified only - if the underlying model supports it. - - Key init args — client params: - api_key: SecretStr | None + model: + Name of `AzureOpenAI` model to use. + dimensions: + Number of dimensions for the embeddings. Can be specified only if the + underlying model supports it. See full list of supported init args and their descriptions in the params section. diff --git a/libs/partners/openai/langchain_openai/embeddings/base.py b/libs/partners/openai/langchain_openai/embeddings/base.py index 5f92002ad72..b0b5c9f770d 100644 --- a/libs/partners/openai/langchain_openai/embeddings/base.py +++ b/libs/partners/openai/langchain_openai/embeddings/base.py @@ -90,21 +90,21 @@ class OpenAIEmbeddings(BaseModel, Embeddings): ``` Key init args — embedding params: - model: str + model: Name of OpenAI model to use. - dimensions: int | None = None + dimensions: The number of dimensions the resulting output embeddings should have. Only supported in `'text-embedding-3'` and later models. Key init args — client params: - api_key: SecretStr | None = None + api_key: OpenAI API key. - organization: str | None = None + organization: OpenAI organization ID. If not passed in will be read from env var `OPENAI_ORG_ID`. - max_retries: int = 2 + max_retries: Maximum number of retries to make when generating. - request_timeout: float | Tuple[float, float] | Any | None = None + request_timeout: Timeout for requests to OpenAI completion API See full list of supported init args and their descriptions in the params section. diff --git a/libs/partners/openai/langchain_openai/llms/base.py b/libs/partners/openai/langchain_openai/llms/base.py index bdffd5eff7b..ed456ec89df 100644 --- a/libs/partners/openai/langchain_openai/llms/base.py +++ b/libs/partners/openai/langchain_openai/llms/base.py @@ -62,46 +62,46 @@ class BaseOpenAI(BaseLLM): ``` Key init args — completion params: - model_name: str + model_name: Name of OpenAI model to use. - temperature: float + temperature: Sampling temperature. - max_tokens: int + max_tokens: Max number of tokens to generate. - top_p: float + top_p: Total probability mass of tokens to consider at each step. - frequency_penalty: float + frequency_penalty: Penalizes repeated tokens according to frequency. - presence_penalty: float + presence_penalty: Penalizes repeated tokens. - n: int + n: How many completions to generate for each prompt. - best_of: int + best_of: Generates best_of completions server-side and returns the "best". - logit_bias: dict[str, float] | None + logit_bias: Adjust the probability of specific tokens being generated. - seed: int | None + seed: Seed for generation. - logprobs: int | None + logprobs: Include the log probabilities on the logprobs most likely output tokens. - streaming: bool + streaming: Whether to stream the results or not. Key init args — client params: - openai_api_key: SecretStr | None + openai_api_key: OpenAI API key. If not passed in will be read from env var `OPENAI_API_KEY`. - openai_api_base: str | None + openai_api_base: Base URL path for API requests, leave blank if not using a proxy or service emulator. - openai_organization: str | None + openai_organization: OpenAI organization ID. If not passed in will be read from env var `OPENAI_ORG_ID`. - request_timeout: Union[float, tuple[float, float], Any, None] + request_timeout: Timeout for requests to OpenAI completion API. - max_retries: int + max_retries: Maximum number of retries to make when generating. - batch_size: int + batch_size: Batch size to use when passing multiple documents to generate. See full list of supported init args and their descriptions in the params section. @@ -707,29 +707,29 @@ class OpenAI(BaseOpenAI): ``` Key init args — completion params: - model: str + model: Name of OpenAI model to use. - temperature: float + temperature: Sampling temperature. - max_tokens: int | None + max_tokens: Max number of tokens to generate. - logprobs: bool | None + logprobs: Whether to return logprobs. - stream_options: Dict + stream_options: Configure 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: Timeout for requests. - max_retries: int + max_retries: Max number of retries. - api_key: str | None + api_key: OpenAI API key. If not passed in will be read from env var `OPENAI_API_KEY`. - base_url: str | None + base_url: Base URL for API requests. Only specify if using a proxy or service emulator. - organization: str | None + organization: OpenAI organization ID. If not passed in will be read from env var `OPENAI_ORG_ID`. diff --git a/libs/partners/perplexity/langchain_perplexity/chat_models.py b/libs/partners/perplexity/langchain_perplexity/chat_models.py index 78ae52ce34e..e46cdd879cf 100644 --- a/libs/partners/perplexity/langchain_perplexity/chat_models.py +++ b/libs/partners/perplexity/langchain_perplexity/chat_models.py @@ -72,21 +72,21 @@ class ChatPerplexity(BaseChatModel): ``` Key init args - completion params: - model: str + model: Name of the model to use. e.g. "sonar" - temperature: float + temperature: Sampling temperature to use. - max_tokens: int | None + max_tokens: Maximum number of tokens to generate. - streaming: bool + streaming: Whether to stream the results or not. Key init args - client params: - pplx_api_key: str | None + pplx_api_key: API key for PerplexityChat API. - request_timeout: float | Tuple[float, float] | None + request_timeout: Timeout for requests to PerplexityChat completion API. - max_retries: int + max_retries: Maximum number of retries to make when generating. See full list of supported init args and their descriptions in the params section. @@ -427,8 +427,10 @@ class ChatPerplexity(BaseChatModel): an error occurs during model output parsing it will be raised. If `True` then both the raw model response (a `BaseMessage`) and the parsed model response will be returned. If an error occurs during output parsing it - will be caught and returned as well. The final output is always a dict - with keys `'raw'`, `'parsed'`, and `'parsing_error'`. + will be caught and returned as well. + + The final output is always a `dict` with keys `'raw'`, `'parsed'`, and + `'parsing_error'`. strict: Unsupported: whether to enable strict schema adherence when generating @@ -438,17 +440,18 @@ class ChatPerplexity(BaseChatModel): kwargs: Additional keyword args aren't supported. Returns: - A Runnable that takes same inputs as a `langchain_core.language_models.chat.BaseChatModel`. + A `Runnable` that takes same inputs as a + `langchain_core.language_models.chat.BaseChatModel`. If `include_raw` is + `False` and `schema` is a Pydantic class, `Runnable` outputs an instance + of `schema` (i.e., a Pydantic object). Otherwise, if `include_raw` is + `False` then `Runnable` outputs a `dict`. - If `include_raw` is False and `schema` is a Pydantic class, Runnable outputs - an instance of `schema` (i.e., a Pydantic object). Otherwise, if `include_raw` is False then Runnable outputs a dict. - - If `include_raw` is True, then Runnable outputs a dict with keys: - - - `'raw'`: BaseMessage - - `'parsed'`: None if there was a parsing error, otherwise the type depends on the `schema` as described above. - - `'parsing_error'`: BaseException | None + If `include_raw` is `True`, then `Runnable` outputs a `dict` with keys: + - `'raw'`: `BaseMessage` + - `'parsed'`: `None` if there was a parsing error, otherwise the type + depends on the `schema` as described above. + - `'parsing_error'`: `BaseException | None` """ # noqa: E501 if method in ("function_calling", "json_mode"): method = "json_schema" diff --git a/libs/partners/qdrant/langchain_qdrant/fastembed_sparse.py b/libs/partners/qdrant/langchain_qdrant/fastembed_sparse.py index d94c39e1757..8838c3ea340 100644 --- a/libs/partners/qdrant/langchain_qdrant/fastembed_sparse.py +++ b/libs/partners/qdrant/langchain_qdrant/fastembed_sparse.py @@ -42,9 +42,9 @@ class FastEmbedSparse(SparseEmbeddings): use default onnxruntime threading instead.\ kwargs: Additional options to pass to `fastembed.SparseTextEmbedding` + Raises: ValueError: If the `model_name` is not supported in `SparseTextEmbedding`. - """ try: from fastembed import ( # type: ignore[import-not-found] # noqa: PLC0415 diff --git a/libs/partners/qdrant/langchain_qdrant/qdrant.py b/libs/partners/qdrant/langchain_qdrant/qdrant.py index 496240a464e..780f9d9eeca 100644 --- a/libs/partners/qdrant/langchain_qdrant/qdrant.py +++ b/libs/partners/qdrant/langchain_qdrant/qdrant.py @@ -47,17 +47,17 @@ class QdrantVectorStore(VectorStore): ``` Key init args — indexing params: - collection_name: str + collection_name: Name of the collection. - embedding: Embeddings + embedding: Embedding function to use. - sparse_embedding: SparseEmbeddings + sparse_embedding: Optional sparse embedding function to use. Key init args — client params: - client: QdrantClient + client: Qdrant client to use. - retrieval_mode: RetrievalMode + retrieval_mode: Retrieval mode to use. Instantiate: @@ -376,6 +376,7 @@ class QdrantVectorStore(VectorStore): """Construct an instance of `QdrantVectorStore` from a list of texts. This is a user-friendly interface that: + 1. Creates embeddings, one for each text 2. Creates a Qdrant collection if it doesn't exist. 3. Adds the text embeddings to the Qdrant database @@ -502,10 +503,10 @@ class QdrantVectorStore(VectorStore): batch_size: int = 64, **kwargs: Any, ) -> list[str | int]: - """Add texts with embeddings to the vectorstore. + """Add texts with embeddings to the `VectorStore`. Returns: - List of ids from adding the texts into the vectorstore. + List of ids from adding the texts into the `VectorStore`. """ added_ids = [] @@ -534,7 +535,7 @@ class QdrantVectorStore(VectorStore): """Return docs most similar to query. Returns: - List of Documents most similar to the query. + List of `Document` objects most similar to the query. """ results = self.similarity_search_with_score( @@ -658,7 +659,7 @@ class QdrantVectorStore(VectorStore): """Return docs most similar to embedding vector. Returns: - List of Documents most similar to the query and distance for each. + List of `Document` objects most similar to the query and distance for each. """ qdrant_filter = filter @@ -712,7 +713,7 @@ class QdrantVectorStore(VectorStore): """Return docs most similar to embedding vector. Returns: - List of Documents most similar to the query. + List of `Document` objects most similar to the query. """ results = self.similarity_search_with_score_by_vector( @@ -745,7 +746,7 @@ class QdrantVectorStore(VectorStore): among selected documents. Returns: - List of Documents selected by maximal marginal relevance. + List of `Document` objects selected by maximal marginal relevance. """ self._validate_collection_for_dense( @@ -788,7 +789,7 @@ class QdrantVectorStore(VectorStore): among selected documents. Returns: - List of Documents selected by maximal marginal relevance. + List of `Document` objects selected by maximal marginal relevance. """ results = self.max_marginal_relevance_search_with_score_by_vector( @@ -822,9 +823,8 @@ class QdrantVectorStore(VectorStore): among selected documents. Returns: - List of Documents selected by maximal marginal relevance and distance for - each. - + List of `Document` objects selected by maximal marginal relevance and + distance for each. """ results = self.client.query_points( collection_name=self.collection_name, diff --git a/libs/partners/qdrant/langchain_qdrant/vectorstores.py b/libs/partners/qdrant/langchain_qdrant/vectorstores.py index f7e3eb5e735..e149fd54b9c 100644 --- a/libs/partners/qdrant/langchain_qdrant/vectorstores.py +++ b/libs/partners/qdrant/langchain_qdrant/vectorstores.py @@ -151,10 +151,10 @@ class Qdrant(VectorStore): batch_size: int = 64, **kwargs: Any, ) -> list[str]: - """Run more texts through the embeddings and add to the vectorstore. + """Run more texts through the embeddings and add to the `VectorStore`. Args: - texts: Iterable of strings to add to the vectorstore. + texts: Iterable of strings to add to the `VectorStore`. metadatas: Optional list of metadatas associated with the texts. ids: Optional list of ids to associate with the texts. Ids have to be @@ -165,7 +165,7 @@ class Qdrant(VectorStore): **kwargs: Additional keyword arguments. Returns: - List of ids from adding the texts into the vectorstore. + List of ids from adding the texts into the `VectorStore`. """ added_ids = [] @@ -188,10 +188,10 @@ class Qdrant(VectorStore): batch_size: int = 64, **kwargs: Any, ) -> list[str]: - """Run more texts through the embeddings and add to the vectorstore. + """Run more texts through the embeddings and add to the `VectorStore`. Args: - texts: Iterable of strings to add to the vectorstore. + texts: Iterable of strings to add to the `VectorStore`. metadatas: Optional list of metadatas associated with the texts. ids: Optional list of ids to associate with the texts. Ids have to be @@ -202,7 +202,7 @@ class Qdrant(VectorStore): **kwargs: Additional keyword arguments. Returns: - List of ids from adding the texts into the vectorstore. + List of ids from adding the texts into the `VectorStore`. """ if self.async_client is None or isinstance( @@ -265,7 +265,7 @@ class Qdrant(VectorStore): Any other named arguments to pass through to QdrantClient.search() Returns: - List of Documents most similar to the query. + List of `Document` objects most similar to the query. """ results = self.similarity_search_with_score( @@ -297,7 +297,7 @@ class Qdrant(VectorStore): **kwargs: Additional keyword arguments. Returns: - List of Documents most similar to the query. + List of `Document` objects most similar to the query. """ results = await self.asimilarity_search_with_score(query, k, filter, **kwargs) @@ -463,7 +463,7 @@ class Qdrant(VectorStore): Any other named arguments to pass through to QdrantClient.search() Returns: - List of Documents most similar to the query. + List of `Document` objects most similar to the query. """ results = self.similarity_search_with_score_by_vector( @@ -523,7 +523,7 @@ class Qdrant(VectorStore): AsyncQdrantClient.Search(). Returns: - List of Documents most similar to the query. + List of `Document` objects most similar to the query. """ results = await self.asimilarity_search_with_score_by_vector( @@ -768,7 +768,7 @@ class Qdrant(VectorStore): Any other named arguments to pass through to QdrantClient.search() Returns: - List of Documents selected by maximal marginal relevance. + List of `Document` objects selected by maximal marginal relevance. """ query_embedding = self._embed_query(query) @@ -1020,17 +1020,16 @@ class Qdrant(VectorStore): - int - number of replicas to query, values should present in all queried replicas - 'majority' - query all replicas, but return values present in the - majority of replicas + majority of replicas - 'quorum' - query the majority of replicas, return values present in - all of them + all of them - 'all' - query all replicas, and return values present in all replicas **kwargs: Any other named arguments to pass through to QdrantClient.search() Returns: - List of Documents selected by maximal marginal relevance and distance for - each. - + List of `Document` objects selected by maximal marginal relevance and + distance for each. """ query_vector = embedding if self.vector_name is not None: @@ -1102,9 +1101,8 @@ class Qdrant(VectorStore): **kwargs: Additional keyword arguments. Returns: - List of Documents selected by maximal marginal relevance and distance for - each. - + List of `Document` objects selected by maximal marginal relevance and + distance for each. """ if self.async_client is None or isinstance( self.async_client._client, AsyncQdrantLocal @@ -1320,9 +1318,10 @@ class Qdrant(VectorStore): Additional arguments passed directly into REST client initialization This is a user-friendly interface that: + 1. Creates embeddings, one for each text 2. Initializes the Qdrant database as an in-memory docstore by default - (and overridable to a remote docstore) + (and overridable to a remote docstore) 3. Adds the text embeddings to the Qdrant database This is intended to be a quick way to get started. @@ -1560,6 +1559,7 @@ class Qdrant(VectorStore): Additional arguments passed directly into REST client initialization This is a user-friendly interface that: + 1. Creates embeddings, one for each text 2. Initializes the Qdrant database as an in-memory docstore by default (and overridable to a remote docstore) @@ -1973,19 +1973,19 @@ class Qdrant(VectorStore): k: int = 4, **kwargs: Any, ) -> list[tuple[Document, float]]: - """Return docs and relevance scores in the range [0, 1]. + """Return docs and relevance scores in the range `[0, 1]`. - 0 is dissimilar, 1 is most similar. + `0` is dissimilar, `1` is most similar. Args: query: input text k: Number of Documents to return. - **kwargs: kwargs to be passed to similarity search. Should include: - score_threshold: Optional, a floating point value between 0 to 1 to - filter the resulting set of retrieved docs + **kwargs: kwargs to be passed to similarity search. Should include + `score_threshold`, An optional floating point value between `0` to `1` + to filter the resulting set of retrieved docs Returns: - List of Tuples of (doc, similarity_score) + List of Tuples of `(doc, similarity_score)` """ return self.similarity_search_with_score(query, k, **kwargs) @@ -1997,19 +1997,19 @@ class Qdrant(VectorStore): k: int = 4, **kwargs: Any, ) -> list[tuple[Document, float]]: - """Return docs and relevance scores in the range [0, 1]. + """Return docs and relevance scores in the range `[0, 1]`. - 0 is dissimilar, 1 is most similar. + `0` is dissimilar, `1` is most similar. Args: query: input text k: Number of Documents to return. - **kwargs: kwargs to be passed to similarity search. Should include: - score_threshold: Optional, a floating point value between 0 to 1 to - filter the resulting set of retrieved docs + **kwargs: kwargs to be passed to similarity search. Should include + `score_threshold`, An optional floating point value between `0` to `1` + to filter the resulting set of retrieved docs Returns: - List of Tuples of (doc, similarity_score) + List of Tuples of `(doc, similarity_score)` """ return await self.asimilarity_search_with_score(query, k, **kwargs) diff --git a/libs/partners/xai/langchain_xai/chat_models.py b/libs/partners/xai/langchain_xai/chat_models.py index c3e1b9bdad6..22701217153 100644 --- a/libs/partners/xai/langchain_xai/chat_models.py +++ b/libs/partners/xai/langchain_xai/chat_models.py @@ -38,24 +38,24 @@ class ChatXAI(BaseChatOpenAI): # type: ignore[override] ``` Key init args — completion params: - model: str + model: Name of model to use. - temperature: float + temperature: Sampling temperature between `0` and `2`. Higher values mean more random completions, while lower values (like `0.2`) mean more focused and deterministic completions. (Default: `1`.) - max_tokens: int | None + max_tokens: Max number of tokens to generate. Refer to your [model's documentation](https://docs.x.ai/docs/models#model-pricing) for the maximum number of tokens it can generate. - logprobs: bool | None + logprobs: Whether to return logprobs. Key init args — client params: - timeout: Union[float, Tuple[float, float], Any, None] + timeout: Timeout for requests. - max_retries: int + max_retries: Max number of retries. - api_key: str | None + api_key: xAI API key. If not passed in will be read from env var `XAI_API_KEY`. Instantiate: @@ -166,7 +166,7 @@ class ChatXAI(BaseChatOpenAI): # type: ignore[override] which allows the model to provide reasoning content along with the response. If provided, reasoning content is returned under the `additional_kwargs` field of the - AIMessage or AIMessageChunk. + `AIMessage` or `AIMessageChunk`. If supported, reasoning effort can be specified in the model constructor's `extra_body` argument, which will control the amount of reasoning the model does. The value can be one of @@ -189,105 +189,106 @@ class ChatXAI(BaseChatOpenAI): # type: ignore[override] reasoning cannot be disabled, and the `reasoning_effort` cannot be specified. Tool calling / function calling: - ```python - from pydantic import BaseModel, Field - model = ChatXAI(model="grok-4") + ```python + from pydantic import BaseModel, Field + + model = ChatXAI(model="grok-4") - class GetWeather(BaseModel): - '''Get the current weather in a given location''' + class GetWeather(BaseModel): + '''Get the current weather in a given location''' - location: str = Field(..., description="The city and state, e.g. San Francisco, CA") + location: str = Field(..., description="The city and state, e.g. San Francisco, CA") - class GetPopulation(BaseModel): - '''Get the current population in a given location''' + class GetPopulation(BaseModel): + '''Get the current population in a given location''' - location: str = Field(..., description="The city and state, e.g. San Francisco, CA") + location: str = Field(..., description="The city and state, e.g. San Francisco, CA") - model_with_tools = model.bind_tools([GetWeather, GetPopulation]) - ai_msg = model_with_tools.invoke("Which city is bigger: LA or NY?") - ai_msg.tool_calls - ``` + model_with_tools = model.bind_tools([GetWeather, GetPopulation]) + ai_msg = model_with_tools.invoke("Which city is bigger: LA or NY?") + ai_msg.tool_calls + ``` - ```python - [ - { - "name": "GetPopulation", - "args": {"location": "NY"}, - "id": "call_m5tstyn2004pre9bfuxvom8x", - "type": "tool_call", - }, - { - "name": "GetPopulation", - "args": {"location": "LA"}, - "id": "call_0vjgq455gq1av5sp9eb1pw6a", - "type": "tool_call", - }, - ] - ``` + ```python + [ + { + "name": "GetPopulation", + "args": {"location": "NY"}, + "id": "call_m5tstyn2004pre9bfuxvom8x", + "type": "tool_call", + }, + { + "name": "GetPopulation", + "args": {"location": "LA"}, + "id": "call_0vjgq455gq1av5sp9eb1pw6a", + "type": "tool_call", + }, + ] + ``` - !!! note - With stream response, the tool / function call will be returned in whole in a - single chunk, instead of being streamed across chunks. + !!! note + With stream response, the tool / function call will be returned in whole in a + single chunk, instead of being streamed across chunks. - Tool choice can be controlled by setting the `tool_choice` parameter in the model - constructor's `extra_body` argument. For example, to disable tool / function calling: + Tool choice can be controlled by setting the `tool_choice` parameter in the model + constructor's `extra_body` argument. For example, to disable tool / function calling: - ```python - model = ChatXAI(model="grok-4", extra_body={"tool_choice": "none"}) - ``` - To require that the model always calls a tool / function, set `tool_choice` to `'required'`: + ```python + model = ChatXAI(model="grok-4", extra_body={"tool_choice": "none"}) + ``` + To require that the model always calls a tool / function, set `tool_choice` to `'required'`: - ```python - model = ChatXAI(model="grok-4", extra_body={"tool_choice": "required"}) - ``` + ```python + model = ChatXAI(model="grok-4", extra_body={"tool_choice": "required"}) + ``` - To specify a tool / function to call, set `tool_choice` to the name of the tool / function: + To specify a tool / function to call, set `tool_choice` to the name of the tool / function: - ```python - from pydantic import BaseModel, Field + ```python + from pydantic import BaseModel, Field - model = ChatXAI( - model="grok-4", - extra_body={ - "tool_choice": {"type": "function", "function": {"name": "GetWeather"}} - }, - ) + model = ChatXAI( + model="grok-4", + extra_body={ + "tool_choice": {"type": "function", "function": {"name": "GetWeather"}} + }, + ) - class GetWeather(BaseModel): - \"\"\"Get the current weather in a given location\"\"\" + class GetWeather(BaseModel): + \"\"\"Get the current weather in a given location\"\"\" - location: str = Field(..., description='The city and state, e.g. San Francisco, CA') + location: str = Field(..., description='The city and state, e.g. San Francisco, CA') - class GetPopulation(BaseModel): - \"\"\"Get the current population in a given location\"\"\" + class GetPopulation(BaseModel): + \"\"\"Get the current population in a given location\"\"\" - location: str = Field(..., description='The city and state, e.g. San Francisco, CA') + location: str = Field(..., description='The city and state, e.g. San Francisco, CA') - model_with_tools = model.bind_tools([GetWeather, GetPopulation]) - ai_msg = model_with_tools.invoke( - "Which city is bigger: LA or NY?", - ) - ai_msg.tool_calls - ``` + model_with_tools = model.bind_tools([GetWeather, GetPopulation]) + ai_msg = model_with_tools.invoke( + "Which city is bigger: LA or NY?", + ) + ai_msg.tool_calls + ``` - The resulting tool call would be: + The resulting tool call would be: - ```python - [ - { - "name": "GetWeather", - "args": {"location": "Los Angeles, CA"}, - "id": "call_81668711", - "type": "tool_call", - } - ] - ``` + ```python + [ + { + "name": "GetWeather", + "args": {"location": "Los Angeles, CA"}, + "id": "call_81668711", + "type": "tool_call", + } + ] + ``` Parallel tool calling / parallel function calling: By default, parallel tool / function calling is enabled, so you can process @@ -373,25 +374,26 @@ class ChatXAI(BaseChatOpenAI): # type: ignore[override] } ``` - Response metadata - ```python - ai_msg = model.invoke(messages) - ai_msg.response_metadata - ``` + Response metadata: - ```python - { - "token_usage": { - "completion_tokens": 4, - "prompt_tokens": 19, - "total_tokens": 23, - }, - "model_name": "grok-4", - "system_fingerprint": None, - "finish_reason": "stop", - "logprobs": None, - } - ``` + ```python + ai_msg = model.invoke(messages) + ai_msg.response_metadata + ``` + + ```python + { + "token_usage": { + "completion_tokens": 4, + "prompt_tokens": 19, + "total_tokens": 23, + }, + "model_name": "grok-4", + "system_fingerprint": None, + "finish_reason": "stop", + "logprobs": None, + } + ``` """ # noqa: E501 model_name: str = Field(default="grok-4", alias="model") @@ -589,15 +591,17 @@ class ChatXAI(BaseChatOpenAI): # type: ignore[override] - an OpenAI function/tool schema, - a JSON Schema, - - a `TypedDict` class (support added in 0.1.20), + - a `TypedDict` class, - or a Pydantic class. If `schema` is a Pydantic class then the model output will be a Pydantic instance of that class, and the model-generated fields will be validated by the Pydantic class. Otherwise the model output will be a - dict and will not be validated. See `langchain_core.utils.function_calling.convert_to_openai_tool` - for more on how to properly specify types and descriptions of - schema fields when specifying a Pydantic or `TypedDict` class. + dict and will not be validated. + + See `langchain_core.utils.function_calling.convert_to_openai_tool` for + more on how to properly specify types and descriptions of schema fields + when specifying a Pydantic or `TypedDict` class. method: The method for steering model generation, one of: @@ -613,8 +617,10 @@ class ChatXAI(BaseChatOpenAI): # type: ignore[override] an error occurs during model output parsing it will be raised. If `True` then both the raw model response (a `BaseMessage`) and the parsed model response will be returned. If an error occurs during output parsing it - will be caught and returned as well. The final output is always a dict - with keys `'raw'`, `'parsed'`, and `'parsing_error'`. + will be caught and returned as well. + + The final output is always a `dict` with keys `'raw'`, `'parsed'`, and + `'parsing_error'`. strict: - `True`: @@ -629,17 +635,19 @@ class ChatXAI(BaseChatOpenAI): # type: ignore[override] kwargs: Additional keyword args aren't supported. Returns: - A Runnable that takes same inputs as a `langchain_core.language_models.chat.BaseChatModel`. + A `Runnable` that takes same inputs as a + `langchain_core.language_models.chat.BaseChatModel`. If `include_raw` is + `False` and `schema` is a Pydantic class, `Runnable` outputs an instance + of `schema` (i.e., a Pydantic object). Otherwise, if `include_raw` is + `False` then `Runnable` outputs a `dict`. - If `include_raw` is `False` and `schema` is a Pydantic class, Runnable outputs an instance of `schema` (i.e., a Pydantic object). Otherwise, if `include_raw` is `False` then Runnable outputs a dict. + If `include_raw` is `True`, then `Runnable` outputs a `dict` with keys: - If `include_raw` is `True`, then Runnable outputs a dict with keys: - - - `'raw'`: BaseMessage - - `'parsed'`: None if there was a parsing error, otherwise the type depends on the `schema` as described above. - - `'parsing_error'`: BaseException | None - - """ # noqa: E501 + - `'raw'`: `BaseMessage` + - `'parsed'`: `None` if there was a parsing error, otherwise the type + depends on the `schema` as described above. + - `'parsing_error'`: `BaseException | None` + """ # Some applications require that incompatible parameters (e.g., unsupported # methods) be handled. if method == "function_calling" and strict: diff --git a/libs/standard-tests/langchain_tests/integration_tests/vectorstores.py b/libs/standard-tests/langchain_tests/integration_tests/vectorstores.py index 066d1d30a5b..5e82d53222e 100644 --- a/libs/standard-tests/langchain_tests/integration_tests/vectorstores.py +++ b/libs/standard-tests/langchain_tests/integration_tests/vectorstores.py @@ -151,7 +151,7 @@ class VectorStoreIntegrationTests(BaseStandardTests): assert vectorstore.similarity_search("foo", k=1) == [] def test_add_documents(self, vectorstore: VectorStore) -> None: - """Test adding documents into the vectorstore. + """Test adding documents into the `VectorStore`. ??? note "Troubleshooting" @@ -499,7 +499,7 @@ class VectorStoreIntegrationTests(BaseStandardTests): assert await vectorstore.asimilarity_search("foo", k=1) == [] async def test_add_documents_async(self, vectorstore: VectorStore) -> None: - """Test adding documents into the vectorstore. + """Test adding documents into the `VectorStore`. ??? note "Troubleshooting"