mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-02 13:18:18 +00:00
Do not version API. Premature given current state of the codebase
- Reason - All clients that currently consume the API are part of Khoj - Any breaking API changes will be fixed in clients immediately - So decoupling client from API is not required - This removes the burden of maintaining muliple versions of the API
This commit is contained in:
@@ -226,7 +226,7 @@ Use `which-key` if available, else display simple message in echo area"
|
|||||||
|
|
||||||
(defun khoj--get-enabled-content-types ()
|
(defun khoj--get-enabled-content-types ()
|
||||||
"Get content types enabled for search from API."
|
"Get content types enabled for search from API."
|
||||||
(let ((config-url (format "%s/api/v1.0/config/data" khoj-server-url)))
|
(let ((config-url (format "%s/api/config/data" khoj-server-url)))
|
||||||
(with-temp-buffer
|
(with-temp-buffer
|
||||||
(erase-buffer)
|
(erase-buffer)
|
||||||
(url-insert-file-contents config-url)
|
(url-insert-file-contents config-url)
|
||||||
@@ -243,7 +243,7 @@ Use `which-key` if available, else display simple message in echo area"
|
|||||||
"Construct API Query from QUERY, SEARCH-TYPE and (optional) RERANK params."
|
"Construct API Query from QUERY, SEARCH-TYPE and (optional) RERANK params."
|
||||||
(let ((rerank (or rerank "false"))
|
(let ((rerank (or rerank "false"))
|
||||||
(encoded-query (url-hexify-string query)))
|
(encoded-query (url-hexify-string query)))
|
||||||
(format "%s/api/v1.0/search?q=%s&t=%s&r=%s&n=%s" khoj-server-url encoded-query search-type rerank khoj-results-count)))
|
(format "%s/api/search?q=%s&t=%s&r=%s&n=%s" khoj-server-url encoded-query search-type rerank khoj-results-count)))
|
||||||
|
|
||||||
(defun khoj--query-api-and-render-results (query search-type query-url buffer-name)
|
(defun khoj--query-api-and-render-results (query search-type query-url buffer-name)
|
||||||
"Query Khoj API using QUERY, SEARCH-TYPE, QUERY-URL.
|
"Query Khoj API using QUERY, SEARCH-TYPE, QUERY-URL.
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ var emptyValueDefault = "🖊️";
|
|||||||
/**
|
/**
|
||||||
* Fetch the existing config file.
|
* Fetch the existing config file.
|
||||||
*/
|
*/
|
||||||
fetch("/api/v1.0/config/data")
|
fetch("/api/config/data")
|
||||||
.then(response => response.json())
|
.then(response => response.json())
|
||||||
.then(data => {
|
.then(data => {
|
||||||
rawConfig = data;
|
rawConfig = data;
|
||||||
@@ -26,7 +26,7 @@ fetch("/api/v1.0/config/data")
|
|||||||
configForm.addEventListener("submit", (event) => {
|
configForm.addEventListener("submit", (event) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
console.log(rawConfig);
|
console.log(rawConfig);
|
||||||
fetch("/api/v1.0/config/data", {
|
fetch("/api/config/data", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
credentials: "same-origin",
|
credentials: "same-origin",
|
||||||
headers: {
|
headers: {
|
||||||
@@ -46,7 +46,7 @@ regenerateButton.addEventListener("click", (event) => {
|
|||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
regenerateButton.style.cursor = "progress";
|
regenerateButton.style.cursor = "progress";
|
||||||
regenerateButton.disabled = true;
|
regenerateButton.disabled = true;
|
||||||
fetch("/api/v1.0/update?force=true")
|
fetch("/api/update?force=true")
|
||||||
.then(response => response.json())
|
.then(response => response.json())
|
||||||
.then(data => {
|
.then(data => {
|
||||||
regenerateButton.style.cursor = "pointer";
|
regenerateButton.style.cursor = "pointer";
|
||||||
|
|||||||
@@ -77,8 +77,8 @@
|
|||||||
|
|
||||||
// Generate Backend API URL to execute Search
|
// Generate Backend API URL to execute Search
|
||||||
url = type === "image"
|
url = type === "image"
|
||||||
? `/api/v1.0/search?q=${encodeURIComponent(query)}&t=${type}&n=${results_count}`
|
? `/api/search?q=${encodeURIComponent(query)}&t=${type}&n=${results_count}`
|
||||||
: `/api/v1.0/search?q=${encodeURIComponent(query)}&t=${type}&n=${results_count}&r=${rerank}`;
|
: `/api/search?q=${encodeURIComponent(query)}&t=${type}&n=${results_count}&r=${rerank}`;
|
||||||
|
|
||||||
// Execute Search and Render Results
|
// Execute Search and Render Results
|
||||||
fetch(url)
|
fetch(url)
|
||||||
@@ -94,7 +94,7 @@
|
|||||||
|
|
||||||
function updateIndex() {
|
function updateIndex() {
|
||||||
type = document.getElementById("type").value;
|
type = document.getElementById("type").value;
|
||||||
fetch(`/api/v1.0/update?t=${type}`)
|
fetch(`/api/update?t=${type}`)
|
||||||
.then(response => response.json())
|
.then(response => response.json())
|
||||||
.then(data => {
|
.then(data => {
|
||||||
console.log(data);
|
console.log(data);
|
||||||
@@ -118,7 +118,7 @@
|
|||||||
function populate_type_dropdown() {
|
function populate_type_dropdown() {
|
||||||
// Populate type dropdown field with enabled search types only
|
// Populate type dropdown field with enabled search types only
|
||||||
var possible_search_types = ["org", "markdown", "ledger", "music", "image"];
|
var possible_search_types = ["org", "markdown", "ledger", "music", "image"];
|
||||||
fetch("/api/v1.0/config/data")
|
fetch("/api/config/data")
|
||||||
.then(response => response.json())
|
.then(response => response.json())
|
||||||
.then(data => {
|
.then(data => {
|
||||||
document.getElementById("type").innerHTML =
|
document.getElementById("type").innerHTML =
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ from PyQt6.QtCore import QThread, QTimer
|
|||||||
|
|
||||||
# Internal Packages
|
# Internal Packages
|
||||||
from src.configure import configure_server
|
from src.configure import configure_server
|
||||||
from src.routers.api_v1_0 import api_v1_0
|
from src.routers.api import api
|
||||||
from src.routers.api_beta import api_beta
|
from src.routers.api_beta import api_beta
|
||||||
from src.routers.frontend import frontend_router
|
from src.routers.frontend import frontend_router
|
||||||
from src.utils import constants, state
|
from src.utils import constants, state
|
||||||
@@ -31,7 +31,7 @@ from src.interface.desktop.system_tray import create_system_tray
|
|||||||
# Initialize the Application Server
|
# Initialize the Application Server
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
app.mount("/static", StaticFiles(directory=constants.web_directory), name="static")
|
app.mount("/static", StaticFiles(directory=constants.web_directory), name="static")
|
||||||
app.include_router(api_v1_0, prefix="/api/v1.0")
|
app.include_router(api, prefix="/api")
|
||||||
app.include_router(api_beta, prefix="/api/beta")
|
app.include_router(api_beta, prefix="/api/beta")
|
||||||
app.include_router(frontend_router)
|
app.include_router(frontend_router)
|
||||||
|
|
||||||
|
|||||||
@@ -15,23 +15,23 @@ from src.utils.config import SearchType
|
|||||||
from src.utils import state, constants
|
from src.utils import state, constants
|
||||||
|
|
||||||
|
|
||||||
api_v1_0 = APIRouter()
|
api = APIRouter()
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@api_v1_0.get('/config/data', response_model=FullConfig)
|
@api.get('/config/data', response_model=FullConfig)
|
||||||
def config_data():
|
def get_config_data():
|
||||||
return state.config
|
return state.config
|
||||||
|
|
||||||
@api_v1_0.post('/config/data')
|
@api.post('/config/data')
|
||||||
async def config_data(updated_config: FullConfig):
|
async def set_config_data(updated_config: FullConfig):
|
||||||
state.config = updated_config
|
state.config = updated_config
|
||||||
with open(state.config_file, 'w') as outfile:
|
with open(state.config_file, 'w') as outfile:
|
||||||
yaml.dump(yaml.safe_load(state.config.json(by_alias=True)), outfile)
|
yaml.dump(yaml.safe_load(state.config.json(by_alias=True)), outfile)
|
||||||
outfile.close()
|
outfile.close()
|
||||||
return state.config
|
return state.config
|
||||||
|
|
||||||
@api_v1_0.get('/search', response_model=list[SearchResponse])
|
@api.get('/search', response_model=list[SearchResponse])
|
||||||
def search(q: str, n: Optional[int] = 5, t: Optional[SearchType] = None, r: Optional[bool] = False):
|
def search(q: str, n: Optional[int] = 5, t: Optional[SearchType] = None, r: Optional[bool] = False):
|
||||||
results: list[SearchResponse] = []
|
results: list[SearchResponse] = []
|
||||||
if q is None or q == '':
|
if q is None or q == '':
|
||||||
@@ -121,7 +121,7 @@ def search(q: str, n: Optional[int] = 5, t: Optional[SearchType] = None, r: Opti
|
|||||||
return results
|
return results
|
||||||
|
|
||||||
|
|
||||||
@api_v1_0.get('/update')
|
@api.get('/update')
|
||||||
def update(t: Optional[SearchType] = None, force: Optional[bool] = False):
|
def update(t: Optional[SearchType] = None, force: Optional[bool] = False):
|
||||||
state.model = configure_search(state.model, state.config, regenerate=force, t=t)
|
state.model = configure_search(state.model, state.config, regenerate=force, t=t)
|
||||||
return {'status': 'ok', 'message': 'index updated'}
|
return {'status': 'ok', 'message': 'index updated'}
|
||||||
@@ -7,7 +7,7 @@ from typing import Optional
|
|||||||
from fastapi import APIRouter
|
from fastapi import APIRouter
|
||||||
|
|
||||||
# Internal Packages
|
# Internal Packages
|
||||||
from src.routers.api_v1_0 import search
|
from src.routers.api import search
|
||||||
from src.processor.conversation.gpt import converse, extract_search_type, message_to_log, message_to_prompt, understand, summarize
|
from src.processor.conversation.gpt import converse, extract_search_type, message_to_log, message_to_prompt, understand, summarize
|
||||||
from src.utils.config import SearchType
|
from src.utils.config import SearchType
|
||||||
from src.utils.helpers import get_absolute_path, get_from_dict
|
from src.utils.helpers import get_absolute_path, get_from_dict
|
||||||
|
|||||||
@@ -43,8 +43,8 @@ just generating embeddings*
|
|||||||
|
|
||||||
- **Khoj via API**
|
- **Khoj via API**
|
||||||
- See [Khoj API Docs](http://localhost:8000/docs)
|
- See [Khoj API Docs](http://localhost:8000/docs)
|
||||||
- [Query](http://localhost:8000/api/v1.0/search?q=%22what%20is%20the%20meaning%20of%20life%22)
|
- [Query](http://localhost:8000/api/search?q=%22what%20is%20the%20meaning%20of%20life%22)
|
||||||
- [Update Index](http://localhost:8000/api/v1.0/update?t=ledger)
|
- [Update Index](http://localhost:8000/api/update?t=ledger)
|
||||||
- [Configure Application](https://localhost:8000/ui)
|
- [Configure Application](https://localhost:8000/ui)
|
||||||
- **Khoj via Emacs**
|
- **Khoj via Emacs**
|
||||||
- [Install](https://github.com/debanjum/khoj/tree/master/src/interface/emacs#installation)
|
- [Install](https://github.com/debanjum/khoj/tree/master/src/interface/emacs#installation)
|
||||||
|
|||||||
@@ -27,8 +27,8 @@
|
|||||||
- Run ~M-x khoj <user-query>~ or Call ~C-c C-s~
|
- Run ~M-x khoj <user-query>~ or Call ~C-c C-s~
|
||||||
|
|
||||||
- *Khoj via API*
|
- *Khoj via API*
|
||||||
- Query: ~GET~ [[http://localhost:8000/api/v1.0/search?q=%22what%20is%20the%20meaning%20of%20life%22][http://localhost:8000/api/v1.0/search?q="What is the meaning of life"]]
|
- Query: ~GET~ [[http://localhost:8000/api/search?q=%22what%20is%20the%20meaning%20of%20life%22][http://localhost:8000/api/search?q="What is the meaning of life"]]
|
||||||
- Update Index: ~GET~ [[http://localhost:8000/api/v1.0/update][http://localhost:8000/api/v1.0/update]]
|
- Update Index: ~GET~ [[http://localhost:8000/api/update][http://localhost:8000/api/update]]
|
||||||
- [[http://localhost:8000/docs][Khoj API Docs]]
|
- [[http://localhost:8000/docs][Khoj API Docs]]
|
||||||
|
|
||||||
- *Call Khoj via Python Script Directly*
|
- *Call Khoj via Python Script Directly*
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ def test_search_with_invalid_content_type():
|
|||||||
user_query = quote("How to call Khoj from Emacs?")
|
user_query = quote("How to call Khoj from Emacs?")
|
||||||
|
|
||||||
# Act
|
# Act
|
||||||
response = client.get(f"/api/v1.0/search?q={user_query}&t=invalid_content_type")
|
response = client.get(f"/api/search?q={user_query}&t=invalid_content_type")
|
||||||
|
|
||||||
# Assert
|
# Assert
|
||||||
assert response.status_code == 422
|
assert response.status_code == 422
|
||||||
@@ -43,7 +43,7 @@ def test_search_with_valid_content_type(content_config: ContentConfig, search_co
|
|||||||
# config.content_type.image = search_config.image
|
# config.content_type.image = search_config.image
|
||||||
for content_type in ["org", "markdown", "ledger", "music"]:
|
for content_type in ["org", "markdown", "ledger", "music"]:
|
||||||
# Act
|
# Act
|
||||||
response = client.get(f"/api/v1.0/search?q=random&t={content_type}")
|
response = client.get(f"/api/search?q=random&t={content_type}")
|
||||||
# Assert
|
# Assert
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
|
||||||
@@ -51,7 +51,7 @@ def test_search_with_valid_content_type(content_config: ContentConfig, search_co
|
|||||||
# ----------------------------------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------------------------------
|
||||||
def test_update_with_invalid_content_type():
|
def test_update_with_invalid_content_type():
|
||||||
# Act
|
# Act
|
||||||
response = client.get(f"/api/v1.0/update?t=invalid_content_type")
|
response = client.get(f"/api/update?t=invalid_content_type")
|
||||||
|
|
||||||
# Assert
|
# Assert
|
||||||
assert response.status_code == 422
|
assert response.status_code == 422
|
||||||
@@ -65,7 +65,7 @@ def test_update_with_valid_content_type(content_config: ContentConfig, search_co
|
|||||||
|
|
||||||
for content_type in ["org", "markdown", "ledger", "music"]:
|
for content_type in ["org", "markdown", "ledger", "music"]:
|
||||||
# Act
|
# Act
|
||||||
response = client.get(f"/api/v1.0/update?t={content_type}")
|
response = client.get(f"/api/update?t={content_type}")
|
||||||
# Assert
|
# Assert
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
|
||||||
@@ -73,7 +73,7 @@ def test_update_with_valid_content_type(content_config: ContentConfig, search_co
|
|||||||
# ----------------------------------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------------------------------
|
||||||
def test_regenerate_with_invalid_content_type():
|
def test_regenerate_with_invalid_content_type():
|
||||||
# Act
|
# Act
|
||||||
response = client.get(f"/api/v1.0/update?force=true&t=invalid_content_type")
|
response = client.get(f"/api/update?force=true&t=invalid_content_type")
|
||||||
|
|
||||||
# Assert
|
# Assert
|
||||||
assert response.status_code == 422
|
assert response.status_code == 422
|
||||||
@@ -87,7 +87,7 @@ def test_regenerate_with_valid_content_type(content_config: ContentConfig, searc
|
|||||||
|
|
||||||
for content_type in ["org", "markdown", "ledger", "music", "image"]:
|
for content_type in ["org", "markdown", "ledger", "music", "image"]:
|
||||||
# Act
|
# Act
|
||||||
response = client.get(f"/api/v1.0/update?force=true&t={content_type}")
|
response = client.get(f"/api/update?force=true&t={content_type}")
|
||||||
# Assert
|
# Assert
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
|
||||||
@@ -104,7 +104,7 @@ def test_image_search(content_config: ContentConfig, search_config: SearchConfig
|
|||||||
|
|
||||||
for query, expected_image_name in query_expected_image_pairs:
|
for query, expected_image_name in query_expected_image_pairs:
|
||||||
# Act
|
# Act
|
||||||
response = client.get(f"/api/v1.0/search?q={query}&n=1&t=image")
|
response = client.get(f"/api/search?q={query}&n=1&t=image")
|
||||||
|
|
||||||
# Assert
|
# Assert
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
@@ -122,7 +122,7 @@ def test_notes_search(content_config: ContentConfig, search_config: SearchConfig
|
|||||||
user_query = quote("How to git install application?")
|
user_query = quote("How to git install application?")
|
||||||
|
|
||||||
# Act
|
# Act
|
||||||
response = client.get(f"/api/v1.0/search?q={user_query}&n=1&t=org&r=true")
|
response = client.get(f"/api/search?q={user_query}&n=1&t=org&r=true")
|
||||||
|
|
||||||
# Assert
|
# Assert
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
@@ -139,7 +139,7 @@ def test_notes_search_with_only_filters(content_config: ContentConfig, search_co
|
|||||||
user_query = quote('+"Emacs" file:"*.org"')
|
user_query = quote('+"Emacs" file:"*.org"')
|
||||||
|
|
||||||
# Act
|
# Act
|
||||||
response = client.get(f"/api/v1.0/search?q={user_query}&n=1&t=org")
|
response = client.get(f"/api/search?q={user_query}&n=1&t=org")
|
||||||
|
|
||||||
# Assert
|
# Assert
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
@@ -156,7 +156,7 @@ def test_notes_search_with_include_filter(content_config: ContentConfig, search_
|
|||||||
user_query = quote('How to git install application? +"Emacs"')
|
user_query = quote('How to git install application? +"Emacs"')
|
||||||
|
|
||||||
# Act
|
# Act
|
||||||
response = client.get(f"/api/v1.0/search?q={user_query}&n=1&t=org")
|
response = client.get(f"/api/search?q={user_query}&n=1&t=org")
|
||||||
|
|
||||||
# Assert
|
# Assert
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
@@ -173,7 +173,7 @@ def test_notes_search_with_exclude_filter(content_config: ContentConfig, search_
|
|||||||
user_query = quote('How to git install application? -"clone"')
|
user_query = quote('How to git install application? -"clone"')
|
||||||
|
|
||||||
# Act
|
# Act
|
||||||
response = client.get(f"/api/v1.0/search?q={user_query}&n=1&t=org")
|
response = client.get(f"/api/search?q={user_query}&n=1&t=org")
|
||||||
|
|
||||||
# Assert
|
# Assert
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
|||||||
Reference in New Issue
Block a user