Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] Integrate xAI #563

Merged
merged 8 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added docs/images/external/xai/xai-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion docs/mint.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@
"v1/integrations/multion",
"v1/integrations/ollama",
"v1/integrations/openai",
"v1/integrations/rest"
"v1/integrations/rest",
"v1/integrations/xai"
]
},
{
Expand Down
3 changes: 3 additions & 0 deletions docs/v1/examples/examples.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ mode: "wide"
<Card title="REST API" icon="bolt-lightning" href="/v1/examples/restapi">
Create a REST server that performs and observes agent tasks
</Card>
<Card title="xAI" icon={<img src="https://www.github.com/agentops-ai/agentops/blob/main/docs/images/external/openai/xai-logo.png?raw=true" alt="xAI" />} iconType="image" href="/v1/integrations/xai">
Observe the power of Grok and Grok Vision with AgentOps
</Card>
</CardGroup>

## Video Guides
Expand Down
124 changes: 124 additions & 0 deletions docs/v1/integrations/xai.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
---
title: xAI
description: "Observe the power of Grok and Grok Vision with AgentOps"
---

import CodeTooltip from '/snippets/add-code-tooltip.mdx'
import EnvTooltip from '/snippets/add-env-tooltip.mdx'

[xAI](https://x.ai) develops the Grok and Grok Vision models. Explore their developer docs [here](https://docs.x.ai/docs).

<Steps>
<Step title="Install the AgentOps SDK">
<CodeGroup>
```bash pip
pip install agentops
```
```bash poetry
poetry add agentops
```
</CodeGroup>
</Step>
<Step title="Install XAI libraries">
<Note>
xAI models can be accessed via OpenAI or Anthropic clients.
</Note>
To install the OpenAI client, run:
<CodeGroup>
```bash pip
pip install openai
```
```bash poetry
poetry add openai
```
</CodeGroup>
To install the Anthropic client, run:
<CodeGroup>
```bash pip
pip install anthropic
```
```bash poetry
poetry add anthropic
```
</CodeGroup>
</Step>
<Step title="Add AgentOps to your code">
<CodeTooltip/>
<CodeGroup>
```python python
import agentops

agentops.init(<INSERT YOUR API KEY HERE>)

# Your code here...

agentops.end_session("Success")
```
</CodeGroup>
<EnvTooltip />
<CodeGroup>
```python .env
AGENTOPS_API_KEY=<YOUR API KEY>
```
</CodeGroup>
Read more about environment variables in [Advanced Configuration](/v1/usage/advanced-configuration)
</Step>
<Step title="Run your Analysis">
Execute your program and visit [app.agentops.ai/drilldown](https://app.agentops.ai/drilldown) to observe your XAI analysis! 🔍
<Tip>
AgentOps automatically tracks your XAI metrics and visualizations in the Dashboard
</Tip>
</Step>
</Steps>

## Full Examples

<CodeGroup>
```python openai
from openai import OpenAI

XAI_API_KEY = "you xai api key"
client = OpenAI(
api_key=XAI_API_KEY,
base_url="https://api.x.ai/v1",
)

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

print(completion.choices[0].message)
```

```python anthropic
from anthropic import Anthropic

XAI_API_KEY = "you 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?",
},
],
)
print(message.content)
```
</CodeGroup>

<script type="module" src="/scripts/github_stars.js"></script>
<script type="module" src="/scripts/scroll-img-fadein-animation.js"></script>
<script type="module" src="/scripts/button_heartbeat_animation.js"></script>
<script type="css" src="/styles/styles.css"></script>
<script type="module" src="/scripts/adjust_api_dynamically.js"></script>
238 changes: 238 additions & 0 deletions examples/xai_examples/grok_examples.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# XAI Examples\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": 1,
"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": 2,
"metadata": {},
"outputs": [],
"source": [
"load_dotenv()\n",
"XAI_API_KEY = os.getenv(\"XAI_API_KEY\") or \"<your_xai_key>\"\n",
"AGENTOPS_API_KEY = os.getenv(\"AGENTOPS_API_KEY\") or \"<your_agentops_key>\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next we initialize the AgentOps client."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"agentops.init(AGENTOPS_API_KEY, default_tags=[\"xai-example\", \"grok\"])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And we are all set! Note the seesion 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": 4,
"metadata": {},
"outputs": [],
"source": [
"client = OpenAI(\n",
" api_key=XAI_API_KEY,\n",
" base_url=\"https://api.x.ai/v1\",\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": 16,
"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 isn’t 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 doesn’t get in, and walk through it, step by step. \n",
"There’s no sun there, no moon, no direction, no sense of time. Just fine white sand swirling up into the sky like pulverized bones. \n",
"That’s 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. You’ll catch that blood in your hands, your own blood and the blood of others.\n",
"\n",
"And once the storm is over you won’t remember how you made it through, how you managed to survive. You won’t 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 won’t be the same person who walked in. That’s what this storm’s 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": 14,
"metadata": {},
"outputs": [],
"source": [
"response = client.chat.completions.create(\n",
" model=\"grok-beta\",\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": SYSTEM_PROMPT},\n",
" {\"role\": \"user\", \"content\": INSTRUCTION_PROMPT}\n",
" ],\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(f\"Original Response:\\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_session(\"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": "ops",
"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.10.15"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading
Loading