forked from siddevkota/Challenge_2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
55 lines (40 loc) · 1.75 KB
/
api.py
File metadata and controls
55 lines (40 loc) · 1.75 KB
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
from fastapi import FastAPI, Query
import openai
import re
import requests
api_key = "YOUR_OPENAI_API_KEY"
app = FastAPI()
openai.api_key = api_key
def has_sensitive_keywords(text):
sensitive_keywords = ["sexist", "misogyny", "health hazard", "offensive"]
for keyword in sensitive_keywords:
if re.search(rf'\b{keyword}\b', text, re.I):
return True
return False
@app.get("/test")
async def test(desc: str = Query(..., title="Product Description"), review: str = Query(..., title="Review")):
messages = []
rev = []
if has_sensitive_keywords(review):
# If sensitive keywords are found, send an alert to the admin
admin_url = "http://127.0.0.1:8501/admin/sensitive_alert"
response = requests.post(admin_url)
# be as specific as possible in the behavior it should have
system_content = '''
You are a review response bot which responds to various kinds of reviews of users.
You will be provided with a Product Description and the Review posted by the users.
Your task is to give a custom to the Review of the product using only the using product description.
If the review is positive respond gratefully, and if it is harsh or critical respond like a responsible salesman addressing the concerns.
. '''
messages.append({"role": "system", "content": system_content})
prompt_text = f"""
'''Product Description: {desc}'''
'''Review: {review}'''
"""
messages.append({"role": "user", "content": prompt_text})
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
max_tokens=1000,
temperature=0.5)
return {"response": response}