Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions hospexplorer/ask/llm_connector.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import requests
import httpx
from django.conf import settings

def query_llm(query):

async def query_llm(query):
headers = {
"Authorization": f"Bearer {settings.LLM_TOKEN}",
"Content-Type": "application/json",
Expand All @@ -19,8 +20,13 @@ def query_llm(query):
"max_tokens": settings.LLM_MAX_TOKENS
}

response = requests.post(settings.LLM_HOST + settings.LLM_QUERY_ENDPOINT, json=payload, headers=headers, timeout=60)
async with httpx.AsyncClient() as client:
response = await client.post(
settings.LLM_HOST + settings.LLM_QUERY_ENDPOINT,
json=payload,
headers=headers,
timeout=settings.LLM_TIMEOUT
)

response.raise_for_status() # raises on 4xx/5xx
print(response)
response.raise_for_status()
return response.json()
31 changes: 31 additions & 0 deletions hospexplorer/ask/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Generated by Django 6.0.2 on 2026-02-10 16:50

import django.db.models.deletion
import uuid
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name='QueryTask',
fields=[
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('query_text', models.TextField()),
('status', models.CharField(choices=[('pending', 'Pending'), ('processing', 'Processing'), ('completed', 'Completed'), ('failed', 'Failed')], db_index=True, default='pending', max_length=20)),
('result', models.TextField(blank=True, default='')),
('error_message', models.TextField(blank=True, default='')),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='query_tasks', to=settings.AUTH_USER_MODEL)),
],
),
]
29 changes: 28 additions & 1 deletion hospexplorer/ask/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
import uuid

from django.conf import settings
from django.db import models

# Create your models here.

class QueryTask(models.Model):
class Status(models.TextChoices):
PENDING = "pending", "Pending"
PROCESSING = "processing", "Processing"
COMPLETED = "completed", "Completed"
FAILED = "failed", "Failed"

id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
related_name="query_tasks",
)
query_text = models.TextField()
status = models.CharField(
max_length=20,
choices=Status.choices,
default=Status.PENDING,
db_index=True,
)
result = models.TextField(blank=True, default="")
error_message = models.TextField(blank=True, default="")
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
1 change: 1 addition & 0 deletions hospexplorer/ask/templates/_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<link rel="icon" type="image/x-icon" href="assets/favicon.ico" />
<!-- Core theme CSS (includes Bootstrap)-->
<link href="{% static 'css/styles.css' %}" rel="stylesheet" />
<script defer src="https://cdn.jsdelivr.net/npm/@imacrayon/alpine-ajax@0.12.6/dist/cdn.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
</head>
Expand Down
3 changes: 3 additions & 0 deletions hospexplorer/ask/templates/_response.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<ul id="responses">
<li class="list-group-item">{{ message }}</li>
</ul>
86 changes: 70 additions & 16 deletions hospexplorer/ask/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,88 @@ <h1 class="mt-4">Hopper</h1>
userQuery: '',
answers: [],
isLoading: false,
pollInterval: null,

async getAnswer() {
if (!this.userQuery.trim()) return;
this.isLoading = true;

try {
const response = await fetch('{% url 'ask:query-llm' %}?query=' + encodeURIComponent(this.userQuery));
const data = await response.json();
if (!response.ok || data.error) {
this.answers.push('Something went wrong. Please try again.');
} else {
this.answers.push(data.message);
const submitResponse = await fetch(
'{% url 'ask:submit-query' %}?query=' + encodeURIComponent(this.userQuery)
);
const submitData = await submitResponse.json();

if (!submitResponse.ok || submitData.error) {
this.answers.push(submitData.error || 'Something went wrong. Please try again.');
this.isLoading = false;
return;
}

this.pollForResult(submitData.task_id);
} catch (error) {
this.answers.push('Something went wrong. Please try again.');
this.isLoading = false;
}
this.isLoading = false;
},

pollForResult(taskId) {
if (this.pollInterval) clearInterval(this.pollInterval);

let attempts = 0;
const maxAttempts = 50;
const pollUrl = '{% url 'ask:poll-query' task_id='00000000-0000-0000-0000-000000000000' %}'.replace(
'00000000-0000-0000-0000-000000000000', taskId
);

this.pollInterval = setInterval(async () => {
attempts++;
if (attempts >= maxAttempts) {
clearInterval(this.pollInterval);
this.pollInterval = null;
this.answers.push('Request timed out. Please try again.');
this.isLoading = false;
return;
}

try {
const pollResponse = await fetch(pollUrl);
const pollData = await pollResponse.json();

if (pollData.status === 'completed') {
clearInterval(this.pollInterval);
this.pollInterval = null;
this.answers.push(pollData.result);
this.isLoading = false;
} else if (pollData.status === 'failed') {
clearInterval(this.pollInterval);
this.pollInterval = null;
this.answers.push(pollData.error || 'Something went wrong. Please try again.');
this.isLoading = false;
}
} catch (error) {
clearInterval(this.pollInterval);
this.pollInterval = null;
this.answers.push('Something went wrong. Please try again.');
this.isLoading = false;
}
}, 3000);
}
}">

<div class="input-group mb-3">
<input type="text" class="form-control" placeholder="Ask me something!" aria-label="Recipient's username" aria-describedby="button-addon2" x-model="userQuery" @keyup.enter="getAnswer()">
<button type="button" class="btn btn-outline-secondary" id="button-addon2" @click="getAnswer()">Go!</button>
</div>
<form @submit.prevent="getAnswer()">
<div class="input-group mb-3">
<input type="text" x-model="userQuery" class="form-control"
placeholder="Ask me something!" required>
<button type="submit" class="btn btn-outline-secondary" :disabled="isLoading">
<span x-show="!isLoading">Go!</span>
<span x-show="isLoading">Thinking...</span>
</button>
</div>
</form>

<div class="d-flex" style="height:100%;">
<div class="vr" style="margin-right: 30px;"></div>
<div class="vr" style="margin-right: 30px;"></div>
<ul class="list-group responses" style="width: 100%">
<template x-for="(answer, index) in answers" :key="index">
<li class="list-group-item markdown-content" x-html="marked.parse(answer)"></li>
Expand All @@ -50,9 +108,5 @@ <h1 class="mt-4">Hopper</h1>
</li>
</ul>
</div>
<div>

</div>
</div>
{% endblock %}

2 changes: 2 additions & 0 deletions hospexplorer/ask/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
path("", views.index, name="index"),
path("mock", views.mock_response, name="mock-response"),
path("query", views.query, name="query-llm"),
path("submit", views.submit_query, name="submit-query"),
path("poll/<uuid:task_id>", views.poll_query, name="poll-query"),
]
96 changes: 87 additions & 9 deletions hospexplorer/ask/views.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,44 @@
from django.shortcuts import render
from django.http import JsonResponse
from django.conf import settings
import asyncio
import logging
import threading

from django.contrib.auth.decorators import login_required
from django.db import close_old_connections
from django.http import JsonResponse
from django.shortcuts import render
from django.views.decorators.http import require_GET

import ask.llm_connector
from ask.models import QueryTask


logger = logging.getLogger(__name__)


def _run_llm_task(task_id):
"""Background thread that calls the LLM and writes the result to the DB."""
try:
task = QueryTask.objects.get(pk=task_id)
task.status = QueryTask.Status.PROCESSING
task.save(update_fields=["status", "updated_at"])

llm_response = asyncio.run(ask.llm_connector.query_llm(task.query_text))
content = llm_response["choices"][0]["message"]["content"]

task.result = content
task.status = QueryTask.Status.COMPLETED
task.save(update_fields=["result", "status", "updated_at"])
except Exception:
logger.exception("Background LLM task failed for task_id=%s", task_id)
try:
task = QueryTask.objects.get(pk=task_id)
task.status = QueryTask.Status.FAILED
task.error_message = "Something went wrong. Please try again."
task.save(update_fields=["status", "error_message", "updated_at"])
except Exception:
logger.exception("Failed to mark task as failed, task_id=%s", task_id)
finally:
close_old_connections()


@login_required
Expand All @@ -16,13 +52,55 @@ def mock_response(request):
"message": "Okay, the user wants a three-sentence bedtime story about a unicorn. Let's start by thinking about the key elements of a good bedtime story. They usually have a peaceful setting, a gentle conflict or quest, and a happy ending.\n\nFirst sentence needs to set the scene. Maybe a magical forest with a unicorn. Luna is a common unicorn name, sounds soft. Moonlight and stars could add a calming effect.\n\nSecond sentence should introduce a small problem or something the unicorn does. Healing powers are typical for unicorns. Maybe she finds an injured animal, like a fox. Using her horn to heal adds magic.\n\nThird sentence wraps it up with a happy ending. The fox recovers, they become friends, and the forest is peaceful. Emphasize safety and dreams to make it soothing for bedtime.\n\nCheck if it's exactly three sentences. Yes. Language is simple and comforting, suitable for a child. Avoid any scary elements. Make sure it flows smoothly and conveys warmth.\n</think>\n\nUnder the shimmering moonlit sky, a silver-maned unicorn named Luna trotted through the enchanted forest, her hooves leaving trails of stardust. When she discovered a wounded fox whimpering beneath an ancient oak, she touched her glowing horn to its paw, weaving magic that healed the hurt. With the fox curled beside her, Luna rested on a bed of moss, her heart full as the forest whispered lullabies, ensuring all creatures drifted into dreams of peace."
})


@login_required
def query(request):
async def query(request):
try:
llm_response = ask.llm_connector.query_llm(request.GET["query"])
llm_response = await ask.llm_connector.query_llm(request.GET["query"])
content = llm_response["choices"][0]["message"]["content"]
return JsonResponse({"message": content})
except (KeyError, IndexError, TypeError) as e:
return JsonResponse({"error": f"Unexpected response from server: {e}"}, status=500)
except Exception as e:
return JsonResponse({"error": f"Failed to connect to server: {e}"}, status=500)
except Exception:
logger.exception("Failed to query LLM")
return JsonResponse({"error": "Something went wrong. Please try again."}, status=500)


@login_required
@require_GET
def submit_query(request):
"""Accept a query, create a task, spawn a background thread, return task ID."""
query_text = request.GET.get("query", "").strip()
if not query_text:
return JsonResponse({"error": "Query is required."}, status=400)

task = QueryTask.objects.create(
user=request.user,
query_text=query_text,
status=QueryTask.Status.PENDING,
)

thread = threading.Thread(target=_run_llm_task, args=(task.id,), daemon=True)
thread.start()

return JsonResponse({"task_id": str(task.id)})


@login_required
@require_GET
def poll_query(request, task_id):
"""Return the current status of a QueryTask. Only the owning user can poll."""
try:
task = QueryTask.objects.get(pk=task_id, user=request.user)
except QueryTask.DoesNotExist:
return JsonResponse({"error": "Task not found."}, status=404)

response_data = {
"task_id": str(task.id),
"status": task.status,
}

if task.status == QueryTask.Status.COMPLETED:
response_data["result"] = task.result
elif task.status == QueryTask.Status.FAILED:
response_data["error"] = task.error_message

return JsonResponse(response_data)
2 changes: 2 additions & 0 deletions hospexplorer/hospexplorer/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@
LLM_TOKEN = os.getenv("LLM_TOKEN", "")
LLM_MODEL = os.getenv("LLM_MODEL", "")
LLM_QUERY_ENDPOINT = os.getenv("LLM_QUERY_ENDPOINT", "v1/chat/completions")
# Timeout in seconds for LLM API requests.
LLM_TIMEOUT = int(os.getenv("LLM_TIMEOUT", 120))
LLM_MAX_TOKENS = int(os.getenv("LLM_MAX_TOKENS", "4096"))


Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ dependencies = [
"django>=6.0.1",
"django-allauth>=65.0.0",
"gunicorn>=23.0.0",
"httpx>=0.27.0",
"psycopg[binary]>=3.0",
"requests>=2.32.5",
]
Loading