Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
Merge branch 'master' of github.com:khoj-ai/lantern
Browse files Browse the repository at this point in the history
  • Loading branch information
sabaimran committed Jul 19, 2023
2 parents 5775a9f + 3278c83 commit 2adec2d
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": "postgres",
"NAME": os.getenv("DB_NAME", "postgres"),
"USER": "postgres",
"PASSWORD": os.getenv("POSTGRES_PASSWORD"),
"HOST": os.environ.get("POSTGRES_HOST", "localhost"),
Expand Down
7 changes: 7 additions & 0 deletions app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
path("beta/", include("beta_product.urls")),
path("auth/", include("user_manager.urls")),
path("token/", views.obtain_auth_token, name="token"),
re_path(
r"^api/chat/.*", include("khoj_service.api_urls"), name="khoj_service_chat"
),
# api/chat/ is a special case, because the response needs to be streamed just for this endpoint.
re_path(
r"^api/chat.*", include("khoj_service.chat_urls"), name="khoj_service_chat"
),
re_path(r"^api/.*", include("khoj_service.api_urls"), name="khoj_service_api"),
path("", include("khoj_service.urls"), name="khoj_service"),
]
1 change: 1 addition & 0 deletions khoj_service/api_urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.urls import path
from khoj_service import views


urlpatterns = [
path("", views.DefaultRedirectApiView.as_view()),
]
7 changes: 7 additions & 0 deletions khoj_service/chat_urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.urls import path
from khoj_service import views


urlpatterns = [
path("", views.StreamingApiView.as_view()),
]
3 changes: 3 additions & 0 deletions khoj_service/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from app.settings import DEBUG

KHOJ_HOME = "http://localhost:3000" if DEBUG else "https://khoj.dev"
48 changes: 43 additions & 5 deletions khoj_service/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
from rest_framework.response import Response
from rest_framework import status
from khoj_service.models import RoutingTable
from django.contrib.auth.models import AnonymousUser
from django.shortcuts import redirect
from django.http import StreamingHttpResponse
from khoj_service.constants import KHOJ_HOME


class DefaultRedirectApiView(APIView):
Expand All @@ -21,8 +25,7 @@ def get(self, request):
status=status.HTTP_404_NOT_FOUND,
)
khoj_response = requests.get(f"{service_url}{request_path}")
respond = HttpResponse(khoj_response)
return respond
return HttpResponse(khoj_response)

def post(self, request):
user = request.user
Expand All @@ -37,8 +40,41 @@ def post(self, request):
khoj_response = requests.post(
f"{service_url}{request_path}", json=body, headers=request.headers
)
respond = HttpResponse(khoj_response)
return respond
return HttpResponse(khoj_response)


class StreamingApiView(APIView):
permission_classes = [IsAuthenticated]
routing_table = RoutingTable.objects.all()

def _streaming_response(self, response):
aggregator = b""
for chunk in response.iter_content(100):
# This indicates that the compiled references are coming. We want to batch this response in a single chunk.
# Bear in mind it's possible that the compiled references indicator is split across two chunks. In this case, rendering will fail.
if (
len(aggregator) != 0
or str(chunk).find("### compiled references:") != -1
):
aggregator += chunk
continue
yield chunk
yield aggregator

def get(self, request):
user = request.user
request_path = request.get_full_path()
service_url = self.routing_table.get_service_url_by_user(user)
if service_url is None:
return Response(
{"error": "user does not have a service with Khoj"},
status=status.HTTP_404_NOT_FOUND,
)
response = requests.get(f"{service_url}{request_path}", stream=True)
return StreamingHttpResponse(
streaming_content=self._streaming_response(response),
content_type=response.headers["Content-Type"],
)


class RedirectToKhojAncilliaryAssets(APIView):
Expand All @@ -60,11 +96,13 @@ def get(self, request):


class RedirectToKhojHomePage(APIView):
permission_classes = [IsAuthenticated]
routing_table = RoutingTable.objects.all()

def get(self, request):
user = request.user
if isinstance(user, AnonymousUser):
return redirect(f"{KHOJ_HOME}/login/")

service_url = self.routing_table.get_service_url_by_user(user)
if service_url is None:
# TODO: We probably want to redirect to a Sign-Up page here.
Expand Down

0 comments on commit 2adec2d

Please sign in to comment.