This repository has been archived by the owner on Aug 28, 2024. It is now read-only.
generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 🚀 Initialise project * 🎨 Add logout button * 🔧 Authentication via AzureAD * 🔧 ->
- Loading branch information
Showing
5 changed files
with
89 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
CLIENT_ID=<your_client_id> | ||
CLIENT_SECRET=<your_client_secret> | ||
AZURE_TENANT_ID=<your_azure_tenant_id> | ||
REDIRECT_URI=<your_redirect_uri> | ||
|
||
# when running locally, you can use | ||
REDIRECT_URI="https://127.0.0.1:8000/azure_auth/callback" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,50 @@ | ||
from django.shortcuts import render | ||
from django.shortcuts import render, redirect | ||
from django.http import JsonResponse | ||
from azure_auth.decorators import azure_auth_required | ||
import requests | ||
import logging | ||
import json | ||
|
||
# Configure logging | ||
logging.basicConfig(level=logging.DEBUG) | ||
|
||
@azure_auth_required | ||
def call_ollama(request): | ||
if request.method == 'POST': | ||
user_input = request.POST.get('userInput', '') | ||
conversation_history = json.loads(user_input) | ||
try: | ||
conversation_history = json.loads(user_input) | ||
except json.JSONDecodeError as e: | ||
logging.error("JSONDecodeError: %s", e) | ||
return JsonResponse({"error": "Invalid input format"}, status=400) | ||
|
||
url = 'http://localhost:11434/api/chat' | ||
headers = {'Content-Type': 'application/json'} | ||
data = { | ||
"model": "llama3", | ||
"messages": conversation_history, | ||
"stream": False | ||
} | ||
|
||
logging.debug("Sending data to Ollama API: %s", json.dumps(data, indent=2)) | ||
|
||
try: | ||
response = requests.post(url, json=data, headers=headers) | ||
response.raise_for_status() | ||
response_data = response.json() | ||
logging.debug("Response data: %s", json.dumps(response_data, indent=2)) | ||
# Extract the actual response message | ||
|
||
ollama_response = response_data.get("message", {}).get("content", "") | ||
if not ollama_response: | ||
logging.error("Empty response from Ollama API") | ||
return JsonResponse({"error": "Empty response from Ollama API"}, status=500) | ||
|
||
return JsonResponse({"response": ollama_response}) | ||
except requests.RequestException as e: | ||
logging.error("RequestException: %s", e) | ||
return JsonResponse({"error": str(e)}, status=500) | ||
else: | ||
return render(request, 'streamingapp/input_form.html') | ||
|
||
def redirect_to_ollama(request): | ||
return redirect('/stream/call-ollama') |