mavinsao commited on
Commit
f817a5c
·
verified ·
1 Parent(s): 612b6d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -23
app.py CHANGED
@@ -1,12 +1,11 @@
1
  import os
2
  import gradio as gr
3
- import openai
4
- from langchain.llms import OpenAI
5
  from langchain.chains import LLMChain
6
- from langchain.prompts import PromptTemplate
7
 
8
  # Set up OpenAI API key
9
- openai.api_key = os.environ.get("OPENAI_API_KEY")
10
 
11
  # Define mental health resources
12
  mental_health_resources = """
@@ -38,17 +37,13 @@ Remember, seeking professional help is important if symptoms persist or interfer
38
  """
39
 
40
  # Define prompt template
41
- template = """
42
- You are an empathetic and supportive mental health chatbot. A user has shared the following:
43
-
44
- {user_input}
45
-
46
- Based on this, provide a response that:
47
- 1. Acknowledges their feelings
48
- 2. Offers a comforting perspective
49
- 3. Suggests a coping strategy
50
- 4. Asks a follow-up question to encourage further discussion
51
- 5. Reminds them that professional help is available if needed
52
 
53
  Keep the tone warm and supportive throughout.
54
 
@@ -57,19 +52,19 @@ Consider the following mental health information when responding:
57
 
58
  Chat History:
59
  {chat_history}
60
-
61
- Response:
62
  """
63
 
64
- prompt = PromptTemplate(
65
- template=template,
66
- input_variables=["user_input", "mental_health_info", "chat_history"]
67
- )
 
 
68
 
69
  # Set up main chatbot chain
70
  def setup_chatbot_chain():
71
- llm = OpenAI(temperature=0.7)
72
- return LLMChain(llm=llm, prompt=prompt)
73
 
74
  chatbot_chain = setup_chatbot_chain()
75
 
 
1
  import os
2
  import gradio as gr
3
+ from langchain.chat_models import ChatOpenAI
 
4
  from langchain.chains import LLMChain
5
+ from langchain.prompts import ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate
6
 
7
  # Set up OpenAI API key
8
+ os.environ["OPENAI_API_KEY"] = os.environ.get("OPENAI_API_KEY")
9
 
10
  # Define mental health resources
11
  mental_health_resources = """
 
37
  """
38
 
39
  # Define prompt template
40
+ system_template = """
41
+ You are an empathetic and supportive mental health chatbot. Provide responses that:
42
+ 1. Acknowledge the user's feelings
43
+ 2. Offer a comforting perspective
44
+ 3. Suggest a coping strategy
45
+ 4. Ask a follow-up question to encourage further discussion
46
+ 5. Remind them that professional help is available if needed
 
 
 
 
47
 
48
  Keep the tone warm and supportive throughout.
49
 
 
52
 
53
  Chat History:
54
  {chat_history}
 
 
55
  """
56
 
57
+ human_template = "{user_input}"
58
+
59
+ system_message_prompt = SystemMessagePromptTemplate.from_template(system_template)
60
+ human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
61
+
62
+ chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])
63
 
64
  # Set up main chatbot chain
65
  def setup_chatbot_chain():
66
+ llm = ChatOpenAI(temperature=0.7, model_name="gpt-3.5-turbo")
67
+ return LLMChain(llm=llm, prompt=chat_prompt)
68
 
69
  chatbot_chain = setup_chatbot_chain()
70