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: game builder agent added #1342

Merged
merged 2 commits into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions python/examples/advanced_agents/game_builder/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
OPENAI_API_KEY=KEY
COMPOSIO_API_KEY=KEY
Comment on lines +1 to +2

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sensitive API keys should not be committed to version control. Use placeholder values like your_key_here in example files to prevent accidental key exposure.

📝 Committable Code Suggestion

‼️ Ensure you review the code suggestion before committing it to the branch. Make sure it replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
OPENAI_API_KEY=KEY
COMPOSIO_API_KEY=KEY
OPENAI_API_KEY=your_key_here
COMPOSIO_API_KEY=your_key_here

55 changes: 55 additions & 0 deletions python/examples/advanced_agents/game_builder/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import os
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The script is missing module-level docstring and type hints. Consider adding:

\"\"\"
Game Builder Agent using Composio and LlamaIndex.

This module provides an interactive interface for creating games using AI assistance.
It leverages PyGame for game development and OpenAI/Groq for AI guidance.
\"\"\"

from typing import Optional, List, Dict

Also consider breaking down the script into smaller functions with proper docstrings for better maintainability.

import dotenv
from composio_llamaindex import App, ComposioToolSet
from llama_index.core.agent import FunctionCallingAgentWorker
from llama_index.core.llms import ChatMessage
from llama_index.llms.openai import OpenAI
from llama_index.llms.groq import Groq

dotenv.load_dotenv()
composio_toolset = ComposioToolSet()
tools = composio_toolset.get_tools(apps=[App.EXA, App.SHELLTOOL, App.FILETOOL, App.CODEINTERPRETER])

#llm = Groq(model="llama3-70b-8192", api_key=os.environ['GROQ_API_KEY'])
llm = OpenAI(model='gpt-4o')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The OpenAI model name gpt-4o appears to be incorrect. Should likely be gpt-4 or gpt-4-turbo. This will cause an API error when running.

📝 Committable Code Suggestion

‼️ Ensure you review the code suggestion before committing it to the branch. Make sure it replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
llm = OpenAI(model='gpt-4o')
llm = OpenAI(model='gpt-4')

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a typo in the model name - gpt-4o should be gpt-4. This could cause runtime errors when initializing the OpenAI client.

prefix_messages = [
ChatMessage(
role="system",
content=(
"You are a Game Creation Agent specialized in designing and implementing games. "
"Your role is to help users create games by:"
"1. Understanding their game concept and requirements"
"2. Breaking down the game into core mechanics and features"
"3. Suggesting appropriate implementation approaches"
"4. Helping with code structure and game logic"
"5. Providing guidance on best practices for game development"
"THE GAME SHOULD BE PLAYABLE and WORKING WHEN RUN WITH THE SHELL TOOL"
"BUILD USING PYGAME AND THEN RUN THE GAME CODE AT THE END"
"You can use various tools to create, modify, and test game code, manage files, "
"and interpret code results. Always aim to create well-structured, maintainable games."
),
)
]

while True:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main game creation loop lacks error handling. Consider wrapping the LLM calls and agent execution in try-except blocks to handle potential API errors, rate limits, or network issues gracefully. Example:

while True:
    try:
        main_task = input("Describe your game idea (or type 'exit' to quit): ")
        if main_task.lower() == 'exit':
            break
            
        main_task_refined = llm.complete(...)
        agent = FunctionCallingAgentWorker(...)
        response = agent.chat(...)
        
    except Exception as e:
        print(f"Error occurred: {e}")
        continue

main_task = input("Describe your game idea (or type 'exit' to quit): ")
main_task_refined = llm.complete(f"Refine this into a better prompt for someone building this game. Give detail on the gameplay mechanics, what 3 single player core features of the game are and more, and detailed. Not long and unnecessary but better prompt:{main_task}. The core idea should be same")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check for 'exit' before calling llm.complete to avoid unnecessary API calls.

if main_task.lower() == 'exit':
break
Comment on lines +35 to +38

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exit condition check occurs after the LLM call, wasting an API call. Move the check before the refinement to prevent unnecessary API usage.

📝 Committable Code Suggestion

‼️ Ensure you review the code suggestion before committing it to the branch. Make sure it replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
main_task = input("Describe your game idea (or type 'exit' to quit): ")
main_task_refined = llm.complete(f"Refine this into a better prompt for someone building this game. Give detail on the gameplay mechanics, what 3 single player core features of the game are and more, and detailed. Not long and unnecessary but better prompt:{main_task}. The core idea should be same")
if main_task.lower() == 'exit':
break
main_task = input("Describe your game idea (or type 'exit' to quit): ")
if main_task.lower() == 'exit':
break
main_task_refined = llm.complete(f"Refine this into a better prompt for someone building this game. Give detail on the gameplay mechanics, what 3 single player core features of the game are and more, and detailed. Not long and unnecessary but better prompt:{main_task}. The core idea should be same")


agent = FunctionCallingAgentWorker(
tools=tools, # type: ignore
llm=llm,
prefix_messages=prefix_messages,
max_function_calls=10,
allow_parallel_tool_calls=False,
verbose=True,
).as_agent()

response = agent.chat(
f"Let's create a game based on this idea: {main_task_refined.text}. "
"Create the game in a file called game.py in the current directory"
"After that is done use the Anthropic tool to find the file and run it"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mentions 'Anthropic tool', but it's not included in the tools list; verify intended tool.

Suggested change
"After that is done use the Anthropic tool to find the file and run it"
"After that is done use the shell tool to find the file and run it"

"Or use the shell tool to run it."
)
print("Response:", response)
30 changes: 30 additions & 0 deletions python/examples/advanced_agents/game_builder/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Game Builder Agent Guide

This guide provides detailed steps to create a Game Builder Agent that leverages Composio, agentic frameworks such as LlamaIndex and GPT to create a Game Builder agent. Ensure you have Python 3.8 or higher installed.


## Steps to Run

**Navigate to the Project Directory:**
Change to the directory where the `setup.sh`, `main.py`, `requirements.txt`, and `README.md` files are located. For example:
```sh
cd path/to/project/directory
```

### 1. Run the Setup File
Make the setup.sh Script Executable (if necessary):
On Linux or macOS, you might need to make the setup.sh script executable:
```shell
chmod +x setup.sh
```
Execute the setup.sh script to set up the environment and install dependencies:
```shell
./setup.sh
```
Now, fill in the `.env` file with your secrets.

### 2. Run the Python Script
```shell
python cookbook/python-examples/advanced_agents/game_builder/main.py
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The path in the run command seems incorrect:

python cookbook/python-examples/advanced_agents/game_builder/main.py

Should be:

python python/examples/advanced_agents/game_builder/main.py

Also consider adding:

  1. Example game prompts and outputs
  2. Troubleshooting section
  3. System requirements (Python version, OS compatibility)
  4. Security considerations when using shell tools

```

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
composio-llamaindex
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The requirements.txt is missing some key dependencies:

  1. pygame - Required for game development
  2. openai - Required for OpenAI API access
  3. Version pinning for all dependencies

Consider updating it to:

composio-llamaindex>=1.0.0
python-dotenv>=1.0.0
llama-index-llms-groq>=0.1.0
pygame>=2.5.0
openai>=1.0.0

python-dotenv
llama-index-llms-groq
33 changes: 33 additions & 0 deletions python/examples/advanced_agents/game_builder/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash

# Create a virtual environment
echo "Creating virtual environment..."
python3 -m venv ~/.venvs/game_builder

# Activate the virtual environment
echo "Activating virtual environment..."
source ~/.venvs/game_builder/bin/activate

# Install libraries from requirements.txt
echo "Installing libraries from requirements.txt..."
pip install -r requirements.txt

# Login to your account
echo "Login to your Composio acount"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: change 'composio acount' to 'composio account'.

Suggested change
echo "Login to your Composio acount"
echo "Login to your Composio account"

composio login

composio add exa

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incomplete command composio add exa appears to be truncated or incorrect. Should specify full package name or be removed.

📝 Committable Code Suggestion

‼️ Ensure you review the code suggestion before committing it to the branch. Make sure it replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
composio add exa
composio add exact


# Copy env backup to .env file
if [ -f ".env.example" ]; then
echo "Copying .env.example to .env..."
cp .env.example .env
else
echo "No .env.example file found. Creating a new .env file..."
touch .env
fi

# Prompt user to fill the .env file
echo "Please fill in the .env file with the necessary environment variables."

echo "Setup completed successfully!"
Loading