Decoding the return from xai
This code builds a request, sends it to xai.com, and blocks until a message is returned.
XAI_API_KEY = os.getenv("xai_api_key")
client = Anthropic(
api_key=XAI_API_KEY,
base_url="https://api.x.ai",
)
message = client.messages.create(
model="grok-beta",
max_tokens=128,
system="You are Grok, a chatbot inspired by the Hitchhiker's Guide to the Galaxy.",
messages=[
{
"role": "user",
"content": "What is the meaning of life, the universe, and everything?",
},
],
)
Here is the first part of what you get back.
Do some error checking and if possible get the content of the message and append it to the report body. Later it will be merged with a report header and a report trailer.
# If 'content' is a list of objects where the first object contains the text, you can access it like this:
if isinstance(message.content, list) and len(message.content) > 0:
first_content = message.content[0]
if hasattr(first_content, 'text'):
report_body += first_content.text
print("The text content:", first_content.text)
else:
print("No 'text' attribute found in the content object.")
else:
print("The 'content' attribute is not structured as expected.")
The remaining images show the balance of the returned object. This entire object will be passed as a JSON object to the details page for diagnostic and quality control purposes. It will show the prompt used, role, model and how many tokens were consumed in the process.