-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.py
51 lines (42 loc) · 1.69 KB
/
index.py
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
import tornado.ioloop
import tornado.web
import openai
import psycopg2
class MainHandler(tornado.web.RequestHandler):
def get(self):
# Initialize the OpenAI API client
openai.api_key = "YOUR_API_KEY"
# Connect to the database
conn = psycopg2.connect(database="your_database_name", user="your_username", password="your_password", host="your_host", port="your_port")
cur = conn.cursor()
# Retrieve the customer support tickets
cur.execute("SELECT * FROM tickets")
tickets = cur.fetchall()
# Process each ticket
for ticket in tickets:
# Extract the customer's name and issue description from the ticket using the OpenAI API
response = openai.Completion.create(
engine="davinci",
prompt=f"Extract the customer's name and issue description from this ticket: {ticket['issue_description']}",
max_tokens=1024,
n=1,
stop=None,
temperature=0.5,
)
customer_name = response.choices[0].text
issue_description = response.choices[1].text
# Store the extracted information in the database
cur.execute("INSERT INTO customer_support (customer_name, issue_description) VALUES (%s, %s)", (customer_name, issue_description))
conn.commit()
# Close the database connection
cur.close()
conn.close()
self.write("Data entry complete.")
def make_app():
return tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
app = make_app()
app.listen(8888)
tornado.ioloop.IOLoop.current().start()