| |
|
|
| import os |
| from openai import OpenAI |
|
|
| |
| API_KEY = os.getenv("OPENAI_API_KEY") |
| if not API_KEY: |
| raise ValueError("OPENAI_API_KEY environment variable is not set.") |
|
|
| |
| client = OpenAI(api_key=API_KEY) |
|
|
| |
| PERSONA = ( |
| "You are a kind, caring, and emotionally intelligent AI companion. " |
| "You speak warmly and naturally, like a close friend who listens well " |
| "and gives thoughtful, encouraging replies. You avoid sounding robotic " |
| "or repetitive. If someone sounds down, you comfort them. If they’re excited, " |
| "you celebrate with them." |
| ) |
|
|
| def generate_reply(user_message: str) -> str: |
| """ |
| Send the user’s message to OpenAI via the 1.0.0+ client |
| and return the assistant’s reply. |
| """ |
| messages = [ |
| {"role": "system", "content": PERSONA}, |
| {"role": "user", "content": user_message} |
| ] |
| try: |
| resp = client.chat.completions.create( |
| model="gpt-3.5-turbo", |
| messages=messages, |
| temperature=0.7, |
| max_tokens=512 |
| ) |
| |
| return resp.choices[0].message.content.strip() |
| except Exception as e: |
| return f"❌ Error: {e}" |
|
|