Template refactor, task update
Banter no longer fails if AI offline. Refactoring of templates.
This commit is contained in:
+38
-30
@@ -1,46 +1,54 @@
|
||||
from celery import shared_task
|
||||
from .models import Track
|
||||
|
||||
import os
|
||||
import dotenv
|
||||
|
||||
import os
|
||||
import requests
|
||||
|
||||
import time
|
||||
import json
|
||||
from random import choice
|
||||
from requests import get, post
|
||||
from requests.exceptions import ConnectionError
|
||||
|
||||
dotenv.load_dotenv()
|
||||
|
||||
UPDATE_FREQ = 10 # How often to save to database
|
||||
|
||||
@shared_task
|
||||
def get_banter(id):
|
||||
"""
|
||||
Requests an AI model for some banter.
|
||||
Currently expects an ollama instance to be running,
|
||||
but might be possible to use OpenAI API in future.
|
||||
"""
|
||||
track = Track.objects.get(pk=id)
|
||||
r = requests.post(
|
||||
os.getenv('AI_ENDPOINT'),
|
||||
json={
|
||||
'model': os.getenv('AI_MODEL'),
|
||||
'prompt': str(track)
|
||||
},
|
||||
stream=True
|
||||
)
|
||||
r.raise_for_status()
|
||||
|
||||
token = 0
|
||||
try:
|
||||
get(os.getenv('AI_ENDPOINT'))
|
||||
except ConnectionError:
|
||||
track.banter = "Error"
|
||||
track.banter_done = True
|
||||
track.save()
|
||||
return f"{track}: AI endpoint not available."
|
||||
|
||||
for line in r.iter_lines():
|
||||
body = json.loads(line)
|
||||
response_part = body.get('response', '')
|
||||
token += 1
|
||||
track.banter += response_part
|
||||
try:
|
||||
r = post(
|
||||
os.getenv('AI_ENDPOINT'),
|
||||
json={
|
||||
'model': os.getenv('AI_MODEL'),
|
||||
'prompt': str(track)
|
||||
},
|
||||
stream=True
|
||||
)
|
||||
r.raise_for_status()
|
||||
|
||||
if 'error' in body:
|
||||
raise Exception(body['error'])
|
||||
for line in r.iter_lines():
|
||||
body = json.loads(line)
|
||||
if 'error' in body:
|
||||
raise Exception(body['error'])
|
||||
track.banter += body.get('response', '')
|
||||
|
||||
if body.get('done', False):
|
||||
track.banter_done = True
|
||||
track.save()
|
||||
return body['context']
|
||||
elif token % UPDATE_FREQ == 0:
|
||||
track.save()
|
||||
if body.get('done', False):
|
||||
track.banter_done = True
|
||||
track.save()
|
||||
return f"{track}: {body['context']}"
|
||||
except Exception as e:
|
||||
track.banter_done = True
|
||||
track.save()
|
||||
return f"{track}: {e}"
|
||||
|
||||
Reference in New Issue
Block a user