如何註冊使用Grok 3 API?

如何註冊使用Grok 3 API?

Grok 3 API 已經發布,它的“scary‑smart”推理、即時網路功能,以及在編碼和 STEM 基準測試中的頂級效能,已經撼動了人工智慧世界。無論您是開發人員、研究人員還是人工智慧愛好者,現在都是訪問 Grok 3 API 並探索其功能的最佳時機。在本教程中,您將學習如何註冊使用 Grok 3 API、安全地驗證您的憑據以及使用 Python 連線 Grok 3 API。我們還將介紹一些實際用例,從將 Grok 3 API 整合到工作流程,到使用 Grok 3 API 進行資料檢索和高階推理任務。

Grok 3的主要功能

Grok 3 引入了幾項突破性功能,這些功能增強了 Grok 3 在各個領域的效能和適用性:

  • 高階推理模式:Grok 3 提供了專門的推理模式,包括用於逐步解決問題的 “思考 ”模式和用於處理複雜任務的 “大大腦 ”模式。這些模式可使模型更深入地處理資訊,並提供準確的響應。
  • 深度搜尋功能:該模型整合了 DeepSearch,這是一種人工智慧代理,可即時掃描網際網路和 X(前 Twitter),生成有關特定主題的綜合報告。該功能可確保 Grok 3 的回覆與時俱進、資訊靈通。
  • 增強的效能基準:Grok 3 在 2025 年美國數學邀請考試(AIME)中取得了 93.3% 的高分,並在聊天機器人競技場中取得了 1402 的 Elo 分數,顯示了其在 STEM 領域的優勢。
  • 即時資料整合:與靜態的人工智慧模型不同,Grok 3 整合了來自網路和 X 帖子的即時資料,確保其回覆是最新的、相關的。
  • 多模式功能:該模型可以處理和生成文字、影像和程式碼,從而將其適用範圍擴充套件到各個領域。

這些功能使 Grok 3 成為人工智慧語言模型的一大進步。它提供了增強的推理能力、即時資料處理能力和廣泛的功能,可滿足不同使用者的需求。

價格和模型規格

xAI 推出了 Grok-3 模型系列的多個變體。每種模型都針對不同的效能和成本效益水平進行了微調。這些模型可滿足開發人員、研究人員和組織機構的計算和推理需求。

價格和模型規格

Source: X.com

在呼叫API之前使用xAI的成本計算器

在您開始呼叫多個 API 之前,xAI 為其每種模型(包括 grok-3-beta、grok-3-fast-beta 和迷你變體)提供了一個超級方便的成本計算器。

您可以直接訪問 https://x.ai/api(向下滾動即可)。

xAI的成本計算器

您可以自定義

  • 文字輸入 token
  • 影像輸入 token
  • 輸出 token

該工具可為您提供即時成本估算,以便您有效規劃使用量,避免意外計費。例如,在 grok-3-beta 上使用 50,000 個文字輸入 token 和 500 個輸出 token,只需花費 0.16 美元(如前面的截圖所示)。

智慧開發提示:經常使用此計算器來最佳化您的 API 策略,尤其是在擴充套件或處理大型有效載荷時。

如何訪問API?

1. 訪問 grok.com,使用您的賬戶憑據登入。

 

訪問 grok.com

2. 點選右上角的個人資料頭像,選擇“Settings,然後選擇“Manage”。您將被重定向到 xAI 賬戶頁面。

xAI 賬戶頁面

xAI API控制檯入口

3. 在 xAI 賬戶頁面,導航至 API Console

API Console

4. 在 API 控制檯的左側邊欄,點選金鑰圖示,檢視並複製您的 Grok API 金鑰。

Grok API 金鑰

瞧,這就是你的 API 金鑰!請務必妥善保管。

Grok 3的實現

基本實現

讓我們嘗試使用 Grok xAI 文件中提供的程式碼片段來檢查我們的 Grok 3 模型是否能夠響應。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
!pip install openai
import os
os.environ['GROK_API_KEY'] = "xai-..." # your own api key
from IPython.display import Markdown
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("GROK_API_KEY"),
base_url="https://api.x.ai/v1",
)
completion = client.chat.completions.create(
model="grok-3-mini-beta",
messages=[
{"role": "user", "content": "What is the meaning of life?"}
]
)
Markdown(completion.choices[0].message.content)
!pip install openai import os os.environ['GROK_API_KEY'] = "xai-..." # your own api key from IPython.display import Markdown from openai import OpenAI client = OpenAI( api_key=os.getenv("GROK_API_KEY"), base_url="https://api.x.ai/v1", ) completion = client.chat.completions.create( model="grok-3-mini-beta", messages=[ {"role": "user", "content": "What is the meaning of life?"} ] ) Markdown(completion.choices[0].message.content)
!pip install openai
import os
os.environ['GROK_API_KEY'] = "xai-..." # your own api key
from IPython.display import Markdown 
from openai import OpenAI
client = OpenAI(
  api_key=os.getenv("GROK_API_KEY"),
  base_url="https://api.x.ai/v1",
)
completion = client.chat.completions.create(
  model="grok-3-mini-beta",
  messages=[
    {"role": "user", "content": "What is the meaning of life?"}
  ]
)
Markdown(completion.choices[0].message.content)

輸出:

檢查我們的 Grok 3 模型是否能夠響應

現在,讓我們測試一下 Grok 3 模型: – 生成程式碼

  1. 程式碼生成
  2. 推理能力
  3. 複雜用例
  4. 科學研究理解

您可以隨意更換其他 Grok 3 變體(例如,grok-3-beta、grok-3-fast-beta)或製作您的提示。然後,在筆記本或指令碼中直接比較輸出結果。

在下面的實現中,我們將利用

程式碼生成

1. 讓我們使用 Grok 3 模型生成程式碼,將華氏度轉換為攝氏度,反之亦然。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
prompt = """
Write a Python function that converts a temperature from Fahrenheit to Celsius and vice versa.
The function should take an input, determine the type (Fahrenheit or Celsius), and return the converted temperature.
"""
client = OpenAI(
api_key=os.getenv("GROK_API_KEY"),
base_url="https://api.x.ai/v1",
)
response = client.chat.completions.create(
model="grok-3-mini-beta",
messages=[
{
"role": "user",
"content": prompt
}
]
)
# Output the generated Python code
print(response.choices[0].message.content)
Markdown(response.choices[0].message.content)
prompt = """ Write a Python function that converts a temperature from Fahrenheit to Celsius and vice versa. The function should take an input, determine the type (Fahrenheit or Celsius), and return the converted temperature. """ client = OpenAI( api_key=os.getenv("GROK_API_KEY"), base_url="https://api.x.ai/v1", ) response = client.chat.completions.create( model="grok-3-mini-beta", messages=[ { "role": "user", "content": prompt } ] ) # Output the generated Python code print(response.choices[0].message.content) Markdown(response.choices[0].message.content)
prompt = """
Write a Python function that converts a temperature from Fahrenheit to Celsius and vice versa.
The function should take an input, determine the type (Fahrenheit or Celsius), and return the converted temperature.
"""
client = OpenAI(
  api_key=os.getenv("GROK_API_KEY"),
  base_url="https://api.x.ai/v1",
)
response = client.chat.completions.create(
  model="grok-3-mini-beta",
  messages=[
      {
      "role": "user",
      "content": prompt
      }
  ]
)
# Output the generated Python code
print(response.choices[0].message.content)
Markdown(response.choices[0].message.content)

輸出:

將華氏度轉換為攝氏度

2. 讓我們用 Grok 3 模型生成一個 HTML 頁面,頁面上有一個按鈕,點選後頁面上就會灑滿紙屑。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
prompt = """Create an HTML page with a button that explodes confetti when you click it.
You can use CSS & JS as well."""
client = OpenAI(
api_key=os.getenv("GROK_API_KEY"),
base_url="https://api.x.ai/v1",
)
response = client.chat.completions.create(
model="grok-3-mini-beta",
messages=[
{
"role": "user",
"content": prompt
}
]
)
# Output the generated Python code
print(response.choices[0].message.content)
Markdown(response.choices[0].message.content)
prompt = """Create an HTML page with a button that explodes confetti when you click it. You can use CSS & JS as well.""" client = OpenAI( api_key=os.getenv("GROK_API_KEY"), base_url="https://api.x.ai/v1", ) response = client.chat.completions.create( model="grok-3-mini-beta", messages=[ { "role": "user", "content": prompt } ] ) # Output the generated Python code print(response.choices[0].message.content) Markdown(response.choices[0].message.content)
prompt = """Create an HTML page with a button that explodes confetti when you click it.
            You can use CSS & JS as well."""
client = OpenAI(
  api_key=os.getenv("GROK_API_KEY"),
  base_url="https://api.x.ai/v1",
)
response = client.chat.completions.create(
  model="grok-3-mini-beta",
  messages=[
      {
      "role": "user",
      "content": prompt
      }
  ]
)
# Output the generated Python code
print(response.choices[0].message.content)
Markdown(response.choices[0].message.content)

輸出:

Grok 3 模型生成一個 HTML 頁面

為了測試這段程式碼的功能,我將使用 CodePen 來測試 HTML 程式碼。

這就是它的外觀:

推理能力

1. 讓我們用這道通用能力推理題來測試一下 Grok 3 模型

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
prompt = """Anu is a girl. She has three brothers. Each of her brothers has the same two sisters.
How many sisters does Anu have?"""
client = OpenAI(
api_key=os.getenv("GROK_API_KEY"),
base_url="https://api.x.ai/v1",
)
response = client.chat.completions.create(
model="grok-3-mini-beta",
messages=[
{
"role": "user",
"content": prompt
}
]
)
# Output the generated Python code
print(response.choices[0].message.content)
Markdown(response.choices[0].message.content)
prompt = """Anu is a girl. She has three brothers. Each of her brothers has the same two sisters. How many sisters does Anu have?""" client = OpenAI( api_key=os.getenv("GROK_API_KEY"), base_url="https://api.x.ai/v1", ) response = client.chat.completions.create( model="grok-3-mini-beta", messages=[ { "role": "user", "content": prompt } ] ) # Output the generated Python code print(response.choices[0].message.content) Markdown(response.choices[0].message.content)
prompt = """Anu is a girl. She has three brothers. Each of her brothers has the same two sisters.
            How many sisters does Anu have?"""
client = OpenAI(
  api_key=os.getenv("GROK_API_KEY"),
  base_url="https://api.x.ai/v1",
)
response = client.chat.completions.create(
  model="grok-3-mini-beta",
  messages=[
      {
      "role": "user",
      "content": prompt
      }
  ]
)
# Output the generated Python code
print(response.choices[0].message.content)
Markdown(response.choices[0].message.content)

輸出:

通用能力推理題

已驗證答案正確。

2. 讓我們來測試一下 Grok 3 的模式識別能力,提供這個基於日期的模式問題

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
prompt = """January = 1017, February = 628, March = 1335, April = 145, May = 1353, June = 1064,
July = 1074, August = 186, September = ? Think carefully before answering also show the steps"""
client = OpenAI(
api_key=os.getenv("GROK_API_KEY"),
base_url="https://api.x.ai/v1",
)
response = client.chat.completions.create(
model="grok-3-beta",
messages=[
{
"role": "user",
"content": prompt
}
]
)
# Output the generated Python code
print(response.choices[0].message.content)
Markdown(response.choices[0].message.content)
prompt = """January = 1017, February = 628, March = 1335, April = 145, May = 1353, June = 1064, July = 1074, August = 186, September = ? Think carefully before answering also show the steps""" client = OpenAI( api_key=os.getenv("GROK_API_KEY"), base_url="https://api.x.ai/v1", ) response = client.chat.completions.create( model="grok-3-beta", messages=[ { "role": "user", "content": prompt } ] ) # Output the generated Python code print(response.choices[0].message.content) Markdown(response.choices[0].message.content)
prompt = """January = 1017, February = 628, March = 1335, April = 145, May = 1353, June = 1064,
July = 1074, August = 186, September = ? Think carefully before answering also show the steps"""
client = OpenAI(
  api_key=os.getenv("GROK_API_KEY"),
  base_url="https://api.x.ai/v1",
)
response = client.chat.completions.create(
  model="grok-3-beta",
  messages=[
      {
      "role": "user",
      "content": prompt
      }
  ]
)
# Output the generated Python code
print(response.choices[0].message.content)
Markdown(response.choices[0].message.content)

輸出:

測試一下 Grok 3 的模式識別能力

GROK-BETA 和 GROK-BETA-MINI 都失敗了

答案應為 1999。

3. 讓我們用這個問題來測試一下 Grok 3 模型的簡單數學推理能力吧

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/cod
prompt = """I have two apples, then I buy two more. I bake a pie with two of the apples.
After eating half of the pie, how many apples do I have left?"""
client = OpenAI(
api_key=os.getenv("GROK_API_KEY"),
base_url="https://api.x.ai/v1",
)
response = client.chat.completions.create(
model="grok-3-mini-beta",
messages=[
{
"role": "user",
"content": prompt
}
]
)
# Output the generated Python code
print(response.choices[0].message.content)
Markdown(response.choices[0].message.content)
/cod prompt = """I have two apples, then I buy two more. I bake a pie with two of the apples. After eating half of the pie, how many apples do I have left?""" client = OpenAI( api_key=os.getenv("GROK_API_KEY"), base_url="https://api.x.ai/v1", ) response = client.chat.completions.create( model="grok-3-mini-beta", messages=[ { "role": "user", "content": prompt } ] ) # Output the generated Python code print(response.choices[0].message.content) Markdown(response.choices[0].message.content)
/cod
prompt = """I have two apples, then I buy two more. I bake a pie with two of the apples.
          After eating half of the pie, how many apples do I have left?"""
client = OpenAI(
  api_key=os.getenv("GROK_API_KEY"),
  base_url="https://api.x.ai/v1",
)
response = client.chat.completions.create(
  model="grok-3-mini-beta",
  messages=[
      {
      "role": "user",
      "content": prompt
      }
  ]
)
# Output the generated Python code
print(response.choices[0].message.content)
Markdown(response.choices[0].message.content)

輸出:

簡單數學推理能力

已驗證答案正確。

複雜案例

讓我們要求 Grok 3 模型為我們提供一個完整的專案結構計劃和相應的程式碼片段,這些程式碼片段必須相應地新增到相應的檔案中。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
prompt = """
I want to develop an inventory management system that tracks products, quantities,
and locations. It should notify the user when stock is low. Create a plan for the
directory structure and provide code snippets for the key components.
"""
client = OpenAI(
api_key=os.getenv("GROK_API_KEY"),
base_url="https://api.x.ai/v1",
)
response = client.chat.completions.create(
model="grok-3-mini-beta",
messages=[
{
"role": "user",
"content": prompt
}
]
)
# Output the generated Python code
print(response.choices[0].message.content)
Markdown(response.choices[0].message.content)
prompt = """ I want to develop an inventory management system that tracks products, quantities, and locations. It should notify the user when stock is low. Create a plan for the directory structure and provide code snippets for the key components. """ client = OpenAI( api_key=os.getenv("GROK_API_KEY"), base_url="https://api.x.ai/v1", ) response = client.chat.completions.create( model="grok-3-mini-beta", messages=[ { "role": "user", "content": prompt } ] ) # Output the generated Python code print(response.choices[0].message.content) Markdown(response.choices[0].message.content)
prompt = """
I want to develop an inventory management system that tracks products, quantities,
and locations. It should notify the user when stock is low. Create a plan for the
directory structure and provide code snippets for the key components.
"""
client = OpenAI(
  api_key=os.getenv("GROK_API_KEY"),
  base_url="https://api.x.ai/v1",
)
response = client.chat.completions.create(
  model="grok-3-mini-beta",
  messages=[
      {
      "role": "user",
      "content": prompt
      }
  ]
)
# Output the generated Python code
print(response.choices[0].message.content)
Markdown(response.choices[0].message.content)

輸出:

Grok 3 模型完成複雜案例

科學研究理解

在此,我們將測試 Grok 3 模型基於領域的理解能力,以及它是如何理解問題和闡述某些科學研究主題的。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
prompt = """
Explain how CRISPR technology can be used to treat genetic disorders. What are the
main challenges, and what future advancements might be necessary to make it widely
available?
"""
client = OpenAI(
api_key=os.getenv("GROK_API_KEY"),
base_url="https://api.x.ai/v1",
)
response = client.chat.completions.create(
model="grok-3-mini-beta",
messages=[
{
"role": "user",
"content": prompt
}
]
)
# Output the generated Python code
print(response.choices[0].message.content)
Markdown(response.choices[0].message.content)
prompt = """ Explain how CRISPR technology can be used to treat genetic disorders. What are the main challenges, and what future advancements might be necessary to make it widely available? """ client = OpenAI( api_key=os.getenv("GROK_API_KEY"), base_url="https://api.x.ai/v1", ) response = client.chat.completions.create( model="grok-3-mini-beta", messages=[ { "role": "user", "content": prompt } ] ) # Output the generated Python code print(response.choices[0].message.content) Markdown(response.choices[0].message.content)
prompt = """
Explain how CRISPR technology can be used to treat genetic disorders. What are the
main challenges, and what future advancements might be necessary to make it widely
available?
"""
client = OpenAI(
  api_key=os.getenv("GROK_API_KEY"),
  base_url="https://api.x.ai/v1",
)
response = client.chat.completions.create(
  model="grok-3-mini-beta",
  messages=[
      {
      "role": "user",
      "content": prompt
      }
  ]
)
# Output the generated Python code
print(response.choices[0].message.content)
Markdown(response.choices[0].message.content)

輸出:

Grok 3 模型科學研究理解

我的觀點

編碼能力

我發現 Grok 3 的程式碼生成準確度令人印象深刻。在典型的演算法任務上,它的表現優於 gpt-4o、Deepseek-R1 等較輕的模型,不過我建議在下結論之前,先在更復雜的場景中對它進行壓力測試。

編碼能力

推理能力

Grok 3 具有很強的數學推理能力,能夠可靠地解決多步驟問題。不過,我注意到在基於模式的推理方面出現了一些失誤,如識別不明顯的類比或隱藏序列。

複雜用例科學理解

當被要求概述專案結構時,Grok 3 提供了一份條理清晰的計劃,並附有模板程式碼片段。它對 CRISPR 應用的瞭解給我留下了深刻印象,因為它提供了詳細的概述,顯示了對科學的深刻理解。

Grok 3 擅長闡述,並能清晰、深入地給出答案。它在 LLM 推理方面邁出了一大步,但我們還是應該一如既往地根據您的基準來驗證它的輸出結果。

小結

Grok 3 是大型語言模型發展史上的一個重要里程碑。從其“scary‑smart”推理模式和即時 DeepSearch 功能,到多模態支援和業界領先的基準測試,xAI 推出的工具包不僅功能強大,而且用途廣泛。透過本指南,您將學會如何

  • 保護您的 Grok 3 API 金鑰並確保其安全
  • 使用 xAI 的內建計算器預先估算成本
  • 在幾分鐘內完成基本聊天
  • 在程式碼生成、邏輯謎題、複雜工作流和科學查詢中試用 Grok 3。
  • 評估其優勢並確定需要進一步壓力測試的領域

無論您是開發下一個偉大聊天機器人的開發人員,還是探索人工智慧驅動分析的研究人員,抑或僅僅是對LLM 尖端技術充滿好奇的愛好者,Grok 3 都能將深度、速度和現實世界的相關性完美結合。

當您將 Grok 3 整合到您的專案中時,請記得根據您的領域基準驗證其輸出結果,最佳化令牌的使用,並與社羣分享您的發現。編碼快樂 🙂

評論留言