xAI UI & Management Console. The X AI User Interface and Management… | by Cobus Greyling | Nov, 2024


Below is working Python example code that shows a xAI completion implementation where a conversational UI is defined, with a system description and a user description.

import requests
import json

# Set the API endpoint URL
url = "https://api.x.ai/v1/chat/completions"

# Set the headers, including the authorization token
headers =
"Content-Type": "application/json",
"Authorization": "Bearer <Your xai API Key>"

# Define the payload as a dictionary
data =
"messages": [

"role": "system",
"content": "You are a test assistant."
,

"role": "user",
"content": "Testing. Just say hi and hello world and nothing else."

],
"model": "grok-beta",
"stream": False,
"temperature": 0

# Send a POST request to the API
response = requests.post(url, headers=headers, data=json.dumps(data))

# Print the response
if response.status_code == 200:
print("Response:", response.json())
else:
print("Request failed with status code:", response.status_code)
print("Response:", response.text)

And the response for xAI, notice the system fingerprint which is included, this helps to monitor and track underlying changes to the model which underpins the API.

The model name is listed, the refusal reason, finish reason, usage and completion tokens.

Response: 

'id': 'b792ef77-c427-4b0a-8585-4d3517e5d5f1',
'object': 'chat.completion',
'created': 1731518401,
'model': 'grok-beta',
'choices':

['index': 0,
'message': 'role': 'assistant',
'content': 'Hi\n\nHello world',
'refusal': None,
'finish_reason':
'stop'],

'usage': 'prompt_tokens': 28,
'completion_tokens': 5,
'total_tokens': 33,
'system_fingerprint': 'fp_14b89b2dfc'

Below is an example of the OpenAI SDK making use of the xAI model, hence illustrating the strategy from xAI to make migration as easy as possible.

from openai import OpenAI

# Prompt the user to enter their API key
XAI_API_KEY = input("Please enter your xAI API key: ").strip()

# Initialize the OpenAI client with the provided API key and base URL
client = OpenAI(
api_key=XAI_API_KEY,
base_url="https://api.x.ai/v1",
)

# Create a chat completion request
completion = client.chat.completions.create(
model="grok-beta",
messages=[
"role": "system", "content": "You are Grok, a chatbot inspired by the Hitchhiker's Guide to the Galaxy.",
"role": "user", "content": "What is the meaning of life, the universe, and everything?",
],
)

# Print the response message from the chatbot
# Changed from completion.choices[0].message["content"] to completion.choices[0].message.content
print(completion.choices[0].message.content)

And the output from the grok-beta model…

Ah, the ultimate question! According to the Hitchhiker's 
Guide to the Galaxy, the answer is **42**. However,
the real trick lies in figuring out what the actual
question is. Isn't that just like life - full of answers,
but we often miss the point of what we're really asking?

Keep pondering, my friend, and enjoy the journey of
discovery!

Chief Evangelist @ Kore.ai | I’m passionate about exploring the intersection of AI and language. From Language Models, AI Agents to Agentic Applications, Development Frameworks & Data-Centric Productivity Tools, I share insights and ideas on how these technologies are shaping the future.

Recent Articles

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here