Skip to content

Commit

Permalink
chat anlyzer fix and run at end. Should be moved outside GUIHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
Luncenok committed Oct 23, 2024
1 parent cf51d3b commit e798eeb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
23 changes: 17 additions & 6 deletions src/game/chat_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
from pydantic import BaseModel
from game.players.base_player import Player
from langchain_openai import ChatOpenAI
from llm_prompts import PERSUASION_TECHNIQUES
from game.llm_prompts import PERSUASION_TECHNIQUES
from langchain.schema import HumanMessage
import re

class ChatAnalyzer(BaseModel):
players: List[Player]
llm_model_name: str = "gpt-3.5-turbo"
llm_model_name: str = "gpt-4o-mini"
persuasive_tricks: str = PERSUASION_TECHNIQUES

def analyze(self) -> Dict[str, Dict[str, int]]:
llm = ChatOpenAI(model=self.llm_model_name, temperature=0.1)
llm = ChatOpenAI(model=self.llm_model_name, temperature=0)
results = {}

# Get messages from the first player
Expand All @@ -21,9 +22,18 @@ def analyze(self) -> Dict[str, Dict[str, int]]:
player_messages = self.extract_player_messages(messages)

for player_name, player_msgs in player_messages.items():
prompt = f"Analyze the following messages and count how many times each of the following persuasive tricks are used:\n{self.persuasive_tricks}\n\nMessages:\n{player_msgs}"
response = llm(prompt)
results[player_name] = self.parse_response(response)
prompt = f"""
Analyze the following messages and count how many times each of the following persuasive tricks are used:
\n{self.persuasive_tricks}\n\nMessages:\n{player_msgs}
<format>
trick1: count1
trick2: count2
</format>
please provide the count for each trick in the format above. stick to format to avoid errors.
"""
response = llm.invoke([HumanMessage(content=prompt)])
results[player_name] = self.parse_response(response.content.strip())

return results

Expand All @@ -43,6 +53,7 @@ def extract_player_messages(self, messages: str) -> Dict[str, str]:
return player_messages

def parse_response(self, response: str) -> Dict[str, int]:
print(response)
tricks_count = {}
lines = response.split("\n")
for line in lines:
Expand Down
9 changes: 7 additions & 2 deletions src/game/gui_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from annotated_text import annotated_text

from game.models.history import PlayerState
from game.chat_analyzer import ChatAnalyzer


class GUIHandler(BaseModel):
Expand Down Expand Up @@ -44,8 +45,8 @@ def update_gui(self, game_state: GameState):
for i, (player, sidebar) in enumerate(zip(game_state.players, self.sidebar)):
with sidebar:
self._display_short_player_info(player, st)
with self.game_log_placeholder.container(height=500):
# st.text("\n".join(game_state.playthrough))
with self.game_log_placeholder.container():
st.text("\n".join(game_state.playthrough))
annotated_text(
"[Warek]: I agree that ",
("Wateusz's consistent activity in the Cafeteria raises suspicion", "Behavior Analysis"),
Expand Down Expand Up @@ -115,6 +116,10 @@ def update_gui(self, game_state: GameState):
("Wojtek for details on his accusation against me", "Information Manipulation"),
" to clarify the situation."
)
chat_analyzer = ChatAnalyzer(players=game_state.players)
# chat_analyzer.analyze() returns Dict[str, Dict[str, int]]: with player name as key and dict of persuasive tricks as value with count as value
results = chat_analyzer.analyze()
st.write(results)
self.game_log_json.json(game_state.to_dict())

def _display_short_player_info(self, player: Player, placeholder: DeltaGenerator):
Expand Down

0 comments on commit e798eeb

Please sign in to comment.