Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,8 +6,12 @@ from pymongo import MongoClient
|
|
6 |
from langchain_community.vectorstores import MongoDBAtlasVectorSearch
|
7 |
from langchain_openai import OpenAIEmbeddings
|
8 |
from langchain_community.llms import OpenAI
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
11 |
import json
|
12 |
|
13 |
|
@@ -18,49 +22,38 @@ db_name = 'sample_mflix'
|
|
18 |
collection_name = 'embedded_movies'
|
19 |
collection = client[db_name][collection_name]
|
20 |
|
21 |
-
## Create a vector search index
|
22 |
-
print ('Creating vector search index')
|
23 |
-
# collection.create_search_index(model={"definition": {"mappings":{
|
24 |
-
# "dynamic":True,
|
25 |
-
# "fields": {
|
26 |
-
# "plot_embedding": {
|
27 |
-
# "type": "knnVector",
|
28 |
-
# "dimensions": 1536,
|
29 |
-
# "similarity": "euclidean"
|
30 |
-
# }
|
31 |
-
# }
|
32 |
-
# }}, "name":'default'})
|
33 |
-
|
34 |
-
# sleep for minute
|
35 |
-
# print ('Waiting for vector index on field "embedding" to be created')
|
36 |
-
# time.sleep(60)
|
37 |
-
|
38 |
try:
|
39 |
vector_store = MongoDBAtlasVectorSearch(embedding=OpenAIEmbeddings(), collection=collection, index_name='vector_index', text_key='plot', embedding_key='plot_embedding')
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
except:
|
42 |
-
#
|
43 |
print ('Open AI key is wrong')
|
44 |
vector_store = None
|
45 |
|
46 |
def get_movies(message, history):
|
47 |
-
|
48 |
-
# movies = vector_store.similarity_search(message, 3)
|
49 |
-
print ('Searching for: ' + message)
|
50 |
try:
|
51 |
movies = vector_store.similarity_search(message, 3)
|
52 |
-
|
53 |
for movie in movies:
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
57 |
time.sleep(0.05)
|
58 |
-
yield "Found: " + "\n\n" +
|
59 |
except:
|
60 |
-
yield "Please clone the repo and add your open ai key as well as your MongoDB Atlas
|
61 |
|
62 |
|
63 |
-
demo = gr.ChatInterface(get_movies, examples=["What movies are scary?", "Find me a comedy", "Movies for kids"],
|
64 |
|
65 |
if __name__ == "__main__":
|
66 |
demo.launch()
|
|
|
6 |
from langchain_community.vectorstores import MongoDBAtlasVectorSearch
|
7 |
from langchain_openai import OpenAIEmbeddings
|
8 |
from langchain_community.llms import OpenAI
|
9 |
+
from langchain_openai import ChatOpenAI
|
10 |
+
from langchain_core.prompts import ChatPromptTemplate
|
11 |
+
from langchain_core.output_parsers import StrOutputParser
|
12 |
+
|
13 |
+
output_parser = StrOutputParser()
|
14 |
+
|
15 |
import json
|
16 |
|
17 |
|
|
|
22 |
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:
|
35 |
+
#If open ai key is wrong
|
36 |
print ('Open AI key is wrong')
|
37 |
vector_store = None
|
38 |
|
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 repo 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"
|
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__":
|
59 |
demo.launch()
|