Update private_gpt/components/llm/llm_component.py
Browse files
private_gpt/components/llm/llm_component.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
import logging
|
| 2 |
-
|
| 3 |
-
|
| 4 |
from injector import inject, singleton
|
| 5 |
from llama_index.llms import MockLLM
|
| 6 |
from llama_index.llms.base import LLM
|
|
@@ -11,27 +10,19 @@ from private_gpt.components.llm.prompt_helper import get_prompt_style
|
|
| 11 |
from private_gpt.paths import models_path
|
| 12 |
from private_gpt.settings.settings import Settings
|
| 13 |
|
| 14 |
-
import os
|
| 15 |
logger = logging.getLogger(__name__)
|
| 16 |
|
| 17 |
-
model_url: "https://huggingface.co/TheBloke/Mistral-7B-Instruct-v0.1-GGUF/raw/main/mistral-7b-instruct-v0.1.Q4_K_M.gguf"
|
| 18 |
-
|
| 19 |
@singleton
|
| 20 |
class LLMComponent:
|
| 21 |
llm: LLM
|
| 22 |
|
| 23 |
@inject
|
| 24 |
-
def __init__(self, settings: Settings
|
| 25 |
-
llm_mode =
|
| 26 |
logger.info("Initializing the LLM in mode=%s", llm_mode)
|
| 27 |
|
| 28 |
-
allowed_modes = ["local", "openai", "sagemaker", "mock"]
|
| 29 |
-
|
| 30 |
-
if llm_mode not in allowed_modes:
|
| 31 |
-
raise ValueError(f"Invalid LLM mode: {llm_mode}")
|
| 32 |
-
|
| 33 |
|
| 34 |
-
match
|
| 35 |
case "local":
|
| 36 |
from llama_index.llms import LlamaCPP
|
| 37 |
prompt_style_cls = get_prompt_style(settings.local.prompt_style)
|
|
@@ -39,8 +30,7 @@ class LLMComponent:
|
|
| 39 |
default_system_prompt=settings.local.default_system_prompt
|
| 40 |
)
|
| 41 |
self.llm = LlamaCPP(
|
| 42 |
-
|
| 43 |
-
model_url= "https://huggingface.co/TheBloke/Mistral-7B-Instruct-v0.1-GGUF/resolve/main/mistral-7b-instruct-v0.1.Q4_K_M.gguf?download=true",
|
| 44 |
temperature=0.1,
|
| 45 |
max_new_tokens=settings.llm.max_new_tokens,
|
| 46 |
context_window=3900,
|
|
@@ -61,42 +51,45 @@ class LLMComponent:
|
|
| 61 |
|
| 62 |
#default startup
|
| 63 |
logger.info("Initializing the GPT Model in=%s", "gpt-3.5-turbo")
|
| 64 |
-
self.llm = OpenAI(model="gpt-3.5-turbo", api_key=
|
| 65 |
-
|
| 66 |
-
case "mock":
|
| 67 |
-
self.llm = MockLLM()
|
| 68 |
-
|
| 69 |
|
| 70 |
case "dynamic":
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
|
|
|
| 78 |
def switch_model(self, new_model: str, settings: Settings) -> None:
|
|
|
|
| 79 |
openai_settings = settings.openai.api_key
|
| 80 |
|
|
|
|
| 81 |
if new_model == "gpt-3.5-turbo":
|
| 82 |
-
self.llm = OpenAI(model="gpt-3.5-turbo", api_key=
|
| 83 |
-
|
| 84 |
|
| 85 |
elif new_model == "gpt-4":
|
| 86 |
# Initialize with the new model
|
| 87 |
-
self.llm = OpenAI(model="gpt-4", api_key=
|
| 88 |
-
logger.info("Initializing the
|
| 89 |
|
| 90 |
|
| 91 |
elif new_model == "mistral-7B":
|
| 92 |
-
|
|
|
|
| 93 |
prompt_style_cls = get_prompt_style(settings.local.prompt_style)
|
| 94 |
prompt_style = prompt_style_cls(
|
| 95 |
default_system_prompt=settings.local.default_system_prompt
|
| 96 |
)
|
| 97 |
self.llm = LlamaCPP(
|
| 98 |
-
|
| 99 |
-
model_url=
|
| 100 |
temperature=0.1,
|
| 101 |
max_new_tokens=settings.llm.max_new_tokens,
|
| 102 |
context_window=3900,
|
|
@@ -106,15 +99,6 @@ class LLMComponent:
|
|
| 106 |
completion_to_prompt=prompt_style.completion_to_prompt,
|
| 107 |
verbose=True,
|
| 108 |
)
|
| 109 |
-
logger.info("Initializing the LLM Model in=%s", "Mistral-7B")
|
| 110 |
-
|
| 111 |
-
return self
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
|
| 119 |
|
| 120 |
|
|
|
|
| 1 |
import logging
|
| 2 |
+
import os
|
|
|
|
| 3 |
from injector import inject, singleton
|
| 4 |
from llama_index.llms import MockLLM
|
| 5 |
from llama_index.llms.base import LLM
|
|
|
|
| 10 |
from private_gpt.paths import models_path
|
| 11 |
from private_gpt.settings.settings import Settings
|
| 12 |
|
|
|
|
| 13 |
logger = logging.getLogger(__name__)
|
| 14 |
|
|
|
|
|
|
|
| 15 |
@singleton
|
| 16 |
class LLMComponent:
|
| 17 |
llm: LLM
|
| 18 |
|
| 19 |
@inject
|
| 20 |
+
def __init__(self, settings: Settings) -> None:
|
| 21 |
+
llm_mode = settings.llm.mode
|
| 22 |
logger.info("Initializing the LLM in mode=%s", llm_mode)
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
match settings.llm.mode:
|
| 26 |
case "local":
|
| 27 |
from llama_index.llms import LlamaCPP
|
| 28 |
prompt_style_cls = get_prompt_style(settings.local.prompt_style)
|
|
|
|
| 30 |
default_system_prompt=settings.local.default_system_prompt
|
| 31 |
)
|
| 32 |
self.llm = LlamaCPP(
|
| 33 |
+
model_path=str(models_path / settings.local.llm_hf_model_file),
|
|
|
|
| 34 |
temperature=0.1,
|
| 35 |
max_new_tokens=settings.llm.max_new_tokens,
|
| 36 |
context_window=3900,
|
|
|
|
| 51 |
|
| 52 |
#default startup
|
| 53 |
logger.info("Initializing the GPT Model in=%s", "gpt-3.5-turbo")
|
| 54 |
+
self.llm = OpenAI(model="gpt-3.5-turbo", api_key=openai_settings)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
case "dynamic":
|
| 57 |
+
from llama_index.llms import OpenAI
|
| 58 |
+
openai_settings = settings.openai.api_key
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
#default startup
|
| 61 |
+
logger.info("Initializing the GPT Model in=%s", "gpt-3.5-turbo")
|
| 62 |
+
self.llm = OpenAI(model="gpt-3.5-turbo", api_key=openai_settings)
|
| 63 |
+
|
| 64 |
+
case "mock":
|
| 65 |
+
self.llm = MockLLM()
|
| 66 |
|
| 67 |
+
@inject
|
| 68 |
def switch_model(self, new_model: str, settings: Settings) -> None:
|
| 69 |
+
from llama_index.llms import LlamaCPP
|
| 70 |
openai_settings = settings.openai.api_key
|
| 71 |
|
| 72 |
+
|
| 73 |
if new_model == "gpt-3.5-turbo":
|
| 74 |
+
self.llm = OpenAI(model="gpt-3.5-turbo", api_key=openai_settings)
|
| 75 |
+
|
| 76 |
|
| 77 |
elif new_model == "gpt-4":
|
| 78 |
# Initialize with the new model
|
| 79 |
+
self.llm = OpenAI(model="gpt-4", api_key=openai_settings)
|
| 80 |
+
logger.info("Initializing the GPT Model in=%s", "gpt-4")
|
| 81 |
|
| 82 |
|
| 83 |
elif new_model == "mistral-7B":
|
| 84 |
+
model_url= "https://huggingface.co/TheBloke/Mistral-7B-Instruct-v0.1-GGUF/resolve/main/mistral-7b-instruct-v0.1.Q4_K_M.gguf?download=true"
|
| 85 |
+
#model_filename = os.path.basename(model_url)
|
| 86 |
prompt_style_cls = get_prompt_style(settings.local.prompt_style)
|
| 87 |
prompt_style = prompt_style_cls(
|
| 88 |
default_system_prompt=settings.local.default_system_prompt
|
| 89 |
)
|
| 90 |
self.llm = LlamaCPP(
|
| 91 |
+
model_path=str(models_path / settings.local.llm_hf_model_file),
|
| 92 |
+
#model_url= model_filename,
|
| 93 |
temperature=0.1,
|
| 94 |
max_new_tokens=settings.llm.max_new_tokens,
|
| 95 |
context_window=3900,
|
|
|
|
| 99 |
completion_to_prompt=prompt_style.completion_to_prompt,
|
| 100 |
verbose=True,
|
| 101 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
|
| 104 |
|