Commit Graph

4634 Commits

Author SHA1 Message Date
Debanjum
0751f2ea30 Refactor Openai chat response to stream async, no separate thread
- Refactor chat API to use async/await for Openai streaming
- Fix and clean Openai chat response async streaming
2025-04-21 10:44:49 +05:30
Debanjum
c93c0d982e Create async get anthropic, openai client funcs, move to reusable package
This package is where the get openai client functions also reside.
2025-04-21 09:30:26 +05:30
Debanjum
973aded6c5 Fix system prompt to make openai reasoning models md format response 2025-04-20 20:33:45 +05:30
Debanjum
21d19163ba Just pass user rather than whole request object to doc search func 2025-04-20 20:33:45 +05:30
Debanjum
b2390fa977 Allow attaching typescript files to chat on web app 2025-04-19 19:08:11 +05:30
Debanjum
4d331e5ad2 Bump documentation dependencies 2025-04-19 18:38:31 +05:30
Debanjum
d6aafef464 Fix formatting of FAQ section in README.md 2025-04-19 18:31:16 +05:30
Debanjum
8f9090940b Resolve datetime utcnow deprecation warnings (#1164)
# PR Summary
This small PR resolves the deprecation warnings on `datetime` in
Python3.12+. You can find them in the [CI
logs](https://github.com/khoj-ai/khoj/actions/runs/14538833837/job/40792624987#step:9:134):
```python
  /__w/khoj/khoj/src/khoj/processor/content/images/image_to_entries.py:61: DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
    timestamp_now = datetime.utcnow().timestamp()
```
2025-04-19 18:26:52 +05:30
Debanjum
5441793a10 Allow AI model switching based on User Tier (#1151)
Overview
---
Enable free tier users to chat with any AI model made available on free tier 
of production deployments like [Khoj cloud](https://app.khoj.dev).

Previously model switching was completely disabled for users on free tier.

Details
---
- Track price tier of each Chat, Speech, Image, Voice AI model in DB
- Update API to allow free tier users to switch between free models
- Update web app to allow model switching on agent creation, settings
  chat page (via right side pane), even for free tier users.
2025-04-19 18:14:37 +05:30
Debanjum
ab29ffd799 Fix web app packaging for pypi since upgrade to python 3.11.12 in CI 2025-04-19 18:03:29 +05:30
Debanjum
79fc911633 Enable free tier users to switch between free tier AI models
- Update API to allow free tier users to switch between free models
- Update web app to allow model switching on agent creation, settings
  chat page (via right side pane), even for free tier users.

Previously the model switching APIs and UX fields on web app were
completely disabled for free tier users
2025-04-19 17:29:53 +05:30
Debanjum
30570e3e06 Track Price tier for each Chat, Speech, Image, Voice AI model in DB
Enables users on free plan to choose AI models marked for free tier
2025-04-19 09:44:33 +05:30
Debanjum
fdaf51f0ea Fix formatting in readme and documentation 2025-04-19 00:36:47 +05:30
Emmanuel Ferdman
fee1d3682b Resolve datetime deprecation warnings
Signed-off-by: Emmanuel Ferdman <emmanuelferdman@gmail.com>
2025-04-18 10:41:16 -07:00
Debanjum
eb1406bcb4 Support deepthought in research mode with new Gemini 2.5 reasoning model
The 2.5 flash model is the first hybrid reasoning models by Google

- Track costs of thoughts separately as they are priced differently
2025-04-18 17:37:05 +05:30
Debanjum
f95173bb0a Support deepthought in research mode with new Grok 3 reasoning model
Rely on deepthought flag to control reasoning effort of low/high for
the grok model

This is different from the openai reasoning models which support
low/medium/high and for which we use low/medium effort based on the
deepthought flag

Note: grok is accessible over an openai compatible API
2025-04-18 17:37:05 +05:30
Debanjum
9c70a0f3f5 Support recently released Openai reasoning models
- Rely on deepthought flag to control reasoning effort
- Generalize Openai reasoning model check for all o- series models
2025-04-18 17:32:29 +05:30
Debanjum
2f8283935a Warn and drop empty messages when format messages for Anthropic
Log dropped empty messages to debug this unexpected state.

Related 0eb2d17
2025-04-18 17:32:29 +05:30
Debanjum
51e19c6199 Simplify KHOJ_DOMAIN states. All production deployments should set it.
Do not need KHOJ_DOMAIN to be tri-state.
KHOJ_DOMAIN set to empty does not change behavior anymore.

Related 5a3c7b1
2025-04-18 17:32:29 +05:30
Debanjum
e072530471 Deduplicate images generated using the e2b code tool
Disregard chart types as not using rich chart rendering
and they are duplicate of chart images that are rendered

Disregard text output associated with generated image files
2025-04-18 17:32:29 +05:30
RIKIN BRIGHT
dc398d30f8 Add FAQ section to Readme and Troubleshooting Tips to setup GCP Vertex (#1158)
Added a “Troubleshooting & Tips” section to the GCP Vertex documentation.

This section provides guidance for self-hosted users on common issues
they may encounter when setting up Google Vertex AI integration in Khoj.
Topics covered include permissions, region compatibility, prompt size
limits, API key testing, and secure key management with environment
variables. The goal is to improve the onboarding experience and reduce
setup errors for contributors and self-hosters using Vertex AI models
like Claude and Gemini.

Signed off by: brightally6@gmail.com
2025-04-15 08:19:44 +05:30
sabaimran
6a30da3e9e Fix default state for tools in the agent settings for the chat sidebar 2025-04-11 11:12:22 -07:00
Debanjum
2470eea421 Release Khoj version 1.40.0 2025-04-11 18:10:56 +05:30
Debanjum
d0a933b072 Add email based rate limiting to email login API endpoint
Server:
 - Rate limit based on unverified email before creating user
 - Check email address for deliverability before creating user
 - Track rate limit for unverified email in new non-user keyed table

Web app:
 - Show error in login popup to user on failure/throttling
 - Simplify login popup logic by moving magic link handling logic
   into EmailSigninContext instead of passing require props via parent
2025-04-11 17:49:18 +05:30
Debanjum
fe308c2911 Handle scenario where no valid otps for selected users on admin panel 2025-04-11 17:49:18 +05:30
Debanjum
02a6ce9f14 Upgrade server django dependencies 2025-04-11 17:49:18 +05:30
Debanjum
d84a0abb7f Fix and Improve Chat sidebar and component setup on Web App (#1157)
- Set chatSidebar prompt, Setting name fields to empty str if value null
- Track if agent modified in chatSidebar to simplify code, fix looping
- Suppress spurious dark mode hydration warnings on the web app
- Set key for chatMessage parent to get UX efficiently updated by react
- Let only root next.js layout handle html, body tags, not child layouts
2025-04-11 16:12:03 +05:30
Dmitry
50b0b8a6e7 Fix typo in Development documentation (#1159) 2025-04-11 16:05:40 +05:30
Darya
f609a2d050 Fix typos in admin panel documentation (#1156)
These adjustments should make the text clearer and more accurate.
2025-04-11 07:00:17 +05:30
Debanjum
2935ea52cf Set chatSidebar prompt, Setting name fields to empty str if value null
TextArea and Input field values cannot be null.
2025-04-10 19:59:01 +05:30
Debanjum
aea7b90fec Track if agent modified in chatSidebar to simplify code, fix looping
Previously the sidebar could recurse on opening chat page (from home?)
due to child modelSelector component updating parent chatSidebar prop
which was passed back down to it in a loop.

The chatSidebar decides if agent has been modified in a single
useEffect and enables the Save button accordingly.
- Track agent modification wrt agent info received from server in
  chatSidebar instead.
- Reduce modelSelector's mandate to just notify
  when the user changes the model.

- Fix to infer, show & update agent state from chat sidebar on web app
  This logic is fragile and convoluted because:
  - the default agent chat model is dynamically determined.
  - need to disambiguate tools not set vs none set vs all set by user
    The default agent's tool selection is stored as undefined to show
    not set scenario, which allows for all tools to be dynamically
    used by  agent.
    But the user can also set no tools or all tools for their agents.
    All 3 scenarios are handled differently.
  - Track tools to be displayed vs tools to be stored
2025-04-10 19:59:01 +05:30
Debanjum
e9ee9004fb Suppress spurious dark mode hydration warnings on the web app
This is triggered by mismatch between "dark" class present on server
sent layout but not in client sent layout on initial render.

That mismatch exists because the server applies dark-mode styling
early to avoid FOUC flickering of UX.

Related 43e032e
2025-04-10 19:59:01 +05:30
Debanjum
9ab5ead3ca Set key for chatMessage parent to get UX efficiently updated by react
By fixing the no key prop in ChatHistory error on web app
2025-04-10 19:59:01 +05:30
Debanjum
1ad7314fe6 Let only root next.js layout handle html, body tags, not child layouts
Remove html, body elements from child page layouts. Let only the root
layout handle it.

Next.js router structure mounts child layouts inside parent layouts,
as defined by their directory hierarchy. So the html, body component
should only be defined in the parent layout.

This avoids the child layout mounting its html, body component within
the actual root layout's existing html, body component.
2025-04-10 19:59:01 +05:30
Debanjum
33665dee50 Dynamically set default agent chat model to server > user > first chat model
Previously the chat model associated with the default agent was always
the first chat model populated on the server. This doesn't match
behavior of the rest of the system, where the server chat settings is
preferred over the user chat settings over the first chat model.

This change brings the default agent's chat model in line with the
preference order used in the reset of the system.
2025-04-10 19:58:01 +05:30
Debanjum
1eb092010c Fix handling unset response_schema being passed to gemini models
Use of `is_none_or_empty' doesn't work well with classes, which
response_schema can get set to.
2025-04-10 19:58:01 +05:30
Debanjum
5b248e8515 Fix fallback to default agent if none set for conversation
Previous change to fallback to default agent was not functional. It
would error out if the conversation agent wasn't set when trying to
get conversation.agent.slug for calling aget_agent_by_slug func
2025-04-10 19:54:01 +05:30
Debanjum
4012a6372f Fix pgvector docker image pull by using postgres 15 tag
We were previously relying on an older, unmaintained version of
pgvector docker image, ankane/pgvector.

Moving to new docker image requires selecting from tags based on the
pg major version (14, 15, 16 or 17).

This change uses pg15 tag to resolve image pull.
Note: we use postgres 15 for khoj docker images currently

Fixes #1154
2025-04-08 18:07:08 +05:30
Debanjum
19618605a5 Upgrade documentation packages 2025-04-07 20:07:10 +05:30
Debanjum
3fc1435cd1 Fix login to local admin panel without need to set KHOJ_DEBUG
Issue introduced in commit 5a3c7b1.

Usage of KHOJ_DOMAIN
---
KHOJ_DOMAIN is tri-state for local, official and other production deployments:
- If KHOJ_DOMAIN is unset (for local):
  - sets CSRF cookie to localhost
  - adds khoj.dev variants to ALLOWED_HOSTS, CSRF_TRUSTED_ORIGINS
  - adds app.khoj.dev variants to CORS origins
- If KHOJ_DOMAIN is set to empty (for official):
  - sets CSRF to khoj.dev
  - adds khoj.dev variants to ALLOWED_HOSTS, CSRF_TRUSTED_ORIGINS
  - adds app.khoj.dev variants to CORS origins
- If KHOJ_DOMAIN is set (for other prod deployments):
  - sets CSRF cookie to KHOJ_DOMAIN
  - adds KHOJ_DOMAIN variants to ALLOWED_HOSTS, CSRF_TRUSTED_ORIGINS
  - adds KHOJ_DOMAIN variants to CORS origins

Related #1137, #1152

Resolves #1123
2025-04-07 19:43:58 +05:30
Debanjum
ed70d2254e Suppress spurious RequestAborted ASGI errors on the admin panel
Unsure why this error triggers on every request to the Django admin
panel these days but all the requests are completing fine and the
client is clearly not aborting the request when the RequestAborted
exception is raised.

Suppress these errors for now via middleware to prevent them from
unnecessarily cluttering up the server logs and confusing folks.

Related #1152
2025-04-07 19:43:58 +05:30
Debanjum
353a4aa597 Upgrade pgvector to use the new official docker image
- ankane/pgvector has been deprecated in place of pgvector/pgvector
- use full path to image on docker.io for consistency
2025-04-07 19:43:58 +05:30
Debanjum
50508d97f9 Disable telemetry only via KHOJ_TELEMETRY_DISABLE environment variable
Do not unnecessarily overload usage of KHOJ_DEBUG environment variable
2025-04-07 19:43:58 +05:30
sabaimran
2ae8c62547 Repopulate the client API-key generation section 2025-04-07 06:54:47 -07:00
Debanjum
6872817d41 Remove request to set default chat model during interactive init
It wasn't being set correctly and seems unnecessary as can switch
to desired chat models via the settings page or chat side pane easily
2025-04-07 14:38:55 +05:30
Debanjum
0c257c044e Handle unset response_schema being passed to gemini models 2025-04-05 23:12:35 +05:30
Debanjum
c1912f8ca7 Default eval to use 10 iterations for research mode 2025-04-05 10:09:58 +05:30
Debanjum
645c2bc546 Improve Khoj is ready message 2025-04-05 10:09:28 +05:30
Debanjum
6e61ec64a4 Release Khoj version 1.39.0 2025-04-04 20:35:50 +05:30
Debanjum
751215a701 Improve response quality with Gemini. Improve evaluation harness (#1150)
### Improve Gemini usage
- Allow text tool to give agent ability to terminate research
- Set default context for gemini 2 flash models
  2x context window for small, commercial models to 120K
- Default temperature of Gemini models to 1.0 to reduce repetition

### Improve evaluation harness
- Add more knobs to control eval workflow
  - Allow running eval with any chat model served over an openai compatible api
  - Control random sampling from eval set
  - Auto read web page
- Use embedded postgres instead of postgres server for eval workflow
- Use Gemini 2.0 flash as evaluator. Set seed for evaluator to reduce decision variance
2025-04-04 20:17:36 +05:30