Spaces:
Paused
Paused
Releasing ChatLGONoData
Browse files- Dockerfile +11 -0
- app.py +108 -0
- chainlit.md +11 -0
- requirements.txt +4 -0
Dockerfile
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9
|
| 2 |
+
RUN useradd -m -u 1000 user
|
| 3 |
+
USER user
|
| 4 |
+
ENV HOME=/home/user \
|
| 5 |
+
PATH=/home/user/.local/bin:$PATH
|
| 6 |
+
WORKDIR $HOME/app
|
| 7 |
+
COPY --chown=user . $HOME/app
|
| 8 |
+
COPY ./requirements.txt ~/app/requirements.txt
|
| 9 |
+
RUN pip install -r requirements.txt
|
| 10 |
+
COPY . .
|
| 11 |
+
CMD ["chainlit", "run", "app.py", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from typing import List
|
| 3 |
+
|
| 4 |
+
import chainlit as cl
|
| 5 |
+
|
| 6 |
+
from llama_index.callbacks.base import CallbackManager
|
| 7 |
+
from llama_index import (
|
| 8 |
+
ServiceContext,
|
| 9 |
+
StorageContext,
|
| 10 |
+
load_index_from_storage,
|
| 11 |
+
)
|
| 12 |
+
from llama_index.llms import OpenAI
|
| 13 |
+
from llama_index.postprocessor.cohere_rerank import CohereRerank
|
| 14 |
+
from llama_index.tools import QueryEngineTool, ToolMetadata
|
| 15 |
+
from llama_index.query_engine import SubQuestionQueryEngine
|
| 16 |
+
from llama_index.embeddings import HuggingFaceEmbedding
|
| 17 |
+
from chainlit.types import AskFileResponse
|
| 18 |
+
from llama_index import download_loader
|
| 19 |
+
|
| 20 |
+
print("Loading Storage Context...")
|
| 21 |
+
storage_context = StorageContext.from_defaults(persist_dir="index/")
|
| 22 |
+
print("Loading Index...")
|
| 23 |
+
index = load_index_from_storage(storage_context)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def process_file(file: AskFileResponse):
|
| 27 |
+
import tempfile
|
| 28 |
+
|
| 29 |
+
with tempfile.NamedTemporaryFile(mode="w", delete=False) as tempfile:
|
| 30 |
+
with open(tempfile.name, "wb") as f:
|
| 31 |
+
f.write(file.content)
|
| 32 |
+
|
| 33 |
+
PDFReader = download_loader("PDFReader")
|
| 34 |
+
|
| 35 |
+
loader = PDFReader()
|
| 36 |
+
|
| 37 |
+
documents = loader.load_data(tempfile.name)
|
| 38 |
+
return documents
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
@cl.on_chat_start
|
| 42 |
+
async def on_chat_start():
|
| 43 |
+
files = None
|
| 44 |
+
|
| 45 |
+
# Wait for the user to upload a file
|
| 46 |
+
while files == None:
|
| 47 |
+
files = await cl.AskFileMessage(
|
| 48 |
+
content="Please upload a PDF file to begin!",
|
| 49 |
+
accept=["application/pdf"],
|
| 50 |
+
max_size_mb=20,
|
| 51 |
+
timeout=180,
|
| 52 |
+
).send()
|
| 53 |
+
|
| 54 |
+
file = files[0]
|
| 55 |
+
|
| 56 |
+
msg = cl.Message(
|
| 57 |
+
content=f"Processing `{file.name}`...", disable_human_feedback=True
|
| 58 |
+
)
|
| 59 |
+
await msg.send()
|
| 60 |
+
|
| 61 |
+
# load the file
|
| 62 |
+
documents = process_file(file)
|
| 63 |
+
|
| 64 |
+
index = await cl.make_async(index.add_documents)(documents)
|
| 65 |
+
|
| 66 |
+
llm = OpenAI(model="gpt-4-1106-preview", temperature=0)
|
| 67 |
+
|
| 68 |
+
embed_model = HuggingFaceEmbedding(model_name="ai-maker-space/chatlgo-finetuned")
|
| 69 |
+
|
| 70 |
+
service_context = ServiceContext.from_defaults(
|
| 71 |
+
embed_model=embed_model,
|
| 72 |
+
llm=llm,
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
cohere_rerank = CohereRerank(top_n=5)
|
| 76 |
+
|
| 77 |
+
query_engine = index.as_query_engine(
|
| 78 |
+
similarity_top_k=10,
|
| 79 |
+
node_postprocessors=[cohere_rerank],
|
| 80 |
+
service_context=service_context,
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
query_engine_tools = [
|
| 84 |
+
QueryEngineTool(
|
| 85 |
+
query_engine=query_engine,
|
| 86 |
+
metadata=ToolMetadata(
|
| 87 |
+
name="mit_theses",
|
| 88 |
+
description="A collection of MIT theses.",
|
| 89 |
+
),
|
| 90 |
+
),
|
| 91 |
+
]
|
| 92 |
+
|
| 93 |
+
query_engine = SubQuestionQueryEngine.from_defaults(
|
| 94 |
+
query_engine_tools=query_engine_tools,
|
| 95 |
+
service_context=service_context,
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
cl.user_session.set("query_engine", query_engine)
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
@cl.on_message
|
| 102 |
+
async def main(message: cl.Message):
|
| 103 |
+
query_engine = cl.user_session.get("query_engine")
|
| 104 |
+
response = await cl.make_async(query_engine.query)(message.content)
|
| 105 |
+
|
| 106 |
+
response_message = cl.Message(content=str(response))
|
| 107 |
+
|
| 108 |
+
await response_message.send()
|
chainlit.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Welcome to ChatLGO
|
| 2 |
+
|
| 3 |
+
In this space we'll examine a chat application that is able to query a large repository of LGO Theses using a combination of tools provided through the LlamaIndex library.
|
| 4 |
+
|
| 5 |
+
We'll be leveraging:
|
| 6 |
+
|
| 7 |
+
- [Fine-tuned Embedding Model](https://huggingface.co/ai-maker-space/chatlgo-finetuned)
|
| 8 |
+
- [Cohere's Reranking Service](https://cohere.com/rerank)
|
| 9 |
+
- [Sub Query Query Engine](https://docs.llamaindex.ai/en/stable/examples/query_engine/sub_question_query_engine.html)
|
| 10 |
+
|
| 11 |
+
This should enable us to retrieve documents with expanded context generated by our LLM!
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
tiktoken
|
| 2 |
+
chainlit
|
| 3 |
+
openai
|
| 4 |
+
llamaindex
|