summaryrefslogtreecommitdiffstats
path: root/g4f/local
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--g4f/local/__init__.py19
-rw-r--r--g4f/local/_engine.py42
-rw-r--r--g4f/local/_models.py86
-rw-r--r--g4f/local/models/model-here1
-rw-r--r--g4f/locals/__init__.py0
-rw-r--r--g4f/locals/models.py50
-rw-r--r--g4f/locals/provider.py72
7 files changed, 131 insertions, 139 deletions
diff --git a/g4f/local/__init__.py b/g4f/local/__init__.py
index c9d3d74a..d13c8c1d 100644
--- a/g4f/local/__init__.py
+++ b/g4f/local/__init__.py
@@ -1,17 +1,17 @@
-from ..typing import Union, Iterator, Messages
-from ..stubs import ChatCompletion, ChatCompletionChunk
-from ._engine import LocalProvider
-from ._models import models
-from ..client import iter_response, filter_none, IterResponse
+from ..typing import Union, Messages
+from ..locals.provider import LocalProvider
+from ..locals.models import get_models
+from ..client.client import iter_response, filter_none
+from ..client.types import IterResponse
class LocalClient():
def __init__(self, **kwargs) -> None:
self.chat: Chat = Chat(self)
-
+
@staticmethod
def list_models():
- return list(models.keys())
-
+ return list(get_models())
+
class Completions():
def __init__(self, client: LocalClient):
self.client: LocalClient = client
@@ -25,8 +25,7 @@ class Completions():
max_tokens: int = None,
stop: Union[list[str], str] = None,
**kwargs
- ) -> Union[ChatCompletion, Iterator[ChatCompletionChunk]]:
-
+ ) -> IterResponse:
stop = [stop] if isinstance(stop, str) else stop
response = LocalProvider.create_completion(
model, messages, stream,
diff --git a/g4f/local/_engine.py b/g4f/local/_engine.py
deleted file mode 100644
index 917de16c..00000000
--- a/g4f/local/_engine.py
+++ /dev/null
@@ -1,42 +0,0 @@
-import os
-
-from gpt4all import GPT4All
-from ._models import models
-
-class LocalProvider:
- @staticmethod
- def create_completion(model, messages, stream, **kwargs):
- if model not in models:
- raise ValueError(f"Model '{model}' not found / not yet implemented")
-
- model = models[model]
- model_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'models/')
- full_model_path = os.path.join(model_dir, model['path'])
-
- if not os.path.isfile(full_model_path):
- print(f"Model file '{full_model_path}' not found.")
- download = input(f'Do you want to download {model["path"]} ? [y/n]')
-
- if download in ['y', 'Y']:
- GPT4All.download_model(model['path'], model_dir)
- else:
- raise ValueError(f"Model '{model['path']}' not found.")
-
- model = GPT4All(model_name=model['path'],
- #n_threads=8,
- verbose=False,
- allow_download=False,
- model_path=model_dir)
-
- system_template = next((message['content'] for message in messages if message['role'] == 'system'),
- 'A chat between a curious user and an artificial intelligence assistant.')
-
- prompt_template = 'USER: {0}\nASSISTANT: '
- conversation = '\n'.join(f"{msg['role'].upper()}: {msg['content']}" for msg in messages) + "\nASSISTANT: "
-
- with model.chat_session(system_template, prompt_template):
- if stream:
- for token in model.generate(conversation, streaming=True):
- yield token
- else:
- yield model.generate(conversation) \ No newline at end of file
diff --git a/g4f/local/_models.py b/g4f/local/_models.py
deleted file mode 100644
index ec36fe41..00000000
--- a/g4f/local/_models.py
+++ /dev/null
@@ -1,86 +0,0 @@
-models = {
- "mistral-7b": {
- "path": "mistral-7b-openorca.gguf2.Q4_0.gguf",
- "ram": "8",
- "prompt": "<|im_start|>user\n%1<|im_end|>\n<|im_start|>assistant\n",
- "system": "<|im_start|>system\nYou are MistralOrca, a large language model trained by Alignment Lab AI. For multi-step problems, write out your reasoning for each step.\n<|im_end|>"
- },
- "mistral-7b-instruct": {
- "path": "mistral-7b-instruct-v0.1.Q4_0.gguf",
- "ram": "8",
- "prompt": "[INST] %1 [/INST]",
- "system": None
- },
- "gpt4all-falcon": {
- "path": "gpt4all-falcon-newbpe-q4_0.gguf",
- "ram": "8",
- "prompt": "### Instruction:\n%1\n### Response:\n",
- "system": None
- },
- "orca-2": {
- "path": "orca-2-13b.Q4_0.gguf",
- "ram": "16",
- "prompt": None,
- "system": None
- },
- "wizardlm-13b": {
- "path": "wizardlm-13b-v1.2.Q4_0.gguf",
- "ram": "16",
- "prompt": None,
- "system": None
- },
- "nous-hermes-llama2": {
- "path": "nous-hermes-llama2-13b.Q4_0.gguf",
- "ram": "16",
- "prompt": "### Instruction:\n%1\n### Response:\n",
- "system": None
- },
- "gpt4all-13b-snoozy": {
- "path": "gpt4all-13b-snoozy-q4_0.gguf",
- "ram": "16",
- "prompt": None,
- "system": None
- },
- "mpt-7b-chat": {
- "path": "mpt-7b-chat-newbpe-q4_0.gguf",
- "ram": "8",
- "prompt": "<|im_start|>user\n%1<|im_end|>\n<|im_start|>assistant\n",
- "system": "<|im_start|>system\n- You are a helpful assistant chatbot trained by MosaicML.\n- You answer questions.\n- You are excited to be able to help the user, but will refuse to do anything that could be considered harmful to the user.\n- You are more than just an information source, you are also able to write poetry, short stories, and make jokes.<|im_end|>"
- },
- "orca-mini-3b": {
- "path": "orca-mini-3b-gguf2-q4_0.gguf",
- "ram": "4",
- "prompt": "### User:\n%1\n### Response:\n",
- "system": "### System:\nYou are an AI assistant that follows instruction extremely well. Help as much as you can.\n\n"
- },
- "replit-code-3b": {
- "path": "replit-code-v1_5-3b-newbpe-q4_0.gguf",
- "ram": "4",
- "prompt": "%1",
- "system": None
- },
- "starcoder": {
- "path": "starcoder-newbpe-q4_0.gguf",
- "ram": "4",
- "prompt": "%1",
- "system": None
- },
- "rift-coder-7b": {
- "path": "rift-coder-v0-7b-q4_0.gguf",
- "ram": "8",
- "prompt": "%1",
- "system": None
- },
- "all-MiniLM-L6-v2": {
- "path": "all-MiniLM-L6-v2-f16.gguf",
- "ram": "1",
- "prompt": None,
- "system": None
- },
- "mistral-7b-german": {
- "path": "em_german_mistral_v01.Q4_0.gguf",
- "ram": "8",
- "prompt": "USER: %1 ASSISTANT: ",
- "system": "Du bist ein hilfreicher Assistent. "
- }
-} \ No newline at end of file
diff --git a/g4f/local/models/model-here b/g4f/local/models/model-here
deleted file mode 100644
index 945c9b46..00000000
--- a/g4f/local/models/model-here
+++ /dev/null
@@ -1 +0,0 @@
-. \ No newline at end of file
diff --git a/g4f/locals/__init__.py b/g4f/locals/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/g4f/locals/__init__.py
diff --git a/g4f/locals/models.py b/g4f/locals/models.py
new file mode 100644
index 00000000..85777ef2
--- /dev/null
+++ b/g4f/locals/models.py
@@ -0,0 +1,50 @@
+
+import os
+import requests
+import json
+
+from ..requests.raise_for_status import raise_for_status
+
+def load_models():
+ response = requests.get("https://gpt4all.io/models/models3.json")
+ raise_for_status(response)
+ return format_models(response.json())
+
+def get_model_name(filename: str) -> str:
+ name = filename.split(".", 1)[0]
+ for replace in ["-v1_5", "-v1", "-q4_0", "_v01", "-v0", "-f16", "-gguf2", "-newbpe"]:
+ name = name.replace(replace, "")
+ return name
+
+def format_models(models: list) -> dict:
+ return {get_model_name(model["filename"]): {
+ "path": model["filename"],
+ "ram": model["ramrequired"],
+ "prompt": model["promptTemplate"] if "promptTemplate" in model else None,
+ "system": model["systemPrompt"] if "systemPrompt" in model else None,
+ } for model in models}
+
+def read_models(file_path: str):
+ with open(file_path, "rb") as f:
+ return json.load(f)
+
+def save_models(file_path: str, data):
+ with open(file_path, 'w') as f:
+ json.dump(data, f, indent=4)
+
+def get_model_dir() -> str:
+ local_dir = os.path.dirname(os.path.abspath(__file__))
+ project_dir = os.path.dirname(os.path.dirname(local_dir))
+ model_dir = os.path.join(project_dir, "models")
+ if os.path.exists(model_dir):
+ return model_dir
+
+def get_models() -> dict[str, dict]:
+ model_dir = get_model_dir()
+ file_path = os.path.join(model_dir, "models.json")
+ if os.path.isfile(file_path):
+ return read_models(file_path)
+ else:
+ models = load_models()
+ save_models(file_path, models)
+ return models \ No newline at end of file
diff --git a/g4f/locals/provider.py b/g4f/locals/provider.py
new file mode 100644
index 00000000..09dfd4fe
--- /dev/null
+++ b/g4f/locals/provider.py
@@ -0,0 +1,72 @@
+import os
+
+from gpt4all import GPT4All
+from .models import get_models
+from ..typing import Messages
+
+MODEL_LIST: dict[str, dict] = None
+
+def find_model_dir(model_file: str) -> str:
+ local_dir = os.path.dirname(os.path.abspath(__file__))
+ project_dir = os.path.dirname(os.path.dirname(local_dir))
+
+ new_model_dir = os.path.join(project_dir, "models")
+ new_model_file = os.path.join(new_model_dir, model_file)
+ if os.path.isfile(new_model_file):
+ return new_model_dir
+
+ old_model_dir = os.path.join(local_dir, "models")
+ old_model_file = os.path.join(old_model_dir, model_file)
+ if os.path.isfile(old_model_file):
+ return old_model_dir
+
+ working_dir = "./"
+ for root, dirs, files in os.walk(working_dir):
+ if model_file in files:
+ return root
+
+ return new_model_dir
+
+class LocalProvider:
+ @staticmethod
+ def create_completion(model: str, messages: Messages, stream: bool = False, **kwargs):
+ global MODEL_LIST
+ if MODEL_LIST is None:
+ MODEL_LIST = get_models()
+ if model not in MODEL_LIST:
+ raise ValueError(f'Model "{model}" not found / not yet implemented')
+
+ model = MODEL_LIST[model]
+ model_file = model["path"]
+ model_dir = find_model_dir(model_file)
+ if not os.path.isfile(os.path.join(model_dir, model_file)):
+ print(f'Model file "models/{model_file}" not found.')
+ download = input(f"Do you want to download {model_file}? [y/n]: ")
+ if download in ["y", "Y"]:
+ GPT4All.download_model(model_file, model_dir)
+ else:
+ raise ValueError(f'Model "{model_file}" not found.')
+
+ model = GPT4All(model_name=model_file,
+ #n_threads=8,
+ verbose=False,
+ allow_download=False,
+ model_path=model_dir)
+
+ system_message = "\n".join(message["content"] for message in messages if message["role"] == "system")
+ if system_message:
+ system_message = "A chat between a curious user and an artificial intelligence assistant."
+
+ prompt_template = "USER: {0}\nASSISTANT: "
+ conversation = "\n" . join(
+ f"{message['role'].upper()}: {message['content']}"
+ for message in messages
+ if message["role"] != "system"
+ ) + "\nASSISTANT: "
+
+ with model.chat_session(system_message, prompt_template):
+ if stream:
+ for token in model.generate(conversation, streaming=True):
+ yield token
+ else:
+ yield model.generate(conversation) \ No newline at end of file