davoodwadi's picture
gpt-4o-mini
0e0e116 verified
raw
history blame
7.38 kB
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()