Create provider/gizai.py
Browse files- api/provider/gizai.py +154 -0
api/provider/gizai.py
ADDED
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import uuid
|
3 |
+
from typing import List, Dict, Any
|
4 |
+
from datetime import datetime
|
5 |
+
|
6 |
+
from aiohttp import ClientSession
|
7 |
+
from api.logger import setup_logger
|
8 |
+
|
9 |
+
logger = setup_logger(__name__)
|
10 |
+
|
11 |
+
class GizAIProvider:
|
12 |
+
# Class variables
|
13 |
+
url = "https://app.giz.ai/assistant/"
|
14 |
+
api_endpoint = "https://app.giz.ai/api/data/users/inferenceServer.infer"
|
15 |
+
working = True
|
16 |
+
|
17 |
+
supports_system_message = True
|
18 |
+
supports_message_history = True
|
19 |
+
|
20 |
+
# Chat models
|
21 |
+
default_model = 'chat-gemini-flash'
|
22 |
+
chat_models = [
|
23 |
+
default_model,
|
24 |
+
'chat-gemini-pro',
|
25 |
+
'chat-gpt4m',
|
26 |
+
'chat-gpt4',
|
27 |
+
'claude-sonnet',
|
28 |
+
'claude-haiku',
|
29 |
+
'llama-3-70b',
|
30 |
+
'llama-3-8b',
|
31 |
+
'mistral-large',
|
32 |
+
'chat-o1-mini'
|
33 |
+
]
|
34 |
+
|
35 |
+
# Image models
|
36 |
+
image_models = [
|
37 |
+
'flux1',
|
38 |
+
'sdxl',
|
39 |
+
'sd',
|
40 |
+
'sd35',
|
41 |
+
]
|
42 |
+
|
43 |
+
models = [*chat_models, *image_models]
|
44 |
+
|
45 |
+
model_aliases = {
|
46 |
+
# Chat model aliases
|
47 |
+
"gemini-flash": "chat-gemini-flash",
|
48 |
+
"gemini-pro": "chat-gemini-pro",
|
49 |
+
"gpt-4o-mini": "chat-gpt4m",
|
50 |
+
"gpt-4o": "chat-gpt4",
|
51 |
+
"claude-3.5-sonnet": "claude-sonnet",
|
52 |
+
"claude-3-haiku": "claude-haiku",
|
53 |
+
"llama-3.1-70b": "llama-3-70b",
|
54 |
+
"llama-3.1-8b": "llama-3-8b",
|
55 |
+
"o1-mini": "chat-o1-mini",
|
56 |
+
# Image model aliases
|
57 |
+
"sd-1.5": "sd",
|
58 |
+
"sd-3.5": "sd35",
|
59 |
+
"flux-schnell": "flux1",
|
60 |
+
}
|
61 |
+
|
62 |
+
@classmethod
|
63 |
+
def get_model(cls, model: str) -> str:
|
64 |
+
if model in cls.models:
|
65 |
+
return model
|
66 |
+
elif model in cls.model_aliases:
|
67 |
+
return cls.model_aliases[model]
|
68 |
+
else:
|
69 |
+
return cls.default_model
|
70 |
+
|
71 |
+
@classmethod
|
72 |
+
def is_image_model(cls, model: str) -> bool:
|
73 |
+
return model in cls.image_models
|
74 |
+
|
75 |
+
@classmethod
|
76 |
+
async def create_async_generator(
|
77 |
+
cls,
|
78 |
+
model: str,
|
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 |
+
headers = {
|
89 |
+
'Accept': 'application/json, text/plain, */*',
|
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 |
+
async with ClientSession() as session:
|
107 |
+
if cls.is_image_model(model):
|
108 |
+
# Image generation
|
109 |
+
prompt = messages[-1]["content"]
|
110 |
+
data = {
|
111 |
+
"model": model,
|
112 |
+
"input": {
|
113 |
+
"width": "1024",
|
114 |
+
"height": "1024",
|
115 |
+
"steps": 4,
|
116 |
+
"output_format": "webp",
|
117 |
+
"batch_size": 1,
|
118 |
+
"mode": "plan",
|
119 |
+
"prompt": prompt
|
120 |
+
}
|
121 |
+
}
|
122 |
+
async with session.post(
|
123 |
+
cls.api_endpoint,
|
124 |
+
headers=headers,
|
125 |
+
data=json.dumps(data),
|
126 |
+
) as response:
|
127 |
+
response.raise_for_status()
|
128 |
+
response_data = await response.json()
|
129 |
+
if response_data.get('status') == 'completed' and response_data.get('output'):
|
130 |
+
for url in response_data['output']:
|
131 |
+
yield {"images": url, "alt": "Generated Image"}
|
132 |
+
else:
|
133 |
+
# Chat completion
|
134 |
+
data = {
|
135 |
+
"model": model,
|
136 |
+
"input": {
|
137 |
+
"messages": [
|
138 |
+
{
|
139 |
+
"type": "human",
|
140 |
+
"content": " ".join([msg['content'] for msg in messages])
|
141 |
+
}
|
142 |
+
],
|
143 |
+
"mode": "plan"
|
144 |
+
},
|
145 |
+
"noStream": True
|
146 |
+
}
|
147 |
+
async with session.post(
|
148 |
+
cls.api_endpoint,
|
149 |
+
headers=headers,
|
150 |
+
data=json.dumps(data),
|
151 |
+
) as response:
|
152 |
+
response.raise_for_status()
|
153 |
+
result = await response.json()
|
154 |
+
yield result.get('output', '')
|