From 4e98acbca754dc485eb257a567361da967871542 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Mon, 20 Nov 2023 14:52:37 -0800 Subject: [PATCH 1/3] Update minimum pydantic version to one with model_validate function --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index a457aec6..17c11e77 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -46,7 +46,7 @@ dependencies = [ "tiktoken >= 0.3.2", "tenacity >= 8.2.2", "pillow == 9.3.0", - "pydantic >= 1.10.10", + "pydantic >= 2.0.0", "pyyaml == 6.0", "rich >= 13.3.1", "schedule == 1.1.0", From d61b0dd55c0b78db6ea5cf080a03dc7f74ec9471 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Mon, 20 Nov 2023 14:55:00 -0800 Subject: [PATCH 2/3] Add Khoj Django app package to sys path to load Django module via pip install --- src/khoj/main.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/khoj/main.py b/src/khoj/main.py index 9fa65fc3..022efcc1 100644 --- a/src/khoj/main.py +++ b/src/khoj/main.py @@ -25,6 +25,7 @@ from django.core.asgi import get_asgi_application from django.core.management import call_command # Initialize Django +sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings") django.setup() From c07401cf76ae56f298b0c312ef2347ff43245b64 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Mon, 20 Nov 2023 16:43:48 -0800 Subject: [PATCH 3/3] Fix, Improve chat config via CLI on first run by using defaults - Fix setting prompt size for online chat - generally improve chat config via cli by using default chat model, prompt size for online and offline chat --- src/khoj/utils/constants.py | 1 + src/khoj/utils/initialization.py | 44 +++++++++++++++++++++++--------- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/src/khoj/utils/constants.py b/src/khoj/utils/constants.py index 8a106153..ec23cddd 100644 --- a/src/khoj/utils/constants.py +++ b/src/khoj/utils/constants.py @@ -7,6 +7,7 @@ app_env_filepath = "~/.khoj/env" telemetry_server = "https://khoj.beta.haletic.com/v1/telemetry" content_directory = "~/.khoj/content/" default_offline_chat_model = "mistral-7b-instruct-v0.1.Q4_0.gguf" +default_online_chat_model = "gpt-4" empty_config = { "search-type": { diff --git a/src/khoj/utils/initialization.py b/src/khoj/utils/initialization.py index c797f848..b2d7aad5 100644 --- a/src/khoj/utils/initialization.py +++ b/src/khoj/utils/initialization.py @@ -8,7 +8,8 @@ from database.models import ( ChatModelOptions, ) -from khoj.utils.constants import default_offline_chat_model +from khoj.utils.constants import default_offline_chat_model, default_online_chat_model +from khoj.processor.conversation.utils import model_to_prompt_size, model_to_tokenizer from database.adapters import ConversationAdapters @@ -30,11 +31,6 @@ def initialization(): logger.info( "🗣️ Configure chat models available to your server. You can always update these at /server/admin using the credentials of your admin account" ) - try: - # Some environments don't support interactive input. We catch the exception and return if that's the case. The admin can still configure their settings from the admin page. - input() - except EOFError: - return try: # Note: gpt4all package is not available on all devices. @@ -47,15 +43,27 @@ def initialization(): OfflineChatProcessorConversationConfig.objects.create(enabled=True) offline_chat_model = input( - f"Enter the name of the offline chat model you want to use, based on the models in HuggingFace (press enter to use the default: {default_offline_chat_model}): " + f"Enter the offline chat model you want to use, See GPT4All for supported models (default: {default_offline_chat_model}): " ) if offline_chat_model == "": ChatModelOptions.objects.create( chat_model=default_offline_chat_model, model_type=ChatModelOptions.ModelType.OFFLINE ) else: - max_tokens = input("Enter the maximum number of tokens to use for the offline chat model:") - tokenizer = input("Enter the tokenizer to use for the offline chat model:") + default_max_tokens = model_to_prompt_size.get(offline_chat_model, 2000) + max_tokens = input( + f"Enter the maximum number of tokens to use for the offline chat model (default {default_max_tokens}):" + ) + max_tokens = max_tokens or default_max_tokens + + default_tokenizer = model_to_tokenizer.get( + offline_chat_model, "hf-internal-testing/llama-tokenizer" + ) + tokenizer = input( + f"Enter the tokenizer to use for the offline chat model (default: {default_tokenizer}):" + ) + tokenizer = tokenizer or default_tokenizer + ChatModelOptions.objects.create( chat_model=offline_chat_model, model_type=ChatModelOptions.ModelType.OFFLINE, @@ -71,10 +79,19 @@ def initialization(): logger.info("🗣️ Setting up OpenAI chat model") api_key = input("Enter your OpenAI API key: ") OpenAIProcessorConversationConfig.objects.create(api_key=api_key) - openai_chat_model = input("Enter the name of the OpenAI chat model you want to use: ") - max_tokens = input("Enter the maximum number of tokens to use for the OpenAI chat model:") + + openai_chat_model = input( + f"Enter the OpenAI chat model you want to use (default: {default_online_chat_model}): " + ) + openai_chat_model = openai_chat_model or default_online_chat_model + + default_max_tokens = model_to_prompt_size.get(openai_chat_model, 2000) + max_tokens = input( + f"Enter the maximum number of tokens to use for the OpenAI chat model (default: {default_max_tokens}): " + ) + max_tokens = max_tokens or default_max_tokens ChatModelOptions.objects.create( - chat_model=openai_chat_model, model_type=ChatModelOptions.ModelType.OPENAI, max_tokens=max_tokens + chat_model=openai_chat_model, model_type=ChatModelOptions.ModelType.OPENAI, max_prompt_size=max_tokens ) logger.info("🗣️ Chat model configuration complete") @@ -94,5 +111,8 @@ def initialization(): try: _create_chat_configuration() break + # Some environments don't support interactive input. We catch the exception and return if that's the case. The admin can still configure their settings from the admin page. + except EOFError: + return except Exception as e: logger.error(f"🚨 Failed to create chat configuration: {e}", exc_info=True)