Skip to main content
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 环境变量:
要启用模型调用的自动追踪,请设置您的 LangSmith 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()

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:
and passed in every model invocation.
xAI supports a Live Search feature that enables Grok to ground its answers using results from web searches:
See xAI docs for the full set of web search options.

API 参考

有关所有 ChatXAI 功能和配置的详细文档,请前往 API reference.