Spaces:
Sleeping
Sleeping
angelesteban00
commited on
Commit
•
ef24768
1
Parent(s):
1054da1
- app.py +75 -4
- app_old.py +7 -0
app.py
CHANGED
@@ -1,7 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pymongo import MongoClient
|
2 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
3 |
+
from langchain.vectorstores import MongoDBAtlasVectorSearch
|
4 |
+
from langchain.document_loaders import DirectoryLoader
|
5 |
+
from langchain.llms import OpenAI
|
6 |
+
from langchain.chains import RetrievalQA
|
7 |
import gradio as gr
|
8 |
+
from gradio.themes.base import Base
|
9 |
+
#import key_param
|
10 |
+
import os
|
11 |
|
12 |
+
mongo_uri = os.getenv("MONGO_URI")
|
13 |
+
openai_api_key = os.getenv("OPENAI_API_KEY")
|
14 |
|
15 |
+
client = MongoClient(mongo_uri)
|
16 |
+
dbName = "langchain_demo"
|
17 |
+
collectionName = "collection_of_text_blobs"
|
18 |
+
collection = client[dbName][collectionName]
|
19 |
+
|
20 |
+
# Define the text embedding model
|
21 |
+
embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
|
22 |
+
|
23 |
+
# Initialize the Vector Store
|
24 |
+
vectorStore = MongoDBAtlasVectorSearch( collection, embeddings, index_name="default" )
|
25 |
+
|
26 |
+
def query_data(query):
|
27 |
+
# Convert question to vector using OpenAI embeddings
|
28 |
+
# Perform Atlas Vector Search using Langchain's vectorStore
|
29 |
+
# similarity_search returns MongoDB documents most similar to the query
|
30 |
+
|
31 |
+
docs = vectorStore.similarity_search(query, K=1)
|
32 |
+
as_output = docs[0].page_content
|
33 |
+
|
34 |
+
# Leveraging Atlas Vector Search paired with Langchain's QARetriever
|
35 |
+
|
36 |
+
# Define the LLM that we want to use -- note that this is the Language Generation Model and NOT an Embedding Model
|
37 |
+
# If it's not specified (for example like in the code below),
|
38 |
+
# then the default OpenAI model used in LangChain is OpenAI GPT-3.5-turbo, as of August 30, 2023
|
39 |
+
|
40 |
+
llm = OpenAI(openai_api_key=openai_api_key, temperature=0)
|
41 |
+
|
42 |
+
|
43 |
+
# Get VectorStoreRetriever: Specifically, Retriever for MongoDB VectorStore.
|
44 |
+
# Implements _get_relevant_documents which retrieves documents relevant to a query.
|
45 |
+
retriever = vectorStore.as_retriever()
|
46 |
+
|
47 |
+
# Load "stuff" documents chain. Stuff documents chain takes a list of documents,
|
48 |
+
# inserts them all into a prompt and passes that prompt to an LLM.
|
49 |
+
|
50 |
+
qa = RetrievalQA.from_chain_type(llm, chain_type="stuff", retriever=retriever)
|
51 |
+
|
52 |
+
# Execute the chain
|
53 |
+
|
54 |
+
retriever_output = qa.run(query)
|
55 |
+
|
56 |
+
|
57 |
+
# Return Atlas Vector Search output, and output generated using RAG Architecture
|
58 |
+
return as_output, retriever_output
|
59 |
+
|
60 |
+
# Create a web interface for the app, using Gradio
|
61 |
+
|
62 |
+
with gr.Blocks(theme=Base(), title="Question Answering App using Vector Search + RAG") as demo:
|
63 |
+
gr.Markdown(
|
64 |
+
"""
|
65 |
+
# Question Answering App using Atlas Vector Search + RAG Architecture
|
66 |
+
""")
|
67 |
+
textbox = gr.Textbox(label="Enter your Question:")
|
68 |
+
with gr.Row():
|
69 |
+
button = gr.Button("Submit", variant="primary")
|
70 |
+
with gr.Column():
|
71 |
+
output1 = gr.Textbox(lines=1, max_lines=10, label="Output with just Atlas Vector Search (returns text field as is):")
|
72 |
+
output2 = gr.Textbox(lines=1, max_lines=10, label="Output generated by chaining Atlas Vector Search to Langchain's RetrieverQA + OpenAI LLM:")
|
73 |
+
|
74 |
+
# Call query_data function upon clicking the Submit button
|
75 |
+
|
76 |
+
button.click(query_data, textbox, outputs=[output1, output2])
|
77 |
+
|
78 |
+
demo.launch()
|
app_old.py
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
def greet(name):
|
4 |
+
return "Hola " + name + "!!"
|
5 |
+
|
6 |
+
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
7 |
+
iface.launch()
|