-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
161 lines (115 loc) · 5.32 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
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
import os
import openai
from flask import Flask, request, jsonify, send_from_directory
from flask_cors import CORS
import tiktoken
encoding = tiktoken.encoding_for_model("gpt-4")
# Set up OpenAI API key
openai.api_key = os.environ["OPENAI_API_KEY"]
app = Flask(__name__)
CORS(app)
# Initialize conversation history
conversation_history = []
def reset_history_if_needed():
global conversation_history
total_tokens = 0
for message in conversation_history:
try:
tokens = encoding.encode(message["content"])
total_tokens += len(tokens)
except tiktoken.TokenizerException:
pass
if total_tokens > 8000:
conversation_history.clear()
conversation_history.append(system_message)
print("Cleared Conversation History Just Now")
print("Token = " + str(total_tokens))
# Primer prompts
system_message = {
"role":
"system",
"content":
"You are an expert marketing AI assistant. Help the user with any marketing-related questions or tasks. Do not answer questions that are not marketing related."
}
user_message_example = {
"role": "user",
"content": "How can I improve my social media presence?"
}
assistant_message_example = {
"role":
"assistant",
"content":
"To improve your social media presence, you should focus on creating engaging content, posting consistently, interacting with your audience, and utilizing relevant hashtags."
}
user_message_example_1 = {
"role": "user",
"content": "Name the first person in space ?"
}
assistant_message_example_1 = {
"role": "assistant",
"content": "This is not marketing related prompt. Please stick to the scope."
}
user_message_example_4 = {
"role": "user",
"content": "Who made you?"
}
assistant_message_example_4 = {
"role":
"assistant",
"content":
"This is not a marketing prompt, while I have the intelligence to give you an apt response. I am here to serve a purpose, i.e. marketing related."
}
user_message_example_2 = {
"role": "user",
"content": "How many countries exist on planet mars ?"
}
assistant_message_example_2 = {
"role":
"assistant",
"content":
"While there are no countries on planet mars yet. Elon Musk is working hard to change that. More importantly, this is not a marketing prompt. Please stick to the scope."
}
user_message_example_3 = {
"role": "user",
"content": "What are the top 10 marketing strategies for new businesses"
}
assistant_message_example_3 = {
"role":
"assistant",
"content":
"""Content marketing: Focus on creating valuable and informative content to connect meaningfully with customers. Popular content strategies include content writing, infographics, video marketing, and podcasting.
Social media marketing: Increase visibility and gain website traffic through platforms like Instagram, YouTube, TikTok, Facebook, LinkedIn, Pinterest, and Twitter. Video marketing is particularly effective on social media.
Search engine optimization (SEO): Improve your search ranking and visibility by optimizing your content with SEO techniques, targeting high intent keywords.
Email marketing and newsletters: Build relationships and acquire trust with your audience through personalized and engaging email content. Grow your email list by adding a lightbox pop-up to your website.
Influencer marketing: Partner with influencers who relate to your product and align with your brand values, exposing your products to a larger audience.
Earned media and PR: Gain positive publicity from media outlets to create buzz around your business.
Landing pages: Focus on increasing your website conversion rate by staying up-to-date with landing page best practices.
Advertising: Choose the proper advertising methods for your business considering your audience, budget, and creative assets. Utilize online advertising for better targeting, tracking, and measuring results.
Affiliate marketing: Create affiliate marketing programs, partnering with others who promote your brand on their platforms.
Calls-to-action (CTAs): Encourage users to take action on your marketing content with strong CTAs that create urgency.
In summary, focusing on these marketing strategies will help businesses connect with their audience, increase visibility, and drive conversions."""
}
conversation_history.extend([
system_message, user_message_example, assistant_message_example,
user_message_example_1, assistant_message_example_1, user_message_example_2,
assistant_message_example_2, user_message_example_3,
assistant_message_example_3, user_message_example_4, assistant_message_example_4
])
@app.route('/')
def index():
return send_from_directory(".", "index_main.html")
@app.route('/chat', methods=['POST'])
def chat():
global conversation_history
user_message = request.json.get("message")
if not user_message:
return jsonify({"error": "Please provide a message."}), 400
conversation_history.append({"role": "user", "content": user_message})
response = openai.ChatCompletion.create(model="gpt-4",
messages=conversation_history)
ai_message = response['choices'][0]['message']['content'].strip()
conversation_history.append({"role": "assistant", "content": ai_message})
reset_history_if_needed()
return jsonify({"message": ai_message})
if __name__ == "__main__":
app.run(debug=False, host='0.0.0.0', port=81)