diff --git a/.github/workflows/pypi.yml b/.github/workflows/pypi.yml index 1ac735aa..92bf9276 100644 --- a/.github/workflows/pypi.yml +++ b/.github/workflows/pypi.yml @@ -21,16 +21,18 @@ on: jobs: publish: name: Publish Python Package to PyPI - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest + permissions: + id-token: write steps: - uses: actions/checkout@v3 with: fetch-depth: 0 - - name: Set up Python 3.10 + - name: Set up Python 3.11 uses: actions/setup-python@v4 with: - python-version: '3.10' + python-version: '3.11' - name: ⬇️ Install Application run: python -m pip install --upgrade pip && pip install --upgrade . @@ -59,6 +61,4 @@ jobs: - name: πŸ“¦ Publish Python Package to PyPI if: startsWith(github.ref, 'refs/tags') || github.ref == 'refs/heads/master' - uses: pypa/gh-action-pypi-publish@v1.6.4 - with: - password: ${{ secrets.PYPI_API_KEY }} + uses: pypa/gh-action-pypi-publish@v1.8.14 diff --git a/documentation/assets/img/agents_demo.gif b/documentation/assets/img/agents_demo.gif new file mode 100644 index 00000000..2669033e Binary files /dev/null and b/documentation/assets/img/agents_demo.gif differ diff --git a/documentation/assets/img/dream_house.png b/documentation/assets/img/dream_house.png new file mode 100644 index 00000000..adfc9a37 Binary files /dev/null and b/documentation/assets/img/dream_house.png differ diff --git a/documentation/assets/img/plants_i_got.png b/documentation/assets/img/plants_i_got.png new file mode 100644 index 00000000..72e0d193 Binary files /dev/null and b/documentation/assets/img/plants_i_got.png differ diff --git a/documentation/assets/img/using_khoj_for_studying.gif b/documentation/assets/img/using_khoj_for_studying.gif new file mode 100644 index 00000000..898a3710 Binary files /dev/null and b/documentation/assets/img/using_khoj_for_studying.gif differ diff --git a/documentation/docs/features/agents.md b/documentation/docs/features/agents.md new file mode 100644 index 00000000..249f5bde --- /dev/null +++ b/documentation/docs/features/agents.md @@ -0,0 +1,15 @@ +--- +sidebar_position: 4 +--- + +# Agents + +You can use agents to setup custom system prompts with Khoj. The server host can setup their own agents, which are accessible to all users. You can see ours at https://app.khoj.dev/agents. + +![Demo](/img/agents_demo.gif) + +## Creating an Agent (Self-Hosted) + +Go to `server/admin/database/agent` on your server and click `Add Agent` to create a new one. You have to set it to `public` in order for it to be accessible to all the users on your server. To limit access to a specific user, do not set the `public` flag and add the user in the `Creator` field. + +Set your custom prompt in the `personality` field. diff --git a/documentation/docs/features/all_features.md b/documentation/docs/features/all_features.md index c482805b..3d3b8941 100644 --- a/documentation/docs/features/all_features.md +++ b/documentation/docs/features/all_features.md @@ -2,7 +2,7 @@ sidebar_position: 1 --- -# Features +# Overview Khoj supports a variety of features, including search and chat with a wide range of data sources and interfaces. diff --git a/documentation/docs/features/image_generation.md b/documentation/docs/features/image_generation.md new file mode 100644 index 00000000..da6af1ac --- /dev/null +++ b/documentation/docs/features/image_generation.md @@ -0,0 +1,15 @@ +# Image Generation +You can use Khoj to generate images from text prompts. You can get deeper into the details of our image generation flow in this blog post: https://blog.khoj.dev/posts/how-khoj-generates-images/. + +To generate images, you just need to provide a prompt to Khoj in which the image generation is in the instructions. Khoj will automatically detect the image generation intent, augment your generation prompt, and then create the image. Here are some examples: +| Prompt | Image | +| --- | --- | +| Paint a picture of the plants I got last month, pixar-animation | ![plants](/img/plants_i_got.png) | +| Create a picture of my dream house, based on my interests | ![house](/img/dream_house.png) | + + +## Setup (Self-Hosting) + +Right now, we only support integration with OpenAI's DALL-E. You need to have an OpenAI API key to use this feature. Here's how you can set it up: +1. Setup your OpenAI API key. See instructions [here](/get-started/setup#2-configure) +2. Create a text to image config at http://localhost:42110/server/admin/database/texttoimagemodelconfig/. We recommend the value `dall-e-3`. diff --git a/documentation/docs/features/voice_chat.md b/documentation/docs/features/voice_chat.md new file mode 100644 index 00000000..370a1737 --- /dev/null +++ b/documentation/docs/features/voice_chat.md @@ -0,0 +1,14 @@ +# Voice + +You can talk to Khoj using your voice. Khoj will respond to your queries using the same models as the chat feature. You can use voice chat on the web, Desktop, and Obsidian apps. Click on the little mic icon to send your voice message to Khoj. It will send back what it heard via text. You'll have some time to edit it before sending it, if required. Try it at https://app.khoj.dev/. + +:::info[Voice Response] +Khoj doesn't yet respond with voice, but it will send back a text response. Let us know if you're interested in voice responses at team at khoj.dev. +::: + +## Setup (Self-Hosting) + +Voice chat will automatically be configured when you initialize the application. The default configuration will run locally. If you want to use the OpenAI whisper API for voice chat, you can set it up by following these steps: + +1. Setup your OpenAI API key. See instructions [here](/get-started/setup#2-configure). +2. Create a new configuration at http://localhost:42110/server/admin/database/speechtotextmodeloptions/. We recommend the value `whisper-1` and model type `Openai`. diff --git a/documentation/docs/get-started/overview.md b/documentation/docs/get-started/overview.md index 4b571226..b0d2a51c 100644 --- a/documentation/docs/get-started/overview.md +++ b/documentation/docs/get-started/overview.md @@ -37,9 +37,7 @@ Welcome to the Khoj Docs! This is the best place to get setup and explore Khoj's - [Read these instructions](/get-started/setup) to self-host a private instance of Khoj ## At a Glance - -   - +![demo_chat](/img/using_khoj_for_studying.gif) #### [Search](/features/search) - **Natural**: Use natural language queries to quickly find relevant notes and documents. diff --git a/documentation/docs/get-started/setup.mdx b/documentation/docs/get-started/setup.mdx index b209bbee..7900633e 100644 --- a/documentation/docs/get-started/setup.mdx +++ b/documentation/docs/get-started/setup.mdx @@ -25,6 +25,10 @@ These are the general setup instructions for self-hosted Khoj. For Installation, you can either use Docker or install the Khoj server locally. +:::info[Offline Model + GPU] +If you want to use the offline chat model and you have a GPU, you should use Installation Option 2 - local setup via the Python package directly. Our Docker image doesn't currently support running the offline chat model on GPU, making inference times really slow. +::: + ### Installation Option 1 (Docker) #### Prerequisites @@ -183,15 +187,15 @@ Khoj should now be running at http://localhost:42110. You can see the web UI in Note: To start Khoj automatically in the background use [Task scheduler](https://www.windowscentral.com/how-create-automated-task-using-task-scheduler-windows-10) on Windows or [Cron](https://en.wikipedia.org/wiki/Cron) on Mac, Linux (e.g with `@reboot khoj`) -### 2. Download the desktop client +### Setup Notes -You can use our desktop executables to select file paths and folders to index. You can simply select the folders or files, and they'll be automatically uploaded to the server. Once you specify a file or file path, you don't need to update the configuration again; it will grab any data diffs dynamically over time. +Optionally, you can use Khoj with a custom domain as well. To do so, you need to set the `KHOJ_DOMAIN` environment variable to your domain (e.g., `export KHOJ_DOMAIN=my-khoj-domain.com` or add it to your `docker-compose.yml`). By default, the Khoj server you set up will not be accessible outside of `localhost` or `127.0.0.1`. -**To download the latest desktop client, go to https://download.khoj.dev** and the correct executable for your OS will automatically start downloading. You can also go to https://khoj.dev/downloads to explicitly download your image of choice. Once downloaded, you can configure your folders for indexing using the settings tab. To set your chat configuration, you'll have to use the web interface for the Khoj server you setup in the previous step. +:::warning[Must use an SSL certificate] +If you're using a custom domain, you must use an SSL certificate. You can use [Let's Encrypt](https://letsencrypt.org/) to get a free SSL certificate for your domain. +::: -To use the desktop client, you need to go to your Khoj server's settings page (http://localhost:42110/config) and copy the API key. Then, paste it into the desktop client's settings page. Once you've done that, you can select files and folders to index. Set the desktop client settings to use `http://127.0.0.1:42110` as the host URL. - -### 3. Configure +### 2. Configure 1. Go to http://localhost:42110/server/admin and login with your admin credentials. 1. Go to [OpenAI settings](http://localhost:42110/server/admin/database/openaiprocessorconversationconfig/) in the server admin settings to add an OpenAI processor conversation config. This is where you set your API key. Alternatively, you can go to the [offline chat settings](http://localhost:42110/server/admin/database/offlinechatprocessorconversationconfig/) and simply create a new setting with `Enabled` set to `True`. 2. Go to the ChatModelOptions if you want to add additional models for chat. @@ -207,6 +211,14 @@ To use the desktop client, you need to go to your Khoj server's settings page (h Using Safari on Mac? You might not be able to login to the admin panel. Try using Chrome or Firefox instead. ::: +### 3. Download the desktop client (Optional) + +You can use our desktop executables to select file paths and folders to index. You can simply select the folders or files, and they'll be automatically uploaded to the server. Once you specify a file or file path, you don't need to update the configuration again; it will grab any data diffs dynamically over time. + +**To download the latest desktop client, go to https://download.khoj.dev** and the correct executable for your OS will automatically start downloading. You can also go to https://khoj.dev/downloads to explicitly download your image of choice. Once downloaded, you can configure your folders for indexing using the settings tab. To set your chat configuration, you'll have to use the web interface for the Khoj server you setup in the previous step. + +To use the desktop client, you need to go to your Khoj server's settings page (http://localhost:42110/config) and copy the API key. Then, paste it into the desktop client's settings page. Once you've done that, you can select files and folders to index. Set the desktop client settings to use `http://127.0.0.1:42110` as the host URL. + ### 4. Install Client Plugins (Optional) Khoj exposes a web interface to search, chat and configure by default.
diff --git a/manifest.json b/manifest.json index a4bdc42c..11e57675 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "khoj", "name": "Khoj", - "version": "1.7.0", + "version": "1.8.0", "minAppVersion": "0.15.0", "description": "An AI copilot for your Second Brain", "author": "Khoj Inc.", diff --git a/pyproject.toml b/pyproject.toml index 11520f9b..0c13fd3e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,7 +7,7 @@ name = "khoj-assistant" description = "An AI copilot for your Second Brain" readme = "README.md" license = "AGPL-3.0-or-later" -requires-python = ">=3.8" +requires-python = ">=3.9" authors = [ { name = "Debanjum Singh Solanky, Saba Imran" }, ] @@ -23,8 +23,8 @@ keywords = [ "pdf", ] classifiers = [ - "Development Status :: 4 - Beta", - "License :: OSI Approved :: GNU General Public License v3 (GPLv3)", + "Development Status :: 5 - Production/Stable", + "License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)", "Operating System :: OS Independent", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.9", @@ -33,7 +33,7 @@ classifiers = [ "Topic :: Internet :: WWW/HTTP :: Indexing/Search", "Topic :: Scientific/Engineering :: Artificial Intelligence", "Topic :: Scientific/Engineering :: Human Machine Interfaces", - "Topic :: Text Processing :: Linguistic", + "Intended Audience :: Information Technology", ] dependencies = [ "beautifulsoup4 ~= 4.12.3", diff --git a/src/interface/desktop/package.json b/src/interface/desktop/package.json index 75de44c9..bb1a622e 100644 --- a/src/interface/desktop/package.json +++ b/src/interface/desktop/package.json @@ -1,6 +1,6 @@ { "name": "Khoj", - "version": "1.7.0", + "version": "1.8.0", "description": "An AI copilot for your Second Brain", "author": "Saba Imran, Debanjum Singh Solanky ", "license": "GPL-3.0-or-later", diff --git a/src/interface/emacs/khoj.el b/src/interface/emacs/khoj.el index a5e41868..c08d8eea 100644 --- a/src/interface/emacs/khoj.el +++ b/src/interface/emacs/khoj.el @@ -6,7 +6,7 @@ ;; Saba Imran ;; Description: An AI copilot for your Second Brain ;; Keywords: search, chat, org-mode, outlines, markdown, pdf, image -;; Version: 1.7.0 +;; Version: 1.8.0 ;; Package-Requires: ((emacs "27.1") (transient "0.3.0") (dash "2.19.1")) ;; URL: https://github.com/khoj-ai/khoj/tree/master/src/interface/emacs diff --git a/src/interface/obsidian/manifest.json b/src/interface/obsidian/manifest.json index a4bdc42c..11e57675 100644 --- a/src/interface/obsidian/manifest.json +++ b/src/interface/obsidian/manifest.json @@ -1,7 +1,7 @@ { "id": "khoj", "name": "Khoj", - "version": "1.7.0", + "version": "1.8.0", "minAppVersion": "0.15.0", "description": "An AI copilot for your Second Brain", "author": "Khoj Inc.", diff --git a/src/interface/obsidian/package.json b/src/interface/obsidian/package.json index 66d4a5c5..aec31710 100644 --- a/src/interface/obsidian/package.json +++ b/src/interface/obsidian/package.json @@ -1,6 +1,6 @@ { "name": "Khoj", - "version": "1.7.0", + "version": "1.8.0", "description": "An AI copilot for your Second Brain", "author": "Debanjum Singh Solanky, Saba Imran ", "license": "GPL-3.0-or-later", diff --git a/src/interface/obsidian/versions.json b/src/interface/obsidian/versions.json index 150f851e..10f042ef 100644 --- a/src/interface/obsidian/versions.json +++ b/src/interface/obsidian/versions.json @@ -39,5 +39,6 @@ "1.6.0": "0.15.0", "1.6.1": "0.15.0", "1.6.2": "0.15.0", - "1.7.0": "0.15.0" + "1.7.0": "0.15.0", + "1.8.0": "0.15.0" } diff --git a/src/khoj/configure.py b/src/khoj/configure.py index add685e3..0adbe889 100644 --- a/src/khoj/configure.py +++ b/src/khoj/configure.py @@ -21,6 +21,7 @@ from starlette.middleware.sessions import SessionMiddleware from starlette.requests import HTTPConnection from khoj.database.adapters import ( + AgentAdapters, ClientApplicationAdapters, ConversationAdapters, SubscriptionState, @@ -229,11 +230,16 @@ def configure_server( state.SearchType = configure_search_types() state.search_models = configure_search(state.search_models, state.config.search_type) + setup_default_agent() initialize_content(regenerate, search_type, init, user) except Exception as e: raise e +def setup_default_agent(): + AgentAdapters.create_default_agent() + + def initialize_content(regenerate: bool, search_type: Optional[SearchType] = None, init=False, user: KhojUser = None): # Initialize Content from Config if state.search_models: @@ -262,6 +268,7 @@ def initialize_content(regenerate: bool, search_type: Optional[SearchType] = Non def configure_routes(app): # Import APIs here to setup search types before while configuring server from khoj.routers.api import api + from khoj.routers.api_agents import api_agents from khoj.routers.api_chat import api_chat from khoj.routers.api_config import api_config from khoj.routers.indexer import indexer @@ -269,6 +276,7 @@ def configure_routes(app): app.include_router(api, prefix="/api") app.include_router(api_chat, prefix="/api/chat") + app.include_router(api_agents, prefix="/api/agents") app.include_router(api_config, prefix="/api/config") app.include_router(indexer, prefix="/api/v1/index") app.include_router(web_client) diff --git a/src/khoj/database/adapters/__init__.py b/src/khoj/database/adapters/__init__.py index d7488f97..a9e246a6 100644 --- a/src/khoj/database/adapters/__init__.py +++ b/src/khoj/database/adapters/__init__.py @@ -16,6 +16,7 @@ from pgvector.django import CosineDistance from torch import Tensor from khoj.database.models import ( + Agent, ChatModelOptions, ClientApplication, Conversation, @@ -37,6 +38,7 @@ from khoj.database.models import ( UserRequests, UserSearchModelConfig, ) +from khoj.processor.conversation import prompts from khoj.search_filter.date_filter import DateFilter from khoj.search_filter.file_filter import FileFilter from khoj.search_filter.word_filter import WordFilter @@ -391,6 +393,80 @@ class ClientApplicationAdapters: return await ClientApplication.objects.filter(client_id=client_id, client_secret=client_secret).afirst() +class AgentAdapters: + DEFAULT_AGENT_NAME = "Khoj" + DEFAULT_AGENT_AVATAR = "https://khoj-web-bucket.s3.amazonaws.com/lamp-128.png" + DEFAULT_AGENT_SLUG = "khoj" + + @staticmethod + async def aget_agent_by_slug(agent_slug: str, user: KhojUser): + return await Agent.objects.filter( + (Q(slug__iexact=agent_slug.lower())) & (Q(public=True) | Q(creator=user)) + ).afirst() + + @staticmethod + def get_agent_by_slug(slug: str, user: KhojUser = None): + if user: + return Agent.objects.filter((Q(slug__iexact=slug.lower())) & (Q(public=True) | Q(creator=user))).first() + return Agent.objects.filter(slug__iexact=slug.lower(), public=True).first() + + @staticmethod + def get_all_accessible_agents(user: KhojUser = None): + if user: + return Agent.objects.filter(Q(public=True) | Q(creator=user)).distinct().order_by("created_at") + return Agent.objects.filter(public=True).order_by("created_at") + + @staticmethod + async def aget_all_accessible_agents(user: KhojUser = None) -> List[Agent]: + agents = await sync_to_async(AgentAdapters.get_all_accessible_agents)(user) + return await sync_to_async(list)(agents) + + @staticmethod + def get_conversation_agent_by_id(agent_id: int): + agent = Agent.objects.filter(id=agent_id).first() + if agent == AgentAdapters.get_default_agent(): + # If the agent is set to the default agent, then return None and let the default application code be used + return None + return agent + + @staticmethod + def get_default_agent(): + return Agent.objects.filter(name=AgentAdapters.DEFAULT_AGENT_NAME).first() + + @staticmethod + def create_default_agent(): + default_conversation_config = ConversationAdapters.get_default_conversation_config() + default_personality = prompts.personality.format(current_date="placeholder") + + agent = Agent.objects.filter(name=AgentAdapters.DEFAULT_AGENT_NAME).first() + + if agent: + agent.personality = default_personality + agent.chat_model = default_conversation_config + agent.slug = AgentAdapters.DEFAULT_AGENT_SLUG + agent.name = AgentAdapters.DEFAULT_AGENT_NAME + agent.save() + else: + # The default agent is public and managed by the admin. It's handled a little differently than other agents. + agent = Agent.objects.create( + name=AgentAdapters.DEFAULT_AGENT_NAME, + public=True, + managed_by_admin=True, + chat_model=default_conversation_config, + personality=default_personality, + tools=["*"], + avatar=AgentAdapters.DEFAULT_AGENT_AVATAR, + slug=AgentAdapters.DEFAULT_AGENT_SLUG, + ) + Conversation.objects.filter(agent=None).update(agent=agent) + + return agent + + @staticmethod + async def aget_default_agent(): + return await Agent.objects.filter(name=AgentAdapters.DEFAULT_AGENT_NAME).afirst() + + class ConversationAdapters: @staticmethod def get_conversation_by_user( @@ -403,9 +479,10 @@ class ConversationAdapters: .first() ) else: + agent = AgentAdapters.get_default_agent() conversation = ( Conversation.objects.filter(user=user, client=client_application).order_by("-updated_at").first() - ) or Conversation.objects.create(user=user, client=client_application) + ) or Conversation.objects.create(user=user, client=client_application, agent=agent) return conversation @@ -431,8 +508,16 @@ class ConversationAdapters: return Conversation.objects.filter(id=conversation_id).first() @staticmethod - async def acreate_conversation_session(user: KhojUser, client_application: ClientApplication = None): - return await Conversation.objects.acreate(user=user, client=client_application) + async def acreate_conversation_session( + user: KhojUser, client_application: ClientApplication = None, agent_slug: str = None + ): + if agent_slug: + agent = await AgentAdapters.aget_agent_by_slug(agent_slug, user) + if agent is None: + raise HTTPException(status_code=400, detail="No such agent currently exists.") + return await Conversation.objects.acreate(user=user, client=client_application, agent=agent) + agent = await AgentAdapters.aget_default_agent() + return await Conversation.objects.acreate(user=user, client=client_application, agent=agent) @staticmethod async def aget_conversation_by_user( @@ -443,9 +528,14 @@ class ConversationAdapters: elif title: return await Conversation.objects.filter(user=user, client=client_application, title=title).afirst() else: - return await ( - Conversation.objects.filter(user=user, client=client_application).order_by("-updated_at").afirst() - ) or await Conversation.objects.acreate(user=user, client=client_application) + conversation = Conversation.objects.filter(user=user, client=client_application).order_by("-updated_at") + + if await conversation.aexists(): + return await conversation.prefetch_related("agent").afirst() + + return await ( + Conversation.objects.filter(user=user, client=client_application).order_by("-updated_at").afirst() + ) or await Conversation.objects.acreate(user=user, client=client_application) @staticmethod async def adelete_conversation_by_user( @@ -603,9 +693,14 @@ class ConversationAdapters: return random.sample(all_questions, max_results) @staticmethod - def get_valid_conversation_config(user: KhojUser): + def get_valid_conversation_config(user: KhojUser, conversation: Conversation): offline_chat_config = ConversationAdapters.get_offline_chat_conversation_config() - conversation_config = ConversationAdapters.get_conversation_config(user) + + if conversation.agent and conversation.agent.chat_model: + conversation_config = conversation.agent.chat_model + else: + conversation_config = ConversationAdapters.get_conversation_config(user) + if conversation_config is None: conversation_config = ConversationAdapters.get_default_conversation_config() diff --git a/src/khoj/database/admin.py b/src/khoj/database/admin.py index 22400d14..cc1be7e4 100644 --- a/src/khoj/database/admin.py +++ b/src/khoj/database/admin.py @@ -6,6 +6,7 @@ from django.contrib.auth.admin import UserAdmin from django.http import HttpResponse from khoj.database.models import ( + Agent, ChatModelOptions, ClientApplication, Conversation, @@ -50,6 +51,7 @@ admin.site.register(ReflectiveQuestion) admin.site.register(UserSearchModelConfig) admin.site.register(TextToImageModelConfig) admin.site.register(ClientApplication) +admin.site.register(Agent) @admin.register(Entry) diff --git a/src/khoj/database/migrations/0031_agent_conversation_agent.py b/src/khoj/database/migrations/0031_agent_conversation_agent.py new file mode 100644 index 00000000..1d08a118 --- /dev/null +++ b/src/khoj/database/migrations/0031_agent_conversation_agent.py @@ -0,0 +1,53 @@ +# Generated by Django 4.2.10 on 2024-03-13 07:38 + +import django.db.models.deletion +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("database", "0030_conversation_slug_and_title"), + ] + + operations = [ + migrations.CreateModel( + name="Agent", + fields=[ + ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")), + ("created_at", models.DateTimeField(auto_now_add=True)), + ("updated_at", models.DateTimeField(auto_now=True)), + ("name", models.CharField(max_length=200)), + ("tuning", models.TextField()), + ("avatar", models.URLField(blank=True, default=None, max_length=400, null=True)), + ("tools", models.JSONField(default=list)), + ("public", models.BooleanField(default=False)), + ("managed_by_admin", models.BooleanField(default=False)), + ("slug", models.CharField(max_length=200)), + ( + "chat_model", + models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to="database.chatmodeloptions"), + ), + ( + "creator", + models.ForeignKey( + blank=True, + default=None, + null=True, + on_delete=django.db.models.deletion.CASCADE, + to=settings.AUTH_USER_MODEL, + ), + ), + ], + options={ + "abstract": False, + }, + ), + migrations.AddField( + model_name="conversation", + name="agent", + field=models.ForeignKey( + blank=True, default=None, null=True, on_delete=django.db.models.deletion.SET_NULL, to="database.agent" + ), + ), + ] diff --git a/src/khoj/database/migrations/0031_alter_googleuser_locale.py b/src/khoj/database/migrations/0031_alter_googleuser_locale.py index 99c4573a..a5b4cdab 100644 --- a/src/khoj/database/migrations/0031_alter_googleuser_locale.py +++ b/src/khoj/database/migrations/0031_alter_googleuser_locale.py @@ -3,6 +3,18 @@ from django.db import migrations, models +def set_default_locale(apps, schema_editor): + return + + +def reverse_set_default_locale(apps, schema_editor): + GoogleUser = apps.get_model("database", "GoogleUser") + for user in GoogleUser.objects.all(): + if not user.locale: + user.locale = "en" + user.save() + + class Migration(migrations.Migration): dependencies = [ ("database", "0030_conversation_slug_and_title"), @@ -14,4 +26,5 @@ class Migration(migrations.Migration): name="locale", field=models.CharField(blank=True, default=None, max_length=200, null=True), ), + migrations.RunPython(set_default_locale, reverse_set_default_locale), ] diff --git a/src/khoj/database/migrations/0032_merge_20240322_0427.py b/src/khoj/database/migrations/0032_merge_20240322_0427.py new file mode 100644 index 00000000..aee557c0 --- /dev/null +++ b/src/khoj/database/migrations/0032_merge_20240322_0427.py @@ -0,0 +1,14 @@ +# Generated by Django 4.2.10 on 2024-03-22 04:27 + +from typing import List + +from django.db import migrations + + +class Migration(migrations.Migration): + dependencies = [ + ("database", "0031_agent_conversation_agent"), + ("database", "0031_alter_googleuser_locale"), + ] + + operations: List[str] = [] diff --git a/src/khoj/database/migrations/0033_rename_tuning_agent_personality.py b/src/khoj/database/migrations/0033_rename_tuning_agent_personality.py new file mode 100644 index 00000000..089c86c5 --- /dev/null +++ b/src/khoj/database/migrations/0033_rename_tuning_agent_personality.py @@ -0,0 +1,17 @@ +# Generated by Django 4.2.10 on 2024-03-23 16:01 + +from django.db import migrations + + +class Migration(migrations.Migration): + dependencies = [ + ("database", "0032_merge_20240322_0427"), + ] + + operations = [ + migrations.RenameField( + model_name="agent", + old_name="tuning", + new_name="personality", + ), + ] diff --git a/src/khoj/database/models/__init__.py b/src/khoj/database/models/__init__.py index dc532d78..cff3f065 100644 --- a/src/khoj/database/models/__init__.py +++ b/src/khoj/database/models/__init__.py @@ -1,7 +1,11 @@ import uuid +from random import choice from django.contrib.auth.models import AbstractUser +from django.core.exceptions import ValidationError from django.db import models +from django.db.models.signals import pre_save +from django.dispatch import receiver from pgvector.django import VectorField from phonenumber_field.modelfields import PhoneNumberField @@ -69,6 +73,52 @@ class Subscription(BaseModel): renewal_date = models.DateTimeField(null=True, default=None, blank=True) +class ChatModelOptions(BaseModel): + class ModelType(models.TextChoices): + OPENAI = "openai" + OFFLINE = "offline" + + max_prompt_size = models.IntegerField(default=None, null=True, blank=True) + tokenizer = models.CharField(max_length=200, default=None, null=True, blank=True) + chat_model = models.CharField(max_length=200, default="NousResearch/Hermes-2-Pro-Mistral-7B-GGUF") + model_type = models.CharField(max_length=200, choices=ModelType.choices, default=ModelType.OFFLINE) + + +class Agent(BaseModel): + creator = models.ForeignKey( + KhojUser, on_delete=models.CASCADE, default=None, null=True, blank=True + ) # Creator will only be null when the agents are managed by admin + name = models.CharField(max_length=200) + personality = models.TextField() + avatar = models.URLField(max_length=400, default=None, null=True, blank=True) + tools = models.JSONField(default=list) # List of tools the agent has access to, like online search or notes search + public = models.BooleanField(default=False) + managed_by_admin = models.BooleanField(default=False) + chat_model = models.ForeignKey(ChatModelOptions, on_delete=models.CASCADE) + slug = models.CharField(max_length=200) + + +@receiver(pre_save, sender=Agent) +def verify_agent(sender, instance, **kwargs): + # check if this is a new instance + if instance._state.adding: + if Agent.objects.filter(name=instance.name, public=True).exists(): + raise ValidationError(f"A public Agent with the name {instance.name} already exists.") + if Agent.objects.filter(name=instance.name, creator=instance.creator).exists(): + raise ValidationError(f"A private Agent with the name {instance.name} already exists.") + + slug = instance.name.lower().replace(" ", "-") + observed_random_numbers = set() + while Agent.objects.filter(slug=slug).exists(): + try: + random_number = choice([i for i in range(0, 1000) if i not in observed_random_numbers]) + except IndexError: + raise ValidationError("Unable to generate a unique slug for the Agent. Please try again later.") + observed_random_numbers.add(random_number) + slug = f"{slug}-{random_number}" + instance.slug = slug + + class NotionConfig(BaseModel): token = models.CharField(max_length=200) user = models.ForeignKey(KhojUser, on_delete=models.CASCADE) @@ -153,17 +203,6 @@ class SpeechToTextModelOptions(BaseModel): model_type = models.CharField(max_length=200, choices=ModelType.choices, default=ModelType.OFFLINE) -class ChatModelOptions(BaseModel): - class ModelType(models.TextChoices): - OPENAI = "openai" - OFFLINE = "offline" - - max_prompt_size = models.IntegerField(default=None, null=True, blank=True) - tokenizer = models.CharField(max_length=200, default=None, null=True, blank=True) - chat_model = models.CharField(max_length=200, default="NousResearch/Hermes-2-Pro-Mistral-7B-GGUF") - model_type = models.CharField(max_length=200, choices=ModelType.choices, default=ModelType.OFFLINE) - - class UserConversationConfig(BaseModel): user = models.OneToOneField(KhojUser, on_delete=models.CASCADE) setting = models.ForeignKey(ChatModelOptions, on_delete=models.CASCADE, default=None, null=True, blank=True) @@ -180,6 +219,7 @@ class Conversation(BaseModel): client = models.ForeignKey(ClientApplication, on_delete=models.CASCADE, default=None, null=True, blank=True) slug = models.CharField(max_length=200, default=None, null=True, blank=True) title = models.CharField(max_length=200, default=None, null=True, blank=True) + agent = models.ForeignKey(Agent, on_delete=models.SET_NULL, default=None, null=True, blank=True) class ReflectiveQuestion(BaseModel): diff --git a/src/khoj/interface/web/404.html b/src/khoj/interface/web/404.html index 7041ff80..0762bde8 100644 --- a/src/khoj/interface/web/404.html +++ b/src/khoj/interface/web/404.html @@ -2,14 +2,19 @@ Khoj: An AI Personal Assistant for your digital brain - - + + + + + {% import 'utils.html' as utils %} + {{ utils.heading_pane(user_photo, username, is_active, has_documents) }} +
-

Oops, this is awkward. That page couldn't be found.

+

Oops, this is awkward. Looks like there's nothing here.

- Go Home + Go Home
@@ -18,5 +23,34 @@ body.not-found { padding: 0 10% } + + body { + background-color: var(--background-color); + color: var(--main-text-color); + text-align: center; + font-family: var(--font-family); + font-size: medium; + font-weight: 300; + line-height: 1.5em; + height: 100vh; + margin: 0; + } + + body a.redirect-link { + font-size: 18px; + font-weight: bold; + background-color: var(--primary); + text-decoration: none; + border: 1px solid var(--main-text-color); + color: var(--main-text-color); + border-radius: 8px; + padding: 4px; + } + + body a.redirect-link:hover { + background-color: var(--main-text-color); + color: var(--primary); + } + diff --git a/src/khoj/interface/web/agent.html b/src/khoj/interface/web/agent.html new file mode 100644 index 00000000..6e6a5ef7 --- /dev/null +++ b/src/khoj/interface/web/agent.html @@ -0,0 +1,286 @@ + + + + + Khoj - Agents + + + + + + + + + {% import 'utils.html' as utils %} + {{ utils.heading_pane(user_photo, username, is_active, has_documents) }} +
+
+
+
Agent Settings
+
+
+
+
+ Agent Avatar + +
+
Personality
+
+

{{ agent.personality }}

+
+
+
+

Public

+ +
+ + + +
+
+
+ + + + + diff --git a/src/khoj/interface/web/agents.html b/src/khoj/interface/web/agents.html new file mode 100644 index 00000000..9408d2b0 --- /dev/null +++ b/src/khoj/interface/web/agents.html @@ -0,0 +1,203 @@ + + + + + Khoj - Agents + + + + + + + + + {% import 'utils.html' as utils %} + {{ utils.heading_pane(user_photo, username, is_active, has_documents) }} + + +
+
+
+

Agents

+ +
+ {% for agent in agents %} +
+ +
+ {{ agent.name }} +
+
+
+ +

{{ agent.name }}

+
+

{{ agent.personality }}

+
+
+ +
+
+ {% endfor %} +
+
+ + + + + diff --git a/src/khoj/interface/web/assets/khoj.css b/src/khoj/interface/web/assets/khoj.css index 7ba93c6a..3d7e7d4a 100644 --- a/src/khoj/interface/web/assets/khoj.css +++ b/src/khoj/interface/web/assets/khoj.css @@ -130,7 +130,7 @@ img.khoj-logo { background-color: var(--background-color); min-width: 160px; box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); - right: 15vw; + right: 5vw; top: 64px; z-index: 1; opacity: 0; diff --git a/src/khoj/interface/web/base_config.html b/src/khoj/interface/web/base_config.html index 5c26e060..870f4eb9 100644 --- a/src/khoj/interface/web/base_config.html +++ b/src/khoj/interface/web/base_config.html @@ -162,7 +162,7 @@ height: 40px; } .card-title { - font-size: 20px; + font-size: medium; font-weight: normal; margin: 0; padding: 0; diff --git a/src/khoj/interface/web/chat.html b/src/khoj/interface/web/chat.html index 416b4370..73e9753c 100644 --- a/src/khoj/interface/web/chat.html +++ b/src/khoj/interface/web/chat.html @@ -13,15 +13,17 @@ @@ -1215,13 +1329,27 @@ To get started, just start typing below. You can also type / to see a list of co +
-
+ + +
{%- endmacro %} diff --git a/src/khoj/main.py b/src/khoj/main.py index 5099002f..a9e333d2 100644 --- a/src/khoj/main.py +++ b/src/khoj/main.py @@ -160,7 +160,9 @@ def start_server(app, host=None, port=None, socket=None): if socket: uvicorn.run(app, proxy_headers=True, uds=socket, log_level="debug", use_colors=True, log_config=None) else: - uvicorn.run(app, host=host, port=port, log_level="debug", use_colors=True, log_config=None) + uvicorn.run( + app, host=host, port=port, log_level="debug", use_colors=True, log_config=None, timeout_keep_alive=60 + ) logger.info("πŸŒ’ Stopping Khoj") diff --git a/src/khoj/processor/content/markdown/markdown_to_entries.py b/src/khoj/processor/content/markdown/markdown_to_entries.py index 4cce4876..7274cf1c 100644 --- a/src/khoj/processor/content/markdown/markdown_to_entries.py +++ b/src/khoj/processor/content/markdown/markdown_to_entries.py @@ -114,7 +114,7 @@ class MarkdownToEntries(TextToEntries): # Append base filename to compiled entry for context to model # Increment heading level for heading entries and make filename as its top level heading prefix = f"# {stem}\n#" if heading else f"# {stem}\n" - compiled_entry = f"{prefix}{parsed_entry}" + compiled_entry = f"{entry_filename}\n{prefix}{parsed_entry}" entries.append( Entry( compiled=compiled_entry, diff --git a/src/khoj/processor/conversation/offline/chat_model.py b/src/khoj/processor/conversation/offline/chat_model.py index 67db9629..190422e1 100644 --- a/src/khoj/processor/conversation/offline/chat_model.py +++ b/src/khoj/processor/conversation/offline/chat_model.py @@ -7,6 +7,7 @@ from typing import Any, Iterator, List, Union from langchain.schema import ChatMessage from llama_cpp import Llama +from khoj.database.models import Agent from khoj.processor.conversation import prompts from khoj.processor.conversation.offline.utils import download_model from khoj.processor.conversation.utils import ( @@ -131,6 +132,7 @@ def converse_offline( tokenizer_name=None, location_data: LocationData = None, user_name: str = None, + agent: Agent = None, ) -> Union[ThreadedGenerator, Iterator[str]]: """ Converse with user using Llama @@ -140,6 +142,15 @@ def converse_offline( offline_chat_model = loaded_model or download_model(model) compiled_references_message = "\n\n".join({f"{item}" for item in references}) + current_date = datetime.now().strftime("%Y-%m-%d") + + if agent and agent.personality: + system_prompt = prompts.custom_system_prompt_offline_chat.format( + name=agent.name, bio=agent.personality, current_date=current_date + ) + else: + system_prompt = prompts.system_prompt_offline_chat.format(current_date=current_date) + conversation_primer = prompts.query_prompt.format(query=user_query) if location_data: @@ -169,10 +180,9 @@ def converse_offline( conversation_primer = f"{prompts.notes_conversation_offline.format(references=compiled_references_message)}\n{conversation_primer}" # Setup Prompt with Primer or Conversation History - current_date = datetime.now().strftime("%Y-%m-%d") messages = generate_chatml_messages_with_context( conversation_primer, - prompts.system_prompt_offline_chat.format(current_date=current_date), + system_prompt, conversation_log, model_name=model, loaded_model=offline_chat_model, diff --git a/src/khoj/processor/conversation/openai/gpt.py b/src/khoj/processor/conversation/openai/gpt.py index 8a2059af..584037ed 100644 --- a/src/khoj/processor/conversation/openai/gpt.py +++ b/src/khoj/processor/conversation/openai/gpt.py @@ -5,6 +5,7 @@ from typing import Dict, Optional from langchain.schema import ChatMessage +from khoj.database.models import Agent from khoj.processor.conversation import prompts from khoj.processor.conversation.openai.utils import ( chat_completion_with_backoff, @@ -115,6 +116,7 @@ def converse( tokenizer_name=None, location_data: LocationData = None, user_name: str = None, + agent: Agent = None, ): """ Converse with user using OpenAI's ChatGPT @@ -125,6 +127,13 @@ def converse( conversation_primer = prompts.query_prompt.format(query=user_query) + if agent and agent.personality: + system_prompt = prompts.custom_personality.format( + name=agent.name, bio=agent.personality, current_date=current_date + ) + else: + system_prompt = prompts.personality.format(current_date=current_date) + if location_data: location = f"{location_data.city}, {location_data.region}, {location_data.country}" location_prompt = prompts.user_location.format(location=location) @@ -152,7 +161,7 @@ def converse( # Setup Prompt with Primer or Conversation History messages = generate_chatml_messages_with_context( conversation_primer, - prompts.personality.format(current_date=current_date), + system_prompt, conversation_log, model, max_prompt_size, diff --git a/src/khoj/processor/conversation/prompts.py b/src/khoj/processor/conversation/prompts.py index 9a59688a..2e9b6715 100644 --- a/src/khoj/processor/conversation/prompts.py +++ b/src/khoj/processor/conversation/prompts.py @@ -21,6 +21,24 @@ Today is {current_date} in UTC. """.strip() ) +custom_personality = PromptTemplate.from_template( + """ +You are {name}, a personal agent on Khoj. +Use your general knowledge and past conversation with the user as context to inform your responses. +You were created by Khoj Inc. with the following capabilities: + +- You *CAN REMEMBER ALL NOTES and PERSONAL INFORMATION FOREVER* that the user ever shares with you. +- Users can share files and other information with you using the Khoj Desktop, Obsidian or Emacs app. They can also drag and drop their files into the chat window. +- Say "I don't know" or "I don't understand" if you don't know what to say or if you don't know the answer to a question. +- Ask crisp follow-up questions to get additional context, when the answer cannot be inferred from the provided notes or past conversations. +- Sometimes the user will share personal information that needs to be remembered, like an account ID or a residential address. These can be acknowledged with a simple "Got it" or "Okay". + +Today is {current_date} in UTC. + +Instructions:\n{bio} +""".strip() +) + ## General Conversation ## -- general_conversation = PromptTemplate.from_template( @@ -61,6 +79,20 @@ Today is {current_date} in UTC. """.strip() ) +custom_system_prompt_offline_chat = PromptTemplate.from_template( + """ +You are {name}, a personal agent on Khoj. +- Use your general knowledge and past conversation with the user as context to inform your responses. +- If you do not know the answer, say 'I don't know.' +- Think step-by-step and ask questions to get the necessary information to answer the user's question. +- Do not print verbatim Notes unless necessary. + +Today is {current_date} in UTC. + +Instructions:\n{bio} + """.strip() +) + ## Notes Conversation ## -- notes_conversation = PromptTemplate.from_template( diff --git a/src/khoj/routers/api.py b/src/khoj/routers/api.py index 1639f08f..0977919a 100644 --- a/src/khoj/routers/api.py +++ b/src/khoj/routers/api.py @@ -13,7 +13,7 @@ from fastapi.requests import Request from fastapi.responses import Response from starlette.authentication import requires -from khoj.configure import configure_server, initialize_content +from khoj.configure import initialize_content from khoj.database.adapters import ( ConversationAdapters, EntryAdapters, diff --git a/src/khoj/routers/api_agents.py b/src/khoj/routers/api_agents.py new file mode 100644 index 00000000..cdef6206 --- /dev/null +++ b/src/khoj/routers/api_agents.py @@ -0,0 +1,43 @@ +import json +import logging + +from fastapi import APIRouter, Request +from fastapi.requests import Request +from fastapi.responses import Response + +from khoj.database.adapters import AgentAdapters +from khoj.database.models import KhojUser +from khoj.routers.helpers import CommonQueryParams + +# Initialize Router +logger = logging.getLogger(__name__) + + +api_agents = APIRouter() + + +@api_agents.get("", response_class=Response) +async def all_agents( + request: Request, + common: CommonQueryParams, +) -> Response: + user: KhojUser = request.user.object if request.user.is_authenticated else None + agents = await AgentAdapters.aget_all_accessible_agents(user) + agents_packet = list() + for agent in agents: + agents_packet.append( + { + "slug": agent.slug, + "avatar": agent.avatar, + "name": agent.name, + "personality": agent.personality, + "public": agent.public, + "creator": agent.creator.username if agent.creator else None, + "managed_by_admin": agent.managed_by_admin, + } + ) + + # Make sure that the agent named 'khoj' is first in the list. Everything else is sorted by name. + agents_packet.sort(key=lambda x: x["name"]) + agents_packet.sort(key=lambda x: x["slug"] == "khoj", reverse=True) + return Response(content=json.dumps(agents_packet), media_type="application/json", status_code=200) diff --git a/src/khoj/routers/api_chat.py b/src/khoj/routers/api_chat.py index 94642490..5c417a28 100644 --- a/src/khoj/routers/api_chat.py +++ b/src/khoj/routers/api_chat.py @@ -12,7 +12,11 @@ from starlette.authentication import requires from khoj.database.adapters import ConversationAdapters, EntryAdapters, aget_user_name from khoj.database.models import KhojUser -from khoj.processor.conversation.prompts import help_message, no_entries_found +from khoj.processor.conversation.prompts import ( + help_message, + no_entries_found, + no_notes_found, +) from khoj.processor.conversation.utils import save_to_conversation_log from khoj.processor.tools.online_search import ( online_search_enabled, @@ -85,9 +89,22 @@ def chat_history( status_code=404, ) + agent_metadata = None + if conversation.agent: + agent_metadata = { + "slug": conversation.agent.slug, + "name": conversation.agent.name, + "avatar": conversation.agent.avatar, + "isCreator": conversation.agent.creator == user, + } + meta_log = conversation.conversation_log meta_log.update( - {"conversation_id": conversation.id, "slug": conversation.title if conversation.title else conversation.slug} + { + "conversation_id": conversation.id, + "slug": conversation.title if conversation.title else conversation.slug, + "agent": agent_metadata, + } ) update_telemetry_state( @@ -152,18 +169,24 @@ def chat_sessions( async def create_chat_session( request: Request, common: CommonQueryParams, + agent_slug: Optional[str] = None, ): user = request.user.object # Create new Conversation Session - conversation = await ConversationAdapters.acreate_conversation_session(user, request.user.client_app) + conversation = await ConversationAdapters.acreate_conversation_session(user, request.user.client_app, agent_slug) response = {"conversation_id": conversation.id} + conversation_metadata = { + "agent": agent_slug, + } + update_telemetry_state( request=request, telemetry_type="api", api="create_chat_sessions", + metadata=conversation_metadata, **common.__dict__, ) @@ -242,7 +265,7 @@ async def chat( ) -> Response: user: KhojUser = request.user.object q = unquote(q) - logger.info("Chat request by {user.username}: {q}") + logger.info(f"Chat request by {user.username}: {q}") await is_ready_to_chat(user) conversation_commands = [get_conversation_command(query=q, any_references=True)] @@ -295,6 +318,14 @@ async def chat( response_obj = {"response": no_entries_found_format} return Response(content=json.dumps(response_obj), media_type="text/plain", status_code=200) + if conversation_commands == [ConversationCommand.Notes] and is_none_or_empty(compiled_references): + no_notes_found_format = no_notes_found.format() + if stream: + return StreamingResponse(iter([no_notes_found_format]), media_type="text/event-stream", status_code=200) + else: + response_obj = {"response": no_notes_found_format} + return Response(content=json.dumps(response_obj), media_type="text/plain", status_code=200) + if ConversationCommand.Notes in conversation_commands and is_none_or_empty(compiled_references): conversation_commands.remove(ConversationCommand.Notes) @@ -356,6 +387,7 @@ async def chat( llm_response, chat_metadata = await agenerate_chat_response( defiltered_query, meta_log, + conversation, compiled_references, online_results, inferred_queries, @@ -369,6 +401,7 @@ async def chat( cmd_set = set([cmd.value for cmd in conversation_commands]) chat_metadata["conversation_command"] = cmd_set + chat_metadata["agent"] = conversation.agent.slug if conversation.agent else None update_telemetry_state( request=request, diff --git a/src/khoj/routers/auth.py b/src/khoj/routers/auth.py index 89fef85b..1d7bbfdd 100644 --- a/src/khoj/routers/auth.py +++ b/src/khoj/routers/auth.py @@ -7,6 +7,7 @@ from starlette.authentication import requires from starlette.config import Config from starlette.requests import Request from starlette.responses import HTMLResponse, RedirectResponse, Response +from starlette.status import HTTP_302_FOUND from khoj.database.adapters import ( create_khoj_token, @@ -90,6 +91,7 @@ async def delete_token(request: Request, token: str) -> str: @auth_router.post("/redirect") async def auth(request: Request): form = await request.form() + next_url = request.query_params.get("next", "/") credential = form.get("credential") csrf_token_cookie = request.cookies.get("g_csrf_token") @@ -117,9 +119,9 @@ async def auth(request: Request): metadata={"user_id": str(khoj_user.uuid)}, ) logger.log(logging.INFO, f"New User Created: {khoj_user.uuid}") - RedirectResponse(url="/?status=welcome") + return RedirectResponse(url=f"{next_url}", status_code=HTTP_302_FOUND) - return RedirectResponse(url="/") + return RedirectResponse(url=f"{next_url}") @auth_router.get("/logout") diff --git a/src/khoj/routers/helpers.py b/src/khoj/routers/helpers.py index 8a6925f8..00ca355c 100644 --- a/src/khoj/routers/helpers.py +++ b/src/khoj/routers/helpers.py @@ -10,10 +10,11 @@ import openai from fastapi import Depends, Header, HTTPException, Request, UploadFile from starlette.authentication import has_required_scope -from khoj.database.adapters import ConversationAdapters, EntryAdapters +from khoj.database.adapters import AgentAdapters, ConversationAdapters, EntryAdapters from khoj.database.models import ( ChatModelOptions, ClientApplication, + Conversation, KhojUser, Subscription, TextToImageModelConfig, @@ -407,6 +408,7 @@ async def send_message_to_model_wrapper( def generate_chat_response( q: str, meta_log: dict, + conversation: Conversation, compiled_references: List[str] = [], online_results: Dict[str, Dict] = {}, inferred_queries: List[str] = [], @@ -422,6 +424,7 @@ def generate_chat_response( logger.debug(f"Conversation Types: {conversation_commands}") metadata = {} + agent = AgentAdapters.get_conversation_agent_by_id(conversation.agent.id) if conversation.agent else None try: partial_completion = partial( @@ -436,7 +439,7 @@ def generate_chat_response( conversation_id=conversation_id, ) - conversation_config = ConversationAdapters.get_valid_conversation_config(user) + conversation_config = ConversationAdapters.get_valid_conversation_config(user, conversation) if conversation_config.model_type == "offline": if state.offline_chat_processor_config is None or state.offline_chat_processor_config.loaded_model is None: state.offline_chat_processor_config = OfflineChatProcessorModel(conversation_config.chat_model) @@ -455,6 +458,7 @@ def generate_chat_response( tokenizer_name=conversation_config.tokenizer, location_data=location_data, user_name=user_name, + agent=agent, ) elif conversation_config.model_type == "openai": @@ -474,6 +478,7 @@ def generate_chat_response( tokenizer_name=conversation_config.tokenizer, location_data=location_data, user_name=user_name, + agent=agent, ) metadata.update({"chat_model": conversation_config.chat_model}) diff --git a/src/khoj/routers/web_client.py b/src/khoj/routers/web_client.py index ddbcc283..cb03cb89 100644 --- a/src/khoj/routers/web_client.py +++ b/src/khoj/routers/web_client.py @@ -10,6 +10,7 @@ from starlette.authentication import has_required_scope, requires from khoj.database import adapters from khoj.database.adapters import ( + AgentAdapters, ConversationAdapters, EntryAdapters, get_user_github_config, @@ -114,8 +115,8 @@ def chat_page(request: Request): @web_client.get("/login", response_class=FileResponse) def login_page(request: Request): + next_url = request.query_params.get("next", "/") if request.user.is_authenticated: - next_url = request.query_params.get("next", "/") return RedirectResponse(url=next_url) google_client_id = os.environ.get("GOOGLE_CLIENT_ID") redirect_uri = str(request.app.url_path_for("auth")) @@ -124,7 +125,85 @@ def login_page(request: Request): context={ "request": request, "google_client_id": google_client_id, - "redirect_uri": redirect_uri, + "redirect_uri": f"{redirect_uri}?next={next_url}", + }, + ) + + +@web_client.get("/agents", response_class=HTMLResponse) +def agents_page(request: Request): + user: KhojUser = request.user.object if request.user.is_authenticated else None + user_picture = request.session.get("user", {}).get("picture") if user else None + agents = AgentAdapters.get_all_accessible_agents(user) + agents_packet = list() + for agent in agents: + agents_packet.append( + { + "slug": agent.slug, + "avatar": agent.avatar, + "name": agent.name, + "personality": agent.personality, + "public": agent.public, + "creator": agent.creator.username if agent.creator else None, + "managed_by_admin": agent.managed_by_admin, + } + ) + return templates.TemplateResponse( + "agents.html", + context={ + "request": request, + "agents": agents_packet, + "khoj_version": state.khoj_version, + "username": user.username if user else None, + "has_documents": False, + "is_active": has_required_scope(request, ["premium"]), + "user_photo": user_picture, + }, + ) + + +@web_client.get("/agent/{agent_slug}", response_class=HTMLResponse) +def agent_page(request: Request, agent_slug: str): + user: KhojUser = request.user.object if request.user.is_authenticated else None + user_picture = request.session.get("user", {}).get("picture") if user else None + + agent = AgentAdapters.get_agent_by_slug(agent_slug) + + if agent == None: + return templates.TemplateResponse( + "404.html", + context={ + "request": request, + "khoj_version": state.khoj_version, + "username": user.username if user else None, + "has_documents": False, + "is_active": has_required_scope(request, ["premium"]), + "user_photo": user_picture, + }, + ) + + agent_metadata = { + "slug": agent.slug, + "avatar": agent.avatar, + "name": agent.name, + "personality": agent.personality, + "public": agent.public, + "creator": agent.creator.username if agent.creator else None, + "managed_by_admin": agent.managed_by_admin, + "chat_model": agent.chat_model.chat_model, + "creator_not_self": agent.creator != user, + } + + return templates.TemplateResponse( + "agent.html", + context={ + "request": request, + "agent": agent_metadata, + "khoj_version": state.khoj_version, + "username": user.username if user else None, + "has_documents": False, + "is_active": has_required_scope(request, ["premium"]), + "user_photo": user_picture, }, ) diff --git a/tests/conftest.py b/tests/conftest.py index a7ff1512..8533ba4f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -12,6 +12,7 @@ from khoj.configure import ( configure_search_types, ) from khoj.database.models import ( + Agent, GithubConfig, GithubRepoConfig, KhojApiUser, @@ -181,6 +182,28 @@ def api_user4(default_user4): ) +@pytest.mark.django_db +@pytest.fixture +def offline_agent(): + chat_model = ChatModelOptionsFactory() + return Agent.objects.create( + name="Accountant", + chat_model=chat_model, + personality="You are a certified CPA. You are able to tell me how much I've spent based on my notes. Regardless of what I ask, you should always respond with the total amount I've spent. ALWAYS RESPOND WITH A SUMMARY TOTAL OF HOW MUCH MONEY I HAVE SPENT.", + ) + + +@pytest.mark.django_db +@pytest.fixture +def openai_agent(): + chat_model = ChatModelOptionsFactory(chat_model="gpt-3.5-turbo", model_type="openai") + return Agent.objects.create( + name="Accountant", + chat_model=chat_model, + personality="You are a certified CPA. You are able to tell me how much I've spent based on my notes. Regardless of what I ask, you should always respond with the total amount I've spent.", + ) + + @pytest.fixture(scope="session") def search_models(search_config: SearchConfig): search_models = SearchModels() diff --git a/tests/test_markdown_to_entries.py b/tests/test_markdown_to_entries.py index 4a4a75f3..12ea238e 100644 --- a/tests/test_markdown_to_entries.py +++ b/tests/test_markdown_to_entries.py @@ -34,7 +34,9 @@ def test_markdown_file_with_no_headings_to_jsonl(tmp_path): # Ensure raw entry with no headings do not get heading prefix prepended assert not jsonl_data[0]["raw"].startswith("#") # Ensure compiled entry has filename prepended as top level heading - assert jsonl_data[0]["compiled"].startswith(expected_heading) + assert expected_heading in jsonl_data[0]["compiled"] + # Ensure compiled entry also includes the file name + assert str(tmp_path) in jsonl_data[0]["compiled"] def test_single_markdown_entry_to_jsonl(tmp_path): diff --git a/tests/test_offline_chat_actors.py b/tests/test_offline_chat_actors.py index f82a5fe5..77f2b105 100644 --- a/tests/test_offline_chat_actors.py +++ b/tests/test_offline_chat_actors.py @@ -467,6 +467,47 @@ My sister, Aiyla is married to Tolga. They have 3 kids, Yildiz, Ali and Ahmet."" ) +# ---------------------------------------------------------------------------------------------------- +@pytest.mark.chatquality +def test_agent_prompt_should_be_used(loaded_model, offline_agent): + "Chat actor should ask be tuned to think like an accountant based on the agent definition" + # Arrange + context = [ + f"""I went to the store and bought some bananas for 2.20""", + f"""I went to the store and bought some apples for 1.30""", + f"""I went to the store and bought some oranges for 6.00""", + ] + + # Act + response_gen = converse_offline( + references=context, # Assume context retrieved from notes for the user_query + user_query="What did I buy?", + loaded_model=loaded_model, + ) + response = "".join([response_chunk for response_chunk in response_gen]) + + # Assert that the model without the agent prompt does not include the summary of purchases + expected_responses = ["9.50", "9.5"] + assert all([expected_response not in response for expected_response in expected_responses]), ( + "Expected chat actor to summarize values of purchases" + response + ) + + # Act + response_gen = converse_offline( + references=context, # Assume context retrieved from notes for the user_query + user_query="What did I buy?", + loaded_model=loaded_model, + agent=offline_agent, + ) + response = "".join([response_chunk for response_chunk in response_gen]) + + # Assert that the model with the agent prompt does include the summary of purchases + expected_responses = ["9.50", "9.5"] + assert any([expected_response in response for expected_response in expected_responses]), ( + "Expected chat actor to summarize values of purchases" + response + ) + + # ---------------------------------------------------------------------------------------------------- def test_chat_does_not_exceed_prompt_size(loaded_model): "Ensure chat context and response together do not exceed max prompt size for the model" diff --git a/tests/test_offline_chat_director.py b/tests/test_offline_chat_director.py index ed47bed7..b5e546d0 100644 --- a/tests/test_offline_chat_director.py +++ b/tests/test_offline_chat_director.py @@ -6,6 +6,7 @@ import pytest from faker import Faker from freezegun import freeze_time +from khoj.database.models import Agent, KhojUser from khoj.processor.conversation import prompts from khoj.processor.conversation.utils import message_to_log from khoj.routers.helpers import aget_relevant_information_sources @@ -26,20 +27,20 @@ def generate_history(message_list): # Generate conversation logs conversation_log = {"chat": []} for user_message, gpt_message, context in message_list: - conversation_log["chat"] += message_to_log( + message_to_log( user_message, gpt_message, {"context": context, "intent": {"query": user_message, "inferred-queries": f'["{user_message}"]'}}, + conversation_log=conversation_log.get("chat", []), ) return conversation_log -def populate_chat_history(message_list, user): +def create_conversation(message_list, user, agent=None): # Generate conversation logs conversation_log = generate_history(message_list) - # Update Conversation Metadata Logs in Database - ConversationFactory(user=user, conversation_log=conversation_log) + return ConversationFactory(user=user, conversation_log=conversation_log, agent=agent) # Tests @@ -114,7 +115,7 @@ def test_answer_from_chat_history(client_offline_chat, default_user2): ("Hello, my name is Testatron. Who are you?", "Hi, I am Khoj, a personal assistant. How can I help?", []), ("When was I born?", "You were born on 1st April 1984.", []), ] - populate_chat_history(message_list, default_user2) + create_conversation(message_list, default_user2) # Act response = client_offline_chat.get(f'/api/chat?q="What is my name?"&stream=true') @@ -141,7 +142,7 @@ def test_answer_from_currently_retrieved_content(client_offline_chat, default_us ["Testatron was born on 1st April 1984 in Testville."], ), ] - populate_chat_history(message_list, default_user2) + create_conversation(message_list, default_user2) # Act response = client_offline_chat.get(f'/api/chat?q="Where was Xi Li born?"') @@ -165,7 +166,7 @@ def test_answer_from_chat_history_and_previously_retrieved_content(client_offlin ["Testatron was born on 1st April 1984 in Testville."], ), ] - populate_chat_history(message_list, default_user2) + create_conversation(message_list, default_user2) # Act response = client_offline_chat.get(f'/api/chat?q="Where was I born?"') @@ -191,7 +192,7 @@ def test_answer_from_chat_history_and_currently_retrieved_content(client_offline ("Hello, my name is Xi Li. Who are you?", "Hi, I am Khoj, a personal assistant. How can I help?", []), ("When was I born?", "You were born on 1st April 1984.", []), ] - populate_chat_history(message_list, default_user2) + create_conversation(message_list, default_user2) # Act response = client_offline_chat.get(f'/api/chat?q="Where was I born?"') @@ -217,7 +218,7 @@ def test_no_answer_in_chat_history_or_retrieved_content(client_offline_chat, def ("Hello, my name is Testatron. Who are you?", "Hi, I am Khoj, a personal assistant. How can I help?", []), ("When was I born?", "You were born on 1st April 1984.", []), ] - populate_chat_history(message_list, default_user2) + create_conversation(message_list, default_user2) # Act response = client_offline_chat.get(f'/api/chat?q="Where was I born?"&stream=true') @@ -238,7 +239,7 @@ def test_answer_using_general_command(client_offline_chat, default_user2): # Arrange query = urllib.parse.quote("/general Where was Xi Li born?") message_list = [] - populate_chat_history(message_list, default_user2) + create_conversation(message_list, default_user2) # Act response = client_offline_chat.get(f"/api/chat?q={query}&stream=true") @@ -256,7 +257,7 @@ def test_answer_from_retrieved_content_using_notes_command(client_offline_chat, # Arrange query = urllib.parse.quote("/notes Where was Xi Li born?") message_list = [] - populate_chat_history(message_list, default_user2) + create_conversation(message_list, default_user2) # Act response = client_offline_chat.get(f"/api/chat?q={query}&stream=true") @@ -275,7 +276,7 @@ def test_answer_using_file_filter(client_offline_chat, default_user2): no_answer_query = urllib.parse.quote('Where was Xi Li born? file:"Namita.markdown"') answer_query = urllib.parse.quote('Where was Xi Li born? file:"Xi Li.markdown"') message_list = [] - populate_chat_history(message_list, default_user2) + create_conversation(message_list, default_user2) # Act no_answer_response = client_offline_chat.get(f"/api/chat?q={no_answer_query}&stream=true").content.decode("utf-8") @@ -293,7 +294,7 @@ def test_answer_not_known_using_notes_command(client_offline_chat, default_user2 # Arrange query = urllib.parse.quote("/notes Where was Testatron born?") message_list = [] - populate_chat_history(message_list, default_user2) + create_conversation(message_list, default_user2) # Act response = client_offline_chat.get(f"/api/chat?q={query}&stream=true") @@ -351,7 +352,7 @@ def test_answer_general_question_not_in_chat_history_or_retrieved_content(client ("When was I born?", "You were born on 1st April 1984.", []), ("Where was I born?", "You were born Testville.", []), ] - populate_chat_history(message_list, default_user2) + create_conversation(message_list, default_user2) # Act response = client_offline_chat.get( @@ -394,14 +395,14 @@ def test_ask_for_clarification_if_not_enough_context_in_question(client_offline_ @pytest.mark.xfail(reason="Chat director not capable of answering this question yet") @pytest.mark.chatquality @pytest.mark.django_db(transaction=True) -def test_answer_in_chat_history_beyond_lookback_window(client_offline_chat, default_user2): +def test_answer_in_chat_history_beyond_lookback_window(client_offline_chat, default_user2: KhojUser): # Arrange message_list = [ ("Hello, my name is Testatron. Who are you?", "Hi, I am Khoj, a personal assistant. How can I help?", []), ("When was I born?", "You were born on 1st April 1984.", []), ("Where was I born?", "You were born Testville.", []), ] - populate_chat_history(message_list, default_user2) + create_conversation(message_list, default_user2) # Act response = client_offline_chat.get(f'/api/chat?q="What is my name?"&stream=true') @@ -415,13 +416,77 @@ def test_answer_in_chat_history_beyond_lookback_window(client_offline_chat, defa ) +# ---------------------------------------------------------------------------------------------------- +@pytest.mark.chatquality +@pytest.mark.django_db(transaction=True) +def test_answer_in_chat_history_by_conversation_id(client_offline_chat, default_user2: KhojUser): + # Arrange + message_list = [ + ("Hello, my name is Testatron. Who are you?", "Hi, I am Khoj, a personal assistant. How can I help?", []), + ("When was I born?", "You were born on 1st April 1984.", []), + ("What's my favorite color", "Your favorite color is green.", []), + ("Where was I born?", "You were born Testville.", []), + ] + message_list2 = [ + ("Hello, my name is Julia. Who are you?", "Hi, I am Khoj, a personal assistant. How can I help?", []), + ("When was I born?", "You were born on 14th August 1947.", []), + ("What's my favorite color", "Your favorite color is maroon.", []), + ("Where was I born?", "You were born in a potato farm.", []), + ] + conversation = create_conversation(message_list, default_user2) + create_conversation(message_list2, default_user2) + + # Act + response = client_offline_chat.get( + f'/api/chat?q="What is my favorite color?"&conversation_id={conversation.id}&stream=true' + ) + response_message = response.content.decode("utf-8") + + # Assert + expected_responses = ["green"] + assert response.status_code == 200 + assert any([expected_response in response_message.lower() for expected_response in expected_responses]), ( + "Expected green in response, but got: " + response_message + ) + + +# ---------------------------------------------------------------------------------------------------- +@pytest.mark.xfail(reason="Chat director not great at adhering to agent instructions yet") +@pytest.mark.chatquality +@pytest.mark.django_db(transaction=True) +def test_answer_in_chat_history_by_conversation_id_with_agent( + client_offline_chat, default_user2: KhojUser, offline_agent: Agent +): + # Arrange + message_list = [ + ("Hello, my name is Testatron. Who are you?", "Hi, I am Khoj, a personal assistant. How can I help?", []), + ("When was I born?", "You were born on 1st April 1984.", []), + ("What's my favorite color", "Your favorite color is green.", []), + ("Where was I born?", "You were born Testville.", []), + ("What did I buy?", "You bought an apple for 2.00, an orange for 3.00, and a potato for 8.00", []), + ] + conversation = create_conversation(message_list, default_user2, offline_agent) + + # Act + query = urllib.parse.quote("/general What did I eat for breakfast?") + response = client_offline_chat.get(f"/api/chat?q={query}&conversation_id={conversation.id}&stream=true") + response_message = response.content.decode("utf-8") + + # Assert that agent only responds with the summary of spending + expected_responses = ["13.00", "13", "13.0", "thirteen"] + assert response.status_code == 200 + assert any([expected_response in response_message.lower() for expected_response in expected_responses]), ( + "Expected green in response, but got: " + response_message + ) + + @pytest.mark.chatquality @pytest.mark.django_db(transaction=True) def test_answer_chat_history_very_long(client_offline_chat, default_user2): # Arrange message_list = [(" ".join([fake.paragraph() for _ in range(50)]), fake.sentence(), []) for _ in range(10)] - populate_chat_history(message_list, default_user2) + create_conversation(message_list, default_user2) # Act response = client_offline_chat.get(f'/api/chat?q="What is my name?"&stream=true') @@ -525,7 +590,7 @@ async def test_get_correct_tools_with_chat_history(client_offline_chat, default_ ), ("What's up in New York City?", "A Pride parade has recently been held in New York City, on July 31st.", []), ] - chat_history = populate_chat_history(chat_log, default_user2) + chat_history = create_conversation(chat_log, default_user2) # Act tools = await aget_relevant_information_sources(user_query, chat_history) diff --git a/tests/test_openai_chat_actors.py b/tests/test_openai_chat_actors.py index e7c4f895..5c2855b2 100644 --- a/tests/test_openai_chat_actors.py +++ b/tests/test_openai_chat_actors.py @@ -414,6 +414,42 @@ My sister, Aiyla is married to Tolga. They have 3 kids, Yildiz, Ali and Ahmet."" ) +# ---------------------------------------------------------------------------------------------------- +@pytest.mark.chatquality +def test_agent_prompt_should_be_used(openai_agent): + "Chat actor should ask be tuned to think like an accountant based on the agent definition" + # Arrange + context = [ + f"""I went to the store and bought some bananas for 2.20""", + f"""I went to the store and bought some apples for 1.30""", + f"""I went to the store and bought some oranges for 6.00""", + ] + expected_responses = ["9.50", "9.5"] + + # Act + response_gen = converse( + references=context, # Assume context retrieved from notes for the user_query + user_query="What did I buy?", + api_key=api_key, + ) + no_agent_response = "".join([response_chunk for response_chunk in response_gen]) + response_gen = converse( + references=context, # Assume context retrieved from notes for the user_query + user_query="What did I buy?", + api_key=api_key, + agent=openai_agent, + ) + agent_response = "".join([response_chunk for response_chunk in response_gen]) + + # Assert that the model without the agent prompt does not include the summary of purchases + assert all([expected_response not in no_agent_response for expected_response in expected_responses]), ( + "Expected chat actor to summarize values of purchases" + no_agent_response + ) + assert any([expected_response in agent_response for expected_response in expected_responses]), ( + "Expected chat actor to summarize values of purchases" + agent_response + ) + + # ---------------------------------------------------------------------------------------------------- @pytest.mark.anyio @pytest.mark.django_db(transaction=True) diff --git a/tests/test_openai_chat_director.py b/tests/test_openai_chat_director.py index 890605b1..fffaa0d9 100644 --- a/tests/test_openai_chat_director.py +++ b/tests/test_openai_chat_director.py @@ -5,13 +5,10 @@ from urllib.parse import quote import pytest from freezegun import freeze_time -from khoj.database.models import KhojUser +from khoj.database.models import Agent, KhojUser from khoj.processor.conversation import prompts from khoj.processor.conversation.utils import message_to_log -from khoj.routers.helpers import ( - aget_relevant_information_sources, - aget_relevant_output_modes, -) +from khoj.routers.helpers import aget_relevant_information_sources from tests.helpers import ConversationFactory # Initialize variables for tests @@ -29,20 +26,21 @@ def generate_history(message_list): # Generate conversation logs conversation_log = {"chat": []} for user_message, gpt_message, context in message_list: - conversation_log["chat"] += message_to_log( + message_to_log( user_message, gpt_message, {"context": context, "intent": {"query": user_message, "inferred-queries": f'["{user_message}"]'}}, + conversation_log=conversation_log.get("chat", []), ) return conversation_log -def populate_chat_history(message_list, user): +def create_conversation(message_list, user, agent=None): # Generate conversation logs conversation_log = generate_history(message_list) # Update Conversation Metadata Logs in Database - ConversationFactory(user=user, conversation_log=conversation_log) + return ConversationFactory(user=user, conversation_log=conversation_log, agent=agent) # Tests @@ -116,7 +114,7 @@ def test_answer_from_chat_history(chat_client, default_user2: KhojUser): ("Hello, my name is Testatron. Who are you?", "Hi, I am Khoj, a personal assistant. How can I help?", []), ("When was I born?", "You were born on 1st April 1984.", []), ] - populate_chat_history(message_list, default_user2) + create_conversation(message_list, default_user2) # Act response = chat_client.get(f'/api/chat?q="What is my name?"&stream=true') @@ -143,7 +141,7 @@ def test_answer_from_currently_retrieved_content(chat_client, default_user2: Kho ["Testatron was born on 1st April 1984 in Testville."], ), ] - populate_chat_history(message_list, default_user2) + create_conversation(message_list, default_user2) # Act response = chat_client.get(f'/api/chat?q="Where was Xi Li born?"') @@ -167,7 +165,7 @@ def test_answer_from_chat_history_and_previously_retrieved_content(chat_client_n ["Testatron was born on 1st April 1984 in Testville."], ), ] - populate_chat_history(message_list, default_user2) + create_conversation(message_list, default_user2) # Act response = chat_client_no_background.get(f'/api/chat?q="Where was I born?"') @@ -190,7 +188,7 @@ def test_answer_from_chat_history_and_currently_retrieved_content(chat_client, d ("Hello, my name is Xi Li. Who are you?", "Hi, I am Khoj, a personal assistant. How can I help?", []), ("When was I born?", "You were born on 1st April 1984.", []), ] - populate_chat_history(message_list, default_user2) + create_conversation(message_list, default_user2) # Act response = chat_client.get(f'/api/chat?q="Where was I born?"') @@ -215,7 +213,7 @@ def test_no_answer_in_chat_history_or_retrieved_content(chat_client, default_use ("Hello, my name is Testatron. Who are you?", "Hi, I am Khoj, a personal assistant. How can I help?", []), ("When was I born?", "You were born on 1st April 1984.", []), ] - populate_chat_history(message_list, default_user2) + create_conversation(message_list, default_user2) # Act response = chat_client.get(f'/api/chat?q="Where was I born?"&stream=true') @@ -244,7 +242,7 @@ def test_answer_using_general_command(chat_client, default_user2: KhojUser): # Arrange query = urllib.parse.quote("/general Where was Xi Li born?") message_list = [] - populate_chat_history(message_list, default_user2) + create_conversation(message_list, default_user2) # Act response = chat_client.get(f"/api/chat?q={query}&stream=true") @@ -262,7 +260,7 @@ def test_answer_from_retrieved_content_using_notes_command(chat_client, default_ # Arrange query = urllib.parse.quote("/notes Where was Xi Li born?") message_list = [] - populate_chat_history(message_list, default_user2) + create_conversation(message_list, default_user2) # Act response = chat_client.get(f"/api/chat?q={query}&stream=true") @@ -280,7 +278,7 @@ def test_answer_not_known_using_notes_command(chat_client_no_background, default # Arrange query = urllib.parse.quote("/notes Where was Testatron born?") message_list = [] - populate_chat_history(message_list, default_user2) + create_conversation(message_list, default_user2) # Act response = chat_client_no_background.get(f"/api/chat?q={query}&stream=true") @@ -335,7 +333,7 @@ def test_answer_general_question_not_in_chat_history_or_retrieved_content(chat_c ("When was I born?", "You were born on 1st April 1984.", []), ("Where was I born?", "You were born Testville.", []), ] - populate_chat_history(message_list, default_user2) + create_conversation(message_list, default_user2) # Act response = chat_client.get(f'/api/chat?q="Write a haiku about unit testing. Do not say anything else."&stream=true') @@ -387,7 +385,7 @@ def test_answer_in_chat_history_beyond_lookback_window(chat_client, default_user ("When was I born?", "You were born on 1st April 1984.", []), ("Where was I born?", "You were born Testville.", []), ] - populate_chat_history(message_list, default_user2) + create_conversation(message_list, default_user2) # Act response = chat_client.get(f'/api/chat?q="What is my name?"&stream=true') @@ -401,6 +399,68 @@ def test_answer_in_chat_history_beyond_lookback_window(chat_client, default_user ) +# ---------------------------------------------------------------------------------------------------- +@pytest.mark.chatquality +@pytest.mark.django_db(transaction=True) +def test_answer_in_chat_history_by_conversation_id(chat_client, default_user2: KhojUser): + # Arrange + message_list = [ + ("Hello, my name is Testatron. Who are you?", "Hi, I am Khoj, a personal assistant. How can I help?", []), + ("When was I born?", "You were born on 1st April 1984.", []), + ("What's my favorite color", "Your favorite color is green.", []), + ("Where was I born?", "You were born Testville.", []), + ] + message_list2 = [ + ("Hello, my name is Julia. Who are you?", "Hi, I am Khoj, a personal assistant. How can I help?", []), + ("When was I born?", "You were born on 14th August 1947.", []), + ("What's my favorite color", "Your favorite color is maroon.", []), + ("Where was I born?", "You were born in a potato farm.", []), + ] + conversation = create_conversation(message_list, default_user2) + create_conversation(message_list2, default_user2) + + # Act + query = urllib.parse.quote("/general What is my favorite color?") + response = chat_client.get(f"/api/chat?q={query}&conversation_id={conversation.id}&stream=true") + response_message = response.content.decode("utf-8") + + # Assert + expected_responses = ["green"] + assert response.status_code == 200 + assert any([expected_response in response_message.lower() for expected_response in expected_responses]), ( + "Expected green in response, but got: " + response_message + ) + + +# ---------------------------------------------------------------------------------------------------- +@pytest.mark.chatquality +@pytest.mark.django_db(transaction=True) +def test_answer_in_chat_history_by_conversation_id_with_agent( + chat_client, default_user2: KhojUser, openai_agent: Agent +): + # Arrange + message_list = [ + ("Hello, my name is Testatron. Who are you?", "Hi, I am Khoj, a personal assistant. How can I help?", []), + ("When was I born?", "You were born on 1st April 1984.", []), + ("What's my favorite color", "Your favorite color is green.", []), + ("Where was I born?", "You were born Testville.", []), + ("What did I buy?", "You bought an apple for 2.00, an orange for 3.00, and a potato for 8.00", []), + ] + conversation = create_conversation(message_list, default_user2, openai_agent) + + # Act + query = urllib.parse.quote("/general What did I eat for breakfast?") + response = chat_client.get(f"/api/chat?q={query}&conversation_id={conversation.id}&stream=true") + response_message = response.content.decode("utf-8") + + # Assert that agent only responds with the summary of spending + expected_responses = ["13.00", "13", "13.0", "thirteen"] + assert response.status_code == 200 + assert any([expected_response in response_message.lower() for expected_response in expected_responses]), ( + "Expected green in response, but got: " + response_message + ) + + # ---------------------------------------------------------------------------------------------------- @pytest.mark.django_db(transaction=True) @pytest.mark.chatquality diff --git a/versions.json b/versions.json index 150f851e..10f042ef 100644 --- a/versions.json +++ b/versions.json @@ -39,5 +39,6 @@ "1.6.0": "0.15.0", "1.6.1": "0.15.0", "1.6.2": "0.15.0", - "1.7.0": "0.15.0" + "1.7.0": "0.15.0", + "1.8.0": "0.15.0" }