Skip to content
Snippets Groups Projects
Commit f55f1e0f authored by Clemens Pohle's avatar Clemens Pohle
Browse files

Use LangChain to invoke Ollama

parent 5ea45336
No related branches found
No related tags found
1 merge request!12Dev into main
# ollama_views.py
from django.http import JsonResponse
import requests
import json
import os
from django.views.decorators.csrf import csrf_exempt
from langchain_core.messages import HumanMessage
from langchain_community.chat_models import ChatOllama
OLLAMA_URL = os.environ.get("OLLAMA_ENDPOINT") + '/api/chat'
OLLAMA_ENDPOINT = os.environ.get("OLLAMA_ENDPOINT")
OLLAMA_MODEL = os.environ.get("OLLAMA_MODEL")
@csrf_exempt
......@@ -22,41 +23,13 @@ def ollama_query(request):
if not message:
return JsonResponse({"error": "No message provided"}, status=400)
# Prepare the payload for Ollama API
payload = {
"model": OLLAMA_MODEL,
"messages": [
{ "role": "user", "content": message }
]
}
headers = {'Content-Type': 'application/json'}
# Send the request to Ollama API
response = requests.post(OLLAMA_URL, json=payload, headers=headers)
# Log the request and response for debugging
print(f"Request to Ollama: {payload}")
print(f"Response status code: {response.status_code}")
print(f"Response content: {response.content}")
# Check for successful response
if response.status_code != 200:
return JsonResponse({"error": "Failed to fetch response from Ollama"}, status=response.status_code)
# Parse the response from Ollama
response_content = response.content.decode('utf-8')
# Log the parsed response
print(f"Parsed response content: {response_content}")
# Split the response into lines and process each line
chat = ChatOllama(base_url=OLLAMA_ENDPOINT, model=OLLAMA_MODEL)
complete_message = ""
for line in response_content.split('\n'):
if line.strip():
json_line = json.loads(line)
complete_message += json_line['message']['content']
for chunks in chat.stream([HumanMessage(message)]):
complete_message += chunks.content
print(chunks.content)
# TODO: Send each chunk to the frontend as it is received
return JsonResponse({"message": complete_message.strip()}, safe=False)
return JsonResponse({"message": complete_message}, safe=False)
except Exception as e:
return JsonResponse({"error": str(e)}, status=500)
Django>=3.0,<4.0
sparqlwrapper>=2.0.0
django-cors-headers>=4.0.0
requests
langchain
langchain_community
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment