Spaces:
Sleeping
Sleeping
guangliang.yin
commited on
Commit
β’
40cdb6a
1
Parent(s):
b8cfb8a
init app code
Browse files- app.py +123 -0
- requirements.txt +6 -0
app.py
ADDED
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Callable, Optional
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
5 |
+
from langchain.vectorstores import Zilliz
|
6 |
+
from langchain.document_loaders import WebBaseLoader
|
7 |
+
from langchain.text_splitter import CharacterTextSplitter
|
8 |
+
from langchain.chains import RetrievalQAWithSourcesChain
|
9 |
+
from langchain.llms import OpenAI
|
10 |
+
|
11 |
+
chain: Optional[Callable] = None
|
12 |
+
|
13 |
+
|
14 |
+
def web_loader(url_list, openai_key, zilliz_uri, user, password):
|
15 |
+
if not url_list:
|
16 |
+
return "please enter url list"
|
17 |
+
loader = WebBaseLoader(url_list.split())
|
18 |
+
docs = loader.load()
|
19 |
+
|
20 |
+
text_splitter = CharacterTextSplitter(chunk_size=1024, chunk_overlap=0)
|
21 |
+
docs = text_splitter.split_documents(docs)
|
22 |
+
embeddings = OpenAIEmbeddings(model="ada", openai_api_key=openai_key)
|
23 |
+
|
24 |
+
docsearch = Zilliz.from_documents(
|
25 |
+
docs,
|
26 |
+
embedding=embeddings,
|
27 |
+
connection_args={
|
28 |
+
"uri": zilliz_uri,
|
29 |
+
"user": user,
|
30 |
+
"password": password,
|
31 |
+
"secure": True,
|
32 |
+
},
|
33 |
+
)
|
34 |
+
|
35 |
+
global chain
|
36 |
+
chain = RetrievalQAWithSourcesChain.from_chain_type(
|
37 |
+
OpenAI(temperature=0, openai_api_key=openai_key),
|
38 |
+
chain_type="map_reduce",
|
39 |
+
retriever=docsearch.as_retriever(),
|
40 |
+
)
|
41 |
+
return "success to load data"
|
42 |
+
|
43 |
+
|
44 |
+
def query(question):
|
45 |
+
global chain
|
46 |
+
# "What is milvus?"
|
47 |
+
if not chain:
|
48 |
+
return "please load the data first"
|
49 |
+
return chain(inputs={"question": question}, return_only_outputs=True).get(
|
50 |
+
"answer", "fail to get answer"
|
51 |
+
)
|
52 |
+
|
53 |
+
|
54 |
+
if __name__ == "__main__":
|
55 |
+
block = gr.Blocks()
|
56 |
+
with block as demo:
|
57 |
+
gr.Markdown(
|
58 |
+
"""
|
59 |
+
<h1><center>Langchain And Zilliz Cloud Example</center></h1>
|
60 |
+
This is how to use Zilliz Cloud as vector store in LangChain.
|
61 |
+
The purpose of this example is to allow you to input multiple URLs (separated by newlines) and then ask questions about the content of the corresponding web pages.
|
62 |
+
|
63 |
+
## π Prerequisite:
|
64 |
+
|
65 |
+
1. π To obtain an OpenAI key, please visit https://platform.openai.com/account/api-keys.
|
66 |
+
2. π» Create a Zilliz Cloud account to get free credits for usage by visiting https://cloud.zilliz.com.
|
67 |
+
3. ποΈ Create a database in Zilliz Cloud.
|
68 |
+
|
69 |
+
## π Steps for usage:
|
70 |
+
|
71 |
+
1. ποΈ Fill in the url list input box with multiple URLs.
|
72 |
+
2. π Fill in the OpenAI API key in the openai api key input box.
|
73 |
+
3. π©οΈ Fill in the Zilliz Cloud connection parameters, including the connection URL, corresponding username, and password.
|
74 |
+
4. π Click the Load Data button to load the data. When the load status text box prompts that the data has been successfully loaded, proceed to the next step.
|
75 |
+
5. β In the question input box, enter the relevant question about the web page.
|
76 |
+
6. π Click the Generate button to search for the answer to the question. The final answer will be displayed in the question answer text box.
|
77 |
+
"""
|
78 |
+
)
|
79 |
+
url_list_text = gr.Textbox(
|
80 |
+
label="url list",
|
81 |
+
lines=3,
|
82 |
+
placeholder="https://milvus.io/docs/overview.md",
|
83 |
+
)
|
84 |
+
openai_key_text = gr.Textbox(label="openai api key", type="password", placeholder="sk-******")
|
85 |
+
with gr.Row():
|
86 |
+
zilliz_uri_text = gr.Textbox(
|
87 |
+
label="zilliz cloud uri",
|
88 |
+
placeholder="https://<instance-id>.<cloud-region-id>.vectordb.zillizcloud.com:<port>",
|
89 |
+
)
|
90 |
+
user_text = gr.Textbox(label="username", placeholder="db_admin")
|
91 |
+
password_text = gr.Textbox(
|
92 |
+
label="password", type="password", placeholder="******"
|
93 |
+
)
|
94 |
+
loader_output = gr.Textbox(label="load status")
|
95 |
+
loader_btn = gr.Button("Load Data")
|
96 |
+
loader_btn.click(
|
97 |
+
fn=web_loader,
|
98 |
+
inputs=[
|
99 |
+
url_list_text,
|
100 |
+
openai_key_text,
|
101 |
+
zilliz_uri_text,
|
102 |
+
user_text,
|
103 |
+
password_text,
|
104 |
+
],
|
105 |
+
outputs=loader_output,
|
106 |
+
api_name="web_load",
|
107 |
+
)
|
108 |
+
|
109 |
+
question_text = gr.Textbox(
|
110 |
+
label="question",
|
111 |
+
lines=3,
|
112 |
+
placeholder="What is milvus?",
|
113 |
+
)
|
114 |
+
query_output = gr.Textbox(label="question answer", lines=3)
|
115 |
+
query_btn = gr.Button("Generate")
|
116 |
+
query_btn.click(
|
117 |
+
fn=query,
|
118 |
+
inputs=[question_text],
|
119 |
+
outputs=query_output,
|
120 |
+
api_name="generate_answer",
|
121 |
+
)
|
122 |
+
|
123 |
+
demo.queue().launch(server_name="0.0.0.0", share=False)
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
pymilvus
|
2 |
+
langchain
|
3 |
+
openai
|
4 |
+
tiktoken
|
5 |
+
gradio
|
6 |
+
bs4
|