Update api/utils.py
Browse files- api/utils.py +20 -7
api/utils.py
CHANGED
@@ -1,11 +1,11 @@
|
|
|
|
|
|
1 |
from datetime import datetime
|
2 |
import json
|
3 |
from typing import AsyncGenerator, Union
|
4 |
-
import uuid
|
5 |
|
6 |
import aiohttp
|
7 |
from fastapi import HTTPException
|
8 |
-
from fastapi.responses import JSONResponse
|
9 |
from api.config import GIZAI_API_ENDPOINT, GIZAI_BASE_URL
|
10 |
from api.models import ChatRequest, ImageResponseModel, ChatCompletionResponse
|
11 |
from api.logger import setup_logger
|
@@ -68,10 +68,10 @@ class GizAI:
|
|
68 |
def is_image_model(cls, model: str) -> bool:
|
69 |
return model in cls.image_models
|
70 |
|
71 |
-
async def process_gizai_response(request: ChatRequest, model: str) -> Union[AsyncGenerator[str, None],
|
72 |
async with aiohttp.ClientSession() as session:
|
73 |
if GizAI.is_image_model(model):
|
74 |
-
# Image generation
|
75 |
prompt = request.messages[-1].content if isinstance(request.messages[-1].content, str) else request.messages[-1].content[0].get("text", "")
|
76 |
data = {
|
77 |
"model": model,
|
@@ -110,7 +110,7 @@ async def process_gizai_response(request: ChatRequest, model: str) -> Union[Asyn
|
|
110 |
response_data = await response.json()
|
111 |
if response_data.get('status') == 'completed' and response_data.get('output'):
|
112 |
images = response_data['output']
|
113 |
-
return
|
114 |
else:
|
115 |
raise HTTPException(status_code=500, detail="Image generation failed.")
|
116 |
except aiohttp.ClientResponseError as e:
|
@@ -120,7 +120,7 @@ async def process_gizai_response(request: ChatRequest, model: str) -> Union[Asyn
|
|
120 |
logger.error(f"Unexpected error: {str(e)}")
|
121 |
raise HTTPException(status_code=500, detail=str(e))
|
122 |
else:
|
123 |
-
# Chat completion
|
124 |
messages_formatted = [
|
125 |
{
|
126 |
"type": "human",
|
@@ -170,7 +170,20 @@ async def process_gizai_response(request: ChatRequest, model: str) -> Union[Asyn
|
|
170 |
else:
|
171 |
# Handle non-streaming response
|
172 |
result = await response.json()
|
173 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
174 |
except aiohttp.ClientResponseError as e:
|
175 |
logger.error(f"HTTP error occurred: {e.status} - {e.message}")
|
176 |
raise HTTPException(status_code=e.status, detail=str(e))
|
|
|
1 |
+
# api/utils.py
|
2 |
+
|
3 |
from datetime import datetime
|
4 |
import json
|
5 |
from typing import AsyncGenerator, Union
|
|
|
6 |
|
7 |
import aiohttp
|
8 |
from fastapi import HTTPException
|
|
|
9 |
from api.config import GIZAI_API_ENDPOINT, GIZAI_BASE_URL
|
10 |
from api.models import ChatRequest, ImageResponseModel, ChatCompletionResponse
|
11 |
from api.logger import setup_logger
|
|
|
68 |
def is_image_model(cls, model: str) -> bool:
|
69 |
return model in cls.image_models
|
70 |
|
71 |
+
async def process_gizai_response(request: ChatRequest, model: str) -> Union[AsyncGenerator[str, None], ImageResponseModel, ChatCompletionResponse]:
|
72 |
async with aiohttp.ClientSession() as session:
|
73 |
if GizAI.is_image_model(model):
|
74 |
+
# Image generation logic
|
75 |
prompt = request.messages[-1].content if isinstance(request.messages[-1].content, str) else request.messages[-1].content[0].get("text", "")
|
76 |
data = {
|
77 |
"model": model,
|
|
|
110 |
response_data = await response.json()
|
111 |
if response_data.get('status') == 'completed' and response_data.get('output'):
|
112 |
images = response_data['output']
|
113 |
+
return ImageResponseModel(images=images, alt="Generated Image")
|
114 |
else:
|
115 |
raise HTTPException(status_code=500, detail="Image generation failed.")
|
116 |
except aiohttp.ClientResponseError as e:
|
|
|
120 |
logger.error(f"Unexpected error: {str(e)}")
|
121 |
raise HTTPException(status_code=500, detail=str(e))
|
122 |
else:
|
123 |
+
# Chat completion logic
|
124 |
messages_formatted = [
|
125 |
{
|
126 |
"type": "human",
|
|
|
170 |
else:
|
171 |
# Handle non-streaming response
|
172 |
result = await response.json()
|
173 |
+
return ChatCompletionResponse(
|
174 |
+
id=f"chatcmpl-{uuid.uuid4()}",
|
175 |
+
object="chat.completion",
|
176 |
+
created=int(datetime.now().timestamp()),
|
177 |
+
model=model,
|
178 |
+
choices=[
|
179 |
+
{
|
180 |
+
"index": 0,
|
181 |
+
"message": {"role": "assistant", "content": result.get('output', '')},
|
182 |
+
"finish_reason": "stop",
|
183 |
+
}
|
184 |
+
],
|
185 |
+
usage=None,
|
186 |
+
)
|
187 |
except aiohttp.ClientResponseError as e:
|
188 |
logger.error(f"HTTP error occurred: {e.status} - {e.message}")
|
189 |
raise HTTPException(status_code=e.status, detail=str(e))
|