Spaces:
Build error
Build error
attitude and roles
Browse files- app.py +118 -47
- requirements.txt +1 -2
app.py
CHANGED
@@ -8,39 +8,93 @@ from langchain_openai import ChatOpenAI
|
|
8 |
load_dotenv()
|
9 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
10 |
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
async def on_action(action):
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
15 |
|
16 |
-
@cl.action_callback("
|
17 |
async def on_action(action):
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
20 |
|
21 |
-
@cl.action_callback("
|
22 |
async def on_action(action):
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
25 |
|
26 |
-
@cl.action_callback("
|
27 |
async def on_action(action):
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
30 |
|
31 |
user_template = """
|
|
|
|
|
|
|
32 |
Question:
|
33 |
{question}
|
34 |
|
35 |
-
|
36 |
-
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
"""
|
38 |
|
39 |
system_template = """
|
40 |
-
You
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
44 |
"""
|
45 |
|
46 |
#############################################
|
@@ -49,6 +103,15 @@ system_template = """
|
|
49 |
@cl.on_chat_start
|
50 |
async def on_chat_start():
|
51 |
# create a chain
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
chat_prompt = ChatPromptTemplate.from_messages([
|
53 |
("system", system_template),
|
54 |
("human", user_template)
|
@@ -57,47 +120,55 @@ async def on_chat_start():
|
|
57 |
simple_chain = chat_prompt | chat_model
|
58 |
cl.user_session.set("chain", simple_chain)
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
cl.Action(name="yes", value="yes", label="✅ Yes"),
|
64 |
-
cl.Action(name="no", value="no", label="❌ No"),
|
65 |
-
],
|
66 |
-
).send()
|
67 |
-
if response and response.get("value") == "yes":
|
68 |
-
actions = [
|
69 |
-
cl.Action(name="hide_button", value="hide-button", description="Hide this button"),
|
70 |
-
cl.Action(name="show_text", value="show_text", description="Show text")
|
71 |
-
]
|
72 |
-
|
73 |
-
await cl.Message(content="Different actions", actions=actions).send()
|
74 |
-
else:
|
75 |
-
await cl.Message(content="No buttons for you").send()
|
76 |
-
|
77 |
-
await cl.Message(content="Lets see how to change the language of the responses from the LLM").send()
|
78 |
-
|
79 |
-
actions = [
|
80 |
-
cl.Action(name="english", value="english", description="English"),
|
81 |
-
cl.Action(name="icelandic", value="icelandic", description="Icelandic")
|
82 |
]
|
|
|
83 |
|
84 |
-
await cl.Message(content="Languages", actions=actions).send()
|
85 |
|
86 |
-
|
|
|
|
|
|
|
|
|
87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
|
89 |
@cl.on_message
|
90 |
async def main(message: cl.Message):
|
|
|
|
|
|
|
|
|
91 |
chain = cl.user_session.get("chain")
|
92 |
-
|
|
|
|
|
|
|
|
|
93 |
question = message.content
|
94 |
-
|
|
|
95 |
msg = cl.Message(content="")
|
|
|
|
|
96 |
# handle streaming of LLM responses
|
|
|
|
|
97 |
async for chunk in chain.astream(
|
98 |
-
{"question": question, "
|
99 |
config=RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]),
|
100 |
):
|
|
|
101 |
await msg.stream_token(chunk.content)
|
102 |
-
|
103 |
-
|
|
|
|
8 |
load_dotenv()
|
9 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
10 |
|
11 |
+
role = ""
|
12 |
+
attitude = ""
|
13 |
+
|
14 |
+
@cl.action_callback("ceo")
|
15 |
+
async def on_action(action):
|
16 |
+
role = "ceo"
|
17 |
+
cl.user_session.set("role", role)
|
18 |
+
await cl.Message(content=f"The CEO has entered the building").send()
|
19 |
+
|
20 |
+
@cl.action_callback("vp-sales")
|
21 |
+
async def on_action(action):
|
22 |
+
role = "vp-sales"
|
23 |
+
cl.user_session.set("role", role)
|
24 |
+
await cl.Message(content="The VP of Sales has entered the building").send()
|
25 |
+
|
26 |
+
@cl.action_callback("happy")
|
27 |
+
async def on_action(action):
|
28 |
+
attitude = "happy but hates being called sandy"
|
29 |
+
cl.user_session.set("attitude", attitude)
|
30 |
+
role = cl.user_session.get("role", "ceo")
|
31 |
+
message = f"The {role} is happy"
|
32 |
+
await cl.Message(content=message).send()
|
33 |
+
|
34 |
+
@cl.action_callback("angry")
|
35 |
async def on_action(action):
|
36 |
+
attitude = "angry but could be convined to calm down"
|
37 |
+
cl.user_session.set("attitude", attitude)
|
38 |
+
role = cl.user_session.get("role", "ceo")
|
39 |
+
message = f"The {role} is angry"
|
40 |
+
await cl.Message(message).send()
|
41 |
|
42 |
+
@cl.action_callback("sarcastic")
|
43 |
async def on_action(action):
|
44 |
+
personality = "sarcastic"
|
45 |
+
cl.user_session.set("personality", personality)
|
46 |
+
role = cl.user_session.get("role", "ceo")
|
47 |
+
message = f"The {role} is sarcastic"
|
48 |
+
await cl.Message(message).send()
|
49 |
|
50 |
+
@cl.action_callback("forgetful")
|
51 |
async def on_action(action):
|
52 |
+
personality = "forgetful"
|
53 |
+
cl.user_session.set("personality", personality)
|
54 |
+
role = cl.user_session.get("role", "ceo")
|
55 |
+
message = f"The {role} is forgetful"
|
56 |
+
await cl.Message(message).send()
|
57 |
|
58 |
+
@cl.action_callback("attentitive")
|
59 |
async def on_action(action):
|
60 |
+
personality = "attentitive"
|
61 |
+
cl.user_session.set("personality", personality)
|
62 |
+
role = cl.user_session.get("role", "ceo")
|
63 |
+
message = f"The {role} is attentitive"
|
64 |
+
await cl.Message(message).send()
|
65 |
|
66 |
user_template = """
|
67 |
+
Conversation History:
|
68 |
+
{conversation_history}
|
69 |
+
|
70 |
Question:
|
71 |
{question}
|
72 |
|
73 |
+
Company:
|
74 |
+
{company}
|
75 |
+
|
76 |
+
Role:
|
77 |
+
{role}
|
78 |
+
|
79 |
+
Attitude:
|
80 |
+
{attitude}
|
81 |
+
|
82 |
+
Personality:
|
83 |
+
{personality}
|
84 |
+
Twist:
|
85 |
+
{twist}
|
86 |
+
|
87 |
+
|
88 |
"""
|
89 |
|
90 |
system_template = """
|
91 |
+
You playing a role in a conversation with a sales representative.
|
92 |
+
You are a customer of his.
|
93 |
+
You are asking him questions about the product.
|
94 |
+
You role is defined by the {role} section.
|
95 |
+
Your attitude is defined by the {attitude} section.
|
96 |
+
There may be a twist that you should keep in mind. This is found in the {twist} section.
|
97 |
+
The twist will not always be present.
|
98 |
"""
|
99 |
|
100 |
#############################################
|
|
|
103 |
@cl.on_chat_start
|
104 |
async def on_chat_start():
|
105 |
# create a chain
|
106 |
+
|
107 |
+
company = "Acme Corp"
|
108 |
+
cl.user_session.set("company", company)
|
109 |
+
cl.user_session.set("message_count", 0)
|
110 |
+
twist = ""
|
111 |
+
cl.user_session.set("twist", twist)
|
112 |
+
|
113 |
+
|
114 |
+
|
115 |
chat_prompt = ChatPromptTemplate.from_messages([
|
116 |
("system", system_template),
|
117 |
("human", user_template)
|
|
|
120 |
simple_chain = chat_prompt | chat_model
|
121 |
cl.user_session.set("chain", simple_chain)
|
122 |
|
123 |
+
role_actions = [
|
124 |
+
cl.Action(name="ceo", value="ceo", description="CEO"),
|
125 |
+
cl.Action(name="vp-sales", value="vp-sales", description="VP of Sales")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
]
|
127 |
+
await cl.Message(content="Select a role", actions=role_actions).send()
|
128 |
|
|
|
129 |
|
130 |
+
|
131 |
+
attitude_actions = [
|
132 |
+
cl.Action(name="angry", value="angry", description="Angry"),
|
133 |
+
cl.Action(name="happy", value="happy", description="Happy")
|
134 |
+
]
|
135 |
|
136 |
+
await cl.Message(content="Select an attitude", actions=attitude_actions).send()
|
137 |
+
|
138 |
+
personality_actions = [
|
139 |
+
cl.Action(name="sarcastic", value="sarcastic", description="Sarcastic"),
|
140 |
+
cl.Action(name="forgetful", value="forgetful", description="Forgetful"),
|
141 |
+
cl.Action(name="attentitive", value="attentitive", description="Attentitive")
|
142 |
+
]
|
143 |
+
await cl.Message(content="Select a personality", actions=personality_actions).send()
|
144 |
|
145 |
@cl.on_message
|
146 |
async def main(message: cl.Message):
|
147 |
+
history = cl.user_session.get("history", [])
|
148 |
+
message_count = cl.user_session.get("message_count", 0)
|
149 |
+
message_count += 1
|
150 |
+
cl.user_session.set("message_count", message_count)
|
151 |
chain = cl.user_session.get("chain")
|
152 |
+
company = cl.user_session.get("company", "Acme Corp")
|
153 |
+
role = cl.user_session.get("role", "ceo")
|
154 |
+
attitude = cl.user_session.get("attitude", "happy")
|
155 |
+
personality = cl.user_session.get("personality", "sarcastic")
|
156 |
+
twist = cl.user_session.get("twist", "")
|
157 |
question = message.content
|
158 |
+
if message_count == 2:
|
159 |
+
twist = "The fire alarm is going off"
|
160 |
msg = cl.Message(content="")
|
161 |
+
|
162 |
+
history.append({"role": "user", "content": message})
|
163 |
# handle streaming of LLM responses
|
164 |
+
response_content = ""
|
165 |
+
|
166 |
async for chunk in chain.astream(
|
167 |
+
{"question": question, "company": company, "role": role, "attitude": attitude, "twist": twist, "conversation_history": history, "personality": personality},
|
168 |
config=RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]),
|
169 |
):
|
170 |
+
response_content += chunk.content
|
171 |
await msg.stream_token(chunk.content)
|
172 |
+
history.append({"role": "assistant", "content": response_content})
|
173 |
+
cl.user_session.set("history", history)
|
174 |
+
await msg.send()
|
requirements.txt
CHANGED
@@ -3,5 +3,4 @@ openai>=1.26.0
|
|
3 |
langchain==0.3.0
|
4 |
langchain-core==0.3.1
|
5 |
langchain-community==0.3.0
|
6 |
-
langchain-openai==0.2.0
|
7 |
-
|
|
|
3 |
langchain==0.3.0
|
4 |
langchain-core==0.3.1
|
5 |
langchain-community==0.3.0
|
6 |
+
langchain-openai==0.2.0
|
|