Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -15,7 +15,7 @@ output_parser = StrOutputParser()
|
|
15 |
import json
|
16 |
|
17 |
|
18 |
-
## Connect to MongoDB Atlas
|
19 |
MONGODB_ATLAS_CLUSTER_URI = os.getenv('MONGODB_ATLAS_CLUSTER_URI')
|
20 |
client = MongoClient(MONGODB_ATLAS_CLUSTER_URI)
|
21 |
db_name = 'sample_mflix'
|
@@ -23,12 +23,17 @@ collection_name = 'embedded_movies'
|
|
23 |
collection = client[db_name][collection_name]
|
24 |
|
25 |
try:
|
|
|
26 |
vector_store = MongoDBAtlasVectorSearch(embedding=OpenAIEmbeddings(), collection=collection, index_name='vector_index', text_key='plot', embedding_key='plot_embedding')
|
|
|
|
|
27 |
llm = ChatOpenAI(temperature=0)
|
28 |
prompt = ChatPromptTemplate.from_messages([
|
29 |
("system", "You are a movie recommendation engine which post a concise and short summary on relevant movies."),
|
30 |
("user", "List of movies: {input}")
|
31 |
])
|
|
|
|
|
32 |
chain = prompt | llm | output_parser
|
33 |
|
34 |
except:
|
@@ -39,20 +44,23 @@ except:
|
|
39 |
def get_movies(message, history):
|
40 |
|
41 |
try:
|
|
|
42 |
movies = vector_store.similarity_search(message, 3)
|
43 |
return_text = ''
|
44 |
for movie in movies:
|
45 |
return_text = return_text + 'Title : ' + movie.metadata['title'] + '\n------------\n' + 'Plot: ' + movie.page_content + '\n\n'
|
46 |
-
|
|
|
47 |
print_llm_text = chain.invoke({"input": return_text})
|
48 |
|
49 |
for i in range(len(print_llm_text)):
|
50 |
time.sleep(0.05)
|
51 |
yield "Found: " + "\n\n" + print_llm_text[: i+1]
|
52 |
except:
|
53 |
-
yield "Please clone the
|
54 |
|
55 |
|
|
|
56 |
demo = gr.ChatInterface(get_movies, examples=["What movies are scary?", "Find me a comedy", "Movies for kids"], title="Movies Atlas Vector Search",description="This small chat uses a similarity search to find relevant movies, it uses an MongoDB Atlase Vector Search read more here: https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-tutorial",submit_btn="Search").queue()
|
57 |
|
58 |
if __name__ == "__main__":
|
|
|
15 |
import json
|
16 |
|
17 |
|
18 |
+
## Connect to MongoDB Atlas
|
19 |
MONGODB_ATLAS_CLUSTER_URI = os.getenv('MONGODB_ATLAS_CLUSTER_URI')
|
20 |
client = MongoClient(MONGODB_ATLAS_CLUSTER_URI)
|
21 |
db_name = 'sample_mflix'
|
|
|
23 |
collection = client[db_name][collection_name]
|
24 |
|
25 |
try:
|
26 |
+
## Vector store init
|
27 |
vector_store = MongoDBAtlasVectorSearch(embedding=OpenAIEmbeddings(), collection=collection, index_name='vector_index', text_key='plot', embedding_key='plot_embedding')
|
28 |
+
|
29 |
+
## LLM init
|
30 |
llm = ChatOpenAI(temperature=0)
|
31 |
prompt = ChatPromptTemplate.from_messages([
|
32 |
("system", "You are a movie recommendation engine which post a concise and short summary on relevant movies."),
|
33 |
("user", "List of movies: {input}")
|
34 |
])
|
35 |
+
|
36 |
+
## RAG Chain
|
37 |
chain = prompt | llm | output_parser
|
38 |
|
39 |
except:
|
|
|
44 |
def get_movies(message, history):
|
45 |
|
46 |
try:
|
47 |
+
### Get top 3 picks
|
48 |
movies = vector_store.similarity_search(message, 3)
|
49 |
return_text = ''
|
50 |
for movie in movies:
|
51 |
return_text = return_text + 'Title : ' + movie.metadata['title'] + '\n------------\n' + 'Plot: ' + movie.page_content + '\n\n'
|
52 |
+
|
53 |
+
## Invoke RAG on the located documents
|
54 |
print_llm_text = chain.invoke({"input": return_text})
|
55 |
|
56 |
for i in range(len(print_llm_text)):
|
57 |
time.sleep(0.05)
|
58 |
yield "Found: " + "\n\n" + print_llm_text[: i+1]
|
59 |
except:
|
60 |
+
yield "Please clone the space and add your open ai key as well as your MongoDB Atlas URI in the Secret Section of you Space\n OPENAI_API_KEY (your Open AI key) and MONGODB_ATLAS_CLUSTER_URI (0.0.0.0/0 whitelisted instance with Vector index created) \n\n For more information : https://mongodb.com/products/platform/atlas-vector-search"
|
61 |
|
62 |
|
63 |
+
## Start gradio chat interface
|
64 |
demo = gr.ChatInterface(get_movies, examples=["What movies are scary?", "Find me a comedy", "Movies for kids"], title="Movies Atlas Vector Search",description="This small chat uses a similarity search to find relevant movies, it uses an MongoDB Atlase Vector Search read more here: https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-tutorial",submit_btn="Search").queue()
|
65 |
|
66 |
if __name__ == "__main__":
|