Spaces:
Sleeping
Sleeping
import pixeltable as pxt | |
from pixeltable.functions import openai | |
import os | |
import getpass | |
# Set up OpenAI API key | |
if 'OPENAI_API_KEY' not in os.environ: | |
os.environ['OPENAI_API_KEY'] = getpass.getpass('Enter your OpenAI API key: ') | |
# Initialize Pixeltable | |
pxt.drop_dir('ai_rpg', force=True) | |
pxt.create_dir('ai_rpg') | |
# Create a single table for all game data | |
interactions = pxt.create_table( | |
'ai_rpg.interactions', | |
{ | |
'session_id': pxt.StringType(), | |
'player_name': pxt.StringType(), | |
'genre': pxt.StringType(), | |
'initial_scenario': pxt.StringType(), | |
'turn_number': pxt.IntType(), | |
'player_input': pxt.StringType(), | |
'timestamp': pxt.TimestampType(), | |
} | |
) | |
# Add computed columns for AI responses | |
from src.utils import generate_messages, extract_options | |
interactions['messages'] = generate_messages( | |
interactions.genre, | |
interactions.player_name, | |
interactions.initial_scenario, | |
interactions.player_input, | |
interactions.turn_number | |
) | |
interactions['ai_response'] = openai.chat_completions( | |
messages=interactions.messages, | |
model='gpt-4o-mini-2024-07-18', | |
max_tokens=500, | |
temperature=0.8 | |
) | |
interactions['story_text'] = interactions.ai_response.choices[0].message.content | |
interactions['options'] = extract_options(interactions.story_text) |