Spaces:
Runtime error
Runtime error
Update helper.py
Browse files
helper.py
CHANGED
|
@@ -1,42 +1,23 @@
|
|
| 1 |
-
# helper.py
|
| 2 |
-
|
| 3 |
import os
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
# Grab your key from the env (set as a HF Space secret or locally exported)
|
| 7 |
-
API_KEY = os.getenv("OPENAI_API_KEY")
|
| 8 |
-
if not API_KEY:
|
| 9 |
-
raise ValueError("OPENAI_API_KEY environment variable is not set.")
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
|
| 14 |
-
|
| 15 |
-
PERSONA = (
|
| 16 |
-
"You are a kind, caring, and emotionally intelligent AI companion. "
|
| 17 |
-
"You speak warmly and naturally, like a close friend who listens well "
|
| 18 |
-
"and gives thoughtful, encouraging replies. You avoid sounding robotic "
|
| 19 |
-
"or repetitive. If someone sounds down, you comfort them. If they’re excited, "
|
| 20 |
-
"you celebrate with them."
|
| 21 |
-
)
|
| 22 |
-
|
| 23 |
-
def generate_reply(user_message: str) -> str:
|
| 24 |
"""
|
| 25 |
-
|
| 26 |
-
and return the assistant’s reply.
|
| 27 |
"""
|
| 28 |
-
messages = [
|
| 29 |
-
|
| 30 |
-
{"role": "user",
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
return resp.choices[0].message.content.strip()
|
| 41 |
-
except Exception as e:
|
| 42 |
-
return f"❌ Error: {e}"
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import openai
|
| 3 |
+
from dotenv import load_dotenv
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
load_dotenv()
|
| 6 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 7 |
|
| 8 |
+
def generate_reply(message: str, history: list[tuple[str, str]]) -> tuple[str, list[tuple[str, str]]]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
"""
|
| 10 |
+
Generic OpenAI chat reply.
|
|
|
|
| 11 |
"""
|
| 12 |
+
messages = [{"role": "system", "content": "You are kind and caring."}]
|
| 13 |
+
for u, b in history:
|
| 14 |
+
messages.append({"role": "user", "content": u})
|
| 15 |
+
messages.append({"role": "assistant", "content": b})
|
| 16 |
+
messages.append({"role": "user", "content": message})
|
| 17 |
+
|
| 18 |
+
resp = openai.ChatCompletion.create(
|
| 19 |
+
model="gpt-4o-mini",
|
| 20 |
+
messages=messages
|
| 21 |
+
)
|
| 22 |
+
reply = resp.choices[0].message.content
|
| 23 |
+
return reply, history + [(message, reply)]
|
|
|
|
|
|
|
|
|