Skip to content

Commit 1a3a8db

Browse files
authored
docs: anthropic formatting cleanup (#31847)
inline URLs, capitalization, code blocks
1 parent ee37095 commit 1a3a8db

File tree

3 files changed

+39
-28
lines changed

3 files changed

+39
-28
lines changed

libs/partners/anthropic/langchain_anthropic/chat_models.py

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,8 @@ def _handle_anthropic_bad_request(e: anthropic.BadRequestError) -> None:
487487
class ChatAnthropic(BaseChatModel):
488488
"""Anthropic chat models.
489489
490-
See https://docs.anthropic.com/en/docs/models-overview for a list of the latest models.
490+
See `Anthropic's docs <https://docs.anthropic.com/en/docs/models-overview>`__ for a
491+
list of the latest models.
491492
492493
Setup:
493494
Install ``langchain-anthropic`` and set environment variable ``ANTHROPIC_API_KEY``.
@@ -499,9 +500,9 @@ class ChatAnthropic(BaseChatModel):
499500
500501
Key init args — completion params:
501502
model: str
502-
Name of Anthropic model to use. E.g. "claude-3-sonnet-20240229".
503+
Name of Anthropic model to use. e.g. ``'claude-3-sonnet-20240229'``.
503504
temperature: float
504-
Sampling temperature. Ranges from 0.0 to 1.0.
505+
Sampling temperature. Ranges from ``0.0`` to ``1.0``.
505506
max_tokens: int
506507
Max number of tokens to generate.
507508
@@ -511,7 +512,8 @@ class ChatAnthropic(BaseChatModel):
511512
max_retries: int
512513
Max number of retries if a request fails.
513514
api_key: Optional[str]
514-
Anthropic API key. If not passed in will be read from env var ANTHROPIC_API_KEY.
515+
Anthropic API key. If not passed in will be read from env var
516+
``ANTHROPIC_API_KEY``.
515517
base_url: Optional[str]
516518
Base URL for API requests. Only specify if using a proxy or service
517519
emulator.
@@ -1183,17 +1185,16 @@ def get_weather(location: str) -> str:
11831185
"""Base URL for API requests. Only specify if using a proxy or service emulator.
11841186
11851187
If a value isn't passed in, will attempt to read the value first from
1186-
ANTHROPIC_API_URL and if that is not set, ANTHROPIC_BASE_URL.
1187-
If neither are set, the default value of 'https://api.anthropic.com' will
1188+
``ANTHROPIC_API_URL`` and if that is not set, ``ANTHROPIC_BASE_URL``.
1189+
If neither are set, the default value of ``https://api.anthropic.com`` will
11881190
be used.
11891191
"""
11901192

11911193
anthropic_api_key: SecretStr = Field(
11921194
alias="api_key",
11931195
default_factory=secret_from_env("ANTHROPIC_API_KEY", default=""),
11941196
)
1195-
1196-
"""Automatically read from env var `ANTHROPIC_API_KEY` if not provided."""
1197+
"""Automatically read from env var ``ANTHROPIC_API_KEY`` if not provided."""
11971198

11981199
default_headers: Optional[Mapping[str, str]] = None
11991200
"""Headers to pass to the Anthropic clients, will be used for every API call."""
@@ -1211,7 +1212,7 @@ def get_weather(location: str) -> str:
12111212
"""Whether to use streaming or not."""
12121213

12131214
stream_usage: bool = True
1214-
"""Whether to include usage metadata in streaming output. If True, additional
1215+
"""Whether to include usage metadata in streaming output. If ``True``, additional
12151216
message chunks will be generated during the stream including usage metadata.
12161217
"""
12171218

@@ -1570,7 +1571,7 @@ def bind_tools(
15701571
tool_choice: Which tool to require the model to call. Options are:
15711572
15721573
- name of the tool as a string or as dict ``{"type": "tool", "name": "<<tool_name>>"}``: calls corresponding tool;
1573-
- ``"auto"``, ``{"type: "auto"}``, or None: automatically selects a tool (including no tool);
1574+
- ``"auto"``, ``{"type: "auto"}``, or ``None``: automatically selects a tool (including no tool);
15741575
- ``"any"`` or ``{"type: "any"}``: force at least one tool to be called;
15751576
parallel_tool_calls: Set to ``False`` to disable parallel tool use.
15761577
Defaults to ``None`` (no specification, which allows parallel tool use).
@@ -1580,6 +1581,7 @@ def bind_tools(
15801581
:meth:`~langchain_anthropic.chat_models.ChatAnthropic.bind`.
15811582
15821583
Example:
1584+
15831585
.. code-block:: python
15841586
15851587
from langchain_anthropic import ChatAnthropic
@@ -1608,7 +1610,8 @@ class GetPrice(BaseModel):
16081610
# id='run-87b1331e-9251-4a68-acef-f0a018b639cc-0'
16091611
# )
16101612
1611-
Example — force tool call with tool_choice 'any':
1613+
Example — force tool call with tool_choice ``'any'``:
1614+
16121615
.. code-block:: python
16131616
16141617
from langchain_anthropic import ChatAnthropic
@@ -1630,7 +1633,8 @@ class GetPrice(BaseModel):
16301633
llm_with_tools.invoke("what is the weather like in San Francisco",)
16311634
16321635
1633-
Example — force specific tool call with tool_choice '<name_of_tool>':
1636+
Example — force specific tool call with tool_choice ``'<name_of_tool>'``:
1637+
16341638
.. code-block:: python
16351639
16361640
from langchain_anthropic import ChatAnthropic
@@ -1652,6 +1656,7 @@ class GetPrice(BaseModel):
16521656
llm_with_tools.invoke("what is the weather like in San Francisco",)
16531657
16541658
Example — cache specific tools:
1659+
16551660
.. code-block:: python
16561661
16571662
from langchain_anthropic import ChatAnthropic, convert_to_anthropic_tool
@@ -1754,28 +1759,29 @@ def with_structured_output(
17541759
for more on how to properly specify types and descriptions of
17551760
schema fields when specifying a Pydantic or TypedDict class.
17561761
include_raw:
1757-
If False then only the parsed structured output is returned. If
1758-
an error occurs during model output parsing it will be raised. If True
1762+
If ``False`` then only the parsed structured output is returned. If
1763+
an error occurs during model output parsing it will be raised. If ``True``
17591764
then both the raw model response (a BaseMessage) and the parsed model
17601765
response will be returned. If an error occurs during output parsing it
17611766
will be caught and returned as well. The final output is always a dict
1762-
with keys "raw", "parsed", and "parsing_error".
1767+
with keys ``raw``, ``parsed``, and ``parsing_error``.
17631768
kwargs: Additional keyword arguments are ignored.
17641769
17651770
Returns:
17661771
A Runnable that takes same inputs as a :class:`~langchain_core.language_models.chat.BaseChatModel`.
17671772
1768-
If ``include_raw`` is False and ``schema`` is a Pydantic class, Runnable outputs
1773+
If ``include_raw`` is ``False`` and ``schema`` is a Pydantic class, Runnable outputs
17691774
an instance of ``schema`` (i.e., a Pydantic object).
17701775
1771-
Otherwise, if ``include_raw`` is False then Runnable outputs a dict.
1776+
Otherwise, if ``include_raw`` is ``False`` then Runnable outputs a dict.
17721777
17731778
If ``include_raw`` is True, then Runnable outputs a dict with keys:
1774-
- ``"raw"``: BaseMessage
1775-
- ``"parsed"``: None if there was a parsing error, otherwise the type depends on the ``schema`` as described above.
1776-
- ``"parsing_error"``: Optional[BaseException]
1779+
- ``raw``: BaseMessage
1780+
- ``parsed``: None if there was a parsing error, otherwise the type depends on the ``schema`` as described above.
1781+
- ``parsing_error``: Optional[BaseException]
17771782
17781783
Example: Pydantic schema (include_raw=False):
1784+
17791785
.. code-block:: python
17801786
17811787
from langchain_anthropic import ChatAnthropic
@@ -1797,6 +1803,7 @@ class AnswerWithJustification(BaseModel):
17971803
# )
17981804
17991805
Example: Pydantic schema (include_raw=True):
1806+
18001807
.. code-block:: python
18011808
18021809
from langchain_anthropic import ChatAnthropic
@@ -1818,6 +1825,7 @@ class AnswerWithJustification(BaseModel):
18181825
# }
18191826
18201827
Example: Dict schema (include_raw=False):
1828+
18211829
.. code-block:: python
18221830
18231831
from langchain_anthropic import ChatAnthropic
@@ -1902,6 +1910,7 @@ def get_num_tokens_from_messages(
19021910
to be converted to tool schemas.
19031911
19041912
Basic usage:
1913+
19051914
.. code-block:: python
19061915
19071916
from langchain_anthropic import ChatAnthropic
@@ -1920,6 +1929,7 @@ def get_num_tokens_from_messages(
19201929
14
19211930
19221931
Pass tool schemas:
1932+
19231933
.. code-block:: python
19241934
19251935
from langchain_anthropic import ChatAnthropic
@@ -1948,9 +1958,9 @@ def get_weather(location: str) -> str:
19481958
19491959
.. versionchanged:: 0.3.0
19501960
1951-
Uses Anthropic's token counting API to count tokens in messages. See:
1952-
https://docs.anthropic.com/en/docs/build-with-claude/token-counting
1953-
"""
1961+
Uses Anthropic's `token counting API <https://docs.anthropic.com/en/docs/build-with-claude/token-counting>`__ to count tokens in messages.
1962+
1963+
""" # noqa: E501
19541964
formatted_system, formatted_messages = _format_messages(messages)
19551965
if isinstance(formatted_system, str):
19561966
kwargs["system"] = formatted_system
@@ -2044,7 +2054,7 @@ def _make_message_chunk_from_anthropic_event(
20442054
"""Convert Anthropic event to AIMessageChunk.
20452055
20462056
Note that not all events will result in a message chunk. In these cases
2047-
we return None.
2057+
we return ``None``.
20482058
"""
20492059
message_chunk: Optional[AIMessageChunk] = None
20502060
# See https://github.com/anthropics/anthropic-sdk-python/blob/main/src/anthropic/lib/streaming/_messages.py # noqa: E501

libs/partners/anthropic/langchain_anthropic/llms.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ class _AnthropicCommon(BaseLanguageModel):
6666
"""Base URL for API requests. Only specify if using a proxy or service emulator.
6767
6868
If a value isn't passed in, will attempt to read the value from
69-
ANTHROPIC_API_URL. If not set, the default value of 'https://api.anthropic.com' will
70-
be used.
69+
``ANTHROPIC_API_URL``. If not set, the default value ``https://api.anthropic.com``
70+
will be used.
7171
"""
7272

7373
anthropic_api_key: SecretStr = Field(
7474
alias="api_key",
7575
default_factory=secret_from_env("ANTHROPIC_API_KEY", default=""),
7676
)
77-
"""Automatically read from env var `ANTHROPIC_API_KEY` if not provided."""
77+
"""Automatically read from env var ``ANTHROPIC_API_KEY`` if not provided."""
7878

7979
HUMAN_PROMPT: Optional[str] = None
8080
AI_PROMPT: Optional[str] = None
@@ -311,6 +311,7 @@ def _stream(
311311
Returns:
312312
A generator representing the stream of tokens from Anthropic.
313313
Example:
314+
314315
.. code-block:: python
315316
316317
prompt = "Write a poem about a stream."
@@ -347,6 +348,7 @@ async def _astream(
347348
A generator representing the stream of tokens from Anthropic.
348349
Example:
349350
.. code-block:: python
351+
350352
prompt = "Write a poem about a stream."
351353
prompt = f"\n\nHuman: {prompt}\n\nAssistant:"
352354
generator = anthropic.stream(prompt)

libs/partners/anthropic/langchain_anthropic/output_parsers.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ def parse_result(self, result: list[Generation], *, partial: bool = False) -> An
2727
Args:
2828
result: A list of Generations to be parsed. The Generations are assumed
2929
to be different candidate outputs for a single model input.
30-
3130
Returns:
3231
Structured output.
3332
"""

0 commit comments

Comments
 (0)