Commit Graph

3659 Commits

Author SHA1 Message Date
Debanjum
a557031447 Refactor Gemini chat response to stream async, no separate thread 2025-04-21 10:46:07 +05:30
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
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
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
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
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
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
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
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
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
7f18bc0840 Add default context for gemini 2 flash. 2x it for small, commercial models
Previously Gemini 2 flash and flash lite were using context window of
10K by default as no defaults were added for it.

Increase default context for small commercial models to 120K from 60K
as cheaper and faster than their pro models equivalents at 60K context.
2025-04-04 20:11:00 +05:30
Debanjum
47a081c7bd Allow text tool to give agent ability to terminate research
We'd moved research planner to only use tools in enum of schema. This
enum tool enforcement prevented model from terminating research by
setting tool field to empty.

Fix the issue by adding text tool to research tools enum and tell
model to use that to terminate research and start response instead.
2025-04-04 20:11:00 +05:30
Debanjum
38dd02afbf Make ordering of fields expected by research planner consistent
Make research planner consistently select tool before query. As the
model should tune it's query for the selected tool. It got space to
think about tool to use in the scratchpad already.
2025-04-04 20:11:00 +05:30
Debanjum
443c5a4420 Consistently wrap queries in online search prompt in double quotes
The queries field name in the first example isn't wrapped in double
quotes, rest are.
2025-04-04 20:11:00 +05:30
Debanjum
ae8fb6f9ac Default temperature of Gemini models to 1.0 to try avoid repetition
This is the default temperature for non-thinking gemini models on ai
studio. See if using this alleviates the problem.
2025-04-04 20:11:00 +05:30
Debanjum
e9928d3c50 Eval more model, control randomization & auto read webpage via workflow
- Control auto read webpage via eval workflow. Prefix env var with KHOJ_
  Default to false as it is the default that is going to be used in prod
  going forward.

- Set openai api key via input param in manual eval workflow runs
  - Simplify evaluating other chat models available over openai
    compatible api via eval workflow.
  - Mask input api key as secret in workflow.
  - Discard unnecessary null setting of env vars.

- Control randomization of samples in eval workflow.
  If randomization is turned off, it'll take the first SAMPLE_SIZE
  items from the eval dataset instead of a random collection of
  SAMPLE_SIZE items.
2025-04-04 20:11:00 +05:30
sabaimran
abfdf7b1fb Temporarily remove the OTP feature on the settings page while we fix our number 2025-04-03 19:41:18 -05:00
Debanjum
17997d26c5 Make generated code block extractor less strict to improve code tool 2025-04-03 14:07:15 +05:30
Debanjum
ef5b19479f Improve setup of openai compatible speech to text, image models
Only setup speech to text and text to image models served via openai
compatible APIs when explicitly specified during initialization.

This avoids setup of whisper and dalle when an openai compatible API
is being setup instead of the openai API itself.
2025-04-03 13:44:19 +05:30
Debanjum
c2b9006a7a Improve Gemini Response Reliability (#1148)
- Specify min, max number of list items expected in AI response via JSON schema enforcement. Used by Gemini models
- Warn and drop invalid/empty messages when format messages for Gemini models
- Make Gemini response adhere to the order of the schema property definitions
- Improve agent creation safety checker by using response schema, better prompt
2025-04-03 13:42:35 +05:30
Debanjum
f77e871cc8 Improve agent creation safety checker with response schema, better prompt 2025-04-03 02:49:40 +05:30
Debanjum
aab010723c Make Gemini response adhere to the order of the schema property definitions
Without explicitly using the property ordering field, gemini returns
responses in alphabetically sorted property order.

We want the model to respect the schema property definition order.
This ensures control during development to maintain response quality.

For example in CoT make it fill scratchpad before answers.
2025-04-03 01:19:48 +05:30
Debanjum
ae9ca58ab9 Specify min, max items expected in ai response via schema enforcement
Require at least 1 item in lists. Otherwise gemini flash will
sometimes return an empty list. For chat actors where max items is
known, set that as well.

OpenAI API does not support specifying min, max items in response
schema lists, so drop those properties when response schema is
passed. Add other enforcements to response schema to comply with
response schema format expected by OpenAI API.
2025-04-03 01:19:39 +05:30