Spaces:
Sleeping
Sleeping
Commit ·
b0fa2a5
1
Parent(s): 96c35aa
Add llama-index and llama-cpp-python with proper imports for pydantic v2 compatibility
Browse files- requirements.txt +3 -0
- src/backend/chatbot.py +30 -27
requirements.txt
CHANGED
|
@@ -94,3 +94,6 @@ validators==0.20.0
|
|
| 94 |
yarl==1.9.2
|
| 95 |
zipp==3.15.0
|
| 96 |
torch>=2.0.0
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
yarl==1.9.2
|
| 95 |
zipp==3.15.0
|
| 96 |
torch>=2.0.0
|
| 97 |
+
llama-index-core==0.10.0
|
| 98 |
+
llama-index-llms-llama-cpp==0.1.0
|
| 99 |
+
llama-cpp-python>=0.2.0
|
src/backend/chatbot.py
CHANGED
|
@@ -9,7 +9,7 @@ def _lazy_import_llm_libs():
|
|
| 9 |
"""Import heavy LLM libraries only when needed"""
|
| 10 |
global ChatOpenAI, ChatPromptTemplate, SystemMessagePromptTemplate
|
| 11 |
global AIMessagePromptTemplate, HumanMessagePromptTemplate
|
| 12 |
-
global SimpleDirectoryReader, VectorStoreIndex
|
| 13 |
global LlamaCPP, messages_to_prompt, completion_to_prompt
|
| 14 |
|
| 15 |
from langchain_community.chat_models import ChatOpenAI
|
|
@@ -19,16 +19,21 @@ def _lazy_import_llm_libs():
|
|
| 19 |
AIMessagePromptTemplate,
|
| 20 |
HumanMessagePromptTemplate,
|
| 21 |
)
|
| 22 |
-
from llama_index import (
|
| 23 |
SimpleDirectoryReader,
|
| 24 |
VectorStoreIndex,
|
| 25 |
-
ServiceContext,
|
| 26 |
-
)
|
| 27 |
-
from llama_index.llms import LlamaCPP
|
| 28 |
-
from llama_index.llms.llama_utils import (
|
| 29 |
-
messages_to_prompt,
|
| 30 |
-
completion_to_prompt,
|
| 31 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
# set version
|
| 34 |
# st.session_state.demo_lite = False
|
|
@@ -157,25 +162,23 @@ def init_llm(model, demo_lite):
|
|
| 157 |
print(f"❌ Model file not found: {model_path}")
|
| 158 |
return None
|
| 159 |
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
verbose=True,
|
| 178 |
-
)
|
| 179 |
elif model == "deci-7b_CPP":
|
| 180 |
model_path = os.path.join(model_base_path, "decilm-7b-uniform-gqa-q8_0.gguf")
|
| 181 |
print("model path: ", model_path)
|
|
|
|
| 9 |
"""Import heavy LLM libraries only when needed"""
|
| 10 |
global ChatOpenAI, ChatPromptTemplate, SystemMessagePromptTemplate
|
| 11 |
global AIMessagePromptTemplate, HumanMessagePromptTemplate
|
| 12 |
+
global SimpleDirectoryReader, VectorStoreIndex
|
| 13 |
global LlamaCPP, messages_to_prompt, completion_to_prompt
|
| 14 |
|
| 15 |
from langchain_community.chat_models import ChatOpenAI
|
|
|
|
| 19 |
AIMessagePromptTemplate,
|
| 20 |
HumanMessagePromptTemplate,
|
| 21 |
)
|
| 22 |
+
from llama_index.core import (
|
| 23 |
SimpleDirectoryReader,
|
| 24 |
VectorStoreIndex,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
)
|
| 26 |
+
from llama_index.llms.llama_cpp import LlamaCPP
|
| 27 |
+
|
| 28 |
+
# Try to import prompt utilities (may not exist in newer versions)
|
| 29 |
+
try:
|
| 30 |
+
from llama_index.llms.llama_cpp.llama_utils import (
|
| 31 |
+
messages_to_prompt,
|
| 32 |
+
completion_to_prompt,
|
| 33 |
+
)
|
| 34 |
+
except ImportError:
|
| 35 |
+
messages_to_prompt = None
|
| 36 |
+
completion_to_prompt = None
|
| 37 |
|
| 38 |
# set version
|
| 39 |
# st.session_state.demo_lite = False
|
|
|
|
| 162 |
print(f"❌ Model file not found: {model_path}")
|
| 163 |
return None
|
| 164 |
|
| 165 |
+
# Build kwargs for LlamaCPP
|
| 166 |
+
llm_kwargs = {
|
| 167 |
+
"model_path": model_path,
|
| 168 |
+
"temperature": 0.1,
|
| 169 |
+
"max_new_tokens": 1000,
|
| 170 |
+
"context_window": 3000,
|
| 171 |
+
"generate_kwargs": {},
|
| 172 |
+
"model_kwargs": {"n_gpu_layers": n_gpu_layers},
|
| 173 |
+
"verbose": True,
|
| 174 |
+
}
|
| 175 |
+
# Add prompt formatters if available (optional in newer versions)
|
| 176 |
+
if messages_to_prompt is not None:
|
| 177 |
+
llm_kwargs["messages_to_prompt"] = messages_to_prompt
|
| 178 |
+
if completion_to_prompt is not None:
|
| 179 |
+
llm_kwargs["completion_to_prompt"] = completion_to_prompt
|
| 180 |
+
|
| 181 |
+
llm = LlamaCPP(**llm_kwargs)
|
|
|
|
|
|
|
| 182 |
elif model == "deci-7b_CPP":
|
| 183 |
model_path = os.path.join(model_base_path, "decilm-7b-uniform-gqa-q8_0.gguf")
|
| 184 |
print("model path: ", model_path)
|