Update api/provider/gizai.py
Browse files- api/provider/gizai.py +164 -135
api/provider/gizai.py
CHANGED
@@ -1,154 +1,183 @@
|
|
1 |
-
import json
|
2 |
import uuid
|
3 |
-
from typing import List, Dict, Any
|
4 |
from datetime import datetime
|
|
|
|
|
5 |
|
6 |
-
|
|
|
|
|
7 |
from api.logger import setup_logger
|
8 |
|
9 |
logger = setup_logger(__name__)
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
api_endpoint = "https://app.giz.ai/api/data/users/inferenceServer.infer"
|
15 |
-
working = True
|
16 |
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
]
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
]
|
42 |
|
43 |
-
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
return model in cls.image_models
|
74 |
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
messages: List[Dict[str, Any]],
|
80 |
-
max_tokens: int,
|
81 |
-
top_p: float,
|
82 |
-
temperature: float,
|
83 |
-
stream: bool = True,
|
84 |
-
**kwargs
|
85 |
-
):
|
86 |
-
model = cls.get_model(model)
|
87 |
|
88 |
-
|
89 |
-
|
90 |
-
'Accept-Language': 'en-US,en;q=0.9',
|
91 |
-
'Cache-Control': 'no-cache',
|
92 |
-
'Connection': 'keep-alive',
|
93 |
-
'Content-Type': 'application/json',
|
94 |
-
'Origin': 'https://app.giz.ai',
|
95 |
-
'Pragma': 'no-cache',
|
96 |
-
'Sec-Fetch-Dest': 'empty',
|
97 |
-
'Sec-Fetch-Mode': 'cors',
|
98 |
-
'Sec-Fetch-Site': 'same-origin',
|
99 |
-
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 '
|
100 |
-
'(KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36',
|
101 |
-
'sec-ch-ua': '"Not?A_Brand";v="99", "Chromium";v="130"',
|
102 |
-
'sec-ch-ua-mobile': '?0',
|
103 |
-
'sec-ch-ua-platform': '"Linux"'
|
104 |
-
}
|
105 |
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
}
|
121 |
}
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
"
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
}
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
result = await response.json()
|
154 |
-
yield result.get('output', '')
|
|
|
|
|
1 |
import uuid
|
|
|
2 |
from datetime import datetime
|
3 |
+
import json
|
4 |
+
from typing import Any, Dict, Optional
|
5 |
|
6 |
+
import httpx
|
7 |
+
from fastapi import HTTPException
|
8 |
+
from api.models import ChatRequest
|
9 |
from api.logger import setup_logger
|
10 |
|
11 |
logger = setup_logger(__name__)
|
12 |
|
13 |
+
# Base URL for giz.ai
|
14 |
+
GIZAI_BASE_URL = "https://app.giz.ai"
|
15 |
+
GIZAI_API_ENDPOINT = f"{GIZAI_BASE_URL}/api/data/users/inferenceServer.infer"
|
|
|
|
|
16 |
|
17 |
+
# Headers for giz.ai
|
18 |
+
GIZAI_HEADERS = {
|
19 |
+
'Accept': 'application/json, text/plain, */*',
|
20 |
+
'Accept-Language': 'en-US,en;q=0.9',
|
21 |
+
'Cache-Control': 'no-cache',
|
22 |
+
'Connection': 'keep-alive',
|
23 |
+
'Content-Type': 'application/json',
|
24 |
+
'Origin': 'https://app.giz.ai',
|
25 |
+
'Pragma': 'no-cache',
|
26 |
+
'Sec-Fetch-Dest': 'empty',
|
27 |
+
'Sec-Fetch-Mode': 'cors',
|
28 |
+
'Sec-Fetch-Site': 'same-origin',
|
29 |
+
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36',
|
30 |
+
'sec-ch-ua': '"Not?A_Brand";v="99", "Chromium";v="130"',
|
31 |
+
'sec-ch-ua-mobile': '?0',
|
32 |
+
'sec-ch-ua-platform': '"Linux"'
|
33 |
+
}
|
34 |
|
35 |
+
# List of models supported by giz.ai
|
36 |
+
GIZAI_CHAT_MODELS = [
|
37 |
+
'chat-gemini-flash',
|
38 |
+
'chat-gemini-pro',
|
39 |
+
'chat-gpt4m',
|
40 |
+
'chat-gpt4',
|
41 |
+
'claude-sonnet',
|
42 |
+
'claude-haiku',
|
43 |
+
'llama-3-70b',
|
44 |
+
'llama-3-8b',
|
45 |
+
'mistral-large',
|
46 |
+
'chat-o1-mini'
|
47 |
+
]
|
|
|
48 |
|
49 |
+
GIZAI_IMAGE_MODELS = [
|
50 |
+
'flux1',
|
51 |
+
'sdxl',
|
52 |
+
'sd',
|
53 |
+
'sd35',
|
54 |
+
]
|
|
|
55 |
|
56 |
+
GIZAI_MODELS = GIZAI_CHAT_MODELS + GIZAI_IMAGE_MODELS
|
57 |
|
58 |
+
GIZAI_MODEL_ALIASES = {
|
59 |
+
# Chat model aliases
|
60 |
+
"gemini-flash": "chat-gemini-flash",
|
61 |
+
"gemini-pro": "chat-gemini-pro",
|
62 |
+
"gpt-4o-mini": "chat-gpt4m",
|
63 |
+
"gpt-4o": "chat-gpt4",
|
64 |
+
"claude-3.5-sonnet": "claude-sonnet",
|
65 |
+
"claude-3-haiku": "claude-haiku",
|
66 |
+
"llama-3.1-70b": "llama-3-70b",
|
67 |
+
"llama-3.1-8b": "llama-3-8b",
|
68 |
+
"o1-mini": "chat-o1-mini",
|
69 |
+
# Image model aliases
|
70 |
+
"sd-1.5": "sd",
|
71 |
+
"sd-3.5": "sd35",
|
72 |
+
"flux-schnell": "flux1",
|
73 |
+
}
|
74 |
|
75 |
+
def get_gizai_model(model: str) -> str:
|
76 |
+
if model in GIZAI_MODELS:
|
77 |
+
return model
|
78 |
+
elif model in GIZAI_MODEL_ALIASES:
|
79 |
+
return GIZAI_MODEL_ALIASES[model]
|
80 |
+
else:
|
81 |
+
# Default model
|
82 |
+
return 'chat-gemini-flash'
|
83 |
|
84 |
+
def is_image_model(model: str) -> bool:
|
85 |
+
return model in GIZAI_IMAGE_MODELS
|
|
|
86 |
|
87 |
+
async def process_streaming_response(request: ChatRequest):
|
88 |
+
# giz.ai does not support streaming
|
89 |
+
# So we can raise an error or process as non-streaming
|
90 |
+
return await process_non_streaming_response(request)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
|
92 |
+
async def process_non_streaming_response(request: ChatRequest):
|
93 |
+
model = get_gizai_model(request.model)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
|
95 |
+
async with httpx.AsyncClient() as client:
|
96 |
+
if is_image_model(model):
|
97 |
+
# Image generation
|
98 |
+
prompt = request.messages[-1].content
|
99 |
+
data = {
|
100 |
+
"model": model,
|
101 |
+
"input": {
|
102 |
+
"width": "1024",
|
103 |
+
"height": "1024",
|
104 |
+
"steps": 4,
|
105 |
+
"output_format": "webp",
|
106 |
+
"batch_size": 1,
|
107 |
+
"mode": "plan",
|
108 |
+
"prompt": prompt
|
|
|
109 |
}
|
110 |
+
}
|
111 |
+
try:
|
112 |
+
response = await client.post(
|
113 |
+
GIZAI_API_ENDPOINT,
|
114 |
+
headers=GIZAI_HEADERS,
|
115 |
+
json=data,
|
116 |
+
timeout=100,
|
117 |
+
)
|
118 |
+
response.raise_for_status()
|
119 |
+
response_data = response.json()
|
120 |
+
if response_data.get('status') == 'completed' and response_data.get('output'):
|
121 |
+
images = response_data['output']
|
122 |
+
# Return image response (e.g., URLs)
|
123 |
+
return {
|
124 |
+
"id": f"imggen-{uuid.uuid4()}",
|
125 |
+
"object": "image_generation",
|
126 |
+
"created": int(datetime.now().timestamp()),
|
127 |
+
"model": request.model,
|
128 |
+
"data": images,
|
129 |
+
}
|
130 |
+
else:
|
131 |
+
raise HTTPException(status_code=500, detail="Image generation failed")
|
132 |
+
except httpx.HTTPStatusError as e:
|
133 |
+
logger.error(f"HTTP error occurred: {e}")
|
134 |
+
raise HTTPException(status_code=e.response.status_code, detail=str(e))
|
135 |
+
except httpx.RequestError as e:
|
136 |
+
logger.error(f"Error occurred during request: {e}")
|
137 |
+
raise HTTPException(status_code=500, detail=str(e))
|
138 |
+
else:
|
139 |
+
# Chat completion
|
140 |
+
messages_content = "\n".join([f"{msg.role}: {msg.content}" for msg in request.messages])
|
141 |
+
data = {
|
142 |
+
"model": model,
|
143 |
+
"input": {
|
144 |
+
"messages": [
|
145 |
+
{
|
146 |
+
"type": "human",
|
147 |
+
"content": messages_content
|
148 |
+
}
|
149 |
+
],
|
150 |
+
"mode": "plan"
|
151 |
+
},
|
152 |
+
"noStream": True
|
153 |
+
}
|
154 |
+
try:
|
155 |
+
response = await client.post(
|
156 |
+
GIZAI_API_ENDPOINT,
|
157 |
+
headers=GIZAI_HEADERS,
|
158 |
+
json=data,
|
159 |
+
timeout=100,
|
160 |
+
)
|
161 |
+
response.raise_for_status()
|
162 |
+
response_data = response.json()
|
163 |
+
output = response_data.get('output', '')
|
164 |
+
return {
|
165 |
+
"id": f"chatcmpl-{uuid.uuid4()}",
|
166 |
+
"object": "chat.completion",
|
167 |
+
"created": int(datetime.now().timestamp()),
|
168 |
+
"model": request.model,
|
169 |
+
"choices": [
|
170 |
+
{
|
171 |
+
"index": 0,
|
172 |
+
"message": {"role": "assistant", "content": output},
|
173 |
+
"finish_reason": "stop",
|
174 |
+
}
|
175 |
+
],
|
176 |
+
"usage": None,
|
177 |
}
|
178 |
+
except httpx.HTTPStatusError as e:
|
179 |
+
logger.error(f"HTTP error occurred: {e}")
|
180 |
+
raise HTTPException(status_code=e.response.status_code, detail=str(e))
|
181 |
+
except httpx.RequestError as e:
|
182 |
+
logger.error(f"Error occurred during request: {e}")
|
183 |
+
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|