agentops/examples/xai/grok_examples.ipynb

236 lines
7.7 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# XAI Example\n",
"This notebook demonstrates how to use XAI with AgentOps via the OpenAI python client. \n",
"\n",
"We are going to use the latest Grok model from XAI to create a transliteration chatbot that can understand the major languages of the world and translate them to a user's native language! We will use AgentOps to track the chatbot's performance."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First let's install the required packages"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install -U openai\n",
"%pip install -U agentops"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then import them"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from openai import OpenAI\n",
"import agentops\n",
"import os\n",
"from dotenv import load_dotenv"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next, we'll grab our API keys. You can use dotenv like below or however else you like to load environment variables"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"load_dotenv()\n",
"os.environ[\"AGENTOPS_API_KEY\"] = os.getenv(\"AGENTOPS_API_KEY\", \"your_api_key_here\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next we initialize the AgentOps client."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"agentops.init(auto_start_session=False)\n",
"tracer = agentops.start_trace(trace_name=\"XAI Example\", tags=[\"xai-example\", \"grok\", \"agentops-example\"])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And we are all set! Note the session URL above. We will use it to track the chatbot.\n",
"\n",
"Let's initialize the OpenAI client with the XAI API key and base url."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"client = OpenAI(\n",
" base_url=\"https://api.x.ai/v1\",\n",
" api_key=os.getenv(\"XAI_API_KEY\", \"your_xai_api_key_here\"),\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we will set the system and instruction prompts for the chatbot. We will set the native language to Spanish and the user prompt to transliterate an excerpt from Haruki Murakami's \"Kafka On The Shore\"."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"SYSTEM_PROMPT = \"\"\"\n",
"You are a highly intelligent, multilingual assistant designed to understand user prompts in English and respond in the user's specified native language. \n",
"Your key responsibilities include:\n",
"1. Translating and generating meaningful, contextually appropriate responses in the user's native language.\n",
"2. Ensuring the output is accurate, coherent, and in Unicode format for proper display in the specified language.\n",
"3. Adhering to the nuances of the specified language's grammar, tone, and cultural context.\n",
"\n",
"When asked to respond in a language, generate the response entirely in that language without using English unless explicitly requested.\n",
"\n",
"If the specified language is unfamiliar or ambiguous, politely ask for clarification in English.\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"native_language = \"Spanish\"\n",
"\n",
"USER_PROMPT = \"\"\"\n",
"Sometimes fate is like a small sandstorm that keeps changing directions. You change direction but the sandstorm chases you. \n",
"You turn again, but the storm adjusts. Over and over you play this out, like some ominous dance with death just before dawn. Why? \n",
"Because this storm isnt something that blew in from far away, something that has nothing to do with you. This storm is you. \n",
"Something inside of you. So all you can do is give in to it, step right inside the storm, closing your eyes and plugging up your ears so the sand doesnt get in, and walk through it, step by step. \n",
"Theres no sun there, no moon, no direction, no sense of time. Just fine white sand swirling up into the sky like pulverized bones. \n",
"Thats the kind of sandstorm you need to imagine.\n",
"\n",
"And you really will have to make it through that violent, metaphysical, symbolic storm. \n",
"No matter how metaphysical or symbolic it might be, make no mistake about it: it will cut through flesh like a thousand razor blades. People will bleed there, and you will bleed too. \n",
"Hot, red blood. Youll catch that blood in your hands, your own blood and the blood of others.\n",
"\n",
"And once the storm is over you wont remember how you made it through, how you managed to survive. You wont even be sure, in fact, whether the storm is really over. \n",
"But one thing is certain. When you come out of the storm you wont be the same person who walked in. Thats what this storms all about.\n",
"\"\"\"\n",
"\n",
"INSTRUCTION_PROMPT = f\"\"\"\n",
"You are a multilingual chatbot. Take the user's prompt: \"{USER_PROMPT}\" and respond naturally in {native_language}. \n",
"Ensure that the response is in Unicode characters appropriate for {native_language}.\n",
"\"\"\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we will use the OpenAI client to generate the response by passing in the system and instruction prompts."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"response = client.chat.completions.create(\n",
" model=\"grok-3-mini\",\n",
" messages=[{\"role\": \"system\", \"content\": SYSTEM_PROMPT}, {\"role\": \"user\", \"content\": INSTRUCTION_PROMPT}],\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(f\"Original Prompt:\\n{USER_PROMPT}\")\n",
"generated_response = response.choices[0].message.content\n",
"print(f\"Response in {native_language}:\\n{generated_response}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Awesome! We can now transliterate from English to any language! And all of this can be tracked with AgentOps by going to the session url above."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"agentops.end_trace(tracer, end_state=\"Success\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We end the session with a success state and a success reason. This is useful if you want to track the success or failure of the chatbot. In that case you can set the end state to failure and provide a reason. By default the session will have an indeterminate end state."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "agentops (3.11.11)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}