xAI Grok

xAI is an AI company founded by Elon Musk that develops the Grok series of large language models. Grok models are designed to have access to real-time information and are built with a focus on truthfulness, competence, and maximum benefit to humanity.

This guide explains how to integrate Opik with xAI Grok via LiteLLM. By using the LiteLLM integration provided by Opik, you can easily track and evaluate your xAI API calls within your Opik projects as Opik will automatically log the input prompt, model used, token usage, and response generated.

Getting Started

Configuring Opik

To get started, you need to configure Opik to send traces to your Comet project. You can do this by setting the OPIK_PROJECT_NAME environment variable:

$export OPIK_PROJECT_NAME="your-project-name"
>export OPIK_WORKSPACE="your-workspace-name"

You can also call the opik.configure method:

1import opik
2
3opik.configure(
4 project_name="your-project-name",
5 workspace="your-workspace-name",
6)

Configuring LiteLLM

Install the required packages:

$pip install opik litellm

Create a LiteLLM configuration file (e.g., litellm_config.yaml):

1model_list:
2 - model_name: grok-beta
3 litellm_params:
4 model: xai/grok-beta
5 api_key: os.environ/XAI_API_KEY
6 - model_name: grok-vision-beta
7 litellm_params:
8 model: xai/grok-vision-beta
9 api_key: os.environ/XAI_API_KEY
10
11litellm_settings:
12 callbacks: ["opik"]

Authentication

Set your xAI API key as an environment variable:

$export XAI_API_KEY="your-xai-api-key"

You can obtain an xAI API key from the xAI Console.

Usage

Using LiteLLM Proxy Server

Start the LiteLLM proxy server:

$litellm --config litellm_config.yaml

Use the proxy server to make requests:

1import openai
2
3client = openai.OpenAI(
4 api_key="anything", # can be anything
5 base_url="http://0.0.0.0:4000"
6)
7
8response = client.chat.completions.create(
9 model="grok-beta",
10 messages=[
11 {"role": "user", "content": "What are the latest developments in AI technology?"}
12 ]
13)
14
15print(response.choices[0].message.content)

Direct Integration

You can also use LiteLLM directly in your Python code:

1import os
2from litellm import completion
3
4# Configure Opik
5import opik
6opik.configure()
7
8# Configure LiteLLM for Opik
9from litellm.integrations.opik.opik import OpikLogger
10import litellm
11
12litellm.callbacks = ["opik"]
13
14os.environ["XAI_API_KEY"] = "your-xai-api-key"
15
16response = completion(
17 model="xai/grok-beta",
18 messages=[
19 {"role": "user", "content": "What is the current state of renewable energy adoption worldwide?"}
20 ]
21)
22
23print(response.choices[0].message.content)

Supported Models

xAI provides access to several Grok model variants:

  • Grok Beta: grok-beta - The main conversational AI model with real-time information access
  • Grok Vision Beta: grok-vision-beta - Multimodal model capable of processing text and images
  • Grok Mini: grok-mini - A smaller, faster variant optimized for simpler tasks

For the most up-to-date list of available models, visit the xAI API documentation.

Real-time Information Access

One of Grok’s key features is its ability to access real-time information. This makes it particularly useful for questions about current events:

1response = completion(
2 model="xai/grok-beta",
3 messages=[
4 {"role": "user", "content": "What are the latest news headlines today?"}
5 ]
6)
7
8print(response.choices[0].message.content)

Vision Capabilities

Grok Vision Beta can process both text and images:

1from litellm import completion
2
3response = completion(
4 model="xai/grok-vision-beta",
5 messages=[
6 {
7 "role": "user",
8 "content": [
9 {"type": "text", "text": "What do you see in this image?"},
10 {"type": "image_url", "image_url": {"url": "data:image/jpeg;base64,..."}}
11 ]
12 }
13 ]
14)
15
16print(response.choices[0].message.content)

Function Calling

Grok models support function calling for enhanced capabilities:

1tools = [
2 {
3 "type": "function",
4 "function": {
5 "name": "get_current_time",
6 "description": "Get the current time in a specific timezone",
7 "parameters": {
8 "type": "object",
9 "properties": {
10 "timezone": {
11 "type": "string",
12 "description": "The timezone to get the time for",
13 }
14 },
15 "required": ["timezone"],
16 },
17 },
18 }
19]
20
21response = completion(
22 model="xai/grok-beta",
23 messages=[{"role": "user", "content": "What time is it in Tokyo right now?"}],
24 tools=tools,
25)

Advanced Features

Temperature and Creativity Control

Control the creativity of Grok’s responses:

1# More creative responses
2response = completion(
3 model="xai/grok-beta",
4 messages=[{"role": "user", "content": "Write a creative story about space exploration"}],
5 temperature=0.9,
6 max_tokens=1000
7)
8
9# More factual responses
10response = completion(
11 model="xai/grok-beta",
12 messages=[{"role": "user", "content": "Explain quantum computing"}],
13 temperature=0.1,
14 max_tokens=500
15)

System Messages for Behavior Control

Use system messages to guide Grok’s behavior:

1response = completion(
2 model="xai/grok-beta",
3 messages=[
4 {"role": "system", "content": "You are a helpful scientific advisor. Provide accurate, evidence-based information."},
5 {"role": "user", "content": "What are the current challenges in fusion energy research?"}
6 ]
7)

Feedback Scores and Evaluation

Once your xAI calls are logged with Opik, you can evaluate your LLM application using Opik’s evaluation framework:

1from opik.evaluation import evaluate
2from opik.evaluation.metrics import Hallucination
3
4# Define your evaluation task
5def evaluation_task(x):
6 return {
7 "message": x["message"],
8 "output": x["output"],
9 "reference": x["reference"]
10 }
11
12# Create the Hallucination metric
13hallucination_metric = Hallucination()
14
15# Run the evaluation
16evaluation_results = evaluate(
17 experiment_name="xai-grok-evaluation",
18 dataset=your_dataset,
19 task=evaluation_task,
20 scoring_metrics=[hallucination_metric],
21)

Environment Variables

Make sure to set the following environment variables:

$# xAI Configuration
>export XAI_API_KEY="your-xai-api-key"
>
># Opik Configuration
>export OPIK_PROJECT_NAME="your-project-name"
>export OPIK_WORKSPACE="your-workspace-name"