Commit Graph

5049 Commits

Author SHA1 Message Date
Debanjum
edf9ea6312 Release Khoj version 2.0.0-beta.15 2025-08-31 13:21:58 -07:00
Debanjum
d53ede604c Only enable web search with Searxng if KHOJ_SEARXNG_URL env var set
Instead of implicitly defaulting to assuming it is available as:
- For pip install searxng has to be explicitly setup to work
- For docker install we explicitly do set it up and set the
  KHOJ_SEARXNG_URL env var already

Also check if Searxng URL is also unset before disable web search
tools now that it is required explicit enablement.
2025-08-31 13:17:05 -07:00
Debanjum
7533e3eecf Use prompt cache key to improve cache hits with openai responses api
Using prompt cache key enables sticky routing to openai servers.
This increases probability of a chat actor hitting same server and
reusing cached prompts.

We use stable hash of first N characters to uniquely identify a chat
actor prompt
2025-08-31 12:44:38 -07:00
Debanjum
3c1948e9de Disable code sandbox if no code sandbox configured by admin
Either set the Terrarium sandbox url or the E2B api key to enable code
sandbox
2025-08-31 10:15:14 -07:00
Debanjum
3441783d5b Disable web search tool if no search engine configured by admin
Webpage read is gated behind having a web search engine configured for
now. It can later be decoupled from web search and depend on whether
any web scrapers is configured.
2025-08-30 00:22:26 -07:00
Debanjum
3aa6f8ba1f Add gemini cached tokens costs for more accurate cost tracking 2025-08-29 15:55:07 -07:00
Debanjum
0babab580a Avoid null ref error when no organic online search results found 2025-08-29 15:54:06 -07:00
Debanjum
00f0d23224 Fix indexing Github, Notion content by linking embeddings model on init 2025-08-29 15:54:06 -07:00
Debanjum
81c651b5b2 Fix truncation tests to check output chat history for truncation
New truncation logic return a new message list.
It does not update message list by reference/in place since 8a16f5a2a.
So truncation tests should run verification on the truncated chat
history returned by the truncation func instead of the original chat
history passed into the truncation func.
2025-08-28 15:50:32 -07:00
Debanjum
c0f192b436 Set minimum table width on web app for better readability 2025-08-28 01:58:04 -07:00
Debanjum
dd8e805cfe Add support for Cerebras ai model api
- It does not support strict mode for json schema, tool use
- It likes text content to be plain string, not nested in a dictionary
- Verified to work with gpt oss models on cerebras
2025-08-28 01:57:39 -07:00
Debanjum
0a5a882e54 Check if openai compatible ai api supports the responses api endpoint
Responses API is starting to get supported by other ai apis as well.
This change does preparatory improvements to ease moving to use
responses api with other ai apis.

Use the new, better named `supports_responses_api' method.
The method currently just maps to `is_openai_api'. It will add other
ai apis once support for using responses api with them is added.
2025-08-28 01:38:47 -07:00
Debanjum
9395c17f34 Fix openai reasoning model handling
- Fix identifying gpt-oss as openai reasoning model
- Drop unsupported stop param for openai reasoning models
- Drop the Formatting re-enabled logic for openai reasoing only models
  We use responses api for openai models and latest openai models are
  hybrid models, they don't seem to need this convoluted system
  message to format response as markdown
2025-08-28 01:38:47 -07:00
Debanjum
be79b8a633 Drop unused arguments to default tool picker, research mode
is_automated_task check isn't required as automation cannot be created
via chat anymore.

conversation specific file_filters are extracted directly in document
search, so doesn't need to be passed down from chat api endpoint
2025-08-27 14:37:28 -07:00
Debanjum
9d7adbcbaa Pass user attached images to default tool picker for informed selection
Previously we were just passing placeholder informing the default mode
tool picker that images were attached.
2025-08-27 14:37:10 -07:00
Debanjum
2091044db5 Prefer agent chat model to extract document search queries
Make chat model preference order for document search consistent with
all other tools.
2025-08-27 13:55:50 -07:00
Debanjum
7a42042488 Share context builder for chat final response across model types
The context building logic was nearly identical across all model
types.

This change extracts that logic into a shared function and calls it
once in the `agenerate_chat_response', the entrypoint to the converse
methods for all 3 model types.

Main differences handled are
- Gemini system prompt had additional verbosity instructions. Keep it
- Pass system messsage via chatml messages list to anthropic, gemini
  models as well (like openai models) instead of passing it as
  separate arg to chat_completion_* funcs.

  The model specific message formatters for both already extract
  system instruction from the messages list. So system messages wil be
  automatically extracted from the chat_completion_* funcs to pass as
  separate arg required by anthropic, gemini api libraries.
2025-08-27 13:48:33 -07:00
Debanjum
02e220f5f5 Pass args to context builder funcs grouped consistently
Put context params together, followed by model params
Use consistent ordering to improve readability
2025-08-27 13:45:36 -07:00
Debanjum
4976b244a4 Set fast, deep think models for intermediary steps via admin panel
Overview
Enable improving speed and cost of chat by setting fast, deep think
models for intermediate steps and non user facing operations.

Details
- Allow decoupling default chat models from models used for
  intermediate steps by setting server chat settings on admin panel
- Use deep think models for most intermediate steps like tool
  selection, subquery construction etc. in default and research mode
- Use fast think models for webpage read, chat title setting etc.
  Faster webpage read should improve conversation latency
2025-08-27 13:45:36 -07:00
Debanjum
a99eb841ff Do not search documents when default tool selected by agent
What
Explicit selection of notes tool/conversation command by agent is
required now.

Why
- Newer models are good at deciding when to look up notes
- Modern khoj is less of a notes only chat to search notes by default
2025-08-27 13:45:28 -07:00
Debanjum
a52a06ad9d Prefer olostep over firecrawl for webpage read by default
Default to Olostep as faster and higher webpage read success rate.
Fallback logic will use Firecrawl if Olostep fails.
2025-08-27 13:45:09 -07:00
Debanjum
e150dc5a91 Improve copying message with math, file links to clipboard on web app 2025-08-27 13:45:09 -07:00
Debanjum
15d1f39d0b Improve instruction to ai model for writing math expressions in LaTeX 2025-08-27 13:45:09 -07:00
Debanjum
05dbb6a7c1 Drop unused generated_files arg from chat context
generated_files wasn't being set (anymore?). But it was being passed
around through for chat context and being saved to db.

Also reduce variables used to set mermaid diagram description
2025-08-27 13:45:09 -07:00
Debanjum
8a16f5a2af Reduce logical complexity of constructing context from chat history
- Process chat history in default order instead of processing it in
  reverse. Improve legibility of context construction for minor
  performance hit in dropping message from front of list.
- Handle multiple system messages by collating them into list
- Remove logic to drop system role for gemma-2, o1 models. Better to
  make code more readable than support old models.
2025-08-27 13:43:10 -07:00
Debanjum
1e81b51abc Support generating images with different aspect ratios
You can now specify shape of images to be generated. It can be one of
portrait, landscape or square.
2025-08-27 13:43:10 -07:00
Debanjum
5a2cae3756 Improve, simplify image generation prompts and context flow
Use seed to stabilize image change consistency across turns when
- KHOJ_LLM_SEED env var is set
- Using Image models via Replicate
  OpenAI, Google do not support image seed
2025-08-27 13:43:10 -07:00
Debanjum
0fb6020f30 Remove model type check to construct structured messages
All model types use a normalized, chatml structured message format
This check isn't used since offline model support was dropped.
2025-08-27 13:43:10 -07:00
Debanjum
386a17371d Fix identifying deepseek r1 model to process its thinking tokens 2025-08-27 13:43:04 -07:00
Debanjum
ff004d31ef Fix extracting inferred queries from chat history db
Inferred queries is stored with underscore in db but aliased with - in memory.

This conversation.messages logic was broken, so inferred queries field
of chat message history was getting ignored.

This change fixes that issue and improve previous image generation
description for better context for subsequent image generation attempts.
2025-08-25 14:19:27 -07:00
Debanjum
892e4d4077 Fix system prompt construction for gemini models
System prompt was duplicating instructions for gemini models
previously
2025-08-24 18:22:31 -07:00
Debanjum
00c5aec614 Release Khoj version 2.0.0-beta.14 2025-08-23 12:12:33 -07:00
Debanjum
b99ccbc4c3 Improve table styling, fix chat sidebar height on web app 2025-08-23 02:05:50 -07:00
Debanjum
29ae476a26 Use groq with service tier auto to fallback to flex on rate limit
Merge gpt-oss config with openai reasoning config as similar tuning.
Add pricing for gpt oss 20b model
2025-08-23 02:05:50 -07:00
Debanjum
c89c5c7b46 Reorder ai model api columns on admin panel for readability 2025-08-23 01:40:05 -07:00
Debanjum
464c1546b7 Support deepseek v3.1 via official deepseek api
The new deepseek-chat is powered by deepseek v3.1, which is a hybrid
reasoning model unlike it's predecessor, deepseek v3.
2025-08-23 01:40:05 -07:00
Debanjum
40488b3b68 Remove redundant exception for retry calls to gemini api
httpx ReadError inherits from NetworkError so not required to mention
it explicitly in gemini api call retry check
2025-08-23 00:48:10 -07:00
Debanjum
8aa9c0f534 Reduce max reasoning tokens for gemini models
A high reasoning tokens does not seem to help for standard Khoj use
cases. And hopefully reducing it may avoid repetition loops by model.
2025-08-23 00:48:10 -07:00
Debanjum
2823c84bb4 Default to gemini 2.5 model series on init and for eval 2025-08-22 20:34:38 -07:00
Debanjum
c53a70c997 Share debug logs from github eval run for debugging 2025-08-22 19:06:37 -07:00
Debanjum
e2f377c27b Render file reference as link with file preview on hover/click in web app
Overview
- Khoj references files it used in its response as markdown links.
  For example [1](file://path/to/file.txt#line=121)
- Previously these file links were just shown as raw text
- This change renders khoj's inline file references as a proper links
  and shows file content preview (around specified line if deeplink)
  on hover or click in the web app

Details
- Render inline file references as links in chat message on web app.
  Previously references like [1](file://path/to/file.txt#line=120)
  would be shown as plain text. Now they are rendered as links
- Preview file content of referenced files on click or hover.
  If reference uses a deeplink with line number, the file content
  around that line is shown on hover, click. Click allows viewing file
  preview on mobile, unlike hover. Hover is easier with mouse.
2025-08-22 18:24:27 -07:00
Debanjum
d8b7e9c8a5 Handle unset content type key when indexing knowledge base on server 2025-08-22 18:24:27 -07:00
Debanjum
3c3205bb06 Fix and improve file read, write handling in Obsidian
Fixes
- Fix to allow khoj to delete content in obsidian write mode
- Do not throw error when no edit blocks in write mode on obsidian
- Limit retries to fix invalid edit blocks in obsidian write mode

Improvements
- Only show 3 recent files as context in obsidian file read, write mode
- Persist open file access mode setting across restarts in obsidian
- Make khoj obsidian keyboard shortcuts toggle voice chat, chat history
- Do not show <SYSTEM> instructions in chat session title on obsidian

Closes #1209
2025-08-20 20:20:12 -07:00
Debanjum
48ed7afab8 Do not show <SYSTEM> instructions in chat session title on obsidian
In obsidian we have a hacky system instruction being passed in read,
write file access modes. This shouldn't be shown in chat sessions list
during view or edit. It is an internal implementation detail.
2025-08-20 20:18:27 -07:00
Debanjum
82dc7b115b Fix to allow khoj to delete content in obsidian write mode
Previous regex and replacement logic did not allow replace block to be
empty
2025-08-20 20:18:27 -07:00
Debanjum
7645cbea3b Do not throw error when no edit blocks in write mode on obsidian
Editing is an option, not a requirement in file write/edit mode.
2025-08-20 20:18:27 -07:00
Debanjum
2e6928c582 Limit retries to fix invalid edit blocks in obsidian write mode 2025-08-20 20:18:27 -07:00
Debanjum
c5e2373d73 Make khoj obsidian keyboard shortcuts toggle voice chat, chat history
Previously hitting voice chat keybinding would just start voice chat,
not end it and just open chat history and not close it.

This is unintuitive and different from the equivalent button click
behaviors.

Fix toggles voice chat on/off and shows/hides chat history when hit
Ctrl+Alt+V, Ctrl+Alt+O keybindings in khoj obsidian chat view
2025-08-20 20:18:27 -07:00
Debanjum
d8b2df4107 Only show 3 recent files as context in obsidian file read, write mode
Related #1209
2025-08-20 20:18:27 -07:00
Debanjum
eb2f0ec6bc Persist open file access mode setting across restarts in obsidian
Allows a lightweight mechanism to persist this user preference.
Improve hover text a bit for readability.

Resolves #1209
2025-08-20 20:18:27 -07:00