style: .. code-block:: admonition translations (#33400)

biiiiiiiiiiiiiiiigggggggg pass
This commit is contained in:
Mason Daugherty
2025-10-09 16:52:58 -04:00
committed by GitHub
parent 50445d4a27
commit 6fc21afbc9
199 changed files with 10133 additions and 10940 deletions

View File

@@ -32,11 +32,10 @@ class ChatXAI(BaseChatOpenAI): # type: ignore[override]
Setup:
Install `langchain-xai` and set environment variable `XAI_API_KEY`.
.. code-block:: bash
pip install -U langchain-xai
export XAI_API_KEY="your-api-key"
```bash
pip install -U langchain-xai
export XAI_API_KEY="your-api-key"
```
Key init args — completion params:
model: str
@@ -60,107 +59,107 @@ class ChatXAI(BaseChatOpenAI): # type: ignore[override]
xAI API key. If not passed in will be read from env var `XAI_API_KEY`.
Instantiate:
.. code-block:: python
```python
from langchain_xai import ChatXAI
from langchain_xai import ChatXAI
llm = ChatXAI(
model="grok-4",
temperature=0,
max_tokens=None,
timeout=None,
max_retries=2,
# api_key="...",
# other params...
)
llm = ChatXAI(
model="grok-4",
temperature=0,
max_tokens=None,
timeout=None,
max_retries=2,
# api_key="...",
# other params...
)
```
Invoke:
.. code-block:: python
```python
messages = [
(
"system",
"You are a helpful translator. Translate the user sentence to French.",
),
("human", "I love programming."),
]
llm.invoke(messages)
```
messages = [
(
"system",
"You are a helpful translator. Translate the user sentence to French.",
),
("human", "I love programming."),
]
llm.invoke(messages)
.. code-block:: python
AIMessage(
content="J'adore la programmation.",
response_metadata={
"token_usage": {
"completion_tokens": 9,
"prompt_tokens": 32,
"total_tokens": 41,
},
"model_name": "grok-4",
"system_fingerprint": None,
"finish_reason": "stop",
"logprobs": None,
},
id="run-168dceca-3b8b-4283-94e3-4c739dbc1525-0",
usage_metadata={
"input_tokens": 32,
"output_tokens": 9,
```python
AIMessage(
content="J'adore la programmation.",
response_metadata={
"token_usage": {
"completion_tokens": 9,
"prompt_tokens": 32,
"total_tokens": 41,
},
)
"model_name": "grok-4",
"system_fingerprint": None,
"finish_reason": "stop",
"logprobs": None,
},
id="run-168dceca-3b8b-4283-94e3-4c739dbc1525-0",
usage_metadata={
"input_tokens": 32,
"output_tokens": 9,
"total_tokens": 41,
},
)
```
Stream:
.. code-block:: python
```python
for chunk in llm.stream(messages):
print(chunk.text, end="")
```
for chunk in llm.stream(messages):
print(chunk.text, end="")
.. code-block:: python
content='J' id='run-1bc996b5-293f-4114-96a1-e0f755c05eb9'
content="'" id='run-1bc996b5-293f-4114-96a1-e0f755c05eb9'
content='ad' id='run-1bc996b5-293f-4114-96a1-e0f755c05eb9'
content='ore' id='run-1bc996b5-293f-4114-96a1-e0f755c05eb9'
content=' la' id='run-1bc996b5-293f-4114-96a1-e0f755c05eb9'
content=' programm' id='run-1bc996b5-293f-4114-96a1-e0f755c05eb9'
content='ation' id='run-1bc996b5-293f-4114-96a1-e0f755c05eb9'
content='.' id='run-1bc996b5-293f-4114-96a1-e0f755c05eb9'
content='' response_metadata={'finish_reason': 'stop', 'model_name': 'grok-4'} id='run-1bc996b5-293f-4114-96a1-e0f755c05eb9'
```python
content='J' id='run-1bc996b5-293f-4114-96a1-e0f755c05eb9'
content="'" id='run-1bc996b5-293f-4114-96a1-e0f755c05eb9'
content='ad' id='run-1bc996b5-293f-4114-96a1-e0f755c05eb9'
content='ore' id='run-1bc996b5-293f-4114-96a1-e0f755c05eb9'
content=' la' id='run-1bc996b5-293f-4114-96a1-e0f755c05eb9'
content=' programm' id='run-1bc996b5-293f-4114-96a1-e0f755c05eb9'
content='ation' id='run-1bc996b5-293f-4114-96a1-e0f755c05eb9'
content='.' id='run-1bc996b5-293f-4114-96a1-e0f755c05eb9'
content='' response_metadata={'finish_reason': 'stop', 'model_name': 'grok-4'} id='run-1bc996b5-293f-4114-96a1-e0f755c05eb9'
```
Async:
.. code-block:: python
```python
await llm.ainvoke(messages)
await llm.ainvoke(messages)
# stream:
# async for chunk in (await llm.astream(messages))
# stream:
# async for chunk in (await llm.astream(messages))
# batch:
# await llm.abatch([messages])
```
# batch:
# await llm.abatch([messages])
.. code-block:: python
AIMessage(
content="J'adore la programmation.",
response_metadata={
"token_usage": {
"completion_tokens": 9,
"prompt_tokens": 32,
"total_tokens": 41,
},
"model_name": "grok-4",
"system_fingerprint": None,
"finish_reason": "stop",
"logprobs": None,
},
id="run-09371a11-7f72-4c53-8e7c-9de5c238b34c-0",
usage_metadata={
"input_tokens": 32,
"output_tokens": 9,
```python
AIMessage(
content="J'adore la programmation.",
response_metadata={
"token_usage": {
"completion_tokens": 9,
"prompt_tokens": 32,
"total_tokens": 41,
},
)
"model_name": "grok-4",
"system_fingerprint": None,
"finish_reason": "stop",
"logprobs": None,
},
id="run-09371a11-7f72-4c53-8e7c-9de5c238b34c-0",
usage_metadata={
"input_tokens": 32,
"output_tokens": 9,
"total_tokens": 41,
},
)
```
Reasoning:
[Certain xAI models](https://docs.x.ai/docs/models#model-pricing) support reasoning,
@@ -173,12 +172,12 @@ class ChatXAI(BaseChatOpenAI): # type: ignore[override]
argument, which will control the amount of reasoning the model does. The value can be one of
`'low'` or `'high'`.
.. code-block:: python
model = ChatXAI(
model="grok-3-mini",
extra_body={"reasoning_effort": "high"},
)
```python
model = ChatXAI(
model="grok-3-mini",
extra_body={"reasoning_effort": "high"},
)
```
!!! note
As of 2025-07-10, `reasoning_content` is only returned in Grok 3 models, such as
@@ -190,45 +189,45 @@ class ChatXAI(BaseChatOpenAI): # type: ignore[override]
reasoning cannot be disabled, and the `reasoning_effort` cannot be specified.
Tool calling / function calling:
.. code-block:: python
```python
from pydantic import BaseModel, Field
from pydantic import BaseModel, Field
llm = ChatXAI(model="grok-4")
llm = 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")
llm_with_tools = llm.bind_tools([GetWeather, GetPopulation])
ai_msg = llm_with_tools.invoke("Which city is bigger: LA or NY?")
ai_msg.tool_calls
llm_with_tools = llm.bind_tools([GetWeather, GetPopulation])
ai_msg = llm_with_tools.invoke("Which city is bigger: LA or NY?")
ai_msg.tool_calls
```
.. code-block:: 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
@@ -236,59 +235,59 @@ class ChatXAI(BaseChatOpenAI): # type: ignore[override]
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:
.. code-block:: python
llm = ChatXAI(model="grok-4", extra_body={"tool_choice": "none"})
```python
llm = ChatXAI(model="grok-4", extra_body={"tool_choice": "none"})
```
To require that the model always calls a tool / function, set `tool_choice` to `'required'`:
.. code-block:: python
llm = ChatXAI(model="grok-4", extra_body={"tool_choice": "required"})
```python
llm = 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:
.. code-block:: python
```python
from pydantic import BaseModel, Field
from pydantic import BaseModel, Field
llm = ChatXAI(
model="grok-4",
extra_body={
"tool_choice": {"type": "function", "function": {"name": "GetWeather"}}
},
)
llm = 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')
llm_with_tools = llm.bind_tools([GetWeather, GetPopulation])
ai_msg = llm_with_tools.invoke(
"Which city is bigger: LA or NY?",
)
ai_msg.tool_calls
llm_with_tools = llm.bind_tools([GetWeather, GetPopulation])
ai_msg = llm_with_tools.invoke(
"Which city is bigger: LA or NY?",
)
ai_msg.tool_calls
```
The resulting tool call would be:
.. code-block:: 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
@@ -296,104 +295,103 @@ class ChatXAI(BaseChatOpenAI): # type: ignore[override]
are required, all of the tool call requests will be included in the response body.
Structured output:
.. code-block:: python
```python
from typing import Optional
from typing import Optional
from pydantic import BaseModel, Field
from pydantic import BaseModel, Field
class Joke(BaseModel):
'''Joke to tell user.'''
class Joke(BaseModel):
'''Joke to tell user.'''
setup: str = Field(description="The setup of the joke")
punchline: str = Field(description="The punchline to the joke")
rating: int | None = Field(description="How funny the joke is, from 1 to 10")
setup: str = Field(description="The setup of the joke")
punchline: str = Field(description="The punchline to the joke")
rating: int | None = Field(description="How funny the joke is, from 1 to 10")
structured_llm = llm.with_structured_output(Joke)
structured_llm.invoke("Tell me a joke about cats")
structured_llm = llm.with_structured_output(Joke)
structured_llm.invoke("Tell me a joke about cats")
```
.. code-block:: python
Joke(
setup="Why was the cat sitting on the computer?",
punchline="To keep an eye on the mouse!",
rating=7,
)
```python
Joke(
setup="Why was the cat sitting on the computer?",
punchline="To keep an eye on the mouse!",
rating=7,
)
```
Live Search:
xAI supports a [Live Search](https://docs.x.ai/docs/guides/live-search)
feature that enables Grok to ground its answers using results from web searches.
.. code-block:: python
```python
from langchain_xai import ChatXAI
from langchain_xai import ChatXAI
llm = ChatXAI(
model="grok-4",
search_parameters={
"mode": "auto",
# Example optional parameters below:
"max_search_results": 3,
"from_date": "2025-05-26",
"to_date": "2025-05-27",
},
)
llm = ChatXAI(
model="grok-4",
search_parameters={
"mode": "auto",
# Example optional parameters below:
"max_search_results": 3,
"from_date": "2025-05-26",
"to_date": "2025-05-27",
},
)
llm.invoke("Provide me a digest of world news in the last 24 hours.")
llm.invoke("Provide me a digest of world news in the last 24 hours.")
```
!!! note
[Citations](https://docs.x.ai/docs/guides/live-search#returning-citations)
are only available in [Grok 3](https://docs.x.ai/docs/models/grok-3).
Token usage:
.. code-block:: python
```python
ai_msg = llm.invoke(messages)
ai_msg.usage_metadata
```
ai_msg = llm.invoke(messages)
ai_msg.usage_metadata
.. code-block:: python
{"input_tokens": 37, "output_tokens": 6, "total_tokens": 43}
```python
{"input_tokens": 37, "output_tokens": 6, "total_tokens": 43}
```
Logprobs:
.. code-block:: python
```python
logprobs_llm = llm.bind(logprobs=True)
messages = [("human", "Say Hello World! Do not return anything else.")]
ai_msg = logprobs_llm.invoke(messages)
ai_msg.response_metadata["logprobs"]
```
logprobs_llm = llm.bind(logprobs=True)
messages = [("human", "Say Hello World! Do not return anything else.")]
ai_msg = logprobs_llm.invoke(messages)
ai_msg.response_metadata["logprobs"]
.. code-block:: python
{
"content": None,
"token_ids": [22557, 3304, 28808, 2],
"tokens": [" Hello", " World", "!", "</s>"],
"token_logprobs": [-4.7683716e-06, -5.9604645e-07, 0, -0.057373047],
}
```python
{
"content": None,
"token_ids": [22557, 3304, 28808, 2],
"tokens": [" Hello", " World", "!", "</s>"],
"token_logprobs": [-4.7683716e-06, -5.9604645e-07, 0, -0.057373047],
}
```
Response metadata
.. code-block:: python
ai_msg = llm.invoke(messages)
ai_msg.response_metadata
.. code-block:: 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 = llm.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")