LLM view refactor

This commit is contained in:
Luciano Gervasoni
2025-04-14 13:49:06 +02:00
parent 43c6c3aabf
commit b876f6d720
4 changed files with 39 additions and 21 deletions

View File

@@ -9,7 +9,7 @@ from django.utils.timezone import now, timedelta
from .models import Urls, Source, Search, UrlContent, UrlsSourceSearch, UrlsDuplicate
import ollama
import os
#from datetime import timedelta
import json
####################################################################################################
@@ -30,14 +30,10 @@ class OllamaClient():
def get_prompt(self):
return "Rewrite the text below into a clear and concise summary, presenting the key points as if they are newly written insights. Do not mention or reference the original text, its source, or any phrases like 'According to' or 'The text states'. Instead, write in a natural, standalone format that feels like an original explanation. Keep it brief, engaging, informative, in the style of a news article, and no longer than a paragraph:"
def fetch_details(request, id):
url_item = get_object_or_404(Urls, id=id)
url_param = request.GET.get("url", "") # Get URL
model = request.GET.get("model", "") # Get LLM model
# TODO: post with body
text = request.GET.get("text", "") # Get LLM prompt
def stream_response():
def llm(request):
def stream_response(model, text):
msg_content = {
"role": "user",
"content": text,
@@ -45,8 +41,21 @@ def fetch_details(request, id):
response = OllamaClient().client.chat(model=model, messages=[msg_content], stream=True)
for chunk in response:
yield chunk["message"]["content"] # Stream each chunk of text
return StreamingHttpResponse(stream_response(), content_type="text/plain")
if request.method == 'POST':
try:
body_data = json.loads(request.body)
message = body_data.get('message')
model = body_data.get('model')
if message is None:
return JsonResponse({'error': 'No message found in request'}, status=400)
return StreamingHttpResponse(stream_response(model, message), content_type="text/plain")
except json.JSONDecodeError:
return JsonResponse({'error': 'Invalid JSON'}, status=400)
return JsonResponse({'error': 'Only POST method allowed'}, status=405)
def url_detail_view(request, id):
url_item = get_object_or_404(Urls, id=id)