Spaces:
Runtime error
Runtime error
Upload 4 files
Browse filesAdded files from my local machine
- Dockerfile +11 -0
- app.py +144 -0
- nvidia_2tables.pdf +0 -0
- requirements.txt +14 -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,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# chainlit run app.py -w
|
2 |
+
# You can find this code for Chainlit python streaming here (https://docs.chainlit.io/concepts/streaming/python)
|
3 |
+
# OpenAI Chat completion
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
load_dotenv()
|
6 |
+
|
7 |
+
import os
|
8 |
+
import sys
|
9 |
+
import getpass
|
10 |
+
import nest_asyncio
|
11 |
+
# import pandas as pd
|
12 |
+
import faiss
|
13 |
+
import openai
|
14 |
+
|
15 |
+
import chainlit as cl # importing chainlit for our app
|
16 |
+
# https://docs.chainlit.io/api-reference/step-class#update-a-step
|
17 |
+
# DEPRICATED: from chainlit.prompt import Prompt, PromptMessage # importing prompt tools
|
18 |
+
|
19 |
+
import llama_index
|
20 |
+
from llama_index.core import Settings
|
21 |
+
from llama_index.core import VectorStoreIndex
|
22 |
+
from llama_index.core import StorageContext
|
23 |
+
from llama_index.vector_stores.faiss import FaissVectorStore
|
24 |
+
from llama_index.core import set_global_handler
|
25 |
+
from llama_index.core.node_parser import MarkdownElementNodeParser
|
26 |
+
from llama_index.llms.openai import OpenAI
|
27 |
+
from llama_index.embeddings.openai import OpenAIEmbedding
|
28 |
+
from llama_index.postprocessor.flag_embedding_reranker import FlagEmbeddingReranker
|
29 |
+
from llama_parse import LlamaParse
|
30 |
+
|
31 |
+
from openai import AsyncOpenAI # importing openai for API usage
|
32 |
+
|
33 |
+
os.environ["CUDA_VISIBLE_DEVICES"] = ""
|
34 |
+
# GET KEYS
|
35 |
+
LLAMA_CLOUD_API_KEY= os.getenv('LLAMA_CLOUD_API_KEY')
|
36 |
+
OPENAI_API_KEY=os.getenv("OPENAI_API_KEY")
|
37 |
+
|
38 |
+
"""
|
39 |
+
os.environ["LLAMA_CLOUD_API_KEY"] = getpass.getpass("LLamaParse API Key:")
|
40 |
+
os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:")
|
41 |
+
# os.environ["WANDB_API_KEY"] = getpass.getpass("WandB API Key: ")
|
42 |
+
"""
|
43 |
+
|
44 |
+
nest_asyncio.apply()
|
45 |
+
|
46 |
+
# PARSING the pdf file
|
47 |
+
parser = LlamaParse(
|
48 |
+
result_type="markdown",
|
49 |
+
verbose=True,
|
50 |
+
language="en",
|
51 |
+
num_workers=2,
|
52 |
+
)
|
53 |
+
|
54 |
+
nvidia_docs = parser.load_data(["./nvidia_2tables.pdf"])
|
55 |
+
# Note: nvidia_docs contains only one file (it could contain more). nvidia_docs[0] is the pdf we loaded.
|
56 |
+
print(nvidia_docs[0].text[:1000])
|
57 |
+
|
58 |
+
# Getting Settings out of llama_index.core which is a major part of their v0.10 update!
|
59 |
+
Settings.llm = OpenAI(model="gpt-3.5-turbo")
|
60 |
+
Settings.embed_model = OpenAIEmbedding(model="text-embedding-3-small")
|
61 |
+
|
62 |
+
# Using MarkdownElementNodeParser to help make sense of our Markdown objects so we can leverage the potentially structured information in the parsed documents.
|
63 |
+
|
64 |
+
node_parser = MarkdownElementNodeParser(llm=OpenAI(model="gpt-3.5-turbo"), num_workers=8)
|
65 |
+
|
66 |
+
nodes = node_parser.get_nodes_from_documents(documents=[nvidia_docs[0]])
|
67 |
+
# Let's see what's in the metadata of the nodes:
|
68 |
+
for nd in nodes:
|
69 |
+
print(nd.metadata)
|
70 |
+
for k,v in nd:
|
71 |
+
if k=='table_df':
|
72 |
+
print(nd)
|
73 |
+
# Now we extract our `base_nodes` and `objects` to create the `VectorStoreIndex`.
|
74 |
+
base_nodes, objects = node_parser.get_nodes_and_objects(nodes)
|
75 |
+
|
76 |
+
# We could use the VectorStoreIndex from llama_index.core
|
77 |
+
# Or we can use the llama_index FAISS llama-index-vector-stores-faiss
|
78 |
+
# Trying the faiss, and setting its vectors' dimension.
|
79 |
+
faiss_dim = 1536
|
80 |
+
faiss_index = faiss.IndexFlatL2(faiss_dim) # default param overwrite=False, so it will append new vector.
|
81 |
+
# Parameter overwrite=True suppresses appending a vector.
|
82 |
+
|
83 |
+
# Creating the FaissVectorStore and its recursicve_index_faiss
|
84 |
+
llama_faiss_vector_store = FaissVectorStore(faiss_index=faiss_index)
|
85 |
+
storage_context = StorageContext.from_defaults(vector_store=llama_faiss_vector_store)
|
86 |
+
recursive_index_faiss = VectorStoreIndex(nodes=base_nodes+objects, storage_context=storage_context)
|
87 |
+
|
88 |
+
# Now we can build our Recursive Query Engine with reranking!
|
89 |
+
|
90 |
+
# We'll need to do a couple steps:
|
91 |
+
# 1. Initalize our reranker using `FlagEmbeddingReranker` powered by the `BAAI/bge-reranker-large`.
|
92 |
+
# 2. Set up our recursive query engine!
|
93 |
+
|
94 |
+
reranker = FlagEmbeddingReranker(
|
95 |
+
top_n=5,
|
96 |
+
model="BAAI/bge-reranker-large",
|
97 |
+
)
|
98 |
+
|
99 |
+
recursive_query_engine = recursive_index_faiss.as_query_engine(
|
100 |
+
similarity_top_k=15,
|
101 |
+
node_postprocessors=[reranker],
|
102 |
+
verbose=True
|
103 |
+
)
|
104 |
+
|
105 |
+
"""
|
106 |
+
# Create pandas dataframe to store query+generated response+added truth
|
107 |
+
columns=["Query", "Response", "Truth"]
|
108 |
+
gen_df = pd.DataFrame(columns=columns,dtype='str')
|
109 |
+
"""
|
110 |
+
|
111 |
+
# ChatOpenAI Templates
|
112 |
+
system_template = """Use the following pieces of context to answer the user's question.
|
113 |
+
If you don't know the answer, say that you don't know, do not try to make up an answer.
|
114 |
+
ALWAYS return a "SOURCES" part in your answer.
|
115 |
+
The "SOURCES" part should be a reference to the source inside the document from which you got your answer.
|
116 |
+
You are a helpful assistant who always speaks in a pleasant tone! """
|
117 |
+
|
118 |
+
user_template = """ Think through your response step by step."""
|
119 |
+
|
120 |
+
#user_query = "Who are the E-VP, Operations - and how old are they?"
|
121 |
+
|
122 |
+
#response = recursive_query_engine.query(system_template + user_query + user_template)
|
123 |
+
|
124 |
+
#str_resp ="{}".format(response)
|
125 |
+
|
126 |
+
|
127 |
+
def retriever_resp(prompt):
|
128 |
+
import time
|
129 |
+
response = "this is my response"
|
130 |
+
time.sleep(5)
|
131 |
+
return response
|
132 |
+
|
133 |
+
@cl.on_message # marks a function that should be run each time the chatbot receives a message from a user
|
134 |
+
async def main(message: cl.Message):
|
135 |
+
settings = cl.user_session.get("settings")
|
136 |
+
|
137 |
+
user_query = message.content
|
138 |
+
# prompt = system_template+user_query+user_template
|
139 |
+
response = recursive_query_engine.query(system_template + user_query + user_template)
|
140 |
+
# response = retriever_resp(prompt)
|
141 |
+
# print("AAA",user_query)
|
142 |
+
str_resp ="{}".format(response)
|
143 |
+
msg = cl.Message(content= str_resp)
|
144 |
+
await msg.send()
|
nvidia_2tables.pdf
ADDED
Binary file (125 kB). View file
|
|
requirements.txt
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
chainlit==1.0.401
|
2 |
+
cohere==5.0.0a10
|
3 |
+
openai==1.14.1
|
4 |
+
python-dotenv==1.0.1
|
5 |
+
faiss-cpu==1.8.0
|
6 |
+
FlagEmbedding==1.2.5
|
7 |
+
llama-index==0.10.20
|
8 |
+
llama-index-vector-stores-faiss==0.1.2
|
9 |
+
llama-index-llms-openai==0.1.12
|
10 |
+
llama-index-embeddings-openai==0.1.6
|
11 |
+
llama-index-postprocessor-flag-embedding-reranker==0.1.2
|
12 |
+
llama-parse==0.3.9
|
13 |
+
# tiktoken==0.5.1
|
14 |
+
# nest-asyncio==1.6.0
|