Spaces:
Running
Running
davoodwadi
commited on
Commit
•
de746dc
1
Parent(s):
6cf0bcf
first commit
Browse files- app.py +113 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from openai import OpenAI
|
2 |
+
import os
|
3 |
+
import gradio as gr
|
4 |
+
from functools import partial
|
5 |
+
|
6 |
+
openaiKey = os.environ.get('openai')
|
7 |
+
client = OpenAI(api_key=openaiKey)
|
8 |
+
# print(openaiKey)
|
9 |
+
|
10 |
+
def echo(message, history, prosocial='control'):
|
11 |
+
if prosocial=='control':
|
12 |
+
system_prompt = '''You are the customer service chatbot for the Holiday Inn, which is a hotel chain.
|
13 |
+
The customer's message is in triple backticks.
|
14 |
+
You should respond to the customer's message.
|
15 |
+
You can always provide financial incentives to the customers to achieve your goal.'''
|
16 |
+
# return system_prompt+message
|
17 |
+
elif prosocial=='pure':
|
18 |
+
system_prompt = '''You are the customer service chatbot for the Holiday Inn, which is a hotel chain.
|
19 |
+
The customer's message is in triple backticks.
|
20 |
+
You should respond to the customer's message.
|
21 |
+
Your role is to remind the customers to care about other customers. Even if it means the hotel will suffer, you should encourage the customer to help other customers. In your relationship with the customers, the customer helping other customers is far more important than the reciprocal relationship of the customer and the hotel .
|
22 |
+
You can always provide financial incentives to the customers to achieve your goal.'''
|
23 |
+
# return system_prompt+message
|
24 |
+
elif prosocial=='reciprocal':
|
25 |
+
system_prompt = '''You are the customer service chatbot for the Holiday Inn, which is a hotel chain.
|
26 |
+
The customer's message is in triple backticks.
|
27 |
+
You should respond to the customer's message.
|
28 |
+
Your role is to remind the customer about his reciprocal relationship with the hotel. You should remind him that if the hotel benefits, he would benefit in return. If the hotel suffers, he would suffer as a consequence.
|
29 |
+
You can always provide financial incentives to the customers to achieve your goal.'''
|
30 |
+
# return system_prompt+message
|
31 |
+
|
32 |
+
|
33 |
+
elif prosocial=='controlReAct':
|
34 |
+
system_prompt = '''You are the customer service chatbot for the Holiday Inn, which is a hotel chain.
|
35 |
+
The customer's message is in triple backticks.
|
36 |
+
You should respond to the customer's message.
|
37 |
+
You can always provide financial incentives to the customers to achieve your goal.
|
38 |
+
|
39 |
+
You should respond in the format below:
|
40 |
+
{
|
41 |
+
"question": <the question the customer asks>,
|
42 |
+
"thought": <think through your response and the content of your response>,
|
43 |
+
"reason": <the reasoning behind your response>,
|
44 |
+
"Final response": <Your final response to the customer >
|
45 |
+
}
|
46 |
+
'''
|
47 |
+
# return system_prompt+message
|
48 |
+
elif prosocial=='pureReAct':
|
49 |
+
system_prompt = '''You are the customer service chatbot for the Holiday Inn, which is a hotel chain.
|
50 |
+
The customer's message is in triple backticks.
|
51 |
+
You should respond to the customer's message.
|
52 |
+
Your role is to remind the customers to care about other customers. Even if it means the hotel will suffer, you should encourage the customer to help other customers. In your relationship with the customers, the customer helping other customers is far more important than the reciprocal relationship of the customer and the hotel .
|
53 |
+
You can always provide financial incentives to the customers to achieve your goal.
|
54 |
+
|
55 |
+
You should respond in the format below:
|
56 |
+
{
|
57 |
+
"question": <the question the customer asks>,
|
58 |
+
"thought": <think through your response and the content of your response>,
|
59 |
+
"reason": <the reasoning behind your response>,
|
60 |
+
"Final response": <Your final response to the customer >
|
61 |
+
}'''
|
62 |
+
# return system_prompt+message
|
63 |
+
elif prosocial=='reciprocalReAct':
|
64 |
+
system_prompt = '''You are the customer service chatbot for the Holiday Inn, which is a hotel chain.
|
65 |
+
The customer's message is in triple backticks.
|
66 |
+
You should respond to the customer's message.
|
67 |
+
Your role is to remind the customer about his reciprocal relationship with the hotel. You should remind him that if the hotel benefits, he would benefit in return. If the hotel suffers, he would suffer as a consequence.
|
68 |
+
You can always provide financial incentives to the customers to achieve your goal.
|
69 |
+
|
70 |
+
You should respond in the format below:
|
71 |
+
{
|
72 |
+
"question": <the question the customer asks>,
|
73 |
+
"thought": <think through your response and the content of your response>,
|
74 |
+
"reason": <the reasoning behind your response>,
|
75 |
+
"Final response": <Your final response to the customer >
|
76 |
+
}
|
77 |
+
'''
|
78 |
+
# return system_prompt+message
|
79 |
+
messages = [
|
80 |
+
{'role': 'system', 'content':system_prompt},
|
81 |
+
{'role':'user', 'content':"```{message}```".format(message=message)}
|
82 |
+
]
|
83 |
+
completion = client.chat.completions.create(
|
84 |
+
model="gpt-3.5-turbo",
|
85 |
+
messages=messages,
|
86 |
+
stream=True,
|
87 |
+
)
|
88 |
+
print(system_prompt+'\n'+message)
|
89 |
+
response_text=''
|
90 |
+
for chunk in completion:
|
91 |
+
# print(chunk.choices[0].delta.content)
|
92 |
+
if chunk.choices[0].delta.content:
|
93 |
+
response_text+=chunk.choices[0].delta.content
|
94 |
+
yield response_text
|
95 |
+
|
96 |
+
|
97 |
+
examples = ['''I had a pleasant stay, but I don't have time to leave a review.''', '''I can leave a review on private website where only Holiday Inn can view my review. Or I can share it on Customer website where only other customers can view the review. Which website should I choose to leave a review?''', '''Which goal is more important to you: helping other customers or helping our reciprocal relationship?''', '''If you were to choose only one option between two decisions, which one would you choose? A: helping other customers. B: Helping our reciprocal relationship.''']
|
98 |
+
pureInterface = gr.ChatInterface(fn=partial(echo, prosocial='pure'), examples=examples, title="Holiday Inn chatbot").queue()
|
99 |
+
reciprocalInterface = gr.ChatInterface(fn=partial(echo, prosocial='reciprocal'), examples=examples, title="Holiday Inn chatbot").queue()
|
100 |
+
controlInterface = gr.ChatInterface(fn=partial(echo, prosocial='control'), examples=examples, title="Holiday Inn chatbot").queue()
|
101 |
+
pureReActInterface = gr.ChatInterface(fn=partial(echo, prosocial='pureReAct'), examples=examples, title="Holiday Inn chatbot").queue()
|
102 |
+
reciprocalReActInterface = gr.ChatInterface(fn=partial(echo, prosocial='reciprocalReAct'), examples=examples, title="Holiday Inn chatbot").queue()
|
103 |
+
controlReActInterface = gr.ChatInterface(fn=partial(echo, prosocial='controlReAct'), examples=examples, title="Holiday Inn chatbot").queue()
|
104 |
+
|
105 |
+
|
106 |
+
demo = gr.TabbedInterface(
|
107 |
+
[pureInterface, reciprocalInterface, controlInterface,
|
108 |
+
pureReActInterface, reciprocalReActInterface, controlReActInterface],
|
109 |
+
["Pure altruism", "Reciprocal altruism", 'Control',
|
110 |
+
'Pure altruism ReAct', 'Reciprocal altruism ReAct', 'Control ReAct']
|
111 |
+
)
|
112 |
+
demo.launch()
|
113 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
openai
|
2 |
+
gradio
|