cleanup
This commit is contained in:
@@ -4,19 +4,20 @@
|
||||
import logging
|
||||
import traceback
|
||||
import regex
|
||||
from regex import Pattern
|
||||
from typing import Union
|
||||
from aiohttp import ClientSession, ClientTimeout
|
||||
from fastapi import FastAPI, Request, HTTPException, BackgroundTasks
|
||||
from .constructors import ValidHookSongRequest, ValidAISongRequest
|
||||
|
||||
class AI(FastAPI):
|
||||
"""AI Endpoints"""
|
||||
def __init__(self, app: FastAPI, my_util, constants, glob_state): # pylint: disable=super-init-not-called
|
||||
def __init__(self, app: FastAPI, my_util, constants): # pylint: disable=super-init-not-called
|
||||
self.app = app
|
||||
self.util = my_util
|
||||
self.constants = constants
|
||||
self.glob_state = glob_state
|
||||
self.url_clean_regex = regex.compile(r'^\/ai\/(openai|base)\/')
|
||||
self.endpoints = {
|
||||
self.url_clean_regex: Pattern = regex.compile(r'^\/ai\/(openai|base)\/')
|
||||
self.endpoints: dict = {
|
||||
"ai/openai": self.ai_openai_handler,
|
||||
"ai/base": self.ai_handler,
|
||||
"ai/song": self.ai_song_handler,
|
||||
@@ -29,16 +30,20 @@ class AI(FastAPI):
|
||||
include_in_schema=False)
|
||||
|
||||
|
||||
async def respond_via_webhook(self, data: ValidHookSongRequest, originalRequest: Request):
|
||||
async def respond_via_webhook(self, data: ValidHookSongRequest, originalRequest: Request) -> bool:
|
||||
"""Respond via Webhook"""
|
||||
try:
|
||||
logging.debug("Request received: %s", data)
|
||||
data2 = data.copy()
|
||||
del data2.hook
|
||||
|
||||
if not data.hook:
|
||||
return False
|
||||
|
||||
response = await self.ai_song_handler(data2, originalRequest)
|
||||
if not response.get('resp'):
|
||||
logging.critical("NO RESP!")
|
||||
return
|
||||
return False
|
||||
response = response.get('resp')
|
||||
hook_data = {
|
||||
'username': 'Claude',
|
||||
@@ -82,7 +87,6 @@ class AI(FastAPI):
|
||||
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
|
||||
except Exception as e: # pylint: disable=broad-exception-caught
|
||||
@@ -117,7 +121,6 @@ class AI(FastAPI):
|
||||
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
|
||||
except Exception as e: # pylint: disable=broad-exception-caught
|
||||
@@ -134,7 +137,7 @@ class AI(FastAPI):
|
||||
'success': True,
|
||||
}
|
||||
|
||||
async def ai_song_handler(self, data: ValidAISongRequest, request: Request):
|
||||
async def ai_song_handler(self, data: Union[ValidAISongRequest, ValidHookSongRequest], request: Request):
|
||||
"""
|
||||
/ai/song
|
||||
AI (Song Info) Request [Public]
|
||||
@@ -165,9 +168,8 @@ class AI(FastAPI):
|
||||
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()
|
||||
timeout=ClientTimeout(connect=15, sock_read=30)) as aiohttp_request:
|
||||
response = await aiohttp_request.json()
|
||||
logging.debug("Response: %s",
|
||||
response)
|
||||
if response.get('type') == 'error':
|
||||
|
||||
Reference in New Issue
Block a user