lcipolina commited on
Commit
2f86234
·
verified ·
1 Parent(s): e933c26

Upload 2 files

Browse files
Files changed (2) hide show
  1. utils/common_utils.py +29 -0
  2. utils/llm_utils.py +56 -0
utils/common_utils.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # utils/common_utils.py
2
+ """Common utility functions for the OpenSpiel LLM Arena project.
3
+
4
+ This module provides shared utility functions for logging, configuration,
5
+ and other cross-cutting concerns.
6
+ """
7
+
8
+ import logging
9
+
10
+ def setup_logger(name: str) -> logging.Logger:
11
+ """Sets up a logger for the simulation.
12
+
13
+ Args:
14
+ name: The name of the logger.
15
+
16
+ Returns:
17
+ logging.Logger: Configured logger instance.
18
+ """
19
+ logger = logging.getLogger(name)
20
+ if not logger.handlers:
21
+ handler = logging.StreamHandler()
22
+ formatter = logging.Formatter(
23
+ "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
24
+ )
25
+ handler.setFormatter(formatter)
26
+ logger.addHandler(handler)
27
+ logger.setLevel(logging.INFO)
28
+ return logger
29
+
utils/llm_utils.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # utils/llm_utils.py
2
+ """Utility functions for Large Language Model (LLM) integration.
3
+
4
+ This module provides helper functions to generate prompts and interact with LLMs
5
+ for decision-making in game simulations.
6
+ """
7
+
8
+ from functools import lru_cache
9
+ from typing import List, Any
10
+ import random
11
+
12
+ def generate_prompt(game_name: str, state: str, legal_actions: List[int]) -> str:
13
+ """Generate a natural language prompt for the LLM to decide the next move.
14
+
15
+ Args:
16
+ game_name: The name of the game.
17
+ state: The current game state as a string.
18
+ legal_actions: The list of legal actions available to the player.
19
+
20
+ Returns:
21
+ str: A prompt string for the LLM.
22
+ """
23
+ prompt = (
24
+ f"You are playing the Game: {game_name}\n"
25
+ f"State:\n{state}\n"
26
+ f"Legal actions: {legal_actions}\n"
27
+ "Your task is to choose the next action (provide the action number answer with only the number of your next move from the list of legal actions. Do not provide any additional text or explanation."
28
+ )
29
+ return prompt
30
+
31
+
32
+ @lru_cache(maxsize=128)
33
+ def llm_decide_move(llm: Any, prompt: str, legal_actions: tuple) -> int:
34
+ """Use an LLM to decide the next move, with caching for repeated prompts.
35
+
36
+ Args:
37
+ llm: The LLM pipeline instance (e.g., from Hugging Face).
38
+ prompt: The prompt string provided to the LLM.
39
+ legal_actions: The list of legal actions available (converted to tuple).
40
+
41
+ Returns:
42
+ int: The action selected by the LLM.
43
+ """
44
+
45
+ # TODO(lkun): test this: temperature = 0.1 #less creative
46
+
47
+ response = llm(prompt, max_new_tokens=30, pad_token_id=50256)[0]["generated_text"]
48
+ for word in response.split():
49
+ try:
50
+ move = int(word)
51
+ if move in legal_actions: # Validate the move against legal actions
52
+ return move
53
+ except ValueError:
54
+ continue
55
+
56
+ return random.choice(legal_actions) # Fallback if no valid move is found