From 51781b333f0fe0e65a8e0e6fe0a9a36a0dda4b31 Mon Sep 17 00:00:00 2001 From: Sam Pink Date: Sat, 6 Apr 2024 16:39:07 +0100 Subject: [PATCH 1/7] added anthropic thru llama --- .vscode/PythonImportHelper-v2-Completion.json | 666 ++++++++++++++++++ agent/__init__.py | 1 + agent/llm.py | 14 + agent/robot.py | 25 +- requirements.txt | 4 + script.py | 6 +- 6 files changed, 699 insertions(+), 17 deletions(-) create mode 100644 .vscode/PythonImportHelper-v2-Completion.json create mode 100644 agent/llm.py diff --git a/.vscode/PythonImportHelper-v2-Completion.json b/.vscode/PythonImportHelper-v2-Completion.json new file mode 100644 index 0000000..bfa5f83 --- /dev/null +++ b/.vscode/PythonImportHelper-v2-Completion.json @@ -0,0 +1,666 @@ +[ + { + "label": "dotenv", + "kind": 6, + "isExtraImport": true, + "importPath": "dotenv", + "description": "dotenv", + "detail": "dotenv", + "documentation": {} + }, + { + "label": "load_dotenv", + "importPath": "dotenv", + "description": "dotenv", + "isExtraImport": true, + "detail": "dotenv", + "documentation": {} + }, + { + "label": "load_dotenv", + "importPath": "dotenv", + "description": "dotenv", + "isExtraImport": true, + "detail": "dotenv", + "documentation": {} + }, + { + "label": "load_dotenv", + "importPath": "dotenv", + "description": "dotenv", + "isExtraImport": true, + "detail": "dotenv", + "documentation": {} + }, + { + "label": "numpy", + "kind": 6, + "isExtraImport": true, + "importPath": "numpy", + "description": "numpy", + "detail": "numpy", + "documentation": {} + }, + { + "label": "os", + "kind": 6, + "isExtraImport": true, + "importPath": "os", + "description": "os", + "detail": "os", + "documentation": {} + }, + { + "label": "random", + "kind": 6, + "isExtraImport": true, + "importPath": "random", + "description": "random", + "detail": "random", + "documentation": {} + }, + { + "label": "re", + "kind": 6, + "isExtraImport": true, + "importPath": "re", + "description": "re", + "detail": "re", + "documentation": {} + }, + { + "label": "time", + "kind": 6, + "isExtraImport": true, + "importPath": "time", + "description": "time", + "detail": "time", + "documentation": {} + }, + { + "label": "defaultdict", + "importPath": "collections", + "description": "collections", + "isExtraImport": true, + "detail": "collections", + "documentation": {} + }, + { + "label": "Dict", + "importPath": "typing", + "description": "typing", + "isExtraImport": true, + "detail": "typing", + "documentation": {} + }, + { + "label": "List", + "importPath": "typing", + "description": "typing", + "isExtraImport": true, + "detail": "typing", + "documentation": {} + }, + { + "label": "Literal", + "importPath": "typing", + "description": "typing", + "isExtraImport": true, + "detail": "typing", + "documentation": {} + }, + { + "label": "Optional", + "importPath": "typing", + "description": "typing", + "isExtraImport": true, + "detail": "typing", + "documentation": {} + }, + { + "label": "List", + "importPath": "typing", + "description": "typing", + "isExtraImport": true, + "detail": "typing", + "documentation": {} + }, + { + "label": "Optional", + "importPath": "typing", + "description": "typing", + "isExtraImport": true, + "detail": "typing", + "documentation": {} + }, + { + "label": "spaces", + "importPath": "gymnasium", + "description": "gymnasium", + "isExtraImport": true, + "detail": "gymnasium", + "documentation": {} + }, + { + "label": "logger", + "importPath": "loguru", + "description": "loguru", + "isExtraImport": true, + "detail": "loguru", + "documentation": {} + }, + { + "label": "logger", + "importPath": "loguru", + "description": "loguru", + "isExtraImport": true, + "detail": "loguru", + "documentation": {} + }, + { + "label": "logger", + "importPath": "loguru", + "description": "loguru", + "isExtraImport": true, + "detail": "loguru", + "documentation": {} + }, + { + "label": "logger", + "importPath": "loguru", + "description": "loguru", + "isExtraImport": true, + "detail": "loguru", + "documentation": {} + }, + { + "label": "ChatMessage", + "importPath": "llama_index.core.llms", + "description": "llama_index.core.llms", + "isExtraImport": true, + "detail": "llama_index.core.llms", + "documentation": {} + }, + { + "label": "print", + "importPath": "rich", + "description": "rich", + "isExtraImport": true, + "detail": "rich", + "documentation": {} + }, + { + "label": "print", + "importPath": "rich", + "description": "rich", + "isExtraImport": true, + "detail": "rich", + "documentation": {} + }, + { + "label": "datetime", + "kind": 6, + "isExtraImport": true, + "importPath": "datetime", + "description": "datetime", + "detail": "datetime", + "documentation": {} + }, + { + "label": "traceback", + "kind": 6, + "isExtraImport": true, + "importPath": "traceback", + "description": "traceback", + "detail": "traceback", + "documentation": {} + }, + { + "label": "Thread", + "importPath": "threading", + "description": "threading", + "isExtraImport": true, + "detail": "threading", + "documentation": {} + }, + { + "label": "KEN_GREEN", + "importPath": "agent", + "description": "agent", + "isExtraImport": true, + "detail": "agent", + "documentation": {} + }, + { + "label": "KEN_RED", + "importPath": "agent", + "description": "agent", + "isExtraImport": true, + "detail": "agent", + "documentation": {} + }, + { + "label": "Robot", + "importPath": "agent", + "description": "agent", + "isExtraImport": true, + "detail": "agent", + "documentation": {} + }, + { + "label": "MODELS", + "importPath": "agent.config", + "description": "agent.config", + "isExtraImport": true, + "detail": "agent.config", + "documentation": {} + }, + { + "label": "EnvironmentSettingsMultiAgent", + "importPath": "diambra.arena", + "description": "diambra.arena", + "isExtraImport": true, + "detail": "diambra.arena", + "documentation": {} + }, + { + "label": "RecordingSettings", + "importPath": "diambra.arena", + "description": "diambra.arena", + "isExtraImport": true, + "detail": "diambra.arena", + "documentation": {} + }, + { + "label": "SpaceTypes", + "importPath": "diambra.arena", + "description": "diambra.arena", + "isExtraImport": true, + "detail": "diambra.arena", + "documentation": {} + }, + { + "label": "make", + "importPath": "diambra.arena", + "description": "diambra.arena", + "isExtraImport": true, + "detail": "diambra.arena", + "documentation": {} + }, + { + "label": "sys", + "kind": 6, + "isExtraImport": true, + "importPath": "sys", + "description": "sys", + "detail": "sys", + "documentation": {} + }, + { + "label": "Game", + "importPath": "eval.game", + "description": "eval.game", + "isExtraImport": true, + "detail": "eval.game", + "documentation": {} + }, + { + "label": "Player1", + "importPath": "eval.game", + "description": "eval.game", + "isExtraImport": true, + "detail": "eval.game", + "documentation": {} + }, + { + "label": "Player2", + "importPath": "eval.game", + "description": "eval.game", + "isExtraImport": true, + "detail": "eval.game", + "documentation": {} + }, + { + "label": "Game", + "importPath": "eval.game", + "description": "eval.game", + "isExtraImport": true, + "detail": "eval.game", + "documentation": {} + }, + { + "label": "Player1", + "importPath": "eval.game", + "description": "eval.game", + "isExtraImport": true, + "detail": "eval.game", + "documentation": {} + }, + { + "label": "Player2", + "importPath": "eval.game", + "description": "eval.game", + "isExtraImport": true, + "detail": "eval.game", + "documentation": {} + }, + { + "label": "Game", + "importPath": "eval.game", + "description": "eval.game", + "isExtraImport": true, + "detail": "eval.game", + "documentation": {} + }, + { + "label": "Player1", + "importPath": "eval.game", + "description": "eval.game", + "isExtraImport": true, + "detail": "eval.game", + "documentation": {} + }, + { + "label": "Player2", + "importPath": "eval.game", + "description": "eval.game", + "isExtraImport": true, + "detail": "eval.game", + "documentation": {} + }, + { + "label": "tkinter", + "kind": 6, + "isExtraImport": true, + "importPath": "tkinter", + "description": "tkinter", + "detail": "tkinter", + "documentation": {} + }, + { + "label": "Image", + "importPath": "PIL", + "description": "PIL", + "isExtraImport": true, + "detail": "PIL", + "documentation": {} + }, + { + "label": "ImageTk", + "importPath": "PIL", + "description": "PIL", + "isExtraImport": true, + "detail": "PIL", + "documentation": {} + }, + { + "label": "MODELS", + "kind": 5, + "importPath": "agent.config", + "description": "agent.config", + "peekOfCode": "MODELS = {\n \"OPENAI\": {\n \"openai:gpt-4-0125-preview\",\n \"openai:gpt-4\",\n \"openai:gpt-3.5-turbo-0125\",\n # \"openai:gpt-3.5-turbo-instruct\", # not a chat model\n },\n \"MISTRAL\": {\n \"mistral:mistral-small-latest\",\n \"mistral:mistral-medium-latest\",", + "detail": "agent.config", + "documentation": {} + }, + { + "label": "MOVES", + "kind": 5, + "importPath": "agent.config", + "description": "agent.config", + "peekOfCode": "MOVES = {\n \"No-Move\": 0,\n \"Left\": 1,\n \"Left+Up\": 2,\n \"Up+Left\": 2,\n \"Up\": 3,\n \"Up+Right\": 4,\n \"Right+Up\": 4,\n \"Right\": 5,\n \"Right+Down\": 6,", + "detail": "agent.config", + "documentation": {} + }, + { + "label": "MOVES_WITH_LOWER", + "kind": 5, + "importPath": "agent.config", + "description": "agent.config", + "peekOfCode": "MOVES_WITH_LOWER = {\n **MOVES,\n **{key.lower(): value for key, value in MOVES.items()},\n}\nCOMBOS = {\n \"Fireball (Hadouken)\": {\"right\": [7, 6, 5, 10], \"left\": [7, 8, 1, 10]},\n # Refacto with command names\n \"Dragon Punch (Shoryuken)\": {\n \"right\": [\n MOVES[\"Right\"],", + "detail": "agent.config", + "documentation": {} + }, + { + "label": "COMBOS", + "kind": 5, + "importPath": "agent.config", + "description": "agent.config", + "peekOfCode": "COMBOS = {\n \"Fireball (Hadouken)\": {\"right\": [7, 6, 5, 10], \"left\": [7, 8, 1, 10]},\n # Refacto with command names\n \"Dragon Punch (Shoryuken)\": {\n \"right\": [\n MOVES[\"Right\"],\n MOVES[\"Down\"],\n MOVES[\"Down+Right\"],\n MOVES[\"High Punch\"],\n ],", + "detail": "agent.config", + "documentation": {} + }, + { + "label": "SPECIAL_MOVES", + "kind": 5, + "importPath": "agent.config", + "description": "agent.config", + "peekOfCode": "SPECIAL_MOVES = {\n \"EX-Fireball (Hadouken)\": {\"right\": [7, 6, 5, 10, 10], \"left\": [7, 8, 1, 10, 10]},\n \"EX-Dragon Punch (Shoryuken)\": {\n \"right\": [\n MOVES[\"Right\"],\n MOVES[\"Down\"],\n MOVES[\"Down+Right\"],\n MOVES[\"High Punch\"],\n MOVES[\"High Punch\"],\n ],", + "detail": "agent.config", + "documentation": {} + }, + { + "label": "META_INSTRUCTIONS", + "kind": 5, + "importPath": "agent.config", + "description": "agent.config", + "peekOfCode": "META_INSTRUCTIONS = {\n \"Move Closer\": {\"right\": [5, 5, 5, 5], \"left\": [1, 1, 1, 1]},\n \"Move Away\": {\"right\": [1, 1, 1, 1], \"left\": [5, 5, 5, 5]},\n \"Fireball\": COMBOS[\"Fireball (Hadouken)\"],\n \"Megapunch\": COMBOS[\"Dragon Punch (Shoryuken)\"],\n \"Hurricane\": COMBOS[\"Hurricane Kick (Tatsumaki Senpukyaku)\"],\n \"Megafireball\": SPECIAL_MOVES[\"EX-Fireball (Hadouken)\"],\n \"Super attack 2\": SPECIAL_MOVES[\"EX-Dragon Punch (Shoryuken)\"],\n \"Super attack 3\": SPECIAL_MOVES[\"Super Dragon Punch (Shouryuu-Reppa)\"],\n \"Super attack 4\": SPECIAL_MOVES[\"Shippuu-Jinrai-Kyaku\"],", + "detail": "agent.config", + "documentation": {} + }, + { + "label": "META_INSTRUCTIONS_WITH_LOWER", + "kind": 5, + "importPath": "agent.config", + "description": "agent.config", + "peekOfCode": "META_INSTRUCTIONS_WITH_LOWER = {\n **META_INSTRUCTIONS,\n **{key.lower(): value for key, value in META_INSTRUCTIONS.items()},\n ## Also add the combos for Lower, Medium and High\n \"lower\": {\"right\": [12, 0], \"left\": [12, 0]},\n \"medium\": {\"right\": [13, 0], \"left\": [13, 0]},\n \"med\": {\"right\": [13, 0], \"left\": [13, 0]},\n \"high\": {\"right\": [14, 0], \"left\": [14, 0]},\n}\nINDEX_TO_MOVE = {v: k for k, v in MOVES.items()}", + "detail": "agent.config", + "documentation": {} + }, + { + "label": "INDEX_TO_MOVE", + "kind": 5, + "importPath": "agent.config", + "description": "agent.config", + "peekOfCode": "INDEX_TO_MOVE = {v: k for k, v in MOVES.items()}\nX_SIZE = 384\nY_SIZE = 224\n# TODO : Adds some combos to this\nREAL_MOVE_LIST = [\n \"No-Move\",\n \"Left\",\n \"Left+Up\",\n \"Up\",\n \"Right\",", + "detail": "agent.config", + "documentation": {} + }, + { + "label": "X_SIZE", + "kind": 5, + "importPath": "agent.config", + "description": "agent.config", + "peekOfCode": "X_SIZE = 384\nY_SIZE = 224\n# TODO : Adds some combos to this\nREAL_MOVE_LIST = [\n \"No-Move\",\n \"Left\",\n \"Left+Up\",\n \"Up\",\n \"Right\",\n \"Right+Down\",", + "detail": "agent.config", + "documentation": {} + }, + { + "label": "Y_SIZE", + "kind": 5, + "importPath": "agent.config", + "description": "agent.config", + "peekOfCode": "Y_SIZE = 224\n# TODO : Adds some combos to this\nREAL_MOVE_LIST = [\n \"No-Move\",\n \"Left\",\n \"Left+Up\",\n \"Up\",\n \"Right\",\n \"Right+Down\",\n \"Down\",", + "detail": "agent.config", + "documentation": {} + }, + { + "label": "REAL_MOVE_LIST", + "kind": 5, + "importPath": "agent.config", + "description": "agent.config", + "peekOfCode": "REAL_MOVE_LIST = [\n \"No-Move\",\n \"Left\",\n \"Left+Up\",\n \"Up\",\n \"Right\",\n \"Right+Down\",\n \"Down\",\n \"Down+Left\",\n \"Low Punch\",", + "detail": "agent.config", + "documentation": {} + }, + { + "label": "NB_FRAME_WAIT", + "kind": 5, + "importPath": "agent.config", + "description": "agent.config", + "peekOfCode": "NB_FRAME_WAIT = 1", + "detail": "agent.config", + "documentation": {} + }, + { + "label": "get_client", + "kind": 2, + "importPath": "agent.llm", + "description": "agent.llm", + "peekOfCode": "def get_client(model_str):\n provider, model = model_str.split(\":\")\n if provider == \"openai\":\n from llama_index.llms.openai import OpenAI\n return OpenAI(model=model)\n elif provider == \"anthropic\":\n from llama_index.llms.anthropic import Anthropic\n return Anthropic(model=model)\n elif provider == \"mixtral\" or provider == \"groq\":\n from llama_index.llms.groq import Groq", + "detail": "agent.llm", + "documentation": {} + }, + { + "label": "detect_position_from_color", + "kind": 2, + "importPath": "agent.observer", + "description": "agent.observer", + "peekOfCode": "def detect_position_from_color(\n observation: dict, color: list, epsilon=1, save_frame: bool = False\n) -> tuple:\n \"\"\"\n Convert the observation from pixels to player coordinates.\n It works by finding the first pixel that matches the color.\n Returns a tuple of (x, y) coordinates.\n - x is between 0 and 384\n - y is between 0 and 224\n \"\"\"", + "detail": "agent.observer", + "documentation": {} + }, + { + "label": "KEN_RED", + "kind": 5, + "importPath": "agent.observer", + "description": "agent.observer", + "peekOfCode": "KEN_RED = [248, 0, 0]\nKEN_GREEN = [88, 176, 40]\ndef detect_position_from_color(\n observation: dict, color: list, epsilon=1, save_frame: bool = False\n) -> tuple:\n \"\"\"\n Convert the observation from pixels to player coordinates.\n It works by finding the first pixel that matches the color.\n Returns a tuple of (x, y) coordinates.\n - x is between 0 and 384", + "detail": "agent.observer", + "documentation": {} + }, + { + "label": "KEN_GREEN", + "kind": 5, + "importPath": "agent.observer", + "description": "agent.observer", + "peekOfCode": "KEN_GREEN = [88, 176, 40]\ndef detect_position_from_color(\n observation: dict, color: list, epsilon=1, save_frame: bool = False\n) -> tuple:\n \"\"\"\n Convert the observation from pixels to player coordinates.\n It works by finding the first pixel that matches the color.\n Returns a tuple of (x, y) coordinates.\n - x is between 0 and 384\n - y is between 0 and 224", + "detail": "agent.observer", + "documentation": {} + }, + { + "label": "Robot", + "kind": 6, + "importPath": "agent.robot", + "description": "agent.robot", + "peekOfCode": "class Robot:\n observations: List[Optional[dict]] = None # memory\n next_steps: List[int] # action plan\n actions: dict # actions of the agents during a step of the game\n # actions of the agents during the previous step of the game\n previous_actions: Dict[str, List[int]]\n reward: float # reward of the agent\n action_space: spaces.Space\n character: Optional[str] = None # character name\n side: int # side of the stage where playing: 0 = left, 1 = right", + "detail": "agent.robot", + "documentation": {} + }, + { + "label": "Player", + "kind": 6, + "importPath": "eval.game", + "description": "eval.game", + "peekOfCode": "class Player:\n nickname: str\n model: str\n robot: Optional[Robot] = None\n temperature: Optional[float] = 0.0\n def verify_provider_name(self):\n if self.model.startswith(\"openai\"):\n assert (\n os.environ.get(\"OPENAI_API_KEY\") is not None\n ), \"OpenAI API key not set\"", + "detail": "eval.game", + "documentation": {} + }, + { + "label": "Player1", + "kind": 6, + "importPath": "eval.game", + "description": "eval.game", + "peekOfCode": "class Player1(Player):\n def __init__(\n self,\n nickname: str,\n model: str,\n ):\n self.nickname = nickname\n self.model = model\n self.robot = Robot(\n action_space=None,", + "detail": "eval.game", + "documentation": {} + }, + { + "label": "Player2", + "kind": 6, + "importPath": "eval.game", + "description": "eval.game", + "peekOfCode": "class Player2(Player):\n def __init__(\n self,\n nickname: str,\n model: str,\n ):\n self.nickname = nickname\n self.model = model\n self.robot = Robot(\n action_space=None,", + "detail": "eval.game", + "documentation": {} + }, + { + "label": "Episode", + "kind": 6, + "importPath": "eval.game", + "description": "eval.game", + "peekOfCode": "class Episode:\n player_1: Optional[Player1]\n player_2: Player2\n player_1_won: Optional[bool] = None\n def __init__(self, player_1: Optional[Player1], player_2: Player2):\n self.player_1 = player_1\n self.player_2 = player_2\n def save(self):\n if self.player_1 is None:\n # Playing with the controller", + "detail": "eval.game", + "documentation": {} + }, + { + "label": "Game", + "kind": 6, + "importPath": "eval.game", + "description": "eval.game", + "peekOfCode": "class Game:\n player_1: Optional[Player1] = None # First player. None if Human\n player_2: Player2\n render: Optional[bool] = False\n splash_screen: Optional[bool] = False\n save_game: Optional[bool] = False\n characters: Optional[List[str]] = [\"Ken\", \"Ken\"]\n outfits: Optional[List[int]] = [1, 3]\n frame_shape: Optional[List[int]] = [0, 0, 0]\n seed: Optional[int] = 42", + "detail": "eval.game", + "documentation": {} + }, + { + "label": "PlanAndAct", + "kind": 6, + "importPath": "eval.game", + "description": "eval.game", + "peekOfCode": "class PlanAndAct(Thread):\n def __init__(self, game: Game, episode: Episode):\n self.running = True\n self.game = game\n self.episode = episode\n Thread.__init__(self, daemon=True)\n # atexit.register(self.stop)\nclass PlanAndActPlayer1(PlanAndAct):\n def run(self) -> None:\n while self.running:", + "detail": "eval.game", + "documentation": {} + }, + { + "label": "PlanAndActPlayer1", + "kind": 6, + "importPath": "eval.game", + "description": "eval.game", + "peekOfCode": "class PlanAndActPlayer1(PlanAndAct):\n def run(self) -> None:\n while self.running:\n if \"agent_0\" not in self.game.actions:\n # Plan\n self.game.player_1.robot.plan()\n # Act\n self.game.actions[\"agent_0\"] = self.game.player_1.robot.act()\n # Observe the environment\n self.game.player_1.robot.observe(", + "detail": "eval.game", + "documentation": {} + }, + { + "label": "PlanAndActPlayer2", + "kind": 6, + "importPath": "eval.game", + "description": "eval.game", + "peekOfCode": "class PlanAndActPlayer2(PlanAndAct):\n def run(self) -> None:\n while self.running:\n if \"agent_1\" not in self.game.actions:\n # Plan\n self.game.player_2.robot.plan()\n # Act\n self.game.actions[\"agent_1\"] = self.game.player_2.robot.act()\n # Observe the environment\n self.game.player_2.robot.observe(", + "detail": "eval.game", + "documentation": {} + }, + { + "label": "generate_random_model", + "kind": 2, + "importPath": "eval.game", + "description": "eval.game", + "peekOfCode": "def generate_random_model(openai: bool = False, mistral: bool = True):\n models_available = []\n for model, models in MODELS.items():\n if openai and model == \"OPENAI\":\n models_available.extend(models)\n if mistral and model == \"MISTRAL\":\n models_available.extend(models)\n random.seed()\n # Generate a pair of random two models\n random_model = random.choice(models_available)", + "detail": "eval.game", + "documentation": {} + }, + { + "label": "main", + "kind": 2, + "importPath": "demo", + "description": "demo", + "peekOfCode": "def main():\n # Environment Settings\n game = Game(\n render=True,\n save_game=True,\n player_1=Player1(\n nickname=\"Baby\",\n model=\"mistral:mistral-small-latest\",\n ),\n player_2=Player2(", + "detail": "demo", + "documentation": {} + }, + { + "label": "main", + "kind": 2, + "importPath": "ollama", + "description": "ollama", + "peekOfCode": "def main():\n # Environment Settings\n game = Game(\n render=True,\n save_game=True,\n player_1=Player1(\n nickname=\"Baby\",\n model=\"ollama:qwen:14b-chat-v1.5-fp16\",\n # model=\"ollama:mistral\",\n ),", + "detail": "ollama", + "documentation": {} + }, + { + "label": "display_win_screen", + "kind": 2, + "importPath": "result", + "description": "result", + "peekOfCode": "def display_win_screen():\n current_directory = os.path.dirname(os.path.realpath(__file__))\n image_path = os.path.join(current_directory, \"winscreen.jpg\")\n # Create the main application window\n root = tk.Tk()\n root.title(\"Image Display\")\n # Load the image\n image = Image.open(image_path)\n # Convert the Image object into a Tkinter-compatible photo image\n tk_image = ImageTk.PhotoImage(image)", + "detail": "result", + "documentation": {} + }, + { + "label": "main", + "kind": 2, + "importPath": "script", + "description": "script", + "peekOfCode": "def main():\n # Environment Settings\n game = Game(\n render=True,\n player_1=Player1(\n nickname=\"Daddy\",\n model=\"groq:mistral-8x6b-32768\",\n ),\n player_2=Player2(\n nickname=\"Baby\",", + "detail": "script", + "documentation": {} + } +] \ No newline at end of file diff --git a/agent/__init__.py b/agent/__init__.py index 33d8954..e11832f 100644 --- a/agent/__init__.py +++ b/agent/__init__.py @@ -5,3 +5,4 @@ from .robot import Robot from .observer import KEN_GREEN, KEN_RED +from .llm import get_client diff --git a/agent/llm.py b/agent/llm.py new file mode 100644 index 0000000..f6a822a --- /dev/null +++ b/agent/llm.py @@ -0,0 +1,14 @@ +def get_client(model_str): + provider, model = model_str.split(":") + if provider == "openai": + from llama_index.llms.openai import OpenAI + + return OpenAI(model=model) + elif provider == "anthropic": + from llama_index.llms.anthropic import Anthropic + + return Anthropic(model=model) + elif provider == "mixtral" or provider == "groq": + from llama_index.llms.groq import Groq + + return Groq(model=model) diff --git a/agent/robot.py b/agent/robot.py index 80a04a9..278e34b 100644 --- a/agent/robot.py +++ b/agent/robot.py @@ -8,7 +8,7 @@ import numpy as np from gymnasium import spaces from loguru import logger -from phospho.lab import get_provider_and_model, get_sync_client +from llama_index.core.llms import ChatMessage from rich import print from .config import ( @@ -21,6 +21,7 @@ Y_SIZE, ) from .observer import detect_position_from_color +from .llm import get_client class Robot: @@ -330,8 +331,6 @@ def call_llm( Edit this method to change the behavior of the robot! """ - provider_name, model_name = get_provider_and_model(self.model) - client = get_sync_client(provider_name) # Generate the prompts move_list = "- " + "\n - ".join([move for move in META_INSTRUCTIONS]) @@ -351,17 +350,15 @@ def call_llm( - Move closer""" start_time = time.time() - completion = client.chat.completions.create( - model=model_name, - messages=[ - {"role": "system", "content": system_prompt}, - {"role": "user", "content": "Your next moves are:"}, - ], - temperature=temperature, - max_tokens=max_tokens, - top_p=top_p, - ) + + client = get_client(self.model) + + messages = [ + ChatMessage(role="system", content=system_prompt), + ChatMessage(role="user", content="Your next moves are:"), + ] + llm_response = client.chat(messages).message.content + logger.debug(f"LLM call to {self.model}: {system_prompt}") logger.debug(f"LLM call to {self.model}: {time.time() - start_time}s") - llm_response = completion.choices[0].message.content.strip() return llm_response diff --git a/requirements.txt b/requirements.txt index cf401ff..b591484 100644 --- a/requirements.txt +++ b/requirements.txt @@ -47,3 +47,7 @@ tqdm==4.66.2; python_version >= '3.7' typing-extensions==4.10.0; python_version >= '3.8' tzdata==2024.1; python_version >= '2' rich==13.7.1 +llama-index +llama-index-llms-openai +llama-index-llms-anthropic +llama-index-llms-groq \ No newline at end of file diff --git a/script.py b/script.py index 0525a3a..3c804ce 100644 --- a/script.py +++ b/script.py @@ -1,7 +1,7 @@ import sys from dotenv import load_dotenv -from eval.game import Game, Player1, Player2, generate_random_model +from eval.game import Game, Player1, Player2 from loguru import logger logger.remove() @@ -16,11 +16,11 @@ def main(): render=True, player_1=Player1( nickname="Daddy", - model=generate_random_model(mistral=True), + model="groq:mistral-8x6b-32768", ), player_2=Player2( nickname="Baby", - model=generate_random_model(openai=True), + model="anthropic:claude-3-haiku-20240307", ), ) return game.run() From 1ca3d64181993233ebf1ebe357466d0b73f37acf Mon Sep 17 00:00:00 2001 From: Sam Pink Date: Sat, 6 Apr 2024 16:51:55 +0100 Subject: [PATCH 2/7] google vs anthropic --- script.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script.py b/script.py index 3c804ce..27fa62e 100644 --- a/script.py +++ b/script.py @@ -16,7 +16,7 @@ def main(): render=True, player_1=Player1( nickname="Daddy", - model="groq:mistral-8x6b-32768", + model="groq:gemma-7b-it", ), player_2=Player2( nickname="Baby", From 30202658378869f424c799a0cd504e74032817b3 Mon Sep 17 00:00:00 2001 From: Sam Pink Date: Sun, 7 Apr 2024 08:28:13 +0100 Subject: [PATCH 3/7] updated config --- agent/config.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/agent/config.py b/agent/config.py index 25c26ca..64e1ffa 100644 --- a/agent/config.py +++ b/agent/config.py @@ -15,6 +15,8 @@ "mistral:mistral-large-latest", # "groq:mistral-8x6b-32768", }, + "GROQ": {"groq:gemma-7b-it"}, + "ANTHROPIC": {"anthropic:claude-3-haiku-20240307"}, } From bdde44c8202b859504aa331206819d9325d645ec Mon Sep 17 00:00:00 2001 From: Sam Pink <42603236+SamPink@users.noreply.github.com> Date: Mon, 15 Apr 2024 09:17:11 +0100 Subject: [PATCH 4/7] Delete .vscode/PythonImportHelper-v2-Completion.json --- .vscode/PythonImportHelper-v2-Completion.json | 666 ------------------ 1 file changed, 666 deletions(-) delete mode 100644 .vscode/PythonImportHelper-v2-Completion.json diff --git a/.vscode/PythonImportHelper-v2-Completion.json b/.vscode/PythonImportHelper-v2-Completion.json deleted file mode 100644 index bfa5f83..0000000 --- a/.vscode/PythonImportHelper-v2-Completion.json +++ /dev/null @@ -1,666 +0,0 @@ -[ - { - "label": "dotenv", - "kind": 6, - "isExtraImport": true, - "importPath": "dotenv", - "description": "dotenv", - "detail": "dotenv", - "documentation": {} - }, - { - "label": "load_dotenv", - "importPath": "dotenv", - "description": "dotenv", - "isExtraImport": true, - "detail": "dotenv", - "documentation": {} - }, - { - "label": "load_dotenv", - "importPath": "dotenv", - "description": "dotenv", - "isExtraImport": true, - "detail": "dotenv", - "documentation": {} - }, - { - "label": "load_dotenv", - "importPath": "dotenv", - "description": "dotenv", - "isExtraImport": true, - "detail": "dotenv", - "documentation": {} - }, - { - "label": "numpy", - "kind": 6, - "isExtraImport": true, - "importPath": "numpy", - "description": "numpy", - "detail": "numpy", - "documentation": {} - }, - { - "label": "os", - "kind": 6, - "isExtraImport": true, - "importPath": "os", - "description": "os", - "detail": "os", - "documentation": {} - }, - { - "label": "random", - "kind": 6, - "isExtraImport": true, - "importPath": "random", - "description": "random", - "detail": "random", - "documentation": {} - }, - { - "label": "re", - "kind": 6, - "isExtraImport": true, - "importPath": "re", - "description": "re", - "detail": "re", - "documentation": {} - }, - { - "label": "time", - "kind": 6, - "isExtraImport": true, - "importPath": "time", - "description": "time", - "detail": "time", - "documentation": {} - }, - { - "label": "defaultdict", - "importPath": "collections", - "description": "collections", - "isExtraImport": true, - "detail": "collections", - "documentation": {} - }, - { - "label": "Dict", - "importPath": "typing", - "description": "typing", - "isExtraImport": true, - "detail": "typing", - "documentation": {} - }, - { - "label": "List", - "importPath": "typing", - "description": "typing", - "isExtraImport": true, - "detail": "typing", - "documentation": {} - }, - { - "label": "Literal", - "importPath": "typing", - "description": "typing", - "isExtraImport": true, - "detail": "typing", - "documentation": {} - }, - { - "label": "Optional", - "importPath": "typing", - "description": "typing", - "isExtraImport": true, - "detail": "typing", - "documentation": {} - }, - { - "label": "List", - "importPath": "typing", - "description": "typing", - "isExtraImport": true, - "detail": "typing", - "documentation": {} - }, - { - "label": "Optional", - "importPath": "typing", - "description": "typing", - "isExtraImport": true, - "detail": "typing", - "documentation": {} - }, - { - "label": "spaces", - "importPath": "gymnasium", - "description": "gymnasium", - "isExtraImport": true, - "detail": "gymnasium", - "documentation": {} - }, - { - "label": "logger", - "importPath": "loguru", - "description": "loguru", - "isExtraImport": true, - "detail": "loguru", - "documentation": {} - }, - { - "label": "logger", - "importPath": "loguru", - "description": "loguru", - "isExtraImport": true, - "detail": "loguru", - "documentation": {} - }, - { - "label": "logger", - "importPath": "loguru", - "description": "loguru", - "isExtraImport": true, - "detail": "loguru", - "documentation": {} - }, - { - "label": "logger", - "importPath": "loguru", - "description": "loguru", - "isExtraImport": true, - "detail": "loguru", - "documentation": {} - }, - { - "label": "ChatMessage", - "importPath": "llama_index.core.llms", - "description": "llama_index.core.llms", - "isExtraImport": true, - "detail": "llama_index.core.llms", - "documentation": {} - }, - { - "label": "print", - "importPath": "rich", - "description": "rich", - "isExtraImport": true, - "detail": "rich", - "documentation": {} - }, - { - "label": "print", - "importPath": "rich", - "description": "rich", - "isExtraImport": true, - "detail": "rich", - "documentation": {} - }, - { - "label": "datetime", - "kind": 6, - "isExtraImport": true, - "importPath": "datetime", - "description": "datetime", - "detail": "datetime", - "documentation": {} - }, - { - "label": "traceback", - "kind": 6, - "isExtraImport": true, - "importPath": "traceback", - "description": "traceback", - "detail": "traceback", - "documentation": {} - }, - { - "label": "Thread", - "importPath": "threading", - "description": "threading", - "isExtraImport": true, - "detail": "threading", - "documentation": {} - }, - { - "label": "KEN_GREEN", - "importPath": "agent", - "description": "agent", - "isExtraImport": true, - "detail": "agent", - "documentation": {} - }, - { - "label": "KEN_RED", - "importPath": "agent", - "description": "agent", - "isExtraImport": true, - "detail": "agent", - "documentation": {} - }, - { - "label": "Robot", - "importPath": "agent", - "description": "agent", - "isExtraImport": true, - "detail": "agent", - "documentation": {} - }, - { - "label": "MODELS", - "importPath": "agent.config", - "description": "agent.config", - "isExtraImport": true, - "detail": "agent.config", - "documentation": {} - }, - { - "label": "EnvironmentSettingsMultiAgent", - "importPath": "diambra.arena", - "description": "diambra.arena", - "isExtraImport": true, - "detail": "diambra.arena", - "documentation": {} - }, - { - "label": "RecordingSettings", - "importPath": "diambra.arena", - "description": "diambra.arena", - "isExtraImport": true, - "detail": "diambra.arena", - "documentation": {} - }, - { - "label": "SpaceTypes", - "importPath": "diambra.arena", - "description": "diambra.arena", - "isExtraImport": true, - "detail": "diambra.arena", - "documentation": {} - }, - { - "label": "make", - "importPath": "diambra.arena", - "description": "diambra.arena", - "isExtraImport": true, - "detail": "diambra.arena", - "documentation": {} - }, - { - "label": "sys", - "kind": 6, - "isExtraImport": true, - "importPath": "sys", - "description": "sys", - "detail": "sys", - "documentation": {} - }, - { - "label": "Game", - "importPath": "eval.game", - "description": "eval.game", - "isExtraImport": true, - "detail": "eval.game", - "documentation": {} - }, - { - "label": "Player1", - "importPath": "eval.game", - "description": "eval.game", - "isExtraImport": true, - "detail": "eval.game", - "documentation": {} - }, - { - "label": "Player2", - "importPath": "eval.game", - "description": "eval.game", - "isExtraImport": true, - "detail": "eval.game", - "documentation": {} - }, - { - "label": "Game", - "importPath": "eval.game", - "description": "eval.game", - "isExtraImport": true, - "detail": "eval.game", - "documentation": {} - }, - { - "label": "Player1", - "importPath": "eval.game", - "description": "eval.game", - "isExtraImport": true, - "detail": "eval.game", - "documentation": {} - }, - { - "label": "Player2", - "importPath": "eval.game", - "description": "eval.game", - "isExtraImport": true, - "detail": "eval.game", - "documentation": {} - }, - { - "label": "Game", - "importPath": "eval.game", - "description": "eval.game", - "isExtraImport": true, - "detail": "eval.game", - "documentation": {} - }, - { - "label": "Player1", - "importPath": "eval.game", - "description": "eval.game", - "isExtraImport": true, - "detail": "eval.game", - "documentation": {} - }, - { - "label": "Player2", - "importPath": "eval.game", - "description": "eval.game", - "isExtraImport": true, - "detail": "eval.game", - "documentation": {} - }, - { - "label": "tkinter", - "kind": 6, - "isExtraImport": true, - "importPath": "tkinter", - "description": "tkinter", - "detail": "tkinter", - "documentation": {} - }, - { - "label": "Image", - "importPath": "PIL", - "description": "PIL", - "isExtraImport": true, - "detail": "PIL", - "documentation": {} - }, - { - "label": "ImageTk", - "importPath": "PIL", - "description": "PIL", - "isExtraImport": true, - "detail": "PIL", - "documentation": {} - }, - { - "label": "MODELS", - "kind": 5, - "importPath": "agent.config", - "description": "agent.config", - "peekOfCode": "MODELS = {\n \"OPENAI\": {\n \"openai:gpt-4-0125-preview\",\n \"openai:gpt-4\",\n \"openai:gpt-3.5-turbo-0125\",\n # \"openai:gpt-3.5-turbo-instruct\", # not a chat model\n },\n \"MISTRAL\": {\n \"mistral:mistral-small-latest\",\n \"mistral:mistral-medium-latest\",", - "detail": "agent.config", - "documentation": {} - }, - { - "label": "MOVES", - "kind": 5, - "importPath": "agent.config", - "description": "agent.config", - "peekOfCode": "MOVES = {\n \"No-Move\": 0,\n \"Left\": 1,\n \"Left+Up\": 2,\n \"Up+Left\": 2,\n \"Up\": 3,\n \"Up+Right\": 4,\n \"Right+Up\": 4,\n \"Right\": 5,\n \"Right+Down\": 6,", - "detail": "agent.config", - "documentation": {} - }, - { - "label": "MOVES_WITH_LOWER", - "kind": 5, - "importPath": "agent.config", - "description": "agent.config", - "peekOfCode": "MOVES_WITH_LOWER = {\n **MOVES,\n **{key.lower(): value for key, value in MOVES.items()},\n}\nCOMBOS = {\n \"Fireball (Hadouken)\": {\"right\": [7, 6, 5, 10], \"left\": [7, 8, 1, 10]},\n # Refacto with command names\n \"Dragon Punch (Shoryuken)\": {\n \"right\": [\n MOVES[\"Right\"],", - "detail": "agent.config", - "documentation": {} - }, - { - "label": "COMBOS", - "kind": 5, - "importPath": "agent.config", - "description": "agent.config", - "peekOfCode": "COMBOS = {\n \"Fireball (Hadouken)\": {\"right\": [7, 6, 5, 10], \"left\": [7, 8, 1, 10]},\n # Refacto with command names\n \"Dragon Punch (Shoryuken)\": {\n \"right\": [\n MOVES[\"Right\"],\n MOVES[\"Down\"],\n MOVES[\"Down+Right\"],\n MOVES[\"High Punch\"],\n ],", - "detail": "agent.config", - "documentation": {} - }, - { - "label": "SPECIAL_MOVES", - "kind": 5, - "importPath": "agent.config", - "description": "agent.config", - "peekOfCode": "SPECIAL_MOVES = {\n \"EX-Fireball (Hadouken)\": {\"right\": [7, 6, 5, 10, 10], \"left\": [7, 8, 1, 10, 10]},\n \"EX-Dragon Punch (Shoryuken)\": {\n \"right\": [\n MOVES[\"Right\"],\n MOVES[\"Down\"],\n MOVES[\"Down+Right\"],\n MOVES[\"High Punch\"],\n MOVES[\"High Punch\"],\n ],", - "detail": "agent.config", - "documentation": {} - }, - { - "label": "META_INSTRUCTIONS", - "kind": 5, - "importPath": "agent.config", - "description": "agent.config", - "peekOfCode": "META_INSTRUCTIONS = {\n \"Move Closer\": {\"right\": [5, 5, 5, 5], \"left\": [1, 1, 1, 1]},\n \"Move Away\": {\"right\": [1, 1, 1, 1], \"left\": [5, 5, 5, 5]},\n \"Fireball\": COMBOS[\"Fireball (Hadouken)\"],\n \"Megapunch\": COMBOS[\"Dragon Punch (Shoryuken)\"],\n \"Hurricane\": COMBOS[\"Hurricane Kick (Tatsumaki Senpukyaku)\"],\n \"Megafireball\": SPECIAL_MOVES[\"EX-Fireball (Hadouken)\"],\n \"Super attack 2\": SPECIAL_MOVES[\"EX-Dragon Punch (Shoryuken)\"],\n \"Super attack 3\": SPECIAL_MOVES[\"Super Dragon Punch (Shouryuu-Reppa)\"],\n \"Super attack 4\": SPECIAL_MOVES[\"Shippuu-Jinrai-Kyaku\"],", - "detail": "agent.config", - "documentation": {} - }, - { - "label": "META_INSTRUCTIONS_WITH_LOWER", - "kind": 5, - "importPath": "agent.config", - "description": "agent.config", - "peekOfCode": "META_INSTRUCTIONS_WITH_LOWER = {\n **META_INSTRUCTIONS,\n **{key.lower(): value for key, value in META_INSTRUCTIONS.items()},\n ## Also add the combos for Lower, Medium and High\n \"lower\": {\"right\": [12, 0], \"left\": [12, 0]},\n \"medium\": {\"right\": [13, 0], \"left\": [13, 0]},\n \"med\": {\"right\": [13, 0], \"left\": [13, 0]},\n \"high\": {\"right\": [14, 0], \"left\": [14, 0]},\n}\nINDEX_TO_MOVE = {v: k for k, v in MOVES.items()}", - "detail": "agent.config", - "documentation": {} - }, - { - "label": "INDEX_TO_MOVE", - "kind": 5, - "importPath": "agent.config", - "description": "agent.config", - "peekOfCode": "INDEX_TO_MOVE = {v: k for k, v in MOVES.items()}\nX_SIZE = 384\nY_SIZE = 224\n# TODO : Adds some combos to this\nREAL_MOVE_LIST = [\n \"No-Move\",\n \"Left\",\n \"Left+Up\",\n \"Up\",\n \"Right\",", - "detail": "agent.config", - "documentation": {} - }, - { - "label": "X_SIZE", - "kind": 5, - "importPath": "agent.config", - "description": "agent.config", - "peekOfCode": "X_SIZE = 384\nY_SIZE = 224\n# TODO : Adds some combos to this\nREAL_MOVE_LIST = [\n \"No-Move\",\n \"Left\",\n \"Left+Up\",\n \"Up\",\n \"Right\",\n \"Right+Down\",", - "detail": "agent.config", - "documentation": {} - }, - { - "label": "Y_SIZE", - "kind": 5, - "importPath": "agent.config", - "description": "agent.config", - "peekOfCode": "Y_SIZE = 224\n# TODO : Adds some combos to this\nREAL_MOVE_LIST = [\n \"No-Move\",\n \"Left\",\n \"Left+Up\",\n \"Up\",\n \"Right\",\n \"Right+Down\",\n \"Down\",", - "detail": "agent.config", - "documentation": {} - }, - { - "label": "REAL_MOVE_LIST", - "kind": 5, - "importPath": "agent.config", - "description": "agent.config", - "peekOfCode": "REAL_MOVE_LIST = [\n \"No-Move\",\n \"Left\",\n \"Left+Up\",\n \"Up\",\n \"Right\",\n \"Right+Down\",\n \"Down\",\n \"Down+Left\",\n \"Low Punch\",", - "detail": "agent.config", - "documentation": {} - }, - { - "label": "NB_FRAME_WAIT", - "kind": 5, - "importPath": "agent.config", - "description": "agent.config", - "peekOfCode": "NB_FRAME_WAIT = 1", - "detail": "agent.config", - "documentation": {} - }, - { - "label": "get_client", - "kind": 2, - "importPath": "agent.llm", - "description": "agent.llm", - "peekOfCode": "def get_client(model_str):\n provider, model = model_str.split(\":\")\n if provider == \"openai\":\n from llama_index.llms.openai import OpenAI\n return OpenAI(model=model)\n elif provider == \"anthropic\":\n from llama_index.llms.anthropic import Anthropic\n return Anthropic(model=model)\n elif provider == \"mixtral\" or provider == \"groq\":\n from llama_index.llms.groq import Groq", - "detail": "agent.llm", - "documentation": {} - }, - { - "label": "detect_position_from_color", - "kind": 2, - "importPath": "agent.observer", - "description": "agent.observer", - "peekOfCode": "def detect_position_from_color(\n observation: dict, color: list, epsilon=1, save_frame: bool = False\n) -> tuple:\n \"\"\"\n Convert the observation from pixels to player coordinates.\n It works by finding the first pixel that matches the color.\n Returns a tuple of (x, y) coordinates.\n - x is between 0 and 384\n - y is between 0 and 224\n \"\"\"", - "detail": "agent.observer", - "documentation": {} - }, - { - "label": "KEN_RED", - "kind": 5, - "importPath": "agent.observer", - "description": "agent.observer", - "peekOfCode": "KEN_RED = [248, 0, 0]\nKEN_GREEN = [88, 176, 40]\ndef detect_position_from_color(\n observation: dict, color: list, epsilon=1, save_frame: bool = False\n) -> tuple:\n \"\"\"\n Convert the observation from pixels to player coordinates.\n It works by finding the first pixel that matches the color.\n Returns a tuple of (x, y) coordinates.\n - x is between 0 and 384", - "detail": "agent.observer", - "documentation": {} - }, - { - "label": "KEN_GREEN", - "kind": 5, - "importPath": "agent.observer", - "description": "agent.observer", - "peekOfCode": "KEN_GREEN = [88, 176, 40]\ndef detect_position_from_color(\n observation: dict, color: list, epsilon=1, save_frame: bool = False\n) -> tuple:\n \"\"\"\n Convert the observation from pixels to player coordinates.\n It works by finding the first pixel that matches the color.\n Returns a tuple of (x, y) coordinates.\n - x is between 0 and 384\n - y is between 0 and 224", - "detail": "agent.observer", - "documentation": {} - }, - { - "label": "Robot", - "kind": 6, - "importPath": "agent.robot", - "description": "agent.robot", - "peekOfCode": "class Robot:\n observations: List[Optional[dict]] = None # memory\n next_steps: List[int] # action plan\n actions: dict # actions of the agents during a step of the game\n # actions of the agents during the previous step of the game\n previous_actions: Dict[str, List[int]]\n reward: float # reward of the agent\n action_space: spaces.Space\n character: Optional[str] = None # character name\n side: int # side of the stage where playing: 0 = left, 1 = right", - "detail": "agent.robot", - "documentation": {} - }, - { - "label": "Player", - "kind": 6, - "importPath": "eval.game", - "description": "eval.game", - "peekOfCode": "class Player:\n nickname: str\n model: str\n robot: Optional[Robot] = None\n temperature: Optional[float] = 0.0\n def verify_provider_name(self):\n if self.model.startswith(\"openai\"):\n assert (\n os.environ.get(\"OPENAI_API_KEY\") is not None\n ), \"OpenAI API key not set\"", - "detail": "eval.game", - "documentation": {} - }, - { - "label": "Player1", - "kind": 6, - "importPath": "eval.game", - "description": "eval.game", - "peekOfCode": "class Player1(Player):\n def __init__(\n self,\n nickname: str,\n model: str,\n ):\n self.nickname = nickname\n self.model = model\n self.robot = Robot(\n action_space=None,", - "detail": "eval.game", - "documentation": {} - }, - { - "label": "Player2", - "kind": 6, - "importPath": "eval.game", - "description": "eval.game", - "peekOfCode": "class Player2(Player):\n def __init__(\n self,\n nickname: str,\n model: str,\n ):\n self.nickname = nickname\n self.model = model\n self.robot = Robot(\n action_space=None,", - "detail": "eval.game", - "documentation": {} - }, - { - "label": "Episode", - "kind": 6, - "importPath": "eval.game", - "description": "eval.game", - "peekOfCode": "class Episode:\n player_1: Optional[Player1]\n player_2: Player2\n player_1_won: Optional[bool] = None\n def __init__(self, player_1: Optional[Player1], player_2: Player2):\n self.player_1 = player_1\n self.player_2 = player_2\n def save(self):\n if self.player_1 is None:\n # Playing with the controller", - "detail": "eval.game", - "documentation": {} - }, - { - "label": "Game", - "kind": 6, - "importPath": "eval.game", - "description": "eval.game", - "peekOfCode": "class Game:\n player_1: Optional[Player1] = None # First player. None if Human\n player_2: Player2\n render: Optional[bool] = False\n splash_screen: Optional[bool] = False\n save_game: Optional[bool] = False\n characters: Optional[List[str]] = [\"Ken\", \"Ken\"]\n outfits: Optional[List[int]] = [1, 3]\n frame_shape: Optional[List[int]] = [0, 0, 0]\n seed: Optional[int] = 42", - "detail": "eval.game", - "documentation": {} - }, - { - "label": "PlanAndAct", - "kind": 6, - "importPath": "eval.game", - "description": "eval.game", - "peekOfCode": "class PlanAndAct(Thread):\n def __init__(self, game: Game, episode: Episode):\n self.running = True\n self.game = game\n self.episode = episode\n Thread.__init__(self, daemon=True)\n # atexit.register(self.stop)\nclass PlanAndActPlayer1(PlanAndAct):\n def run(self) -> None:\n while self.running:", - "detail": "eval.game", - "documentation": {} - }, - { - "label": "PlanAndActPlayer1", - "kind": 6, - "importPath": "eval.game", - "description": "eval.game", - "peekOfCode": "class PlanAndActPlayer1(PlanAndAct):\n def run(self) -> None:\n while self.running:\n if \"agent_0\" not in self.game.actions:\n # Plan\n self.game.player_1.robot.plan()\n # Act\n self.game.actions[\"agent_0\"] = self.game.player_1.robot.act()\n # Observe the environment\n self.game.player_1.robot.observe(", - "detail": "eval.game", - "documentation": {} - }, - { - "label": "PlanAndActPlayer2", - "kind": 6, - "importPath": "eval.game", - "description": "eval.game", - "peekOfCode": "class PlanAndActPlayer2(PlanAndAct):\n def run(self) -> None:\n while self.running:\n if \"agent_1\" not in self.game.actions:\n # Plan\n self.game.player_2.robot.plan()\n # Act\n self.game.actions[\"agent_1\"] = self.game.player_2.robot.act()\n # Observe the environment\n self.game.player_2.robot.observe(", - "detail": "eval.game", - "documentation": {} - }, - { - "label": "generate_random_model", - "kind": 2, - "importPath": "eval.game", - "description": "eval.game", - "peekOfCode": "def generate_random_model(openai: bool = False, mistral: bool = True):\n models_available = []\n for model, models in MODELS.items():\n if openai and model == \"OPENAI\":\n models_available.extend(models)\n if mistral and model == \"MISTRAL\":\n models_available.extend(models)\n random.seed()\n # Generate a pair of random two models\n random_model = random.choice(models_available)", - "detail": "eval.game", - "documentation": {} - }, - { - "label": "main", - "kind": 2, - "importPath": "demo", - "description": "demo", - "peekOfCode": "def main():\n # Environment Settings\n game = Game(\n render=True,\n save_game=True,\n player_1=Player1(\n nickname=\"Baby\",\n model=\"mistral:mistral-small-latest\",\n ),\n player_2=Player2(", - "detail": "demo", - "documentation": {} - }, - { - "label": "main", - "kind": 2, - "importPath": "ollama", - "description": "ollama", - "peekOfCode": "def main():\n # Environment Settings\n game = Game(\n render=True,\n save_game=True,\n player_1=Player1(\n nickname=\"Baby\",\n model=\"ollama:qwen:14b-chat-v1.5-fp16\",\n # model=\"ollama:mistral\",\n ),", - "detail": "ollama", - "documentation": {} - }, - { - "label": "display_win_screen", - "kind": 2, - "importPath": "result", - "description": "result", - "peekOfCode": "def display_win_screen():\n current_directory = os.path.dirname(os.path.realpath(__file__))\n image_path = os.path.join(current_directory, \"winscreen.jpg\")\n # Create the main application window\n root = tk.Tk()\n root.title(\"Image Display\")\n # Load the image\n image = Image.open(image_path)\n # Convert the Image object into a Tkinter-compatible photo image\n tk_image = ImageTk.PhotoImage(image)", - "detail": "result", - "documentation": {} - }, - { - "label": "main", - "kind": 2, - "importPath": "script", - "description": "script", - "peekOfCode": "def main():\n # Environment Settings\n game = Game(\n render=True,\n player_1=Player1(\n nickname=\"Daddy\",\n model=\"groq:mistral-8x6b-32768\",\n ),\n player_2=Player2(\n nickname=\"Baby\",", - "detail": "script", - "documentation": {} - } -] \ No newline at end of file From e29dee3cdd3141f5aeacdc2f96b5fbe198ee6459 Mon Sep 17 00:00:00 2001 From: Sam Pink <42603236+SamPink@users.noreply.github.com> Date: Mon, 15 Apr 2024 09:34:15 +0100 Subject: [PATCH 5/7] Update llm.py Added support for models with : in the name --- agent/llm.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/agent/llm.py b/agent/llm.py index f6a822a..5cd8efd 100644 --- a/agent/llm.py +++ b/agent/llm.py @@ -1,5 +1,16 @@ def get_client(model_str): - provider, model = model_str.split(":") + split_result = model.split(":") + if len(split_result) == 1: + # Assume default provider to be openai + provider = "openai" + model_name = split_result[0] + elif len(split_result) > 2: + # Some model names have :, so we need to join the rest of the string + provider = split_result[0] + model_name = ":".join(split_result[1:]) + else: + provider = split_result[0] + model_name = split_result[1] if provider == "openai": from llama_index.llms.openai import OpenAI From 0b2359084f98152e5cceb0b80a9bd6252064158a Mon Sep 17 00:00:00 2001 From: Sam Pink Date: Mon, 15 Apr 2024 09:47:54 +0100 Subject: [PATCH 6/7] bug fix --- agent/llm.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/agent/llm.py b/agent/llm.py index 5cd8efd..738eca3 100644 --- a/agent/llm.py +++ b/agent/llm.py @@ -1,5 +1,5 @@ def get_client(model_str): - split_result = model.split(":") + split_result = model_str.split(":") if len(split_result) == 1: # Assume default provider to be openai provider = "openai" @@ -14,12 +14,12 @@ def get_client(model_str): if provider == "openai": from llama_index.llms.openai import OpenAI - return OpenAI(model=model) + return OpenAI(model=model_name) elif provider == "anthropic": from llama_index.llms.anthropic import Anthropic - return Anthropic(model=model) + return Anthropic(model=model_name) elif provider == "mixtral" or provider == "groq": from llama_index.llms.groq import Groq - return Groq(model=model) + return Groq(model=model_name) From 8ed79dff16bd8c2d3769fe4155056fe914a4540a Mon Sep 17 00:00:00 2001 From: Sam Pink Date: Mon, 15 Apr 2024 10:08:55 +0100 Subject: [PATCH 7/7] Working on adding streaming --- agent/robot.py | 72 ++++++++++++++++++++++++++++---------------------- 1 file changed, 41 insertions(+), 31 deletions(-) diff --git a/agent/robot.py b/agent/robot.py index 278e34b..ee36869 100644 --- a/agent/robot.py +++ b/agent/robot.py @@ -290,35 +290,44 @@ def get_moves_from_llm( return [random.choice(list(MOVES.values()))] while len(valid_moves) == 0: - llm_response = self.call_llm() - - # The response is a bullet point list of moves. Use regex - matches = re.findall(r"- ([\w ]+)", llm_response) - moves = ["".join(match) for match in matches] - invalid_moves = [] - valid_moves = [] - for move in moves: - cleaned_move_name = move.strip().lower() - if cleaned_move_name in META_INSTRUCTIONS_WITH_LOWER.keys(): - if self.player_nb == 1: - print( - f"[red] Player {self.player_nb} move: {cleaned_move_name}" - ) - elif self.player_nb == 2: - print( - f"[green] Player {self.player_nb} move: {cleaned_move_name}" - ) - valid_moves.append(cleaned_move_name) - else: - logger.debug(f"Invalid completion: {move}") - logger.debug(f"Cleaned move name: {cleaned_move_name}") - invalid_moves.append(move) - - if len(invalid_moves) > 1: - logger.warning(f"Many invalid moves: {invalid_moves}") - - logger.debug(f"Next moves: {valid_moves}") - return valid_moves + llm_stream = self.call_llm() + + # adding support for streaming the response + # this should make the players faster! + + llm_response = "" + + for r in llm_stream: + print(r.delta, end="") + llm_response += r.delta + + # The response is a bullet point list of moves. Use regex + matches = re.findall(r"- ([\w ]+)", llm_response) + moves = ["".join(match) for match in matches] + invalid_moves = [] + valid_moves = [] + for move in moves: + cleaned_move_name = move.strip().lower() + if cleaned_move_name in META_INSTRUCTIONS_WITH_LOWER.keys(): + if self.player_nb == 1: + print( + f"[red] Player {self.player_nb} move: {cleaned_move_name}" + ) + elif self.player_nb == 2: + print( + f"[green] Player {self.player_nb} move: {cleaned_move_name}" + ) + valid_moves.append(cleaned_move_name) + else: + logger.debug(f"Invalid completion: {move}") + logger.debug(f"Cleaned move name: {cleaned_move_name}") + invalid_moves.append(move) + + if len(invalid_moves) > 1: + logger.warning(f"Many invalid moves: {invalid_moves}") + + logger.debug(f"Next moves: {valid_moves}") + return valid_moves def call_llm( self, @@ -357,8 +366,9 @@ def call_llm( ChatMessage(role="system", content=system_prompt), ChatMessage(role="user", content="Your next moves are:"), ] - llm_response = client.chat(messages).message.content + resp = client.stream_chat(messages) logger.debug(f"LLM call to {self.model}: {system_prompt}") logger.debug(f"LLM call to {self.model}: {time.time() - start_time}s") - return llm_response + + return resp