update utlis.py
Browse files- repochat/utils.py +29 -53
repochat/utils.py
CHANGED
|
@@ -1,66 +1,42 @@
|
|
| 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 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 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 |
-
|
| 40 |
-
|
|
|
|
|
|
|
| 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
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
Context: {context}
|
| 54 |
-
User: {question}"""
|
| 55 |
-
return prompt_format(system_prompt, instruction)
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
Standalone question:"""
|
| 65 |
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import shutil
|
| 3 |
import subprocess
|
| 4 |
import streamlit as st
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
SESSION_DEFAULTS = {
|
| 8 |
+
"messages": [],
|
| 9 |
+
"chroma_db": None,
|
| 10 |
+
"db_loaded": False,
|
| 11 |
+
"repo_path": "./cloned_repo",
|
| 12 |
+
"git_form": False,
|
| 13 |
+
"qa": None,
|
| 14 |
+
"db_name": None,
|
| 15 |
+
}
|
|
|
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
def init_session_state():
|
| 19 |
+
for key, default in SESSION_DEFAULTS.items():
|
| 20 |
+
if key not in st.session_state:
|
| 21 |
+
st.session_state[key] = default
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
def clone_repo(git_url: str, repo_path: str):
|
| 25 |
+
"""Clone a public GitHub repo, stripping the .git metadata afterwards."""
|
| 26 |
+
if os.path.exists(repo_path):
|
| 27 |
+
shutil.rmtree(repo_path)
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
result = subprocess.run(
|
| 30 |
+
["git", "clone", "--depth", "1", git_url, repo_path],
|
| 31 |
+
capture_output=True,
|
| 32 |
+
text=True,
|
| 33 |
+
)
|
| 34 |
|
| 35 |
+
if result.returncode != 0:
|
| 36 |
+
st.error(f"Git clone failed:\n```\n{result.stderr}\n```")
|
| 37 |
+
st.stop()
|
|
|
|
| 38 |
|
| 39 |
+
# Remove .git to save disk space
|
| 40 |
+
git_dir = os.path.join(repo_path, ".git")
|
| 41 |
+
if os.path.exists(git_dir):
|
| 42 |
+
shutil.rmtree(git_dir)
|