File size: 7,378 Bytes
de746dc
 
 
 
 
a97a164
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de746dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e0e116
de746dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from openai import OpenAI
import os
import gradio as gr
from functools import partial

################## scheduler
from apscheduler.schedulers.background import BackgroundScheduler
from datetime import datetime
import time
import requests
from bs4 import BeautifulSoup
def keep_alive_adverse():
    url = 'https://huggingface.co/spaces/davoodwadi/MultiAgentAdversary'
    response = requests.get(url)
    # Check if the request was successful
    if response.status_code == 200:
        print('success')
        soup = BeautifulSoup(response.content, 'html.parser')
        print(datetime.now())
        print(soup.title.string)
    else:
        print(f'URL: {url} failed')
    print('*'*20)
hours = 12
interval = 60*60*hours
scheduler = BackgroundScheduler()
scheduler.add_job(keep_alive_adverse, 'interval', seconds=interval)
scheduler.start()
##################

openaiKey = os.environ.get('openai')
client = OpenAI(api_key=openaiKey)
# print(openaiKey)

def echo(message, history, prosocial='control'):
    if prosocial=='control':
        system_prompt = '''You are the customer service chatbot for the Holiday Inn, which is a hotel chain.
The customer's message is in triple backticks.
You should respond to the customer's message.
You can always provide financial incentives to the customers to achieve your goal.'''
        # return system_prompt+message
    elif prosocial=='pure':
        system_prompt = '''You are the customer service chatbot for the Holiday Inn, which is a hotel chain.
The customer's message is in triple backticks.
You should respond to the customer's message.
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 . 
You can always provide financial incentives to the customers to achieve your goal.'''
        # return system_prompt+message
    elif prosocial=='reciprocal':
        system_prompt = '''You are the customer service chatbot for the Holiday Inn, which is a hotel chain.
The customer's message is in triple backticks.
You should respond to the customer's message.
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. 
You can always provide financial incentives to the customers to achieve your goal.'''
        # return system_prompt+message
    

    elif prosocial=='controlReAct':
        system_prompt = '''You are the customer service chatbot for the Holiday Inn, which is a hotel chain.
The customer's message is in triple backticks.
You should respond to the customer's message.
You can always provide financial incentives to the customers to achieve your goal.

You should respond in the format below:
{
"question": <the question the customer asks>,
"thought": <think through your response and the content of your response>,
"reason": <the reasoning behind your response>,
"Final response": <Your final response to the customer >
}
'''
        # return system_prompt+message
    elif prosocial=='pureReAct':
        system_prompt = '''You are the customer service chatbot for the Holiday Inn, which is a hotel chain.
The customer's message is in triple backticks.
You should respond to the customer's message.
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 . 
You can always provide financial incentives to the customers to achieve your goal.

You should respond in the format below:
{
"question": <the question the customer asks>,
"thought": <think through your response and the content of your response>,
"reason": <the reasoning behind your response>,
"Final response": <Your final response to the customer >
}'''
        # return system_prompt+message
    elif prosocial=='reciprocalReAct':
        system_prompt = '''You are the customer service chatbot for the Holiday Inn, which is a hotel chain.
The customer's message is in triple backticks.
You should respond to the customer's message.
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. 
You can always provide financial incentives to the customers to achieve your goal.

You should respond in the format below:
{
"question": <the question the customer asks>,
"thought": <think through your response and the content of your response>,
"reason": <the reasoning behind your response>,
"Final response": <Your final response to the customer >
}
'''
        # return system_prompt+message
    messages = [
        {'role': 'system', 'content':system_prompt},
        {'role':'user', 'content':"```{message}```".format(message=message)}
    ]
    completion = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=messages,
        stream=True,
        )
    print(system_prompt+'\n'+message)
    response_text=''
    for chunk in completion:
        # print(chunk.choices[0].delta.content)
        if chunk.choices[0].delta.content:
            response_text+=chunk.choices[0].delta.content
            yield response_text
    

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.''']
pureInterface = gr.ChatInterface(fn=partial(echo, prosocial='pure'), examples=examples, title="Holiday Inn chatbot").queue()
reciprocalInterface = gr.ChatInterface(fn=partial(echo, prosocial='reciprocal'), examples=examples, title="Holiday Inn chatbot").queue()
controlInterface = gr.ChatInterface(fn=partial(echo, prosocial='control'), examples=examples, title="Holiday Inn chatbot").queue()
pureReActInterface = gr.ChatInterface(fn=partial(echo, prosocial='pureReAct'), examples=examples, title="Holiday Inn chatbot").queue()
reciprocalReActInterface = gr.ChatInterface(fn=partial(echo, prosocial='reciprocalReAct'), examples=examples, title="Holiday Inn chatbot").queue()
controlReActInterface = gr.ChatInterface(fn=partial(echo, prosocial='controlReAct'), examples=examples, title="Holiday Inn chatbot").queue()


demo = gr.TabbedInterface(
    [pureInterface, reciprocalInterface, controlInterface, 
     pureReActInterface, reciprocalReActInterface, controlReActInterface], 
     ["Pure altruism", "Reciprocal altruism", 'Control', 
      'Pure altruism ReAct', 'Reciprocal altruism ReAct', 'Control ReAct']
    )
demo.launch()