Eric Botti commited on
Commit ·
f566386
1
Parent(s): dd5d973
message class
Browse files- src/message.py +36 -0
- src/messages.py +0 -1
- src/player.py +3 -21
src/message.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Literal
|
| 2 |
+
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
# Lots of AI Libraries use HumanMessage and AIMessage as the base classes for their messages.
|
| 7 |
+
# This doesn't make sense for our as Humans and AIs are both players in the game, meaning they have the same role.
|
| 8 |
+
# The Langchain type field is used to convert to that syntax.
|
| 9 |
+
class Message(BaseModel):
|
| 10 |
+
type: Literal["game", "player", "retry", "error", "format"]
|
| 11 |
+
"""The type of the message. Can be "prompt" or "player"."""
|
| 12 |
+
content: str
|
| 13 |
+
"""The content of the message."""
|
| 14 |
+
@property
|
| 15 |
+
def langchain_type(self):
|
| 16 |
+
"""Returns the langchain message type for the message."""
|
| 17 |
+
if self.type in ["game", "retry", "error", "format"]:
|
| 18 |
+
return "human"
|
| 19 |
+
else:
|
| 20 |
+
return "ai"
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
"""
|
| 24 |
+
Right now we have two separate systems that use the word "message":
|
| 25 |
+
1. The Game class uses messages to communicate with the players
|
| 26 |
+
They have types:
|
| 27 |
+
- "game" used for all players, these are sent to the players and converted into the the above message class
|
| 28 |
+
- "verbose", and "debug" currently for the human player only
|
| 29 |
+
2. The Player class uses messsage is to communicate with the controller (either the AI or the human)
|
| 30 |
+
- "game" type messages come from the Game and are responded to by the format.
|
| 31 |
+
- "retry", "error", and "format"
|
| 32 |
+
- "player" is used to communicate with the AI or human player.
|
| 33 |
+
All of these messages are logged
|
| 34 |
+
|
| 35 |
+
Long term we should investigate merging these two systems so we can log verbose and debug messages if desired.
|
| 36 |
+
"""
|
src/messages.py
DELETED
|
@@ -1 +0,0 @@
|
|
| 1 |
-
|
|
|
|
|
|
src/player.py
CHANGED
|
@@ -12,31 +12,13 @@ from langchain_core.exceptions import OutputParserException
|
|
| 12 |
from pydantic import BaseModel
|
| 13 |
|
| 14 |
from game_utils import log
|
| 15 |
-
from
|
| 16 |
|
| 17 |
Role = Literal["chameleon", "herd"]
|
| 18 |
|
| 19 |
logging.basicConfig(level=logging.WARNING)
|
| 20 |
logger = logging.getLogger("chameleon")
|
| 21 |
|
| 22 |
-
|
| 23 |
-
# Lots of AI Libraries use HumanMessage and AIMessage as the base classes for their messages.
|
| 24 |
-
# This doesn't make sense for our as Humans and AIs are both players in the game, meaning they have the same role.
|
| 25 |
-
# The Langchain type field is used to convert to that syntax.
|
| 26 |
-
class Message(BaseModel):
|
| 27 |
-
type: Literal["prompt", "player", "retry", "error"]
|
| 28 |
-
"""The type of the message. Can be "prompt" or "player"."""
|
| 29 |
-
content: str
|
| 30 |
-
"""The content of the message."""
|
| 31 |
-
@property
|
| 32 |
-
def langchain_type(self):
|
| 33 |
-
"""Returns the langchain message type for the message."""
|
| 34 |
-
if self.type in ["prompt", "retry", "error"]:
|
| 35 |
-
return "human"
|
| 36 |
-
else:
|
| 37 |
-
return "ai"
|
| 38 |
-
|
| 39 |
-
|
| 40 |
class Player:
|
| 41 |
|
| 42 |
role: Role | None = None
|
|
@@ -104,7 +86,7 @@ class Player:
|
|
| 104 |
# Clear the prompt queue
|
| 105 |
self.prompt_queue = []
|
| 106 |
|
| 107 |
-
message = Message(type="
|
| 108 |
output = await self.generate.ainvoke(message)
|
| 109 |
if self.controller_type == "ai":
|
| 110 |
retries = 0
|
|
@@ -165,7 +147,7 @@ class Player:
|
|
| 165 |
|
| 166 |
prompt = prompt_template.invoke({"format_instructions": parser.get_format_instructions()})
|
| 167 |
|
| 168 |
-
message = Message(type="
|
| 169 |
|
| 170 |
response = await self.generate.ainvoke(message)
|
| 171 |
|
|
|
|
| 12 |
from pydantic import BaseModel
|
| 13 |
|
| 14 |
from game_utils import log
|
| 15 |
+
from message import Message
|
| 16 |
|
| 17 |
Role = Literal["chameleon", "herd"]
|
| 18 |
|
| 19 |
logging.basicConfig(level=logging.WARNING)
|
| 20 |
logger = logging.getLogger("chameleon")
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
class Player:
|
| 23 |
|
| 24 |
role: Role | None = None
|
|
|
|
| 86 |
# Clear the prompt queue
|
| 87 |
self.prompt_queue = []
|
| 88 |
|
| 89 |
+
message = Message(type="game", content=prompt)
|
| 90 |
output = await self.generate.ainvoke(message)
|
| 91 |
if self.controller_type == "ai":
|
| 92 |
retries = 0
|
|
|
|
| 147 |
|
| 148 |
prompt = prompt_template.invoke({"format_instructions": parser.get_format_instructions()})
|
| 149 |
|
| 150 |
+
message = Message(type="format", content=prompt.text)
|
| 151 |
|
| 152 |
response = await self.generate.ainvoke(message)
|
| 153 |
|