PierreBrunelle commited on
Commit
ca8bcba
·
verified ·
1 Parent(s): 4b6484b

Delete src

Browse files
Files changed (4) hide show
  1. src/__init__.py +0 -0
  2. src/database.py +0 -47
  3. src/game_logic.py +0 -66
  4. src/utils.py +0 -35
src/__init__.py DELETED
File without changes
src/database.py DELETED
@@ -1,47 +0,0 @@
1
- import pixeltable as pxt
2
- from pixeltable.functions import openai
3
- import os
4
- import getpass
5
-
6
- # Set up OpenAI API key
7
- if 'OPENAI_API_KEY' not in os.environ:
8
- os.environ['OPENAI_API_KEY'] = getpass.getpass('Enter your OpenAI API key: ')
9
-
10
- # Initialize Pixeltable
11
- pxt.drop_dir('ai_rpg', force=True)
12
- pxt.create_dir('ai_rpg')
13
-
14
- # Create a single table for all game data
15
- interactions = pxt.create_table(
16
- 'ai_rpg.interactions',
17
- {
18
- 'session_id': pxt.StringType(),
19
- 'player_name': pxt.StringType(),
20
- 'genre': pxt.StringType(),
21
- 'initial_scenario': pxt.StringType(),
22
- 'turn_number': pxt.IntType(),
23
- 'player_input': pxt.StringType(),
24
- 'timestamp': pxt.TimestampType(),
25
- }
26
- )
27
-
28
- # Add computed columns for AI responses
29
- from src.utils import generate_messages, extract_options
30
-
31
- interactions['messages'] = generate_messages(
32
- interactions.genre,
33
- interactions.player_name,
34
- interactions.initial_scenario,
35
- interactions.player_input,
36
- interactions.turn_number
37
- )
38
-
39
- interactions['ai_response'] = openai.chat_completions(
40
- messages=interactions.messages,
41
- model='gpt-4o-mini-2024-07-18',
42
- max_tokens=500,
43
- temperature=0.8
44
- )
45
-
46
- interactions['story_text'] = interactions.ai_response.choices[0].message.content
47
- interactions['options'] = extract_options(interactions.story_text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/game_logic.py DELETED
@@ -1,66 +0,0 @@
1
- from datetime import datetime
2
- from src.database import interactions
3
-
4
- class RPGGame:
5
- def __init__(self):
6
- self.current_session_id = None
7
- self.turn_number = 0
8
-
9
- def start_game(self, player_name: str, genre: str, scenario: str) -> str:
10
- session_id = f"session_{datetime.now().strftime('%Y%m%d%H%M%S')}_{player_name}"
11
- self.current_session_id = session_id
12
- self.turn_number = 0
13
-
14
- # Create initial interaction with all session data
15
- interactions.insert([{
16
- 'session_id': session_id,
17
- 'player_name': player_name,
18
- 'genre': genre,
19
- 'initial_scenario': scenario,
20
- 'turn_number': 0,
21
- 'player_input': "Game starts",
22
- 'timestamp': datetime.now()
23
- }])
24
-
25
- # Get initial story and options
26
- initial_response = interactions.select(interactions.story_text).where(
27
- (interactions.session_id == session_id) &
28
- (interactions.turn_number == 0)
29
- ).collect()['story_text'][0]
30
-
31
- return session_id, initial_response
32
-
33
- def process_action(self, action: str) -> str:
34
- if not self.current_session_id:
35
- return "No active game session. Please start a new game."
36
-
37
- self.turn_number += 1
38
-
39
- # Get session info from previous turn
40
- prev_turn = interactions.select(
41
- interactions.player_name,
42
- interactions.genre,
43
- interactions.initial_scenario
44
- ).where(
45
- (interactions.session_id == self.current_session_id) &
46
- (interactions.turn_number == 0)
47
- ).collect()
48
-
49
- # Record player action with session data
50
- interactions.insert([{
51
- 'session_id': self.current_session_id,
52
- 'player_name': prev_turn['player_name'][0],
53
- 'genre': prev_turn['genre'][0],
54
- 'initial_scenario': prev_turn['initial_scenario'][0],
55
- 'turn_number': self.turn_number,
56
- 'player_input': action,
57
- 'timestamp': datetime.now()
58
- }])
59
-
60
- # Get AI response
61
- response = interactions.select(interactions.story_text).where(
62
- (interactions.session_id == self.current_session_id) &
63
- (interactions.turn_number == self.turn_number)
64
- ).collect()['story_text'][0]
65
-
66
- return response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/utils.py DELETED
@@ -1,35 +0,0 @@
1
- import pixeltable as pxt
2
- import re
3
-
4
- @pxt.udf
5
- def generate_messages(genre: str, player_name: str, initial_scenario: str, player_input: str, turn_number: int) -> list[dict]:
6
- return [
7
- {
8
- 'role': 'system',
9
- 'content': f"""You are the game master for a {genre} RPG. The player's name is {player_name}.
10
- Provide an engaging response to the player's action and present exactly 3 numbered options for their next move:
11
- 1. A dialogue option (saying something)
12
- 2. A random action they could take
13
- 3. A unique or unexpected choice
14
-
15
- Format each option with a number (1., 2., 3.) followed by the action description."""
16
- },
17
- {
18
- 'role': 'user',
19
- 'content': f"Current scenario: {initial_scenario}\n"
20
- f"Player's action: {player_input}\n"
21
- f"Turn number: {turn_number}\n\n"
22
- "Provide a response and 3 options:"
23
- }
24
- ]
25
-
26
- @pxt.udf
27
- def extract_options(story_text: str) -> list[str]:
28
- """Extract the three options from the story text"""
29
- # Look for numbered options (1., 2., 3.) and grab the text after them
30
- options = re.findall(r'\d\.\s*(.*?)(?=\d\.|$)', story_text, re.DOTALL)
31
- # Clean up the options and ensure we have exactly 3
32
- options = [opt.strip() for opt in options[:3]]
33
- while len(options) < 3:
34
- options.append("Take another action...")
35
- return options