api/endpoints/ai.py

189 lines
7.5 KiB
Python
Raw Normal View History

2024-08-14 22:43:20 -04:00
#!/usr/bin/env python3.12
2025-01-11 20:59:10 -05:00
# pylint: disable=bare-except, broad-exception-caught, invalid-name
2024-08-14 22:43:20 -04:00
import logging
2025-01-11 20:59:10 -05:00
import traceback
2024-08-14 22:43:20 -04:00
import regex
from aiohttp import ClientSession, ClientTimeout
2025-01-11 20:59:10 -05:00
from fastapi import FastAPI, Request, HTTPException, BackgroundTasks
2025-02-11 11:19:52 -05:00
from .constructors import ValidHookSongRequest, ValidAISongRequest
2025-01-11 20:59:10 -05:00
2024-08-14 22:43:20 -04:00
class AI(FastAPI):
2025-01-11 20:59:10 -05:00
"""AI Endpoints"""
2024-08-14 22:43:20 -04:00
def __init__(self, app: FastAPI, my_util, constants, glob_state): # pylint: disable=super-init-not-called
self.app = app
self.util = my_util
self.constants = constants
self.glob_state = glob_state
2024-10-02 20:54:34 -04:00
self.url_clean_regex = regex.compile(r'^\/ai\/(openai|base)\/')
2024-08-14 22:43:20 -04:00
self.endpoints = {
2024-11-14 14:37:32 -05:00
"ai/openai": self.ai_openai_handler,
"ai/base": self.ai_handler,
2025-01-11 20:59:10 -05:00
"ai/song": self.ai_song_handler,
"ai/hook": self.ai_hook_handler,
2024-08-14 22:43:20 -04:00
#tbd
2025-01-11 20:59:10 -05:00
}
2024-08-14 22:43:20 -04:00
for endpoint, handler in self.endpoints.items():
2025-01-29 15:48:47 -05:00
app.add_api_route(f"/{endpoint}", handler, methods=["GET", "POST"],
include_in_schema=False)
2025-01-11 20:59:10 -05:00
async def respond_via_webhook(self, data: ValidHookSongRequest, originalRequest: Request):
"""Respond via Webhook"""
try:
logging.debug("Request received: %s", data)
data2 = data.copy()
del data2.hook
response = await self.ai_song_handler(data2, originalRequest)
if not response.get('resp'):
logging.critical("NO RESP!")
return
response = response.get('resp')
hook_data = {
'username': 'Claude',
"embeds": [{
"title": "Claude's Feedback",
"description": response,
"footer": {
"text": "Current model: claude-3-haiku-20240307",
}
}]
}
async with ClientSession() as session:
2025-01-23 13:02:03 -05:00
async with await session.post(data.hook, json=hook_data,
2025-01-11 20:59:10 -05:00
timeout=ClientTimeout(connect=5, sock_read=5), headers={
'content-type': 'application/json; charset=utf-8',}) as request:
2025-01-14 18:37:49 -05:00
request.raise_for_status()
2025-01-11 20:59:10 -05:00
return True
except:
traceback.print_exc()
return False
2024-10-02 20:54:34 -04:00
2024-11-14 14:37:32 -05:00
async def ai_handler(self, request: Request):
"""
/ai/base
2024-11-14 14:37:32 -05:00
AI BASE Request
2024-11-29 15:33:12 -05:00
(Requires key)
2024-11-14 14:37:32 -05:00
"""
2025-01-11 20:59:10 -05:00
2024-11-14 14:37:32 -05:00
if not self.util.check_key(request.url.path, request.headers.get('X-Authd-With')):
raise HTTPException(status_code=403, detail="Unauthorized")
2024-10-02 20:54:34 -04:00
2024-11-14 14:37:32 -05:00
local_llm_headers = {
'Authorization': f'Bearer {self.constants.LOCAL_LLM_KEY}'
}
2025-01-11 20:59:10 -05:00
2024-11-14 14:37:32 -05:00
forward_path = self.url_clean_regex.sub('', request.url.path)
try:
2025-01-11 20:59:10 -05:00
async with ClientSession() as session:
async with await session.post(f'{self.constants.LOCAL_LLM_BASE}/{forward_path}',
json=await request.json(),
headers=local_llm_headers,
timeout=ClientTimeout(connect=15, sock_read=30)) as out_request:
await self.glob_state.increment_counter('ai_requests')
response = await out_request.json()
return response
2024-11-14 14:37:32 -05:00
except Exception as e: # pylint: disable=broad-exception-caught
2025-01-11 20:59:10 -05:00
logging.error("Error: %s", e)
return {
'err': True,
'errorText': 'General Failure'
}
2024-11-14 14:37:32 -05:00
async def ai_openai_handler(self, request: Request):
"""
/ai/openai
2024-11-14 14:37:32 -05:00
AI Request
2024-11-29 15:33:12 -05:00
(Requires key)
2024-11-14 14:37:32 -05:00
"""
2025-01-11 20:59:10 -05:00
2024-11-14 14:37:32 -05:00
if not self.util.check_key(request.url.path, request.headers.get('X-Authd-With')):
raise HTTPException(status_code=403, detail="Unauthorized")
2024-08-14 22:43:20 -04:00
2024-11-14 14:37:32 -05:00
"""
TODO: Implement Claude
Currently only routes to local LLM
"""
2025-01-11 20:59:10 -05:00
2024-11-14 14:37:32 -05:00
local_llm_headers = {
'Authorization': f'Bearer {self.constants.LOCAL_LLM_KEY}'
}
forward_path = self.url_clean_regex.sub('', request.url.path)
try:
2025-01-11 20:59:10 -05:00
async with ClientSession() as session:
async with await session.post(f'{self.constants.LOCAL_LLM_HOST}/{forward_path}',
json=await request.json(),
headers=local_llm_headers,
timeout=ClientTimeout(connect=15, sock_read=30)) as out_request:
await self.glob_state.increment_counter('ai_requests')
response = await out_request.json()
return response
2024-11-14 14:37:32 -05:00
except Exception as e: # pylint: disable=broad-exception-caught
2025-01-11 20:59:10 -05:00
logging.error("Error: %s", e)
return {
'err': True,
'errorText': 'General Failure'
}
2024-11-14 14:37:32 -05:00
2025-01-11 20:59:10 -05:00
async def ai_hook_handler(self, data: ValidHookSongRequest, request: Request, background_tasks: BackgroundTasks):
"""AI Hook Handler"""
background_tasks.add_task(self.respond_via_webhook, data, request)
return {
'success': True,
}
2024-08-14 22:43:20 -04:00
2024-09-04 20:30:11 -04:00
async def ai_song_handler(self, data: ValidAISongRequest, request: Request):
"""
/ai/song
2024-09-04 20:30:11 -04:00
AI (Song Info) Request [Public]
"""
2024-10-02 20:54:34 -04:00
ai_prompt = "You are a helpful assistant who will provide tidbits of info on songs the user may listen to."
2024-09-10 16:16:10 -04:00
ai_question = f"I am going to listen to the song \"{data.s}\" by \"{data.a}\"."
2024-09-04 20:30:11 -04:00
local_llm_headers = {
2024-11-14 14:37:32 -05:00
'x-api-key': self.constants.CLAUDE_API_KEY,
2024-10-02 20:54:34 -04:00
'anthropic-version': '2023-06-01',
'content-type': 'application/json',
2024-09-04 20:30:11 -04:00
}
2024-10-02 20:54:34 -04:00
request_data = {
2025-01-11 20:59:10 -05:00
'model': 'claude-3-haiku-20240307',
'max_tokens': 512,
'temperature': 0.6,
'system': ai_prompt,
'messages': [
{
"role": "user",
"content": ai_question.strip(),
}
]
2024-09-04 20:30:11 -04:00
}
2024-10-02 20:54:34 -04:00
2024-09-04 20:30:11 -04:00
try:
2025-01-11 20:59:10 -05:00
async with ClientSession() as session:
async with await session.post('https://api.anthropic.com/v1/messages',
json=request_data,
headers=local_llm_headers,
timeout=ClientTimeout(connect=15, sock_read=30)) as request:
await self.glob_state.increment_counter('claude_ai_requests')
response = await request.json()
logging.debug("Response: %s",
response)
if response.get('type') == 'error':
error_type = response.get('error').get('type')
error_message = response.get('error').get('message')
result = {
'resp': f"{error_type} error ({error_message})"
}
else:
result = {
'resp': response.get('content')[0].get('text').strip()
}
return result
2024-09-04 20:30:11 -04:00
except Exception as e: # pylint: disable=broad-exception-caught
2025-01-11 20:59:10 -05:00
logging.error("Error: %s", e)
return {
'err': True,
'errorText': 'General Failure'
}