-
Notifications
You must be signed in to change notification settings - Fork 72
/
main.py
283 lines (196 loc) · 8.31 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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import openai
import tiktoken
import numpy as np
import os
import streamlit as st
import json
from streamlit_chat import message
import pinecone
import random
from PIL import Image
pinecone_api_key = st.secrets["API_KEYS"]["pinecone"]
pinecone.init(api_key=pinecone_api_key, environment="us-east1-gcp")
openai.api_key = st.secrets["API_KEYS"]["openai"]
#gptflix_logo = Image.open('./chat/logo.png')
bens_bites_logo = Image.open('./chat/Bens_Bites_Logo.jpg')
# random user picture
user_av = random.randint(0, 100)
# random bott picture
bott_av = random.randint(0, 100)
def randomize_array(arr):
sampled_arr = []
while arr:
elem = random.choice(arr)
sampled_arr.append(elem)
arr.remove(elem)
return sampled_arr
st.set_page_config(page_title="GPTflix", page_icon="🍿", layout="wide")
st.header("GPTflix is like chatGPT for movie reviews!🍿\n")
# st.header("Thanks for visiting GPTflix! It's been a fun experiment, with over 4000 unique users over four weeks and an average of 10 questions per user while the site was online! Perhaps we will be back some time...🍿\n")
# Define the name of the index and the dimensionality of the embeddings
index_name = "400kmovies"
dimension = 1536
pineconeindex = pinecone.Index(index_name)
######################################
#######
####### OPEN AI SETTINGS !!!
#######
#######
######################################
#COMPLETIONS_MODEL = "text-davinci-003"
COMPLETIONS_MODEL = "gpt-3.5-turbo"
EMBEDDING_MODEL = "text-embedding-ada-002"
COMPLETIONS_API_PARAMS = {
# We use temperature of 0.0 because it gives the most predictable, factual answer.
"temperature": 0.0,
"max_tokens": 400,
"model": COMPLETIONS_MODEL,
}
feedback_url = "https://forms.gle/YMTtGK1zXdCRzRaj6"
bb_url ="https://www.bensbites.co/?utm_source=gptflix"
tech_url = "https://news.ycombinator.com/item?id=34802625"
github_url = "https://github.com/stephansturges/GPTflix"
with st.sidebar:
st.markdown("# About 🙌")
st.markdown(
"GPTflix allows you to talk to version of chatGPT \n"
"that has access to reviews of about 10 000 movies! 🎬 \n"
"Holy smokes, chatGPT and 10x cheaper??! We are BACK! 😝\n"
)
st.markdown(
"Unline chatGPT, GPTflix can't make stuff up\n"
"and will only answer from injected knowlege 👩🏫 \n"
)
st.markdown("---")
st.markdown("A side project by Stephan Sturges")
st.markdown("Kept online by [Ben's Bites](%s)!" %bb_url)
st.image(bens_bites_logo, width=60)
st.markdown("---")
st.markdown("Tech [info](%s) for you nerds out there!" %tech_url)
st.markdown("Give feedback [here](%s)" %feedback_url)
st.markdown("---")
st.markdown("Code open-sourced [here](%s)" %github_url)
st.markdown("---")
# MAIN FUNCTIONS
def num_tokens_from_string(string, encoding_name):
"""Returns the number of tokens in a text string."""
encoding = tiktoken.get_encoding(encoding_name)
num_tokens = len(encoding.encode(string))
return num_tokens
def get_embedding(text, model):
result = openai.Embedding.create(
model=model,
input=text
)
return result["data"][0]["embedding"]
MAX_SECTION_LEN = 2500 #in tokens
SEPARATOR = "\n"
ENCODING = "cl100k_base" # encoding for text-embedding-ada-002
encoding = tiktoken.get_encoding(ENCODING)
separator_len = len(encoding.encode(SEPARATOR))
def construct_prompt_pinecone(question):
"""
Fetch relevant information from pinecone DB
"""
xq = get_embedding(question , EMBEDDING_MODEL)
#print(xq)
res = pineconeindex.query([xq], top_k=30, include_metadata=True, namespace="movies")
#print(res)
# print(most_relevant_document_sections[:2])
chosen_sections = []
chosen_sections_length = 0
for match in res['matches'][:12]:
#print(f"{match['score']:.2f}: {match['metadata']['text']}")
if chosen_sections_length <= MAX_SECTION_LEN:
document_section = match['metadata']['text']
# document_section = str(_[0] + _[1])
chosen_sections.append(SEPARATOR + document_section)
chosen_sections_length += num_tokens_from_string(str(document_section), "gpt2")
for match in randomize_array(res['matches'][-18:]):
#print(f"{match['score']:.2f}: {match['metadata']['text']}")
if chosen_sections_length <= MAX_SECTION_LEN:
document_section = match['metadata']['text']
# document_section = str(_[0] + _[1])
chosen_sections.append(SEPARATOR + document_section)
chosen_sections_length += num_tokens_from_string(str(document_section), "gpt2")
# Useful diagnostic information
#print(f"Selected {len(chosen_sections)} document sections:")
header = """Answer the question as truthfully as possible using the provided context,
and if the answer is not contained within the text below, say "I don't know."
Answer in a very sarcastic tone and make it fun! Surprise the user with your answers. You can give long answers tangentially related to the movie.\n
You are GPTflix, a AI movie-buff that loves talking about movies!\n
Context:\n
"""
return header + "".join(chosen_sections)
#TO BE ADDED: memory with summary of past discussions
def summarize_past_conversation(content):
APPEND_COMPLETION_PARAMS = {
"temperature": 0.0,
"max_tokens": 300,
"model": COMPLETIONS_MODEL,
}
prompt = "Summarize this discussion into a single paragraph keeping the titles of any movies mentioned: \n" + content
try:
response = openai.Completion.create(
prompt=prompt,
**APPEND_COMPLETION_PARAMS
)
except Exception as e:
print("I'm afraid your question failed! This is the error: ")
print(e)
return None
choices = response.get("choices", [])
if len(choices) > 0:
return choices[0]["text"].strip(" \n")
else:
return None
COMPLETIONS_API_PARAMS = {
"temperature": 0.0,
"max_tokens": 500,
"model": COMPLETIONS_MODEL,
}
def answer_query_with_context_pinecone(query):
prompt = construct_prompt_pinecone(query) + "\n\n Q: " + query + "\n A:"
print("---------------------------------------------")
print("prompt:")
print(prompt)
print("---------------------------------------------")
try:
response = openai.ChatCompletion.create(
messages=[{"role": "system", "content": "You are a helpful AI who loves movies."},
{"role": "user", "content": str(prompt)}],
# {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
# {"role": "user", "content": "Where was it played?"}
# ]
**COMPLETIONS_API_PARAMS
)
except Exception as e:
print("I'm afraid your question failed! This is the error: ")
print(e)
return None
choices = response.get("choices", [])
if len(choices) > 0:
return choices[0]["message"]["content"].strip(" \n")
else:
return None
# Storing the chat
if 'generated' not in st.session_state:
st.session_state['generated'] = []
if 'past' not in st.session_state:
st.session_state['past'] = []
def clear_text():
st.session_state["input"] = ""
# We will get the user's input by calling the get_text function
def get_text():
input_text = st.text_input("Input a question here! For example: \"Is X movie good?\". \n It works best if your question contains the title of a movie! You might want to be really specific, like talking about Pixar's Brave rather than just Brave. Also, I have no memory of previous questions!😅😊","Who are you?", key="input")
return input_text
user_input = get_text()
if user_input:
output = answer_query_with_context_pinecone(user_input)
# store the output
st.session_state.past.append(user_input)
st.session_state.generated.append(output)
if st.session_state['generated']:
for i in range(len(st.session_state['generated'])-1, -1, -1):
message(st.session_state["generated"][i],seed=bott_av , key=str(i))
message(st.session_state['past'][i], is_user=True,avatar_style="adventurer",seed=user_av, key=str(i) + '_user')