概述
集成详情
模型功能
设置
要访问 xAI models, you’ll need to create an xAI 账户,获取 API 密钥,并安装langchain-xai 集成包。
凭证
前往 this page 注册 xAI 并生成 API 密钥。 完成后,设置XAI_API_KEY 环境变量:
安装
LangChain 的 xAI 集成位于langchain-xai 包中:
实例化
现在我们可以实例化模型对象并生成聊天补全:调用
工具调用
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.ChatXAI.bind_tools()
WithChatXAI.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:
Live search
xAI supports a Live Search feature that enables Grok to ground its answers using results from web searches:API 参考
有关所有ChatXAI 功能和配置的详细文档,请前往 API reference.
Connect these docs to Claude, VSCode, and more via MCP for real-time answers.

