You can also open this on Google ColabIn this cookbook, I want to show how Multi-Step environments work in CAMEL. Our RL modules were built to mimic OpenAI Gym, so if you’re familiar with Gym’s interface, you’ll feel right at home.We will use the Tic-Tac-Toe environment as an example to show the lifecycle of an environment.The Tic-Tac-Toe environment can be used to evaluate agents, generate synthetic data for distillation, or train an agent to play the game.First, we need to initialize our environment and set it up. Then we can call reset to get our initial observation.Let’s install the CAMEL package with all its dependencies:
Copy
%pip install camel-ai[all]==0.2.46
Copy
import asynciofrom camel.environments.models import Actionfrom camel.environments.tic_tac_toe import TicTacToeEnv, Opponent# we can choose the playstyle of our opponent to be either 'random' or 'optimal' (computed using minimax)opp = Opponent(play_style="random")env = TicTacToeEnv(opponent=opp)await env.setup()obs = await env.reset()print("Initial Observation:\n")print(obs.question)
We will use GPT-4o-mini, so let’s enter our API key.
Copy
import osfrom getpass import getpassopenai_api_key = getpass('Enter your API key: ')os.environ["OPENAI_API_KEY"] = openai_api_key
Alternatively, if running on Colab, you could save your API keys and tokens as Colab Secrets, and use them across notebooks.To do so, comment out the above manual API key prompt code block(s), and uncomment the following codeblock.⚠️ Don’t forget granting access to the API key you would be using to the current notebook.
Copy
# import os# from google.colab import userdata# os.environ["OPENAI_API_KEY"] = userdata.get("OPENAI_API_KEY")
Let’s next define the model-backend and the agent.You can also add a system prompt or equip your agent with tools, but for the sake of simplicity we just create a bare agent with GPT-4o-mini.