-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
216 lines (180 loc) · 7.04 KB
/
app.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
from langchain_openai import OpenAI #to integrate with openAI api with langchain
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain, SimpleSequentialChain #LLMCHAIN allow us to run our topic through our prompt and then genretate output
from flask import Flask, jsonify, request, make_response
from flask_debugtoolbar import DebugToolbarExtension
import os
import openai
from flask_cors import CORS
import logging
from dotenv import load_dotenv
import os
# Load the environment variables from the .env file
load_dotenv()
OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
app = Flask(__name__)
logger = logging.getLogger(__name__)
CORS(app)
debug = DebugToolbarExtension(app)
# LLM Templates => prompt template pass user input to larger piece of text, we dont need to write whole command!
# Market Research template (questions from openAI)
competitors_template = PromptTemplate(
input_variables=["company"],
template="provide me a list of companies that are competitors of {company}",
)
products_template = PromptTemplate(
input_variables=["company"],
template="generate a detailed comparative analysis report on {company} products",
)
# Personalize email template
personalize_template = PromptTemplate(
input_variables=["email"],
template="rewrite this for in a way to maximize response or closing deals: {email}",
)
#CRM template
welcome_template = PromptTemplate(
input_variables=["customerName", "productName"],
template="Create a Customer Service Welcome Message for {customerName} to introduce {productName}",
)
followup_template = PromptTemplate(
input_variables=["prospectName", "followUpReason", "note"],
template="Create a sales follow up for {prospectName} with the folowing reasons {followUpReason} and persoanlize with:{note}",
)
# Marketing template
caption_template = PromptTemplate(
input_variables=["postContent", "postTone"],
template="Write a social media caption for {postContent} with an {postTone} tone",
)
create_post_template = PromptTemplate(
input_variables=["platform", "postObjective", "postContent"],
template="Create a social media post for {platform} with {postObjective} and {postContent}",
)
# Market Research route
@app.route("/api/market-research", methods=["POST"])
def market_research():
user_prompt = request.json["prompt"]
llm = OpenAI(temperature=0.9)
companies_chain = LLMChain(llm=llm, prompt=competitors_template)
details_chain = LLMChain(llm=llm, prompt=products_template)
api_respond_companies = companies_chain.invoke(user_prompt)
api_respond_details = details_chain.invoke(api_respond_companies)
# WE COULD ALSO CHAIN BOTH OF CHAINS, didnt use this approach because of delay => cause flask time out and sometimes not good enouhgh answer
#possiable solution => redis
# sequential_chain = SimpleSequentialChain(chains=[companies_chain, details_chain])
# response = sequential_chain.run(user_prompt)
return jsonify(
{
"id": user_prompt,
"competitors": api_respond_companies,
"analyze": api_respond_details,
}
)
# Personalize Mail Route
@app.route("/api/personalize-email", methods=["POST"])
def personalize_email():
user_prompt = request.json["prompt"]
llm = OpenAI(temperature=0.9)
personalize_chain = LLMChain(llm=llm, prompt=personalize_template)
api_respond = personalize_chain.invoke(user_prompt)
return jsonify({"data": api_respond})
# CRM Route
@app.route("/api/crm", methods=["POST"])
def CRM_api():
req = request.get_json()
if not req:
return make_response("Bad request", 400)
if "customerName" in req and "productName" in req:
return welcome_customer_request(req)
elif "prospectName" in req and "followUpReason" in req and "note" in req:
return followup_request(req)
else:
return make_response("Bad request", 400)
def welcome_customer_request(req):
customer_name = req["customerName"]
product_name = req["productName"]
try:
llm = OpenAI(temperature=0.9)
CRM_chain = LLMChain(llm=llm, prompt=welcome_template)
api_respond = CRM_chain.invoke(
{"customerName": customer_name, "productName": product_name}
)
logger.info(api_respond)
return jsonify({"data": api_respond})
except Exception as e:
logger.error(
f"Error occurred while processing the customer product request: {str(e)}"
)
return make_response("Internal Server Error", 500)
def followup_request(req):
prospect_name = req["prospectName"]
follow_reason = req["followUpReason"]
note = req["note"]
try:
llm = OpenAI(temperature=0.9)
CRM_chain = LLMChain(llm=llm, prompt=followup_template)
api_respond = CRM_chain.invoke(
{
"prospectName": prospect_name,
"followUpReason": follow_reason,
"note": note,
}
)
logger.info(api_respond)
print(api_respond)
return jsonify({"data": api_respond})
except Exception as e:
logger.error(
f"Error occurred while processing the customer product request: {str(e)}"
)
return make_response("Internal Server Error", 500)
# Marketing Route
@app.route("/api/marketing", methods=["POST"])
def marketing_api():
req = request.get_json()
if not req:
return make_response("Bad request", 400)
if "postContent" in req and "postTone" in req:
return caption_create(req)
elif "platform" in req and "postObjective" in req and "postContent" in req:
return create_post(req)
else:
return make_response("Bad request", 400)
def caption_create(req):
post_content = req["postContent"]
post_tone = req["postTone"]
try:
llm = OpenAI(temperature=0.9)
marketing_chain = LLMChain(llm=llm, prompt=caption_template)
api_respond = marketing_chain.invoke(
{"postContent": post_content, "postTone": post_tone}
)
logger.info(api_respond)
print(api_respond)
return jsonify({"data": api_respond})
except Exception as e:
logger.error(
f"Error occurred while processing the customer product request: {str(e)}"
)
return make_response("Internal Server Error", 500)
def create_post(req):
platform = req["platform"]
post_objective = req["postObjective"]
post_contet = req["postContent"]
try:
llm = OpenAI(temperature=0.9)
marketing_chain = LLMChain(llm=llm, prompt=create_post_template)
api_respond = marketing_chain.invoke(
{
"platform": platform,
"postObjective": post_objective,
"postContent": post_contet,
}
)
logger.info(api_respond)
print(api_respond)
return jsonify({"data": api_respond})
except Exception as e:
logger.error(
f"Error occurred while processing the customer product request: {str(e)}"
)
return make_response("Internal Server Error", 500)