Documentation Index
Fetch the complete documentation index at: https://nvd-54.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
This page makes reference to Grok models provided by xAI - not to be confused with Groq, a separate AI hardware and software company. 请参阅 Groq provider page.
xAI offers an API to interact with Grok models.
API 参考有关所有 features and configuration options, 请前往 ChatXAI API reference.
集成详情
模型功能
要访问 xAI models, you’ll need to create an xAI 账户,获取 API 密钥,并安装 langchain-xai 集成包。
前往 this page 注册 xAI 并生成 API 密钥。 完成后,设置 XAI_API_KEY 环境变量:
import getpass
import os
if "XAI_API_KEY" not in os.environ:
os.environ["XAI_API_KEY"] = getpass.getpass("Enter your xAI API key: ")
要启用模型调用的自动追踪,请设置您的 LangSmith API key:
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("请输入您的 LangSmith API 密钥: ")
os.environ["LANGSMITH_TRACING"] = "true"
LangChain 的 xAI 集成位于 langchain-xai 包中:
pip install -qU langchain-xai
实例化
现在我们可以实例化模型对象并生成聊天补全:
from langchain_xai import ChatXAI
llm = ChatXAI(
model="grok-beta",
temperature=0,
max_tokens=None,
timeout=None,
max_retries=2,
# 其他参数...
)
messages = [
(
"system",
"You are a helpful assistant that translates English to French. Translate the user sentence.",
),
("human", "I love programming."),
]
ai_msg = llm.invoke(messages)
ai_msg
AIMessage(content="J'adore programmer.", additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 6, 'prompt_tokens': 30, 'total_tokens': 36, 'completion_tokens_details': None, 'prompt_tokens_details': None}, 'model_name': 'grok-beta', 'system_fingerprint': 'fp_14b89b2dfc', 'finish_reason': 'stop', 'logprobs': None}, id='run-adffb7a3-e48a-4f52-b694-340d85abe5c3-0', usage_metadata={'input_tokens': 30, 'output_tokens': 6, 'total_tokens': 36, 'input_token_details': {}, 'output_token_details': {}})
工具调用
ChatXAI has a tool calling (we use “tool calling” and “function calling” interchangeably here) API that lets you describe tools and their arguments, and have the model return a JSON object with a tool to invoke and the inputs to that tool. Tool-calling is extremely useful for building tool-using chains and agents, and for getting structured outputs from models more generally.
With ChatXAI.bind_tools, we can easily pass in Pydantic classes, dict schemas, LangChain tools, or even functions as tools to the model. Under the hood, these are converted to an OpenAI tool schema, which looks like:
{
"name": "...",
"description": "...",
"parameters": {...} # JSONSchema
}
and passed in every model invocation.
from pydantic import BaseModel, Field
class GetWeather(BaseModel):
"""Get the current weather in a given location"""
location: str = Field(description="The city and state, e.g. San Francisco, CA")
llm_with_tools = llm.bind_tools([GetWeather])
ai_msg = llm_with_tools.invoke(
"what is the weather like in San Francisco",
)
ai_msg
AIMessage(content='I am retrieving the current weather for San Francisco.', additional_kwargs={'tool_calls': [{'id': '0', 'function': {'arguments': '{"location":"San Francisco, CA"}', 'name': 'GetWeather'}, 'type': 'function'}], 'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 11, 'prompt_tokens': 151, 'total_tokens': 162, 'completion_tokens_details': None, 'prompt_tokens_details': None}, 'model_name': 'grok-beta', 'system_fingerprint': 'fp_14b89b2dfc', 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-73707da7-afec-4a52-bee1-a176b0ab8585-0', tool_calls=[{'name': 'GetWeather', 'args': {'location': 'San Francisco, CA'}, 'id': '0', 'type': 'tool_call'}], usage_metadata={'input_tokens': 151, 'output_tokens': 11, 'total_tokens': 162, 'input_token_details': {}, 'output_token_details': {}})
Live search
xAI supports a Live Search feature that enables Grok to ground its answers using results from web searches:
from langchain_xai import ChatXAI
llm = ChatXAI(
model="grok-3-latest",
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.")
See xAI docs for the full set of web search options.
API 参考
有关所有 ChatXAI 功能和配置的详细文档,请前往 API reference.