Spaces:
Sleeping
Sleeping
Create database.py
Browse files- database.py +47 -0
database.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|