-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2e9b195
commit 4c2a2fc
Showing
8 changed files
with
794 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# AI Dev Explainer - AI is useful\n", | ||
"\n", | ||
"This is a notebook for https://www.aiexplainer.dev/stateless\n", | ||
"\n", | ||
"View all notebooks at https://github.com/chroma-core/ai_explainer" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Get an API Key from Anthropic\n", | ||
"1. Create an account - https://console.anthropic.com/\n", | ||
"2. Create an API key - https://console.anthropic.com/settings/keys\n", | ||
"3. Replace `<your_api_key>` below" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Run the code\n", | ||
"In Colab - you can click, \"Runtime -> \"Run All\" in the topbar\n", | ||
"\n", | ||
"In VS Code - you can click \"Run All\" in the topbar" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# install dependencies\n", | ||
"! pip install anthropic" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"\n", | ||
"import os\n", | ||
"import anthropic\n", | ||
"\n", | ||
"# Initialize the Anthropic client\n", | ||
"# Replace '<your_api_key>' with your actual Anthropic API key\n", | ||
"client = anthropic.Client(api_key='<your_api_key>')\n", | ||
"\n", | ||
"alert = \"Slow query performance detected on database-01. Average query time: 2.5 seconds\"\n", | ||
"\n", | ||
"messages = [\n", | ||
" {\n", | ||
" \"role\": \"user\",\n", | ||
" \"content\": alert\n", | ||
" }\n", | ||
"]\n", | ||
"\n", | ||
"response = client.messages.create(\n", | ||
" system=\"You are an AI assistant monitoring database performance. When you receive an alert, gather diagnostic information and generate commands to optimize the database. Send only 1 command that I shoud run and send only the command with no comments before or after.\",\n", | ||
" model=\"claude-3-5-sonnet-20240620\",\n", | ||
" max_tokens=1024,\n", | ||
" messages=messages\n", | ||
")\n", | ||
"\n", | ||
"print(\"Diagnostic command:\")\n", | ||
"print(response.content[0].text)\n", | ||
"\n", | ||
"# Let's assume it asks the users to run\n", | ||
"# #!/bin/bash\n", | ||
"# # Get top CPU-consuming processes\n", | ||
"# ssh admin@web-server-01 \"top -b -n 1 -o %CPU | head -n 5\"\n", | ||
"# # Check system load average\n", | ||
"# ssh admin@web-server-01 \"uptime\"\n", | ||
"\n", | ||
"# Simulate running the diagnostic commands\n", | ||
"diagnostic_results = \"\"\"\n", | ||
"Top 5 slow queries:\n", | ||
"1. SELECT * FROM orders WHERE date > '2023-01-01' (avg time: 3.2s)\n", | ||
"2. SELECT customers.*, orders.* FROM customers JOIN orders ON customers.id = orders.customer_id (avg time: 2.8s)\n", | ||
"3. UPDATE inventory SET stock = stock - 1 WHERE product_id IN (SELECT product_id FROM order_items WHERE order_id = 12345) (avg time: 2.5s)\n", | ||
"4. SELECT COUNT(*) FROM log_entries WHERE timestamp > NOW() - INTERVAL 1 DAY (avg time: 2.3s)\n", | ||
"5. DELETE FROM expired_sessions WHERE last_activity < NOW() - INTERVAL 30 DAY (avg time: 2.1s)\n", | ||
"\n", | ||
"Current database stats:\n", | ||
"- Buffer pool hit ratio: 85%\n", | ||
"- Index usage: 70%\n", | ||
"- Table scans: 25% of queries\n", | ||
"- Temporary tables created on disk: 15% of queries\n", | ||
"\"\"\"\n", | ||
"\n", | ||
"# Add the diagnostic results to the messages\n", | ||
"messages.append({\n", | ||
" \"role\": \"assistant\",\n", | ||
" \"content\": response.content[0].text\n", | ||
"})\n", | ||
"messages.append({\n", | ||
" \"role\": \"user\",\n", | ||
" \"content\": diagnostic_results\n", | ||
"})\n", | ||
"\n", | ||
"# Get optimization commands from the LLM\n", | ||
"response = client.messages.create(\n", | ||
" model=\"claude-3-5-sonnet-20240620\",\n", | ||
" max_tokens=1024,\n", | ||
" messages=messages\n", | ||
")\n", | ||
"\n", | ||
"print(\"\\nOptimization commands:\")\n", | ||
"print(response.content[0].text)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"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.10" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# AI Dev Explainer - AI is useful\n", | ||
"\n", | ||
"This is a notebook for https://www.aiexplainer.dev/stateless\n", | ||
"\n", | ||
"View all notebooks at https://github.com/chroma-core/ai_explainer" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Get an API Key from OpenAI\n", | ||
"1. Create an OpenAI Account https://platform.openai.com/signup\n", | ||
"2. Create an API Key https://platform.openai.com/api-keys\n", | ||
"3. Replace `<your_api_key>` below" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Run the code\n", | ||
"In Colab - you can click, \"Runtime -> \"Run All\" in the topbar\n", | ||
"\n", | ||
"In VS Code - you can click \"Run All\" in the topbar" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# install dependencies\n", | ||
"! pip install openai" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import openai\n", | ||
"\n", | ||
"# Set up the OpenAI API client\n", | ||
"# Replace '<your_api_key>' with your actual OpenAI API key\n", | ||
"client = openai.OpenAI(api_key='<your_api_key>')\n", | ||
"\n", | ||
"alert = \"Slow query performance detected on database-01. Average query time: 2.5 seconds\"\n", | ||
"\n", | ||
"messages = [\n", | ||
" {\n", | ||
" \"role\": \"system\",\n", | ||
" \"content\": \"You are an AI assistant monitoring database performance. When you receive an alert, gather diagnostic information and generate commands to optimize the database. Send only 1 command that I shoud run and send only the command with no comments before or after.\"\n", | ||
" },\n", | ||
" {\n", | ||
" \"role\": \"user\",\n", | ||
" \"content\": alert\n", | ||
" }\n", | ||
"]\n", | ||
"\n", | ||
"response = client.chat.completions.create(\n", | ||
" model=\"gpt-4-turbo\",\n", | ||
" messages=messages,\n", | ||
" max_tokens=1024\n", | ||
")\n", | ||
"\n", | ||
"print(\"Diagnostic commands:\")\n", | ||
"print(response.choices[0].message.content)\n", | ||
"\n", | ||
"# Let's assume it asks the users to run\n", | ||
"# #!/bin/bash\n", | ||
"# # Get top CPU-consuming processes\n", | ||
"# ssh admin@web-server-01 \"top -b -n 1 -o %CPU | head -n 5\"\n", | ||
"# # Check system load average\n", | ||
"# ssh admin@web-server-01 \"uptime\"\n", | ||
"\n", | ||
"# Simulate running the diagnostic commands\n", | ||
"diagnostic_results = \"\"\"\n", | ||
"Top 5 slow queries:\n", | ||
"1. SELECT * FROM orders WHERE date > '2023-01-01' (avg time: 3.2s)\n", | ||
"2. SELECT customers.*, orders.* FROM customers JOIN orders ON customers.id = orders.customer_id (avg time: 2.8s)\n", | ||
"3. UPDATE inventory SET stock = stock - 1 WHERE product_id IN (SELECT product_id FROM order_items WHERE order_id = 12345) (avg time: 2.5s)\n", | ||
"4. SELECT COUNT(*) FROM log_entries WHERE timestamp > NOW() - INTERVAL 1 DAY (avg time: 2.3s)\n", | ||
"5. DELETE FROM expired_sessions WHERE last_activity < NOW() - INTERVAL 30 DAY (avg time: 2.1s)\n", | ||
"\n", | ||
"Current database stats:\n", | ||
"- Buffer pool hit ratio: 85%\n", | ||
"- Index usage: 70%\n", | ||
"- Table scans: 25% of queries\n", | ||
"- Temporary tables created on disk: 15% of queries\n", | ||
"\"\"\"\n", | ||
"\n", | ||
"# Add the diagnostic results to the messages\n", | ||
"messages.append({\n", | ||
" \"role\": \"assistant\",\n", | ||
" \"content\": response.choices[0].message.content\n", | ||
"})\n", | ||
"messages.append({\n", | ||
" \"role\": \"user\",\n", | ||
" \"content\": diagnostic_results\n", | ||
"})\n", | ||
"\n", | ||
"# Get optimization commands from the LLM\n", | ||
"response = client.chat.completions.create(\n", | ||
" model=\"gpt-4-turbo\",\n", | ||
" messages=messages,\n", | ||
" max_tokens=1024\n", | ||
")\n", | ||
"\n", | ||
"print(\"\\nOptimization commands:\")\n", | ||
"print(response.choices[0].message.content)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"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.10" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.