448 lines
27 KiB
Plaintext
Vendored
448 lines
27 KiB
Plaintext
Vendored
{
|
|
"nbformat": 4,
|
|
"nbformat_minor": 0,
|
|
"metadata": {
|
|
"colab": {
|
|
"provenance": []
|
|
},
|
|
"kernelspec": {
|
|
"name": "python3",
|
|
"display_name": "Python 3"
|
|
},
|
|
"language_info": {
|
|
"name": "python"
|
|
}
|
|
},
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"[](https://colab.research.google.com/drive/1ieycgGrlnSLerKqGTPO19FdMLkMxiPp5?usp=sharing)\n"
|
|
],
|
|
"metadata": {
|
|
"id": "s8njmDNrUkal"
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"\n",
|
|
"\n",
|
|
"# Monitoring Autogen agents with Portkey\n",
|
|
"\n",
|
|
"[**AutoGen**](https://github.com/microsoft/autogen) is a framework that enables the development of LLM applications using multiple agents that can converse with each other to solve tasks. AutoGen agents are customizable, conversable, and seamlessly allow human participation. They can operate in various modes that employ combinations of LLMs, human inputs, and tools\n",
|
|
"\n",
|
|
"One of the major challenges of building AI agents is the lack of visibility into key performance metrics such as:\n",
|
|
"\n",
|
|
"- **Cost**: How much does it cost to run the agents\n",
|
|
"- **Token Usage **: How many tokens are being consumed for each agent run.\n",
|
|
"- **Latency**: How long is the agent taking to complete a set of tasks.\n",
|
|
"\n",
|
|
"<br>\n",
|
|
"\n",
|
|
"**Portkey** is an open source [**AI Gateway**](https://github.com/Portkey-AI/gateway) that helps you manage access to 250+ LLMs through a unified API while providing visibility into\n",
|
|
"\n",
|
|
"✅ cost \n",
|
|
"✅ performance \n",
|
|
"✅ accuracy metrics\n",
|
|
"\n",
|
|
"This notebook demonstrates how you can bring visibility and flexbility to Autogen agents using Portkey's AI Gateway."
|
|
],
|
|
"metadata": {
|
|
"id": "NbzKfGtiUwyI"
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [],
|
|
"metadata": {
|
|
"id": "Ff0gXoyWQulq"
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "Bkwr8aOGCOmi",
|
|
"outputId": "d5e81797-f03a-4b1c-9b14-17f0e74d2df1"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"output_type": "stream",
|
|
"name": "stdout",
|
|
"text": [
|
|
"Requirement already satisfied: ag2 in /usr/local/lib/python3.10/dist-packages (0.2.28)\n",
|
|
"Collecting portkey-ai\n",
|
|
" Downloading portkey_ai-1.3.2-py3-none-any.whl (86 kB)\n",
|
|
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m86.3/86.3 kB\u001b[0m \u001b[31m2.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
|
"\u001b[?25hRequirement already satisfied: diskcache in /usr/local/lib/python3.10/dist-packages (from ag2) (5.6.3)\n",
|
|
"Requirement already satisfied: docker in /usr/local/lib/python3.10/dist-packages (from ag2) (7.1.0)\n",
|
|
"Requirement already satisfied: flaml in /usr/local/lib/python3.10/dist-packages (from ag2) (2.1.2)\n",
|
|
"Requirement already satisfied: numpy<2,>=1.17.0 in /usr/local/lib/python3.10/dist-packages (from ag2) (1.25.2)\n",
|
|
"Requirement already satisfied: openai>=1.3 in /usr/local/lib/python3.10/dist-packages (from ag2) (1.30.5)\n",
|
|
"Requirement already satisfied: packaging in /usr/local/lib/python3.10/dist-packages (from ag2) (24.0)\n",
|
|
"Requirement already satisfied: pydantic!=2.6.0,<3,>=1.10 in /usr/local/lib/python3.10/dist-packages (from ag2) (2.7.1)\n",
|
|
"Requirement already satisfied: python-dotenv in /usr/local/lib/python3.10/dist-packages (from ag2) (1.0.1)\n",
|
|
"Requirement already satisfied: termcolor in /usr/local/lib/python3.10/dist-packages (from ag2) (2.4.0)\n",
|
|
"Requirement already satisfied: tiktoken in /usr/local/lib/python3.10/dist-packages (from ag2) (0.7.0)\n",
|
|
"Requirement already satisfied: httpx in /usr/local/lib/python3.10/dist-packages (from portkey-ai) (0.27.0)\n",
|
|
"Collecting mypy<2.0,>=0.991 (from portkey-ai)\n",
|
|
" Downloading mypy-1.10.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (12.7 MB)\n",
|
|
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m12.7/12.7 MB\u001b[0m \u001b[31m36.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
|
"\u001b[?25hRequirement already satisfied: typing-extensions<5.0,>=4.7.1 in /usr/local/lib/python3.10/dist-packages (from portkey-ai) (4.11.0)\n",
|
|
"Collecting mypy-extensions>=1.0.0 (from mypy<2.0,>=0.991->portkey-ai)\n",
|
|
" Downloading mypy_extensions-1.0.0-py3-none-any.whl (4.7 kB)\n",
|
|
"Requirement already satisfied: tomli>=1.1.0 in /usr/local/lib/python3.10/dist-packages (from mypy<2.0,>=0.991->portkey-ai) (2.0.1)\n",
|
|
"Requirement already satisfied: anyio<5,>=3.5.0 in /usr/local/lib/python3.10/dist-packages (from openai>=1.3->ag2) (3.7.1)\n",
|
|
"Requirement already satisfied: distro<2,>=1.7.0 in /usr/lib/python3/dist-packages (from openai>=1.3->ag2) (1.7.0)\n",
|
|
"Requirement already satisfied: sniffio in /usr/local/lib/python3.10/dist-packages (from openai>=1.3->ag2) (1.3.1)\n",
|
|
"Requirement already satisfied: tqdm>4 in /usr/local/lib/python3.10/dist-packages (from openai>=1.3->ag2) (4.66.4)\n",
|
|
"Requirement already satisfied: certifi in /usr/local/lib/python3.10/dist-packages (from httpx->portkey-ai) (2024.2.2)\n",
|
|
"Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.10/dist-packages (from httpx->portkey-ai) (1.0.5)\n",
|
|
"Requirement already satisfied: idna in /usr/local/lib/python3.10/dist-packages (from httpx->portkey-ai) (3.7)\n",
|
|
"Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.10/dist-packages (from httpcore==1.*->httpx->portkey-ai) (0.14.0)\n",
|
|
"Requirement already satisfied: annotated-types>=0.4.0 in /usr/local/lib/python3.10/dist-packages (from pydantic!=2.6.0,<3,>=1.10->ag2) (0.7.0)\n",
|
|
"Requirement already satisfied: pydantic-core==2.18.2 in /usr/local/lib/python3.10/dist-packages (from pydantic!=2.6.0,<3,>=1.10->ag2) (2.18.2)\n",
|
|
"Requirement already satisfied: requests>=2.26.0 in /usr/local/lib/python3.10/dist-packages (from docker->ag2) (2.31.0)\n",
|
|
"Requirement already satisfied: urllib3>=1.26.0 in /usr/local/lib/python3.10/dist-packages (from docker->ag2) (2.0.7)\n",
|
|
"Requirement already satisfied: regex>=2022.1.18 in /usr/local/lib/python3.10/dist-packages (from tiktoken->ag2) (2024.5.15)\n",
|
|
"Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3.5.0->openai>=1.3->ag2) (1.2.1)\n",
|
|
"Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests>=2.26.0->docker->ag2) (3.3.2)\n",
|
|
"Installing collected packages: mypy-extensions, mypy, portkey-ai\n",
|
|
"Successfully installed mypy-1.10.0 mypy-extensions-1.0.0 portkey-ai-1.3.2\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"!pip install -qU ag2 portkey-ai"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"## Game Developer Assistant"
|
|
],
|
|
"metadata": {
|
|
"id": "7Lu7RjEVZ4P7"
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": [
|
|
"from autogen import AssistantAgent, UserProxyAgent, config_list_from_json\n",
|
|
"from google.colab import userdata\n",
|
|
"\n",
|
|
"# Import the portkey library to fetch helper functions\n",
|
|
"from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders\n",
|
|
"\n",
|
|
"gpt3_config = [\n",
|
|
" {\n",
|
|
" \"api_key\": userdata.get(\"OPENAI_API_KEY\"),\n",
|
|
" \"model\": \"gpt-3.5-turbo\",\n",
|
|
" \"base_url\": PORTKEY_GATEWAY_URL,\n",
|
|
" \"api_type\": \"openai\",\n",
|
|
" \"default_headers\": createHeaders(\n",
|
|
" api_key = userdata.get(\"PORTKEY_API_KEY\"),\n",
|
|
" provider = \"openai\",\n",
|
|
" )\n",
|
|
" }\n",
|
|
"]\n",
|
|
"\n",
|
|
"\n",
|
|
"gpt4o_config = [\n",
|
|
" {\n",
|
|
" \"api_key\": userdata.get(\"OPENAI_API_KEY\"),\n",
|
|
" \"model\": \"gpt-4o\",\n",
|
|
" \"base_url\": PORTKEY_GATEWAY_URL,\n",
|
|
" \"api_type\": \"openai\",\n",
|
|
" \"default_headers\": createHeaders(\n",
|
|
" api_key = userdata.get(\"PORTKEY_API_KEY\"),\n",
|
|
" provider = \"openai\",\n",
|
|
" )\n",
|
|
" }\n",
|
|
"]\n",
|
|
"\n",
|
|
"llama3_config = [\n",
|
|
" {\n",
|
|
" \"api_key\": userdata.get(\"GROQ_API_KEY\"),\n",
|
|
" \"model\": \"llama3-70b-8192\",\n",
|
|
" \"base_url\": PORTKEY_GATEWAY_URL,\n",
|
|
" \"api_type\": \"openai\", # Portkey conforms to the openai api_type\n",
|
|
" \"default_headers\": createHeaders(\n",
|
|
" api_key = userdata.get(\"PORTKEY_API_KEY\"),\n",
|
|
" provider = \"groq\",\n",
|
|
" )\n",
|
|
" }\n",
|
|
"]"
|
|
],
|
|
"metadata": {
|
|
"id": "a5zYpfdSRr9S"
|
|
},
|
|
"execution_count": null,
|
|
"outputs": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": [
|
|
"import autogen\n",
|
|
"\n",
|
|
"# Create user proxy agent, coder, product manager\n",
|
|
"user_proxy = autogen.UserProxyAgent(\n",
|
|
" name=\"User_proxy\",\n",
|
|
" system_message=\"A human admin who will give the idea and run the code provided by Coder.\",\n",
|
|
" code_execution_config={\"last_n_messages\": 2, \"work_dir\": \"groupchat\"},\n",
|
|
" human_input_mode=\"ALWAYS\",\n",
|
|
")\n",
|
|
"coder = autogen.AssistantAgent(\n",
|
|
" name=\"Coder\",\n",
|
|
" # system_message = \"You are talented Python developer who is good at developing games. You closely work with Product Manager.\"\n",
|
|
" llm_config={\"config_list\": llama3_config},\n",
|
|
")\n",
|
|
"pm = autogen.AssistantAgent(\n",
|
|
" name=\"product_manager\",\n",
|
|
" system_message=\"You will help break down the initial idea into a well scoped requirement for the coder; Do not involve in future conversations or error fixing\",\n",
|
|
" llm_config={\"config_list\": gpt3_config},\n",
|
|
")\n",
|
|
"\n",
|
|
"# Create groupchat\n",
|
|
"groupchat = autogen.GroupChat(\n",
|
|
" agents=[user_proxy, coder, pm], messages=[])\n",
|
|
"manager = autogen.GroupChatManager(groupchat=groupchat, llm_config={\"config_list\": gpt4o_config})\n",
|
|
"\n",
|
|
"# Start the conversation\n",
|
|
"user_proxy.initiate_chat(\n",
|
|
" manager, message=\"Build a classic & basic pong game with 2 players in python\")"
|
|
],
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "qChRjgaxYhVu",
|
|
"outputId": "9cb5da77-6556-4b9c-be44-99fe4f0fc01e"
|
|
},
|
|
"execution_count": null,
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"User_proxy (to chat_manager):\n",
|
|
"\n",
|
|
"Build a classic & basic pong game with 2 players in python\n",
|
|
"\n",
|
|
"--------------------------------------------------------------------------------\n",
|
|
"\n",
|
|
"Next speaker: product_manager\n",
|
|
"\n",
|
|
"product_manager (to chat_manager):\n",
|
|
"\n",
|
|
"Requirement:\n",
|
|
"- The pong game should be built in Python.\n",
|
|
"- The game should follow the classic pong game mechanics with two paddles and a ball.\n",
|
|
"- There should be two players, each controlling one paddle.\n",
|
|
"- The paddles should move up and down vertically.\n",
|
|
"- The ball should bounce off the paddles and the top/bottom boundaries of the game screen.\n",
|
|
"- The game should keep score and display it on the screen.\n",
|
|
"- The game should end when one player reaches a certain score (e.g., 10 points).\n",
|
|
"- The code should be well-documented and organized.\n",
|
|
"\n",
|
|
"--------------------------------------------------------------------------------\n",
|
|
"\n",
|
|
"Next speaker: Coder\n",
|
|
"\n",
|
|
"Coder (to chat_manager):\n",
|
|
"\n",
|
|
"Here is a basic implementation of a 2-player Pong game using Python with the Pygame library:\n",
|
|
"\n",
|
|
"```\n",
|
|
"# pong_game.py\n",
|
|
"import pygame\n",
|
|
"import sys\n",
|
|
"\n",
|
|
"# Initialize Pygame\n",
|
|
"pygame.init()\n",
|
|
"\n",
|
|
"# Set up some constants\n",
|
|
"WIDTH, HEIGHT = 800, 600\n",
|
|
"PADDLE_WIDTH, PADDLE_HEIGHT = 10, 100\n",
|
|
"BALL_SIZE = 10\n",
|
|
"FPS = 60\n",
|
|
"\n",
|
|
"# Set up some colors\n",
|
|
"WHITE = (255, 255, 255)\n",
|
|
"BLACK = (0, 0, 0)\n",
|
|
"\n",
|
|
"# Set up the display\n",
|
|
"screen = pygame.display.set_mode((WIDTH, HEIGHT))\n",
|
|
"pygame.display.set_caption(\"Pong Game\")\n",
|
|
"\n",
|
|
"# Set up the paddles\n",
|
|
"paddle1 = pygame.Rect(10, HEIGHT / 2 - PADDLE_HEIGHT / 2, PADDLE_WIDTH, PADDLE_HEIGHT)\n",
|
|
"paddle2 = pygame.Rect(WIDTH - 20, HEIGHT / 2 - PADDLE_HEIGHT / 2, PADDLE_WIDTH, PADDLE_HEIGHT)\n",
|
|
"\n",
|
|
"# Set up the ball\n",
|
|
"ball = pygame.Rect(WIDTH / 2, HEIGHT / 2, BALL_SIZE, BALL_SIZE)\n",
|
|
"ball_velocity_x = 5\n",
|
|
"ball_velocity_y = 5\n",
|
|
"\n",
|
|
"# Set up the scores\n",
|
|
"score1, score2 = 0, 0\n",
|
|
"\n",
|
|
"# Game loop\n",
|
|
"clock = pygame.time.Clock()\n",
|
|
"while True:\n",
|
|
" # Event handling\n",
|
|
" for event in pygame.event.get():\n",
|
|
" if event.type == pygame.QUIT:\n",
|
|
" pygame.quit()\n",
|
|
" sys.exit()\n",
|
|
"\n",
|
|
" # Move the paddles\n",
|
|
" keys = pygame.key.get_pressed()\n",
|
|
" if keys[pygame.K_w]:\n",
|
|
" paddle1.y -= 5\n",
|
|
" if keys[pygame.K_s]:\n",
|
|
" paddle1.y += 5\n",
|
|
" if keys[pygame.K_UP]:\n",
|
|
" paddle2.y -= 5\n",
|
|
" if keys[pygame.K_DOWN]:\n",
|
|
" paddle2.y += 5\n",
|
|
"\n",
|
|
" # Move the ball\n",
|
|
" ball.x += ball_velocity_x\n",
|
|
" ball.y += ball_velocity_y\n",
|
|
"\n",
|
|
" # Bounce off the paddles\n",
|
|
" if ball.colliderect(paddle1) or ball.colliderect(paddle2):\n",
|
|
" ball_velocity_x = -ball_velocity_x\n",
|
|
" # Bounce off the top and bottom\n",
|
|
" if ball.y < 0 or ball.y > HEIGHT - BALL_SIZE:\n",
|
|
" ball_velocity_y = -ball_velocity_y\n",
|
|
"\n",
|
|
" # Score points\n",
|
|
" if ball.x < 0:\n",
|
|
" score2 += 1\n",
|
|
" ball.x = WIDTH / 2\n",
|
|
" ball.y = HEIGHT / 2\n",
|
|
" ball_velocity_x = 5\n",
|
|
" ball_velocity_y = 5\n",
|
|
" elif ball.x > WIDTH - BALL_SIZE:\n",
|
|
" score1 += 1\n",
|
|
" ball.x = WIDTH / 2\n",
|
|
" ball.y = HEIGHT / 2\n",
|
|
" ball_velocity_x = -5\n",
|
|
" ball_velocity_y = 5\n",
|
|
"\n",
|
|
" # Draw everything\n",
|
|
" screen.fill(BLACK)\n",
|
|
" pygame.draw.rect(screen, WHITE, paddle1)\n",
|
|
" pygame.draw.rect(screen, WHITE, paddle2)\n",
|
|
" pygame.draw.ellipse(screen, WHITE, ball)\n",
|
|
" pygame.draw.aaline(screen, WHITE, (WIDTH / 2, 0), (WIDTH / 2, HEIGHT))\n",
|
|
" font = pygame.font.Font(None, 36)\n",
|
|
" text = f\"{score1} - {score2}\"\n",
|
|
" img = font.render(text, True, WHITE)\n",
|
|
" screen.blit(img, (WIDTH / 2 - 30, 10))\n",
|
|
"\n",
|
|
" # Update the display\n",
|
|
" pygame.display.flip()\n",
|
|
"\n",
|
|
" # Cap the framerate\n",
|
|
" clock.tick(FPS)\n",
|
|
"\n",
|
|
" # Check for winning condition\n",
|
|
" if score1 >= 10 or score2 >= 10:\n",
|
|
" if score1 >= 10:\n",
|
|
" print(\"Player 1 wins!\")\n",
|
|
" else:\n",
|
|
" print(\"Player 2 wins!\")\n",
|
|
" break\n",
|
|
"\n",
|
|
"pygame.quit()\n",
|
|
"```\n",
|
|
"\n",
|
|
"To run the game, save the above code in a file named `pong_game.py`, and then execute it with `python pong_game.py`. This will launch the game window.\n",
|
|
"\n",
|
|
"In this game, player 1 controls the left paddle with the W and S keys, and player 2 controls the right paddle with the UP and DOWN keys. The first player to score 10 points wins the game.\n",
|
|
"\n",
|
|
"TERMINATE\n",
|
|
"\n",
|
|
"--------------------------------------------------------------------------------\n",
|
|
"\n",
|
|
"Next speaker: User_proxy\n",
|
|
"\n",
|
|
"Provide feedback to chat_manager. Press enter to skip and use auto-reply, or type 'exit' to end the conversation: exit\n"
|
|
]
|
|
},
|
|
{
|
|
"output_type": "execute_result",
|
|
"data": {
|
|
"text/plain": [
|
|
"ChatResult(chat_id=None, chat_history=[{'content': 'Build a classic & basic pong game with 2 players in python', 'role': 'assistant'}, {'content': 'Requirement:\\n- The pong game should be built in Python.\\n- The game should follow the classic pong game mechanics with two paddles and a ball.\\n- There should be two players, each controlling one paddle.\\n- The paddles should move up and down vertically.\\n- The ball should bounce off the paddles and the top/bottom boundaries of the game screen.\\n- The game should keep score and display it on the screen.\\n- The game should end when one player reaches a certain score (e.g., 10 points).\\n- The code should be well-documented and organized.', 'name': 'product_manager', 'role': 'user'}, {'content': 'Here is a basic implementation of a 2-player Pong game using Python with the Pygame library:\\n\\n```\\n# pong_game.py\\nimport pygame\\nimport sys\\n\\n# Initialize Pygame\\npygame.init()\\n\\n# Set up some constants\\nWIDTH, HEIGHT = 800, 600\\nPADDLE_WIDTH, PADDLE_HEIGHT = 10, 100\\nBALL_SIZE = 10\\nFPS = 60\\n\\n# Set up some colors\\nWHITE = (255, 255, 255)\\nBLACK = (0, 0, 0)\\n\\n# Set up the display\\nscreen = pygame.display.set_mode((WIDTH, HEIGHT))\\npygame.display.set_caption(\"Pong Game\")\\n\\n# Set up the paddles\\npaddle1 = pygame.Rect(10, HEIGHT / 2 - PADDLE_HEIGHT / 2, PADDLE_WIDTH, PADDLE_HEIGHT)\\npaddle2 = pygame.Rect(WIDTH - 20, HEIGHT / 2 - PADDLE_HEIGHT / 2, PADDLE_WIDTH, PADDLE_HEIGHT)\\n\\n# Set up the ball\\nball = pygame.Rect(WIDTH / 2, HEIGHT / 2, BALL_SIZE, BALL_SIZE)\\nball_velocity_x = 5\\nball_velocity_y = 5\\n\\n# Set up the scores\\nscore1, score2 = 0, 0\\n\\n# Game loop\\nclock = pygame.time.Clock()\\nwhile True:\\n # Event handling\\n for event in pygame.event.get():\\n if event.type == pygame.QUIT:\\n pygame.quit()\\n sys.exit()\\n\\n # Move the paddles\\n keys = pygame.key.get_pressed()\\n if keys[pygame.K_w]:\\n paddle1.y -= 5\\n if keys[pygame.K_s]:\\n paddle1.y += 5\\n if keys[pygame.K_UP]:\\n paddle2.y -= 5\\n if keys[pygame.K_DOWN]:\\n paddle2.y += 5\\n\\n # Move the ball\\n ball.x += ball_velocity_x\\n ball.y += ball_velocity_y\\n\\n # Bounce off the paddles\\n if ball.colliderect(paddle1) or ball.colliderect(paddle2):\\n ball_velocity_x = -ball_velocity_x\\n # Bounce off the top and bottom\\n if ball.y < 0 or ball.y > HEIGHT - BALL_SIZE:\\n ball_velocity_y = -ball_velocity_y\\n\\n # Score points\\n if ball.x < 0:\\n score2 += 1\\n ball.x = WIDTH / 2\\n ball.y = HEIGHT / 2\\n ball_velocity_x = 5\\n ball_velocity_y = 5\\n elif ball.x > WIDTH - BALL_SIZE:\\n score1 += 1\\n ball.x = WIDTH / 2\\n ball.y = HEIGHT / 2\\n ball_velocity_x = -5\\n ball_velocity_y = 5\\n\\n # Draw everything\\n screen.fill(BLACK)\\n pygame.draw.rect(screen, WHITE, paddle1)\\n pygame.draw.rect(screen, WHITE, paddle2)\\n pygame.draw.ellipse(screen, WHITE, ball)\\n pygame.draw.aaline(screen, WHITE, (WIDTH / 2, 0), (WIDTH / 2, HEIGHT))\\n font = pygame.font.Font(None, 36)\\n text = f\"{score1} - {score2}\"\\n img = font.render(text, True, WHITE)\\n screen.blit(img, (WIDTH / 2 - 30, 10))\\n\\n # Update the display\\n pygame.display.flip()\\n\\n # Cap the framerate\\n clock.tick(FPS)\\n\\n # Check for winning condition\\n if score1 >= 10 or score2 >= 10:\\n if score1 >= 10:\\n print(\"Player 1 wins!\")\\n else:\\n print(\"Player 2 wins!\")\\n break\\n\\npygame.quit()\\n```\\n\\nTo run the game, save the above code in a file named `pong_game.py`, and then execute it with `python pong_game.py`. This will launch the game window.\\n\\nIn this game, player 1 controls the left paddle with the W and S keys, and player 2 controls the right paddle with the UP and DOWN keys. The first player to score 10 points wins the game.\\n\\nTERMINATE', 'name': 'Coder', 'role': 'user'}], summary='Here is a basic implementation of a 2-player Pong game using Python with the Pygame library:\\n\\n```\\n# pong_game.py\\nimport pygame\\nimport sys\\n\\n# Initialize Pygame\\npygame.init()\\n\\n# Set up some constants\\nWIDTH, HEIGHT = 800, 600\\nPADDLE_WIDTH, PADDLE_HEIGHT = 10, 100\\nBALL_SIZE = 10\\nFPS = 60\\n\\n# Set up some colors\\nWHITE = (255, 255, 255)\\nBLACK = (0, 0, 0)\\n\\n# Set up the display\\nscreen = pygame.display.set_mode((WIDTH, HEIGHT))\\npygame.display.set_caption(\"Pong Game\")\\n\\n# Set up the paddles\\npaddle1 = pygame.Rect(10, HEIGHT / 2 - PADDLE_HEIGHT / 2, PADDLE_WIDTH, PADDLE_HEIGHT)\\npaddle2 = pygame.Rect(WIDTH - 20, HEIGHT / 2 - PADDLE_HEIGHT / 2, PADDLE_WIDTH, PADDLE_HEIGHT)\\n\\n# Set up the ball\\nball = pygame.Rect(WIDTH / 2, HEIGHT / 2, BALL_SIZE, BALL_SIZE)\\nball_velocity_x = 5\\nball_velocity_y = 5\\n\\n# Set up the scores\\nscore1, score2 = 0, 0\\n\\n# Game loop\\nclock = pygame.time.Clock()\\nwhile True:\\n # Event handling\\n for event in pygame.event.get():\\n if event.type == pygame.QUIT:\\n pygame.quit()\\n sys.exit()\\n\\n # Move the paddles\\n keys = pygame.key.get_pressed()\\n if keys[pygame.K_w]:\\n paddle1.y -= 5\\n if keys[pygame.K_s]:\\n paddle1.y += 5\\n if keys[pygame.K_UP]:\\n paddle2.y -= 5\\n if keys[pygame.K_DOWN]:\\n paddle2.y += 5\\n\\n # Move the ball\\n ball.x += ball_velocity_x\\n ball.y += ball_velocity_y\\n\\n # Bounce off the paddles\\n if ball.colliderect(paddle1) or ball.colliderect(paddle2):\\n ball_velocity_x = -ball_velocity_x\\n # Bounce off the top and bottom\\n if ball.y < 0 or ball.y > HEIGHT - BALL_SIZE:\\n ball_velocity_y = -ball_velocity_y\\n\\n # Score points\\n if ball.x < 0:\\n score2 += 1\\n ball.x = WIDTH / 2\\n ball.y = HEIGHT / 2\\n ball_velocity_x = 5\\n ball_velocity_y = 5\\n elif ball.x > WIDTH - BALL_SIZE:\\n score1 += 1\\n ball.x = WIDTH / 2\\n ball.y = HEIGHT / 2\\n ball_velocity_x = -5\\n ball_velocity_y = 5\\n\\n # Draw everything\\n screen.fill(BLACK)\\n pygame.draw.rect(screen, WHITE, paddle1)\\n pygame.draw.rect(screen, WHITE, paddle2)\\n pygame.draw.ellipse(screen, WHITE, ball)\\n pygame.draw.aaline(screen, WHITE, (WIDTH / 2, 0), (WIDTH / 2, HEIGHT))\\n font = pygame.font.Font(None, 36)\\n text = f\"{score1} - {score2}\"\\n img = font.render(text, True, WHITE)\\n screen.blit(img, (WIDTH / 2 - 30, 10))\\n\\n # Update the display\\n pygame.display.flip()\\n\\n # Cap the framerate\\n clock.tick(FPS)\\n\\n # Check for winning condition\\n if score1 >= 10 or score2 >= 10:\\n if score1 >= 10:\\n print(\"Player 1 wins!\")\\n else:\\n print(\"Player 2 wins!\")\\n break\\n\\npygame.quit()\\n```\\n\\nTo run the game, save the above code in a file named `pong_game.py`, and then execute it with `python pong_game.py`. This will launch the game window.\\n\\nIn this game, player 1 controls the left paddle with the W and S keys, and player 2 controls the right paddle with the UP and DOWN keys. The first player to score 10 points wins the game.\\n\\n', cost={'usage_including_cached_inference': {'total_cost': 0}, 'usage_excluding_cached_inference': {'total_cost': 0}}, human_input=['exit'])"
|
|
]
|
|
},
|
|
"metadata": {},
|
|
"execution_count": 13
|
|
}
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"## Telemetry with Portkey"
|
|
],
|
|
"metadata": {
|
|
"id": "I643ntBDVlFP"
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"### Part 1: Logs\n",
|
|
"\n",
|
|
"For the above agent run, we have initiated two agents utilizing the GPT-3.5, GPT-4o and Llama 3 models. The detailed logs for this request are available on the Portkey dashboard, providing comprehensive insights into the activities of each agent."
|
|
],
|
|
"metadata": {
|
|
"id": "Rxmiy8oiVrWc"
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": [],
|
|
"metadata": {
|
|
"id": "jg_odrv3VnzI"
|
|
},
|
|
"execution_count": null,
|
|
"outputs": []
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"### Part 2: Overall Analytics\n",
|
|
"\n",
|
|
"The insights from this request can be viewed on the Portkey dashboard. We are able to infer the following:\n",
|
|
"\n",
|
|
"1. Total Tokens Used: ___ tokens\n",
|
|
"2. Total Number of Requests: __ requests; Llama 3 (__ requests), GPT-3.5 (__ requests), GPT-4o (_ requests)\n",
|
|
"\n",
|
|
"You can also review many other metrics from the dashboards like cost, latency, etc."
|
|
],
|
|
"metadata": {
|
|
"id": "YjEdPrlgVzEU"
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": [],
|
|
"metadata": {
|
|
"id": "Hur_yrQMV_of"
|
|
},
|
|
"execution_count": null,
|
|
"outputs": []
|
|
}
|
|
]
|
|
}
|