| 1 | import pickle |
| 2 | import re |
| 3 | import zipfile |
| 4 | from pathlib import Path |
| 5 | |
| 6 | import requests |
| 7 | from langchain.chains import RetrievalQA |
| 8 | from langchain.document_loaders import UnstructuredHTMLLoader |
| 9 | from langchain.embeddings import OpenAIEmbeddings |
| 10 | from langchain.llms import OpenAI |
| 11 | from langchain.text_splitter import RecursiveCharacterTextSplitter |
| 12 | from langchain.vectorstores import LanceDB |
| 13 | from modal import Image, Secret, Stub, web_endpoint |
| 14 | |
| 15 | import lancedb |
| 16 | |
| 17 | lancedb_image = Image.debian_slim().pip_install( |
| 18 | "lancedb", "langchain", "openai", "pandas", "tiktoken", "unstructured", "tabulate" |
| 19 | ) |
| 20 | |
| 21 | stub = Stub( |
| 22 | name="example-langchain-lancedb", |
| 23 | image=lancedb_image, |
| 24 | secrets=[Secret.from_name("my-openai-secret")], |
| 25 | ) |
| 26 | |
| 27 | docsearch = None |
| 28 | docs_path = Path("docs.pkl") |
| 29 | db_path = Path("lancedb") |
| 30 | |
| 31 | |
| 32 | def get_document_title(document): |
| 33 | m = str(document.metadata["source"]) |
| 34 | title = re.findall("pandas.documentation(.*).html", m) |
| 35 | if title[0] is not None: |
| 36 | return title[0] |
| 37 | return "" |
| 38 | |
| 39 | |
| 40 | def download_docs(): |
| 41 | pandas_docs = requests.get( |
| 42 | "https://eto-public.s3.us-west-2.amazonaws.com/datasets/pandas_docs/pandas.documentation.zip" |
| 43 | ) |
| 44 | with open(Path("pandas.documentation.zip"), "wb") as f: |
| 45 | f.write(pandas_docs.content) |
| 46 | |
| 47 | file = zipfile.ZipFile(Path("pandas.documentation.zip")) |
| 48 | file.extractall(path=Path("pandas_docs")) |
| 49 | |
| 50 | |
| 51 | def store_docs(): |
| 52 | docs = [] |
| 53 | |
| 54 | if not docs_path.exists(): |
| 55 | for p in Path("pandas_docs/pandas.documentation").rglob("*.html"): |
| 56 | if p.is_dir(): |
| 57 | continue |
| 58 | loader = UnstructuredHTMLLoader(p) |
| 59 | raw_document = loader.load() |
| 60 | |
| 61 | m = {} |
| 62 | m["title"] = get_document_title(raw_document[0]) |
| 63 | m["version"] = "2.0rc0" |
| 64 | raw_document[0].metadata = raw_document[0].metadata | m |
| 65 | raw_document[0].metadata["source"] = str(raw_document[0].metadata["source"]) |
| 66 | docs = docs + raw_document |
| 67 | |
| 68 | with docs_path.open("wb") as fh: |
| 69 | pickle.dump(docs, fh) |
| 70 | else: |
| 71 | with docs_path.open("rb") as fh: |
| 72 | docs = pickle.load(fh) |
| 73 | |
| 74 | return docs |
| 75 | |
| 76 | |
| 77 | def qanda_langchain(query): |
| 78 | download_docs() |
| 79 | docs = store_docs() |
| 80 | |
| 81 | text_splitter = RecursiveCharacterTextSplitter( |
| 82 | chunk_size=1000, |
| 83 | chunk_overlap=200, |
| 84 | ) |
| 85 | documents = text_splitter.split_documents(docs) |
| 86 | embeddings = OpenAIEmbeddings() |
| 87 | |
| 88 | db = lancedb.connect(db_path) |
| 89 | table = db.create_table( |
| 90 | "pandas_docs", |
| 91 | data=[ |
| 92 | { |
| 93 | "vector": embeddings.embed_query("Hello World"), |
| 94 | "text": "Hello World", |
| 95 | "id": "1", |
| 96 | } |
| 97 | ], |
| 98 | mode="overwrite", |
| 99 | ) |
| 100 | docsearch = LanceDB.from_documents(documents, embeddings, connection=table) |
| 101 | qa = RetrievalQA.from_chain_type( |
| 102 | llm=OpenAI(), chain_type="stuff", retriever=docsearch.as_retriever() |
| 103 | ) |
| 104 | return qa.run(query) |
| 105 | |
| 106 | |
| 107 | @stub.function() |
| 108 | @web_endpoint(method="GET") |
| 109 | def web(query: str): |
| 110 | answer = qanda_langchain(query) |
| 111 | return { |
| 112 | "answer": answer, |
| 113 | } |
| 114 | |
| 115 | |
| 116 | @stub.function() |
| 117 | def cli(query: str): |
| 118 | answer = qanda_langchain(query) |
| 119 | print(answer) |