DronA23 commited on
Commit
18f991e
·
verified ·
1 Parent(s): 8408313

Update helper.py

Browse files
Files changed (1) hide show
  1. helper.py +18 -37
helper.py CHANGED
@@ -1,42 +1,23 @@
1
- # helper.py
2
-
3
  import os
4
- from openai import OpenAI
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
- # Instantiate the new client
12
- client = OpenAI(api_key=API_KEY)
13
 
14
- # Define your chatbot’s persona
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
- Send the user’s message to OpenAI via the 1.0.0+ client
26
- and return the assistant’s reply.
27
  """
28
- messages = [
29
- {"role": "system", "content": PERSONA},
30
- {"role": "user", "content": user_message}
31
- ]
32
- try:
33
- resp = client.chat.completions.create(
34
- model="gpt-3.5-turbo",
35
- messages=messages,
36
- temperature=0.7,
37
- max_tokens=512
38
- )
39
- # Extract the text of the assistant’s reply
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)]