GPTRouter 0.2.10__tar.gz → 0.3.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {GPTRouter-0.2.10 → GPTRouter-0.3.1}/GPTRouter.egg-info/PKG-INFO +1 -1
- {GPTRouter-0.2.10 → GPTRouter-0.3.1}/PKG-INFO +1 -1
- {GPTRouter-0.2.10 → GPTRouter-0.3.1}/gpt_router/client.py +36 -21
- {GPTRouter-0.2.10 → GPTRouter-0.3.1}/gpt_router/models.py +11 -0
- {GPTRouter-0.2.10 → GPTRouter-0.3.1}/setup.py +1 -1
- {GPTRouter-0.2.10 → GPTRouter-0.3.1}/GPTRouter.egg-info/SOURCES.txt +0 -0
- {GPTRouter-0.2.10 → GPTRouter-0.3.1}/GPTRouter.egg-info/dependency_links.txt +0 -0
- {GPTRouter-0.2.10 → GPTRouter-0.3.1}/GPTRouter.egg-info/requires.txt +0 -0
- {GPTRouter-0.2.10 → GPTRouter-0.3.1}/GPTRouter.egg-info/top_level.txt +0 -0
- {GPTRouter-0.2.10 → GPTRouter-0.3.1}/LICENSE +0 -0
- {GPTRouter-0.2.10 → GPTRouter-0.3.1}/README.md +0 -0
- {GPTRouter-0.2.10 → GPTRouter-0.3.1}/gpt_router/__init__.py +0 -0
- {GPTRouter-0.2.10 → GPTRouter-0.3.1}/gpt_router/constants.py +0 -0
- {GPTRouter-0.2.10 → GPTRouter-0.3.1}/gpt_router/enums.py +0 -0
- {GPTRouter-0.2.10 → GPTRouter-0.3.1}/gpt_router/exceptions.py +0 -0
- {GPTRouter-0.2.10 → GPTRouter-0.3.1}/pyproject.toml +0 -0
- {GPTRouter-0.2.10 → GPTRouter-0.3.1}/setup.cfg +0 -0
|
@@ -6,6 +6,7 @@ import httpx
|
|
|
6
6
|
from typing import Generator, List, Optional, Union
|
|
7
7
|
|
|
8
8
|
from gpt_router.models import (
|
|
9
|
+
GPTRouterMetadata,
|
|
9
10
|
ImageGenerationRequest,
|
|
10
11
|
ImageGenerationResponse,
|
|
11
12
|
ModelGenerationRequest,
|
|
@@ -41,17 +42,41 @@ class GPTRouterClient:
|
|
|
41
42
|
models = None
|
|
42
43
|
request_timeout = DEFAULT_REQUEST_TIMEOUT
|
|
43
44
|
|
|
44
|
-
def __init__(self, base_url, api_key, request_timeout: int = 60):
|
|
45
|
+
def __init__(self, base_url, api_key, request_timeout: int = 60, additional_metadata: Optional[GPTRouterMetadata] = None):
|
|
45
46
|
self.base_url = base_url
|
|
46
47
|
self.api_key = api_key
|
|
47
48
|
self.request_timeout = request_timeout
|
|
49
|
+
self.additional_metadata = additional_metadata
|
|
50
|
+
|
|
51
|
+
def add_metadata_info(
|
|
52
|
+
self,
|
|
53
|
+
payload: dict,
|
|
54
|
+
model_router_metadata: Optional[GPTRouterMetadata] = None,
|
|
55
|
+
):
|
|
56
|
+
metadata = {}
|
|
57
|
+
if self.additional_metadata:
|
|
58
|
+
metadata.update(self.additional_metadata.dict())
|
|
59
|
+
if model_router_metadata:
|
|
60
|
+
metadata.update(model_router_metadata.dict())
|
|
61
|
+
|
|
62
|
+
payload.update(
|
|
63
|
+
{
|
|
64
|
+
"metadata": metadata,
|
|
65
|
+
"tag": metadata.get('tag'),
|
|
66
|
+
"createdByUserId": metadata['created_by_user_id'],
|
|
67
|
+
"historyId": str(metadata['history_id']) if metadata.get('history_id') else None,
|
|
68
|
+
}
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
payload = {k: v for k, v in payload.items() if v is not None}
|
|
72
|
+
return payload
|
|
48
73
|
|
|
49
74
|
async def _async_api_call(self, *, path: str, method: str, payload: dict):
|
|
50
75
|
try:
|
|
51
76
|
async with httpx.AsyncClient() as client:
|
|
52
77
|
response = await client.request(
|
|
53
78
|
method,
|
|
54
|
-
url=self.base_url +
|
|
79
|
+
url=self.base_url.rstrip('/') + ('/api' if not self.base_url.endswith('/api') else '') + path,
|
|
55
80
|
headers={
|
|
56
81
|
"content-type": "application/json",
|
|
57
82
|
"ws-secret": self.api_key,
|
|
@@ -77,7 +102,7 @@ class GPTRouterClient:
|
|
|
77
102
|
with httpx.Client() as client:
|
|
78
103
|
response = client.request(
|
|
79
104
|
method,
|
|
80
|
-
url=self.base_url +
|
|
105
|
+
url=self.base_url.rstrip('/') + ('/api' if not self.base_url.endswith('/api') else '') + path,
|
|
81
106
|
headers={
|
|
82
107
|
"content-type": "application/json",
|
|
83
108
|
"ws-secret": self.api_key,
|
|
@@ -103,7 +128,7 @@ class GPTRouterClient:
|
|
|
103
128
|
async with httpx.AsyncClient() as client:
|
|
104
129
|
async with client.stream(
|
|
105
130
|
method,
|
|
106
|
-
url=
|
|
131
|
+
url=self.base_url.rstrip('/') + ('/api' if not self.base_url.endswith('/api') else '') + path,
|
|
107
132
|
data=json.dumps(payload),
|
|
108
133
|
headers={
|
|
109
134
|
"Content-type": "application/json",
|
|
@@ -142,7 +167,7 @@ class GPTRouterClient:
|
|
|
142
167
|
with httpx.Client() as client:
|
|
143
168
|
with client.stream(
|
|
144
169
|
method=method,
|
|
145
|
-
url=
|
|
170
|
+
url=self.base_url.rstrip('/') + ('/api' if not self.base_url.endswith('/api') else '') + path,
|
|
146
171
|
data=json.dumps(payload),
|
|
147
172
|
headers={
|
|
148
173
|
"Content-type": "application/json",
|
|
@@ -178,10 +203,8 @@ class GPTRouterClient:
|
|
|
178
203
|
*,
|
|
179
204
|
ordered_generation_requests: List[ModelGenerationRequest],
|
|
180
205
|
is_stream=False,
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
history_id: Optional[str] = None,
|
|
184
|
-
metadata: Optional[dict] = None,
|
|
206
|
+
model_router_metadata: Optional[GPTRouterMetadata] = None,
|
|
207
|
+
**kwargs,
|
|
185
208
|
) -> Union[GenerationResponse, Generator[ChunkedGenerationResponse]]:
|
|
186
209
|
api_path = "/v1/generate"
|
|
187
210
|
api_method = "POST"
|
|
@@ -191,11 +214,8 @@ class GPTRouterClient:
|
|
|
191
214
|
request.dict(exclude_none=True, by_alias=True)
|
|
192
215
|
for request in ordered_generation_requests
|
|
193
216
|
],
|
|
194
|
-
"tag": tag,
|
|
195
|
-
"createdByUserId": user_id,
|
|
196
|
-
"historyId": history_id,
|
|
197
|
-
"metadata": metadata,
|
|
198
217
|
}
|
|
218
|
+
api_payload = self.add_metadata_info(api_payload, model_router_metadata)
|
|
199
219
|
if is_stream:
|
|
200
220
|
return self.stream_events(
|
|
201
221
|
path=api_path,
|
|
@@ -214,10 +234,8 @@ class GPTRouterClient:
|
|
|
214
234
|
*,
|
|
215
235
|
ordered_generation_requests: List[ModelGenerationRequest],
|
|
216
236
|
is_stream=False,
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
history_id: Optional[str] = None,
|
|
220
|
-
metadata: Optional[dict] = None,
|
|
237
|
+
model_router_metadata: Optional[GPTRouterMetadata] = None,
|
|
238
|
+
**kwargs,
|
|
221
239
|
) -> GenerationResponse:
|
|
222
240
|
api_path = "/v1/generate"
|
|
223
241
|
api_method = "POST"
|
|
@@ -227,11 +245,8 @@ class GPTRouterClient:
|
|
|
227
245
|
request.dict(exclude_none=True, by_alias=True)
|
|
228
246
|
for request in ordered_generation_requests
|
|
229
247
|
],
|
|
230
|
-
"tag": tag,
|
|
231
|
-
"createdByUserId": user_id,
|
|
232
|
-
"historyId": history_id,
|
|
233
|
-
"metadata": metadata,
|
|
234
248
|
}
|
|
249
|
+
api_payload = self.add_metadata_info(api_payload, model_router_metadata)
|
|
235
250
|
if is_stream:
|
|
236
251
|
return self.astream_events(
|
|
237
252
|
path=api_path,
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
from uuid import UUID
|
|
1
2
|
from pydantic import BaseModel, Field
|
|
2
3
|
|
|
3
4
|
from typing import List, Optional, Dict, Any
|
|
@@ -8,6 +9,11 @@ def to_camel(string):
|
|
|
8
9
|
return camelize(string)
|
|
9
10
|
|
|
10
11
|
|
|
12
|
+
class GPTRouterMetadata(BaseModel):
|
|
13
|
+
tag: Optional[str] = None
|
|
14
|
+
history_id: Optional[str] = None
|
|
15
|
+
created_by_user_id: Optional[str] = None
|
|
16
|
+
|
|
11
17
|
class GenerationParams(BaseModel):
|
|
12
18
|
messages: Optional[List[Any]] = None
|
|
13
19
|
max_tokens: Optional[int] = None
|
|
@@ -22,6 +28,11 @@ class GenerationParams(BaseModel):
|
|
|
22
28
|
function_call: Optional[dict] = None
|
|
23
29
|
tools: Optional[List[Any]] = None
|
|
24
30
|
response_format: Optional[Dict[str, Any]] = None
|
|
31
|
+
parallel_tool_calls: Optional[bool] = False
|
|
32
|
+
tag: Optional[str] = None
|
|
33
|
+
user_id: Optional[str] = None
|
|
34
|
+
history_id: Optional[str] = None
|
|
35
|
+
metadata: Optional[dict] = None
|
|
25
36
|
|
|
26
37
|
|
|
27
38
|
class ModelGenerationRequest(BaseModel):
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|