bikrammaharjan commited on
Commit
85912b2
Β·
1 Parent(s): cf72f85

AIE1 midterm submission

Browse files
Files changed (5) hide show
  1. README.md +11 -10
  2. app.py +152 -0
  3. assests/nvidia-logo.png +0 -0
  4. chainlit.md +14 -0
  5. requirements.txt +9 -0
README.md CHANGED
@@ -1,11 +1,12 @@
1
- ---
2
- title: Aie1 Midterm
3
- emoji: πŸ“Š
4
- colorFrom: blue
5
- colorTo: gray
6
- sdk: docker
7
- pinned: false
8
- license: mit
9
- ---
10
 
11
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
1
+ ## Welcome to AIE-1 Midterm demo! πŸ—οΈ, 🚒, & πŸš€
 
 
 
 
 
 
 
 
2
 
3
+ Hello! πŸ‘‹ I'm Bikram, and I'm excited to share my midterm project for the AI Engineering bootcamp at AI Makerspace!
4
+
5
+ You can ask anything related to **NVIDIA 10-k Filings**
6
+
7
+ #### Sample Questionaire you can try πŸ‘¨β€πŸ’»πŸ‘©β€πŸ’»
8
+ - Who is the E-VP, Operations - and how old are they?
9
+ - What is the gross carrying amount of Total Amortizable Intangible Assets for Jan 29, 2023?
10
+ - What is the Total Number of Shares Purchased between December 25, 2023 - January 28, 2024?
11
+ - Summarize S&P 500 vs NVIDIA Corporation growth
12
+
app.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import os
3
+ import chainlit as cl
4
+ from langchain_openai import OpenAIEmbeddings
5
+
6
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
7
+
8
+ from langchain.retrievers import MultiQueryRetriever
9
+ from langchain.chains.combine_documents import create_stuff_documents_chain
10
+
11
+ from langchain_openai import ChatOpenAI
12
+ from langchain.prompts import ChatPromptTemplate
13
+ from langchain_community.vectorstores import FAISS
14
+ from langchain_community.document_loaders import PyMuPDFLoader
15
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
16
+
17
+ from langchain_openai import ChatOpenAI
18
+ from langchain.prompts.chat import ChatPromptTemplate
19
+ from langchain.chains import create_retrieval_chain
20
+
21
+
22
+ from dotenv import load_dotenv
23
+
24
+
25
+ load_dotenv()
26
+
27
+
28
+ # START CODE
29
+ openai_chat_model = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
30
+ embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
31
+
32
+
33
+ def process_file(file: str):
34
+
35
+ pypdf_loader = PyMuPDFLoader(file)
36
+ texts = pypdf_loader.load()
37
+
38
+ text_splitter = RecursiveCharacterTextSplitter(
39
+ chunk_size=700,
40
+ chunk_overlap=50
41
+ )
42
+
43
+ documents = text_splitter.split_documents(texts)
44
+
45
+ return documents
46
+
47
+
48
+ async def RAG_pipeline(question: str, documents: str):
49
+
50
+ template = """
51
+
52
+ Answer the question based only on the following context.
53
+
54
+ If you cannot answer the question with the context, please respond with 'I don't know, can you provide more context?'.
55
+
56
+ If the user question is not related to NVIDIA or NVIDIA 10-k Filings then please respond with "I can only help you with NVIDIA 10-k Filings report.\n\n".
57
+
58
+ Always answer in full sentence.
59
+
60
+ If a user says things like "Ok" or "thank you" or "thank you" or anything that is related to "phatic expressions" or "phatic communication" then respond with "No problem! Always happy to help."
61
+
62
+ Always end your sentence with "\n\n\n Thank you for asking. What else can I help you regarding NVIDIA 10-k Filings report?".
63
+
64
+ Context:
65
+ {context}
66
+
67
+ Question:
68
+ {input}
69
+ """
70
+
71
+ # Create prompt template
72
+ prompt = ChatPromptTemplate.from_template(template)
73
+
74
+ # Initialize FAISS vector store
75
+ vector_store = FAISS.from_documents(documents, embeddings)
76
+
77
+ # Initialize a retriever to retrieve similar context
78
+ retriever = vector_store.as_retriever()
79
+
80
+ # Initialize retriever using a multi-query approach with a language model.
81
+ retriever = MultiQueryRetriever.from_llm(
82
+ retriever=retriever, llm=openai_chat_model)
83
+
84
+ # Create a document chain using OpenAI chat model and a prompt
85
+ document_chain = create_stuff_documents_chain(openai_chat_model, prompt)
86
+
87
+ # Create a retrieval chain using a retriever and a document chain
88
+ retrieval_chain = create_retrieval_chain(retriever, document_chain)
89
+
90
+ # Send a request to OpenAI with the question
91
+ response = retrieval_chain.invoke({"input": question})
92
+
93
+ # Making sure we have 'answer' params so that we can give proper response
94
+ if 'answer' in response:
95
+ llm_answer = response['answer']
96
+ else:
97
+ llm_answer = '**EMPTY RESPONSE**'
98
+
99
+ print("llm_answer: ", llm_answer)
100
+
101
+ return llm_answer
102
+
103
+
104
+ @cl.on_chat_start # marks a function that will be executed at the start of a user session
105
+ async def start_chat():
106
+ settings = {
107
+ "model": "gpt-3.5-turbo",
108
+ "temperature": 0,
109
+ "max_tokens": 500,
110
+ "top_p": 1,
111
+ "frequency_penalty": 0,
112
+ "presence_penalty": 0,
113
+ }
114
+
115
+ print("A new chat session has started!")
116
+
117
+ # Process our document for tokenization
118
+ documents = process_file(
119
+ "https://d18rn0p25nwr6d.cloudfront.net/CIK-0001045810/1cbe8fe7-e08a-46e3-8dcc-b429fc06c1a4.pdf")
120
+
121
+
122
+ # Save session
123
+ cl.user_session.set("documents", documents)
124
+ cl.user_session.set("settings", settings)
125
+
126
+
127
+ @cl.on_message
128
+ async def main(message: cl.Message):
129
+
130
+ print("Human asked: ", message.content)
131
+
132
+ msg = cl.Message(content="")
133
+ await msg.send()
134
+
135
+ # do some work
136
+ await cl.sleep(2)
137
+
138
+ # Retrieve session
139
+ document_chunks = cl.user_session.get("documents")
140
+
141
+ # Wait for OpenAI to return a response and the good ol' RAG stuff
142
+ response = await RAG_pipeline(message.content, document_chunks)
143
+
144
+
145
+ # If there is a response then let the user know else fallback to else statement!
146
+ if response:
147
+ await cl.Message(content=response).send()
148
+ else:
149
+ cl.Message(
150
+ content="Something went wrong! please kindly refresh and try again 🀝").send()
151
+
152
+
assests/nvidia-logo.png ADDED
chainlit.md ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+ ## Welcome to AIE-1 Midterm demo! πŸ—οΈ, 🚒, & πŸš€
4
+
5
+ Hello! πŸ‘‹ I'm Bikram, and I'm excited to share my midterm project for the AI Engineering bootcamp at AI Makerspace!
6
+
7
+ You can ask anything related to **NVIDIA 10-k Filings**
8
+
9
+ #### Sample Questionaire you can try πŸ‘¨β€πŸ’»πŸ‘©β€πŸ’»
10
+ - Who is the E-VP, Operations - and how old are they?
11
+ - What is the gross carrying amount of Total Amortizable Intangible Assets for Jan 29, 2023?
12
+ - What is the Total Number of Shares Purchased between December 25, 2023 - January 28, 2024?
13
+ - Summarize S&P 500 vs NVIDIA Corporation growth
14
+
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ openai
2
+ langchain
3
+ langchain-openai
4
+ langchain_core
5
+ langchain-community
6
+ langchainhub
7
+ faiss_cpu
8
+ pymupdf
9
+ pandas