0
0

More than 1 year has passed since last update.

GPTエージェント化 : camel を試行

Last updated at Posted at 2023-04-22

概要

  • camel は, 2つの AI agent 会話形式で目的達成や、考察を深める。
    • かつ, AI にロールを設定できる

公式

使い方: colab

以下の Open in Colab link から google colab で実行できる.
// だいぶ動作が遅いので、local 動作が良いと思う

  • Open in Colab を開く
  • ファイル から ドライブへコピー を選択
  • openai api key を設定する
    • api openai.api_key = "OPENAI_API_KEY"
  • code block が4つ有るので、それぞれの左手に表示されている再生ボタンを押下する。
    • 一箇所押したら完了するまで待機
  • pygame でゲームを作成する方法、を対話しているさまが見れます

使い方: local

install

git clone https://github.com/lightaime/camel.git
cd camel
pre-commit install
pip install -e .

write code

  • colab 上から copy してきた code を file へ書き込む
  • openai api key を記述する
vi make_game.py
import os
import openai
import time
from colorama import Fore
from camel.agents import RolePlaying

// ---- HERE ----
openai.api_key = "sk-DUMMY-OPENAI-API-KEY"

def print_text_animated(text):
    for char in text:
        print(char, end="", flush=True)
        time.sleep(0.02)

task_prompt = "Design a tower defense game using pygame"

print(Fore.YELLOW + f"Original task prompt:\n{task_prompt}\n")
role_play_session = RolePlaying("Computer Programmer", "Gamer", task_prompt)
print(Fore.CYAN + f"Specified task prompt:\n{role_play_session.task_prompt}\n")

chat_turn_limit, n = 10, 0
assistant_msg, _ = role_play_session.init_chat()
while n < chat_turn_limit:
    n += 1
    (assistant_msg, _, _), (user_msg, _, _) = role_play_session.step(assistant_msg)
    print_text_animated(Fore.BLUE + f"AI User:\n\n{user_msg.content}\n\n")
    print_text_animated(Fore.GREEN + f"AI Assistant:\n\n{assistant_msg.content}\n\n")
    if "<CAMEL_TASK_DONE>" in user_msg.content:
        break

run

python make_game.py

出力例

$ python test.py
Original task prompt:
Design a tower defense game using pygame

Specified task prompt:
Develop a visually stunning tower defense game with a diverse range of enemies and unique levels that escalate in difficulty. Implement intuitive controls, engaging music, and a compelling narrative with player-driven choices. Utilize pygame to create fluid animations and responsive gameplay.

AI User:

Instruction: Set up a development environment for the tower defense game.
Input: None

AI Assistant:

Solution: To set up a development environment for the tower defense game, we need to install Python and Pygame. We can download Python from the official website and install it on our system. After installing Python, we can install Pygame by running the following command in the command prompt or terminal:

pip install pygame

Once Pygame is installed, we can start developing the game using our preferred IDE or text editor. Next request.

AI User:

Instruction: Create a basic game window with a specified size.
Input: Window size (e.g. 800x600)

AI Assistant:

Solution: To create a basic game window with a specified size, we can use the Pygame library. Here's an example code snippet that creates a window with the specified size:

import pygame

# Initialize Pygame
pygame.init()

# Set the window size
window_size = (800, 600)

# Create the window
screen = pygame.display.set_mode(window_size)

# Set the window title
pygame.display.set_caption("Tower Defense Game")

# Game loop
while True:
    # Handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    # Update the screen
    pygame.display.update()

In this code, we first import the Pygame library and initialize it. We then set the window size to the specified size and create the window using the pygame.display.set_mode() function. We also set the window title using the pygame.display.set_caption() function.

Finally, we enter the game loop, which handles events and updates the screen. The loop will continue running until the user closes the window.

Note that this is just a basic example and we can customize the window size, title, and other properties as needed. Next request.

AI User:

Instruction: Add a background image to the game window.
Input: File path of the background image.

AI Assistant:

Solution: To add a background image to the game window, we can use the pygame.image.load() function to load the image and the screen.blit() function to draw it on the screen. Here's an example code snippet that loads the specified image and sets it as the background:

import pygame

# Initialize Pygame
pygame.init()

# Set the window size
window_size = (800, 600)

# Create the window
screen = pygame.display.set_mode(window_size)

# Set the window title
pygame.display.set_caption("Tower Defense Game")

# Load the background image
background_image = pygame.image.load("file_path_of_background_image.jpg")

# Game loop
while True:
    # Handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    # Draw the background image
    screen.blit(background_image, (0, 0))

    # Update the screen
    pygame.display.update()

In this code, we first load the background image using the pygame.image.load() function and store it in the background_image variable. We then draw the image on the screen using the screen.blit() function, which takes the image and the position where it should be drawn as arguments. In this case, we draw the image at position (0, 0) to fill the entire screen.

Note that we can customize the position and size of the image as needed. Also, make sure to replace "file_path_of_background_image.jpg" with the actual file path of the background image. Next request.

所管

  • なんとなく実行できる code が出てきているので凄い
  • gpt4 が使えるなら, もっと良い結果になりそう
  • もっと複数のモデルを定義し, 相互作用すると面白そう
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0