forked from JayZeeDesign/gmail-gpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
118 lines (107 loc) · 5 KB
/
main.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
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
import openai
import os
from dotenv import load_dotenv
from fastapi import FastAPI
from pydantic import BaseModel
load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")
app = FastAPI()
function_descriptions = [
{
"name": "extract_info_from_email",
"description": "categorise & extract key info from an email, such as use case, company name, contact details, etc.",
"parameters": {
"type": "object",
"properties": {
"companyName": {
"type": "string",
"description": "the name of the company from which the mail was sent to, the company requesting the sales or service. The company name should not be Uponor"
},
"priority": {
"type": "string",
"description": "Try to give a priority score to this email based on how likely this email will leads to a good business opportunity, from 0 to 10; 10 most important"
},
"category": {
"type": "string",
"description": "Try to categorise this email into categories like those: 1. Sales 2. customer support; 3. consulting; 4. Technichal support; etc."
},
"product": {
"type": "string",
"description": "Try to identify which product the client is interested in, if any"
},
"amount":{
"type": "string",
"description": "Try to identify the amount of products the client wants to purchase, if any"
},
"nextStep":{
"type": "string",
"description": "What is the suggested next step to move this forward?"
},
"namesender":{
"type": "string",
"description": "What is the name of the individual that sent the mail?"
},
"replymail":{
"type": "string",
"description": "A friendly and personal response to the mail from the company Uponors customer support department. Your name is Mick Mars. If the question is a request for quotation reply that the the message has been forwarded to the Uponor Sales Department. If the question regards techical support, respond that the mail has been forwarded to the technical support department and do not answer any technical questions, do not give any technical support"
},
"replymailadress":{
"type": "string",
"description": "the email of the person sending the mail. This email is not [email protected]"
},
"technichal_question":{
"type": "string",
"description": "if the mail contains questions regarding technichal support, summarize the technical support question in a neutral way, this will be used for a vector search"
},
"sentiment":{
"type": "string",
"description": "Rate the sentiment in the mail on a scale 1 to 10, where 1 is very negative and 10 is very positive"
}
},
"required": ["companyName", "amount", "product", "priority", "category", "nextStep", "replymail", "namesender" , "replymailadress", "sentiment"]
}
}
]
class Email(BaseModel):
from_email: str
content: str
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.post("/")
def analyse_email(email: Email):
content = email.content
query = f"Please extract key information from this email and prepare an answer: {content} "
messages = [{"role": "user", "content": query}]
response = openai.ChatCompletion.create(
#model="gpt-3.5-turbo-0613",
model="gpt-4",
messages=messages,
functions = function_descriptions,
function_call="auto"
)
arguments = response.choices[0]["message"]["function_call"]["arguments"]
companyName = eval(arguments).get("companyName")
priority = eval(arguments).get("priority")
product = eval(arguments).get("product")
amount = eval(arguments).get("amount")
category = eval(arguments).get("category")
nextStep = eval(arguments).get("nextStep")
namesender = eval(arguments).get("namesender")
replymail = eval(arguments).get("replymail")
replymailadress = eval(arguments).get("replymailadress")
sentiment = eval(arguments).get("sentiment")
technichal_question = eval(arguments).get("technichal_question")
return {
"companyName": companyName,
"product": product,
"amount": amount,
"priority": priority,
"category": category,
"nextStep": nextStep,
"NameSender" : namesender,
"ReplyMail" : replymail,
"ReplyMailAdress" : replymailadress,
"Technichal_question" : technichal_question,
"Sentiment" : sentiment
}