终于等到你!Grok API 全面开放,开发者狂欢时刻!

🐕 【我是谁?】

✦ 一只白天写代码、晚上写文章的[全栈柴犬工程师]

✦ CSDN博客专家 | 阿里云专家博主

✦ 擅长用「人话」讲清楚技术原理

✦ 技术沉淀者 | 开发者同行者

欢迎关注公众号「柴犬说编程」,一只热爱技术的柴犬为你叼来的编程知识锦囊 🐕💻 我们不仅是:

🔸 技术实践的记录者------用代码写日记

🔸 知识体系的构建者------化碎片为图谱

更希望成为:

🔹 你技术焦虑时的解压阀

🔹 成长路上的充电站

✨ Github开源库:github.com/Shamee99 (欢迎来踩爪印~)

1、Grok API 概述

2025年2月18号,斯克旗下人的工智能初创公司xAI开发的Grok-3模型正式发布,并免费全员开放使用。马斯克将Grok-3描述为"地球上最聪明的AI",其一以"叛逆倾向"著称,能够回答被大多数其他AI系统拒绝的尖锐问题。

而就在近期,xAI团队正式开放了Grok API接口服务,允许开发者将Grok系列模型集成到自己的应用中。通过集成Grok API,开发者可以在应用中实现高质量的文本生成、信息检索和数据处理等功能,显著提升应用的智能化水平。

Grok API与OpenAI API兼容,这意味着开发者可以使用OpenAI的客户端库直接与Grok API交互,大大降低了集成难度。

2、API申请与认证

2.1、申请步骤

  1. 访问xAI官网(x.ai) 注册账号,登录后进入API管理页面
  1. 下拉找到Quickstart项,点击start building,会跳转到API文档页面。
  1. 点击"Greate API Key"生成密钥
  1. 创建API Key页面会让提输入API Key名称,保存后就会生成相应的Key以及Demo。

2.2、使用方式

Grok API使用Bearer Token进行认证,需要在请求头中添加:

bash 复制代码
curl https://api.x.ai/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
  "messages": [
    {
      "role": "system",
      "content": "You are a test assistant."
    },
    {
      "role": "user",
      "content": "Testing. Just say hi and hello world and nothing else."
    }
  ],
  "model": "grok-3-latest",
  "stream": false,
  "temperature": 0
}'

3、支持的模型 & API定价与限流

目前Grok 3 API 支持4种不同模型,以适配不同的应用场景,相应定价策略也比较灵活,可以按需选择。

3.1、4种模型

目前,Grok 3和 Grok 3 Mini均处于Beta测试阶段,支持常规模式与快速模式。grok-3 和 grok-3-fast 都使用完全相同的底层模型,并提供相同的响应质量。

区别在于它们的服务方式:grok-3-fast 在更快的基础设施上提供服务,提供的响应时间比标准 grok-3 快得多。速度的提高伴随着每个输出代币的成本更高。

Grok-3和grok-3-fast指向的是相同的模型。

  • 如果需要低延迟的应用,选择grok-3-fast
  • 如果想降低成本,可以选择grok-3 grok-3-mini 和 grok-3-mini-fast 也是如此。在后台,两者都是相同的模型,仅在响应延迟方面有所不同。

3.2、定价 & 白嫖

目前4种可用模型定价列表如下:

  • grok-3-beta(标准版):输入3美元/百万token,输出15美元/百万token
  • grok-3-fast-beta(标准版,快速响应):输入5美元/百万token,输出25美元/百万token
  • grok-3-mini-beta(轻量版):输入0.3美元/百万token,输出0.5美元/百万token
  • grok-3-mini-fast-beta(轻量版,快速响应):输入0.6美元/百万token,输出4美元/百万token

目前可以采用API积分模式,调用API消耗积分点。而目前官网提供如果注册数据共享服务,xAI团队会为选择数据共享的团队提供每月 150 USD 的免费 API 积分(适用于符合条件的国家/地区*)。选择加入即表示您同意与 xAI 共享您的 API 请求。但是前提是你需要先支持5美金,才能加入该计划。

xAI is offering $150 per month in free API credits for teams that opt in to data sharing (available in eligible countries*). By opting in, you agree to share your API requests with xAI.

3.3、消耗和速率限制

每个 grok 模型都有不同的速率限制。在API控制台,可以查看我们Token的消耗情况。

同样Grok也是采用通过Token作为推理模型和定价的基本单位。当 Grok 模型处理您的请求时,输入提示将通过分词器分解为令牌列表。然后,该模型将根据提示标记进行推理,并生成完成标记。推理完成后,完成令牌将聚合到发回给您的完成响应中。

在每个完成响应中,都有一个 usage 对象,其中详细说明了您的提示和完成令牌计数。您可能会发现跟踪它很有帮助,以避免达到速率限制或出现成本意外。

json 复制代码
"usage": {
  "prompt_tokens": 41,
  "completion_tokens": 87,
  "total_tokens": 128,
  "prompt_tokens_details": {
    "text_tokens": 41,
    "audio_tokens": 0,
    "image_tokens": 0,
    "cached_tokens": 0
  }
}

实际应用中,我们可以使用OpenAI SDK来进行token使用检查:

python 复制代码
import os
from openai import OpenAI

XAI_API_KEY = os.getenv("XAI_API_KEY")
client = OpenAI(base_url="https://api.x.ai/v1", api_key=XAI_API_KEY)

completion = client.chat.completions.create(
    model="grok-3-latest",
    messages=[
        {
            "role": "system",
            "content": "You are Grok, a chatbot inspired by the Hitchhikers Guide to the Galaxy.",
        },
        {
            "role": "user",
            "content": "What is the meaning of life, the universe, and everything?",
        },
    ],
)

if completion.usage:
    print(completion.usage.to_json())

4、快速集成

API调用方式,我们无需过多集成其他AI框架即可实现。只要使用HttpClient工具就可以直接调用。但是http请求的header头部,需要加入"Authorization: Bearer YOUR_API_KEY"

4.1、代码补全示例

url请求地址:/v1/chat/completions,请求报文:

bash 复制代码
{
  "messages": [
    {
      "role": "system",
      "content": "You are a helpful assistant that can answer questions and help with tasks."
    },
    {
      "role": "user",
      "content": "What is 101*3?"
    }
  ],
  "reasoning_effort": "low",
  "model": "grok-3-mini-fast-latest"
}

响应:

bash 复制代码
{
  "id": "a733959b-03c4-4944-b53a-af900075ba57",
  "object": "chat.completion",
  "created": 1743770302,
  "model": "grok-3-mini-fast-beta",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "101 multiplied by 3 equals 303.",
        "reasoning_content": "First, the user asked: \"What is 101*3?\" This is a simple multiplication question.\n\nI need to calculate 101 multiplied by 3. Let me do that mentally: 101 times 3 is 303.\n\nTo double-check: 100 times 3 is 300, and 1 times 3 is 3, so 300 + 3 = 303. Yes, that's correct.\n\nAs a helpful assistant, I should respond clearly and directly. Since this is straightforward, I don't need to add extra fluff unless it's necessary.\n\nThe system prompt says: \"You are a helpful assistant that can answer questions and help with tasks.\" So, answering directly fits.\n\nI should ensure my response is polite and engaging, but keep it concise.\n\nPossible response: \"101 multiplied by 3 equals 303.\"\n\nI could make it a bit more conversational: \"Sure, let me calculate that for you. 101 times 3 is 303.\"\n\nSince the user might be testing basic math, I could explain briefly, but that might be overkill for such a simple operation.\n\nFinally, structure the response: Start with the answer, and if needed, add any follow-up.\n\nResponse: \"The result of 101 multiplied by 3 is 303.\"",
        "refusal": null
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 32,
    "completion_tokens": 10,
    "total_tokens": 299,
    "prompt_tokens_details": {
      "text_tokens": 32,
      "audio_tokens": 0,
      "image_tokens": 0,
      "cached_tokens": 0
    },
    "completion_tokens_details": {
      "reasoning_tokens": 257,
      "audio_tokens": 0,
      "accepted_prediction_tokens": 0,
      "rejected_prediction_tokens": 0
    }
  },
  "system_fingerprint": "fp_11dc627712"
}

4.2、图像生成示例

url请求地址:/v1/images/generations,请求报文:

bash 复制代码
{
  "prompt": "A cat in a tree",
  "model": "grok-2-image",
  "response_format": "url",
  "n": 2
}

响应:

bash 复制代码
{
  "data": [
    {
      "url": "...",
      "revised_prompt": "A high-resolution photograph of a cat perched on a branch in a lush, green tree during the daytime. The cat, possibly a tabby with distinctive fur patterns, is the central focus of the image, looking curiously forward with its fur slightly ruffled. The background features a serene park setting with other trees and a clear sky, ensuring no distractions from the main subject. The lighting is soft and natural, enhancing the realistic and calm atmosphere of the scene.<|separator|><|eos|>"
    },
    {
      "url": "...",
      "revised_prompt": "1. A high-resolution photograph of a gray tabby cat perched on a branch of a lush, green tree in a suburban backyard during the day. The cat is facing forward, with its fur slightly ruffled by a gentle breeze. The background features other trees and a distant house, all slightly out of focus to emphasize the cat. The lighting is natural, with sunlight filtering through the leaves, creating a serene and realistic setting. The composition focuses primarily on the cat, ensuring it remains the central subject of the image.<|separator|><|eos|>"
    }
  ]
}

5、社区集成

Grok除了使用http请求直接调用外,还可以社区第三方SDK进行访问。目前支持LiteLLM,Vercel AI SDK。也可以将VsCode、JetBrains中的Continue插件一起扩展使用。

5.1、LiteLLM

python 复制代码
from litellm import completion
import os

os.environ['XAI_API_KEY'] = ""
response = completion(
    model="xai/grok-3-latest",
    messages=[
        {
            "role": "user",
            "content": "What's the weather like in Boston today in Fahrenheit?",
        }
    ],
    max_tokens=10,
    response_format={ "type": "json_object" },
    seed=123,
    stop=["

"],
    temperature=0.2,
    top_p=0.9,
    tool_choice="auto",
    tools=[],
    user="user",
)
print(response)

5.2、Vercel AI SDK

javascript 复制代码
import { xai } from '@ai-sdk/xai';
import { generateText } from 'ai';

const { text } = await generateText({
  model: xai('grok-3-latest'),
  prompt: 'Write a vegetarian lasagna recipe for 4 people.',
});

5.3、Continue插件

要开始通过 Continue 使用 xAI 模型,您可以在 Continue 的配置文件中添加以下内容 ~/.continue/config.json(MacOS 和 Linux)/%USERPROFILE%.continue\config.json(Windows)。

json 复制代码
 "models": [
   {
     "title": "Grok-3",
     "provider": "xAI",
     "model": "grok-3-latest",
     "apiKey": "[XAI_API_KEY]"
   }
 ]

6、总结

Grok API为开发者提供了强大的AI能力,通过简单的API调用即可实现复杂的自然语言处理和计算机视觉任务。本文详细介绍了API的申请、定价、应用场景,并提供了完整的示例代码。随着xAI不断更新模型,Grok API的能力还将持续增强,是构建下一代智能应用的理想选择。

开发者可以结合自身业务需求,利用Grok API开发客服系统、内容生成工具、数据分析平台等各种创新应用。特别是在需要"非政治正确"回答的场景下,Grok的"叛逆"特性可能带来独特优势。

相关推荐
汪子熙12 小时前
利用 Trae 开发平面直角坐标系的教学动画
人工智能·trae
027西瓜皮17 小时前
使用 Leaflet.js 生成北京地铁地图(Trae实现)
前端·trae
VioletJack1 天前
使用 AI IDE 不写任何代码做一个博客网站
前端·cursor·trae
用户21411832636021 天前
dify案例分享-基于database插件实现Text2sql的数据库查询图表工作流
trae
汪子熙2 天前
使用 Trae 开发一个演示勾股定理的动画演示
前端·人工智能·trae
夕水2 天前
学到了学到了,一个小小的demo里隐藏着一个有趣的算法
trae
小old弟2 天前
🤔不会搭建技术博客,Trae+vitepress,😎3s搞定
前端·trae
创码小奇客2 天前
Java 对象变形记:BeanUtils 与 MapStruct 的高阶魔法实战
java·spring boot·trae
飞哥数智坊2 天前
ADK开源:谷歌持续发力Agent,请快速跟进
人工智能·trae