Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,11 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
-
import
|
4 |
-
from langchain.llms import OpenAI
|
5 |
from langchain.chains import LLMChain
|
6 |
-
from langchain.prompts import
|
7 |
|
8 |
# Set up OpenAI API key
|
9 |
-
|
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 |
-
|
42 |
-
You are an empathetic and supportive mental health chatbot.
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
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 |
-
|
65 |
-
|
66 |
-
|
67 |
-
)
|
|
|
|
|
68 |
|
69 |
# Set up main chatbot chain
|
70 |
def setup_chatbot_chain():
|
71 |
-
llm =
|
72 |
-
return LLMChain(llm=llm, 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 |
|