kartavya23 commited on
Commit
5f9e152
1 Parent(s): e50a1c7

Upload 3 files

Browse files
Files changed (4) hide show
  1. .gitattributes +1 -0
  2. 2401.08406v3.pdf +3 -0
  3. app.py +248 -0
  4. requirements.txt +0 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ 2401.08406v3.pdf filter=lfs diff=lfs merge=lfs -text
2401.08406v3.pdf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0d14009d8b28062d838bb11ac5cdfb2a3ce5135511bfb4fe343037d3b2309010
3
+ size 1786632
app.py ADDED
@@ -0,0 +1,248 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gc
3
+ import re
4
+ import uuid
5
+ import subprocess
6
+ import requests
7
+ from dotenv import load_dotenv
8
+
9
+ os.environ["HF_HOME"] = "weights"
10
+ os.environ["TORCH_HOME"] = "weights"
11
+ import streamlit as st
12
+
13
+ from llama_index.core import Settings
14
+ from llama_index.llms.ollama import Ollama
15
+ from llama_index.core import PromptTemplate
16
+ from llama_index.core import SimpleDirectoryReader
17
+ from llama_index.core import VectorStoreIndex
18
+ from llama_index.core.storage.storage_context import StorageContext
19
+
20
+ from langchain_community.embeddings import HuggingFaceEmbeddings
21
+ from llama_index.embeddings.langchain import LangchainEmbedding
22
+
23
+ from rag_101.retriever import (
24
+ load_embedding_model,
25
+ load_reranker_model
26
+ )
27
+
28
+ # setup the llm
29
+ ollama_url = 'http://localhost:11434/api/chat'
30
+
31
+ llm = Ollama(model="mistral:instruct", url=ollama_url ,request_timeout=1000.0)
32
+
33
+ # TODO: setup the embedding model
34
+ lc_embedding_model = load_embedding_model()
35
+ embed_model = LangchainEmbedding(lc_embedding_model)
36
+
37
+
38
+ # utility functions
39
+ def parse_github_url(url):
40
+ pattern = r"https://github\.com/([^/]+)/([^/]+)"
41
+ match = re.match(pattern, url)
42
+ return match.groups() if match else (None, None)
43
+
44
+
45
+ def clone_repo(repo_url):
46
+ try:
47
+ result = subprocess.run(["git", "clone", repo_url], check=True, text=True, capture_output=True)
48
+ print(result.stdout)
49
+ return result
50
+ except subprocess.CalledProcessError as e:
51
+ print(f"Error occurred: {e.stderr}")
52
+ raise e
53
+
54
+
55
+ def validate_owner_repo(owner, repo):
56
+ return bool(owner) and bool(repo)
57
+
58
+
59
+ if "id" not in st.session_state:
60
+ st.session_state.id = uuid.uuid4()
61
+ st.session_state.file_cache = {}
62
+
63
+ session_id = st.session_state.id
64
+ client = None
65
+
66
+
67
+ def reset_chat():
68
+ st.session_state.messages = []
69
+ st.session_state.context = None
70
+ gc.collect()
71
+
72
+
73
+ with st.sidebar:
74
+
75
+ # input for Github URL
76
+ github_url = st.text_input("Github Repository URL")
77
+
78
+ # button to load and process the github repository
79
+ process_button = st.button("Load")
80
+
81
+ message_container = st.empty() # placeholder for dynamic messages
82
+
83
+ if process_button and github_url:
84
+ owner, repo = parse_github_url(github_url)
85
+
86
+ if validate_owner_repo(owner, repo):
87
+ with st.spinner(f"Loading {repo} repository by {owner}..."):
88
+ try:
89
+ input_dir_path = f"./{repo}"
90
+
91
+ if not os.path.exists((input_dir_path)):
92
+ clone_repo(github_url)
93
+
94
+ if os.path.exists(input_dir_path):
95
+ loader = SimpleDirectoryReader(
96
+ input_dir=input_dir_path,
97
+ required_exts=[".py", ".ipynb", ".js", ".ts", ".md"],
98
+ recursive=True
99
+ )
100
+ else:
101
+ st.error('Error occurred while cloning the repo, carefully check the URL')
102
+ st.stop()
103
+
104
+ docs = loader.load_data()
105
+
106
+ # TODO: ====== Create vector store and upload data ======
107
+ Settings.embed_model = embed_model
108
+ index = VectorStoreIndex.from_documents(docs)
109
+
110
+ # setup a query engine
111
+ Settings.llm = llm
112
+ query_engine = index.as_query_engine(streaming=True, similarity_top_k=4)
113
+
114
+ # customize prompt template
115
+ qa_prompt_tmpl_str = (
116
+ "Context information is below.\n"
117
+ "---------------------\n"
118
+ "{context_str}\n"
119
+ "---------------------\n"
120
+ "Given the context information above I want you to think step by step to answer the query in a crisp manner, in case you don't know the answer say 'I don't know!'.\n"
121
+ "Query: {query_str}\n"
122
+ "Answer: "
123
+ )
124
+ qa_prompt_tmpl_str = PromptTemplate(qa_prompt_tmpl_str)
125
+
126
+ query_engine.update_prompts(
127
+ {"response_synthesizer:text_qa_template": qa_prompt_tmpl_str}
128
+ )
129
+
130
+ if docs:
131
+ message_container.success("Data loaded successfully!!")
132
+ else:
133
+ message_container.write(
134
+ "No Data found, check if repository is not empty!!"
135
+ )
136
+ st.session_state.query_engine = query_engine
137
+
138
+ except Exception as e:
139
+ st.error(f"An error occurred: {e}")
140
+ st.stop()
141
+
142
+ st.success("Ready to chat!")
143
+ else:
144
+ st.error('Invalid owner or repo')
145
+ st.stop()
146
+
147
+ col1, col2 = st.columns([6, 1])
148
+
149
+ with col1:
150
+ st.header(f"Chat with your code! </>")
151
+
152
+ with col2:
153
+ st.button("Clear ↺", on_click=reset_chat)
154
+
155
+ # Initialize chat history
156
+ if "messages" not in st.session_state:
157
+ reset_chat()
158
+
159
+ # Display chat messages from history on app rerun
160
+ for message in st.session_state.messages:
161
+ with st.chat_message(message["role"]):
162
+ st.markdown(message["content"])
163
+
164
+
165
+ # Accept user input
166
+ # TODO: old one
167
+ if prompt := st.chat_input("What's up?"):
168
+ # Add user message to chat history
169
+ st.session_state.messages.append({"role": "user", "content": prompt})
170
+ # Display user message in chat message container
171
+ with st.chat_message("user"):
172
+ st.markdown(prompt)
173
+
174
+ # Display assistant response in chat message container
175
+ with st.chat_message("assistant"):
176
+ message_placeholder = st.empty()
177
+ full_response = ""
178
+
179
+ # context = st.session_state.context
180
+ query_engine = st.session_state.query_engine
181
+
182
+ # Simulate stream of response with milliseconds delay
183
+ streaming_response = query_engine.query(prompt)
184
+
185
+ for chunk in streaming_response.response_gen:
186
+ full_response += chunk
187
+ message_placeholder.markdown(full_response + "▌")
188
+
189
+ # full_response = query_engine.query(prompt)
190
+
191
+ message_placeholder.markdown(full_response)
192
+ # st.session_state.context = ctx
193
+
194
+ # Add assistant response to chat history
195
+ st.session_state.messages.append({"role": "assistant", "content": full_response})
196
+
197
+ # todo: new one
198
+ # prompt = st.chat_input("What's up?")
199
+ # if prompt:
200
+ # # Add user message to chat history
201
+ # st.session_state.messages.append({"role": "user", "content": prompt})
202
+ #
203
+ # # Display user message in chat message container
204
+ # with st.chat_message("user"):
205
+ # st.markdown(prompt)
206
+ #
207
+ # # Display assistant response in chat message container
208
+ # with st.chat_message("assistant"):
209
+ # message_placeholder = st.empty()
210
+ # full_response = ""
211
+ #
212
+ # # context
213
+ # query_engine = st.session_state.query_engine
214
+ #
215
+ # # simulate stream of response with milliseconds delay
216
+ # try:
217
+ # # Construct the request payload
218
+ # payload = {
219
+ # "message": prompt,
220
+ # "model": "mistral:instruct"
221
+ # }
222
+ #
223
+ # # Send the request
224
+ # response = requests.post(ollama_url, json=payload)
225
+ #
226
+ # # Check for HTTP errors
227
+ # response.raise_for_status()
228
+ #
229
+ # # Print the full response to debug
230
+ # response_json = response.json()
231
+ # print(response_json)
232
+ #
233
+ # # Process the response
234
+ # if "response_gen" in response_json:
235
+ # for chunk in response_json["response_gen"]:
236
+ # full_response += chunk
237
+ # message_placeholder.markdown(full_response + "▌")
238
+ # message_placeholder.markdown(full_response)
239
+ #
240
+ # # add assistant response to chat history
241
+ # st.session_state.messages.append({"role": "assistant", "content": full_response})
242
+ # else:
243
+ # st.error("Unexpected response format: 'response_gen' key not found")
244
+ #
245
+ # except requests.exceptions.HTTPError as e:
246
+ # st.error(f"HTTP error: {e}")
247
+
248
+
requirements.txt ADDED
Binary file (6.13 kB). View file