Skip to content

Commit c425dca

Browse files
committed
make langchain/chroma optional (when using rag)
1 parent 58585b7 commit c425dca

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

pyproject.toml

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,6 @@ dependencies = [
4646
'websockets == 13.1',
4747
'faker',
4848
'fpdf',
49-
'langchain_core',
50-
'langchain_community',
51-
'langchain_chroma',
52-
'langchain_openai',
53-
'markdown',
54-
'chromadb',
5549
]
5650

5751
[project.urls]
@@ -70,17 +64,26 @@ where = ["src"]
7064
[tool.pytest.ini_options]
7165
pythonpath = "src"
7266
addopts = ["--import-mode=importlib"]
67+
7368
[project.optional-dependencies]
74-
testing = ['pytest', 'pytest-mock', 'pandas', 'faker', 'langchain_core']
69+
testing = [
70+
'pytest',
71+
'pytest-mock',
72+
'faker',
73+
'langchain_core'
74+
]
75+
7576
dev = [
7677
'ruff',
7778
]
78-
rag-usecase = [
79-
'langchain-community',
79+
80+
rag = [
81+
'langchain_core',
82+
'langchain-community',
83+
'langchain-chroma',
8084
'langchain-openai',
8185
'markdown',
8286
'chromadb',
83-
'langchain-chroma',
8487
]
8588

8689
[project.scripts]

src/hackingBuddyGPT/usecases/linux_privesc.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99
from hackingBuddyGPT.usecases.usecase import use_case
1010
from hackingBuddyGPT.utils import llm_util
1111
from hackingBuddyGPT.utils.logging import log_conversation
12-
from hackingBuddyGPT.utils.rag import RagBackground
12+
from hackingBuddyGPT.utils.rag import has_langchain
1313
from hackingBuddyGPT.utils.connectors.ssh_connection import SSHConnection
1414
from hackingBuddyGPT.utils.shell_root_detection import got_root
1515

16+
if has_langchain():
17+
from hackingBuddyGPT.utils.rag import RagBackground
18+
1619
template_analyze = Template("""Your task is to analyze the result of an executed command to determina
1720
a way to escalate your privileges into a root shell. Describe your findings including all needed
1821
information while being as concise as possible.
@@ -132,6 +135,10 @@ def init(self):
132135
guidance = []
133136

134137
if self.rag_path != '':
138+
if not has_langchain():
139+
self.log.console.print("[red]RAG path provided but langchain is not installed. Please install langchain to use RAG functionality, e.g., through `pip install -e .\[rag]`.[/red]")
140+
raise ImportError("langchain is not installed")
141+
135142
self._enable_rag = True
136143
self._rag_data = RagBackground(self.rag_path, self.llm)
137144

@@ -254,4 +261,4 @@ def check_success(self, cmd:str, result:str) -> bool:
254261
ansi_escape = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])")
255262
last_line = result.split("\n")[-1] if result else ""
256263
last_line = ansi_escape.sub("", last_line)
257-
return got_root(self.conn.hostname, last_line)
264+
return got_root(self.conn.hostname, last_line)

src/hackingBuddyGPT/utils/rag.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
from langchain_community.document_loaders import DirectoryLoader, TextLoader
2-
from langchain_chroma import Chroma
3-
from langchain_openai import OpenAIEmbeddings
4-
from langchain_text_splitters import MarkdownTextSplitter
1+
try:
2+
from langchain_community.document_loaders import DirectoryLoader, TextLoader
3+
from langchain_chroma import Chroma
4+
from langchain_openai import OpenAIEmbeddings
5+
from langchain_text_splitters import MarkdownTextSplitter
6+
except ImportError:
7+
_has_langchain = False
8+
else:
9+
_has_langchain = True
10+
11+
def has_langchain():
12+
return _has_langchain
513

614
class RagBackground:
715

0 commit comments

Comments
 (0)