rcs3 commited on
Commit
462062c
·
verified ·
1 Parent(s): a1c4522

Upload 14 files

Browse files
.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
+ models/codellama-7b.Q4_K_M.gguf filter=lfs diff=lfs merge=lfs -text
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ import streamlit as st
3
+
4
+ from repochat.utils import init_session_state
5
+ from repochat.git import git_form
6
+ from repochat.db import vector_db, load_to_db
7
+ from repochat.models import hf_embeddings, code_llama
8
+ from repochat.chain import response_chain
9
+
10
+ init_session_state()
11
+
12
+ st.set_page_config(
13
+ page_title="RepoChat",
14
+ page_icon="💻",
15
+ initial_sidebar_state="expanded",
16
+ menu_items={
17
+ 'Report a bug': "https://github.com/pnkvalavala/repochat/issues",
18
+ 'About': "No need to worry if you can't understand GitHub code or repositories anymore! Introducing RepoChat, where you can effortlessly chat and discuss all things related to GitHub repositories."
19
+ }
20
+ )
21
+
22
+ st.markdown(
23
+ "<h1 style='text-align: center;'>RepoChat</h1>",
24
+ unsafe_allow_html=True
25
+ )
26
+
27
+ try:
28
+ st.session_state["db_name"], st.session_state['git_form'] = git_form(st.session_state['repo_path'])
29
+
30
+ if st.session_state['git_form']:
31
+ with st.spinner('Loading the contents to database. This may take some time...'):
32
+ st.session_state["chroma_db"] = vector_db(
33
+ hf_embeddings(),
34
+ load_to_db(st.session_state['repo_path'])
35
+ )
36
+ with st.spinner('Loading model to memory'):
37
+ st.session_state["qa"] = response_chain(
38
+ db=st.session_state["chroma_db"],
39
+ llm=code_llama()
40
+ )
41
+
42
+ st.session_state["db_loaded"] = True
43
+ except TypeError:
44
+ pass
45
+
46
+ if st.session_state["db_loaded"]:
47
+ for message in st.session_state["messages"]:
48
+ with st.chat_message(message["role"]):
49
+ st.markdown(message["content"])
50
+
51
+ if prompt := st.chat_input("Enter your query"):
52
+ st.session_state["messages"].append({"role": "user", "content": prompt})
53
+ with st.chat_message("user"):
54
+ st.markdown(prompt)
55
+
56
+ with st.chat_message("assistant"):
57
+ message_placeholder = st.empty()
58
+ full_response = ""
59
+ with st.spinner("Generating response..."):
60
+ result = st.session_state["qa"](prompt)
61
+ for chunk in result['answer'].split():
62
+ full_response += chunk + " "
63
+ time.sleep(0.05)
64
+ message_placeholder.markdown(full_response + "▌")
65
+ message_placeholder.markdown(full_response)
66
+ st.session_state["messages"].append({"role": "assistant", "content": full_response})
models/codellama-7b.Q4_K_M.gguf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c3866cb985de17d398c2d7e85bb8a9c717a76bb997dce0e72a0cb169063e6d17
3
+ size 4081095360
repochat/__init__.py ADDED
File without changes
repochat/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (162 Bytes). View file
 
repochat/__pycache__/chain.cpython-312.pyc ADDED
Binary file (1.43 kB). View file
 
repochat/__pycache__/db.cpython-312.pyc ADDED
Binary file (2.45 kB). View file
 
repochat/__pycache__/git.cpython-312.pyc ADDED
Binary file (2.35 kB). View file
 
repochat/__pycache__/models.cpython-312.pyc ADDED
Binary file (1.05 kB). View file
 
repochat/__pycache__/utils.cpython-312.pyc ADDED
Binary file (3.28 kB). View file
 
repochat/chain.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.prompts import PromptTemplate
2
+ from langchain.chains.conversation.memory import ConversationBufferMemory
3
+ from langchain.chains import ConversationalRetrievalChain
4
+
5
+ from .utils import model_prompt, custom_que_prompt
6
+
7
+ def response_chain(db, llm):
8
+ retriever = db.as_retriever()
9
+ search_kwargs = {
10
+ "k": 3,
11
+ "distance_metric": "cos"
12
+ }
13
+
14
+ retriever.search_kwargs.update(search_kwargs)
15
+
16
+ memory = ConversationBufferMemory(
17
+ memory_key='chat_history',
18
+ return_messages=True
19
+ )
20
+
21
+ model_template = model_prompt()
22
+ QA_CHAIN_PROMPT = PromptTemplate(
23
+ input_variables=["context", "question"],
24
+ template=model_template
25
+ )
26
+ question_prompt = PromptTemplate.from_template(custom_que_prompt())
27
+
28
+ qa = ConversationalRetrievalChain.from_llm(
29
+ llm=llm,
30
+ retriever=retriever,
31
+ memory=memory,
32
+ chain_type='stuff',
33
+ verbose=True,
34
+ combine_docs_chain_kwargs={
35
+ 'prompt': QA_CHAIN_PROMPT
36
+ },
37
+ condense_question_prompt=question_prompt
38
+ )
39
+
40
+ return qa
repochat/db.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import shutil
3
+ import streamlit as st
4
+ from langchain.vectorstores import Chroma
5
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
6
+ from langchain.document_loaders import NotebookLoader, TextLoader
7
+
8
+ def vector_db(embeddings, code):
9
+ collection_name = "db_collection"
10
+ local_directory = "db_" + st.session_state["db_name"]
11
+ persist_directory = os.path.join(os.getcwd(), local_directory)
12
+
13
+ if os.path.exists(persist_directory):
14
+ shutil.rmtree(persist_directory)
15
+
16
+ vec_db = Chroma.from_documents(
17
+ documents=code,
18
+ embedding=embeddings,
19
+ collection_name=collection_name,
20
+ persist_directory=persist_directory
21
+ )
22
+
23
+ vec_db.persist()
24
+
25
+ return vec_db
26
+
27
+ def load_to_db(repo_path):
28
+ docs = []
29
+ for root, dirs, files in os.walk(repo_path):
30
+ dirs[:] = [d for d in dirs if not d.startswith('.')]
31
+ for filename in files:
32
+ if filename.startswith('.'):
33
+ continue
34
+ if filename == 'package-lock.json':
35
+ continue
36
+ file_path = os.path.join(root, filename)
37
+ try:
38
+ if file_path.endswith('.ipynb'):
39
+ loader = NotebookLoader(file_path)
40
+ else:
41
+ loader = TextLoader(file_path, encoding="utf-8")
42
+ docs.extend(loader.load_and_split())
43
+ except Exception as e:
44
+ pass
45
+
46
+ code_splitter = RecursiveCharacterTextSplitter(chunk_size=2000, chunk_overlap=200)
47
+ code = code_splitter.split_documents(docs)
48
+ return code
repochat/git.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import streamlit as st
3
+
4
+ from .utils import url_name, clone_repo
5
+
6
+ def git_form(repo_path):
7
+ with st.sidebar:
8
+ st.title("GitHub Link")
9
+ with st.form("git"):
10
+ git_url = st.text_input("Enter GitHub Repository Link")
11
+ submit_git = st.form_submit_button("Submit")
12
+ if submit_git:
13
+ with st.spinner("Checking GitHub URL"):
14
+ if not(git_url):
15
+ st.warning("Enter GitHub URL")
16
+ st.stop()
17
+ try:
18
+ response = requests.get(git_url)
19
+ if response.status_code == 200 and url_name(git_url):
20
+ st.success("GitHub Link loaded successfully!")
21
+ db_name = url_name(git_url)
22
+ else:
23
+ st.error("Enter Valid GitHub Repo")
24
+ st.stop()
25
+ except requests.exceptions.MissingSchema:
26
+ st.error("Invalid URL. Please include the scheme (e.g., https://)")
27
+ st.stop()
28
+
29
+ with st.spinner(f"Cloning {db_name} Repository"):
30
+ clone_repo(git_url, repo_path)
31
+ st.success("Cloned successfully!")
32
+ return db_name, 1
repochat/models.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.llms import LlamaCpp
2
+ from langchain.embeddings import HuggingFaceEmbeddings
3
+ from langchain.callbacks.manager import CallbackManager
4
+ from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
5
+
6
+ def hf_embeddings():
7
+ return HuggingFaceEmbeddings(
8
+ model_name = "sentence-transformers/all-mpnet-base-v2",
9
+ )
10
+
11
+ def code_llama():
12
+ callbackmanager = CallbackManager([StreamingStdOutCallbackHandler()])
13
+ llm = LlamaCpp(
14
+ model_path="./models/codellama-7b.Q4_K_M.gguf",
15
+ n_ctx=2048,
16
+ max_tokens=200,
17
+ n_gpu_layers=1,
18
+ f16_kv=True,
19
+ callback_manager=callbackmanager,
20
+ verbose=True,
21
+ use_mlock=True
22
+ )
23
+ return llm
repochat/utils.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import re
3
+ import shutil
4
+ import subprocess
5
+ import streamlit as st
6
+
7
+ def init_session_state():
8
+ SESSION_DEFAULTS = {
9
+ "messages": [],
10
+ "chroma_db": None,
11
+ "db_loaded": False,
12
+ "repo_path": './cloned_repo',
13
+ "git_form": False,
14
+ "qa": None,
15
+ "db_name": None
16
+ }
17
+
18
+ for keys, values in SESSION_DEFAULTS.items():
19
+ if keys not in st.session_state:
20
+ st.session_state[keys] = values
21
+
22
+ def url_name(url):
23
+ pattern = r"https?://github.com/([^/]+)/([^/]+)"
24
+ match = re.match(pattern, url)
25
+ if match:
26
+ owner = match.group(1)
27
+ repo = match.group(2)
28
+ return f"{owner}_{repo}"
29
+ else:
30
+ st.error("Enter valid GitHub URL")
31
+ st.stop()
32
+
33
+ def clone_repo(git_url, repo_path):
34
+ if os.path.exists(repo_path):
35
+ shutil.rmtree(repo_path)
36
+
37
+ git_url = git_url.replace(".git", "")
38
+
39
+ command = f'git clone {git_url}.git {repo_path} && rm -rf {repo_path}/.git'
40
+ subprocess.run(command, shell=True)
41
+
42
+ def prompt_format(system_prompt, instruction):
43
+ B_INST, E_INST = "[INST]", "[/INST]"
44
+ B_SYS, E_SYS = "<SYS>>\n", "\n<</SYS>>\n\n"
45
+ SYSTEM_PROMPT = B_SYS + system_prompt + E_SYS
46
+ prompt_template = B_INST + SYSTEM_PROMPT + instruction + E_INST
47
+ return prompt_template
48
+
49
+ def model_prompt():
50
+ system_prompt = """You are a helpful assistant, you have good knowledge in coding and you will use the provided context to answer user questions with detailed explanations.
51
+ Read the given context before answering questions and think step by step. If you can not answer a user question based on the provided context, inform the user. Do not use any other information for answering user"""
52
+ instruction = """
53
+ Context: {context}
54
+ User: {question}"""
55
+ return prompt_format(system_prompt, instruction)
56
+
57
+ def custom_que_prompt():
58
+ que_system_prompt = """Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question and give only the standalone question as output in the tags <question> and </question>.
59
+ """
60
+
61
+ instr_prompt = """Chat History:
62
+ {chat_history}
63
+ Follow Up Input: {question}
64
+ Standalone question:"""
65
+
66
+ return prompt_format(que_system_prompt, instr_prompt)