deepanything 0.1.6__tar.gz → 0.1.7__tar.gz
Sign up to get free protection for your applications and to get access to all the features.
- {deepanything-0.1.6 → deepanything-0.1.7}/PKG-INFO +1 -1
- {deepanything-0.1.6 → deepanything-0.1.7}/deepanything/DeepAnythingClient.py +27 -10
- {deepanything-0.1.6 → deepanything-0.1.7}/deepanything/ReasonClient.py +26 -0
- {deepanything-0.1.6 → deepanything-0.1.7}/deepanything/Server/Server.py +64 -21
- {deepanything-0.1.6 → deepanything-0.1.7}/deepanything/Utility.py +14 -1
- {deepanything-0.1.6 → deepanything-0.1.7}/deepanything/__main__.py +1 -1
- deepanything-0.1.7/deepanything/metadatas.py +4 -0
- {deepanything-0.1.6 → deepanything-0.1.7}/deepanything.egg-info/PKG-INFO +1 -1
- {deepanything-0.1.6 → deepanything-0.1.7}/test/server.py +1 -1
- deepanything-0.1.6/deepanything/metadatas.py +0 -1
- {deepanything-0.1.6 → deepanything-0.1.7}/LICENSE +0 -0
- {deepanything-0.1.6 → deepanything-0.1.7}/README.md +0 -0
- {deepanything-0.1.6 → deepanything-0.1.7}/deepanything/ResponseClient.py +0 -0
- {deepanything-0.1.6 → deepanything-0.1.7}/deepanything/Server/Types.py +0 -0
- {deepanything-0.1.6 → deepanything-0.1.7}/deepanything/Server/__init__.py +0 -0
- {deepanything-0.1.6 → deepanything-0.1.7}/deepanything/Stream.py +0 -0
- {deepanything-0.1.6 → deepanything-0.1.7}/deepanything/__init__.py +0 -0
- {deepanything-0.1.6 → deepanything-0.1.7}/deepanything.egg-info/SOURCES.txt +0 -0
- {deepanything-0.1.6 → deepanything-0.1.7}/deepanything.egg-info/dependency_links.txt +0 -0
- {deepanything-0.1.6 → deepanything-0.1.7}/deepanything.egg-info/entry_points.txt +0 -0
- {deepanything-0.1.6 → deepanything-0.1.7}/deepanything.egg-info/requires.txt +0 -0
- {deepanything-0.1.6 → deepanything-0.1.7}/deepanything.egg-info/top_level.txt +0 -0
- {deepanything-0.1.6 → deepanything-0.1.7}/requirements.txt +0 -0
- {deepanything-0.1.6 → deepanything-0.1.7}/setup.cfg +0 -0
- {deepanything-0.1.6 → deepanything-0.1.7}/setup.py +0 -0
- {deepanything-0.1.6 → deepanything-0.1.7}/test/think.py +0 -0
- {deepanything-0.1.6 → deepanything-0.1.7}/test/think_async.py +0 -0
@@ -4,7 +4,8 @@ from openai.types.chat.chat_completion import ChatCompletion
|
|
4
4
|
|
5
5
|
from deepanything.Stream import Stream,AsyncStream
|
6
6
|
from deepanything.Utility import make_usage, make_chat_completion_message, merge_chunk, async_merge_chunk, \
|
7
|
-
make_chat_completion_chunk, make_chat_completion, make_chat_completion_choice, merge_usage, make_id_by_timestamp
|
7
|
+
make_chat_completion_chunk, make_chat_completion, make_chat_completion_choice, merge_usage, make_id_by_timestamp, \
|
8
|
+
attend_message
|
8
9
|
from deepanything.ResponseClient import ResponseClient,AsyncResponseClient
|
9
10
|
from deepanything.ReasonClient import ReasonClient,AsyncReasonClient
|
10
11
|
|
@@ -41,10 +42,7 @@ def _build_message(
|
|
41
42
|
reason_content : str,
|
42
43
|
reason_prompt : str
|
43
44
|
) -> List:
|
44
|
-
return messages
|
45
|
-
role="assistant",
|
46
|
-
content=reason_prompt.format(reason_content)
|
47
|
-
)]
|
45
|
+
return attend_message(messages,role="assistant",content=reason_prompt.format(reason_content))
|
48
46
|
def _process_reason_chunk(chunk, reasoning_contents, reason_usage, show_model, created, _id):
|
49
47
|
new_chunk = chunk.model_copy(deep=False)
|
50
48
|
new_chunk.model = show_model
|
@@ -83,6 +81,7 @@ def chat_completion(
|
|
83
81
|
reason_args=None,
|
84
82
|
response_args=None,
|
85
83
|
reason_prompt: str = "<Think>{}</Think>",
|
84
|
+
reason_system_prompt: Optional[str] = None,
|
86
85
|
created: int = int(time.time()),
|
87
86
|
stream = False,
|
88
87
|
_id: str = make_id_by_timestamp(),
|
@@ -105,7 +104,8 @@ def chat_completion(
|
|
105
104
|
created=created,
|
106
105
|
_id=_id,
|
107
106
|
reason_prompt=reason_prompt,
|
108
|
-
|
107
|
+
reason_system_prompt=reason_system_prompt,
|
108
|
+
max_tokens=max_tokens,
|
109
109
|
)
|
110
110
|
|
111
111
|
if max_tokens is not None:
|
@@ -145,6 +145,7 @@ def chat_completion_stream(
|
|
145
145
|
reason_args=None,
|
146
146
|
response_args=None,
|
147
147
|
reason_prompt: str = "<Think>{}</Think>",
|
148
|
+
reason_system_prompt: Optional[str] = None,
|
148
149
|
created: int = int(time.time()),
|
149
150
|
_id: str = make_id_by_timestamp(),
|
150
151
|
max_tokens : Optional[int] = None
|
@@ -166,6 +167,7 @@ def chat_completion_stream(
|
|
166
167
|
reason_stream = reason_client.reason_stream(
|
167
168
|
messages,
|
168
169
|
reason_model,
|
170
|
+
reason_system_prompt,
|
169
171
|
**reason_args
|
170
172
|
)
|
171
173
|
stream = reason_stream
|
@@ -206,6 +208,7 @@ async def chat_completion_async(
|
|
206
208
|
reason_args=None,
|
207
209
|
response_args=None,
|
208
210
|
reason_prompt: str = "<Think>{}</Think>",
|
211
|
+
reason_system_prompt: Optional[str] = None,
|
209
212
|
created: int = int(time.time()),
|
210
213
|
_id: str = make_id_by_timestamp(),
|
211
214
|
stream=False,
|
@@ -228,6 +231,7 @@ async def chat_completion_async(
|
|
228
231
|
created=created,
|
229
232
|
_id=_id,
|
230
233
|
reason_prompt=reason_prompt,
|
234
|
+
reason_system_prompt=reason_system_prompt,
|
231
235
|
max_tokens=max_tokens
|
232
236
|
)
|
233
237
|
|
@@ -246,11 +250,14 @@ async def chat_completion_async(
|
|
246
250
|
return reason_chat_completion
|
247
251
|
response_args["max_tokens"] = max_tokens
|
248
252
|
|
253
|
+
messages = _build_message(
|
254
|
+
messages,
|
255
|
+
reason_chat_completion.choices[0].message.reasoning_content,
|
256
|
+
reason_prompt
|
257
|
+
)
|
258
|
+
|
249
259
|
response_chat_completion:ChatCompletion = await response_client.chat_completions(
|
250
|
-
messages=messages
|
251
|
-
role="assistant",
|
252
|
-
content=reason_prompt.format(reason_chat_completion.choices[0].message.reasoning_content)
|
253
|
-
)],
|
260
|
+
messages=messages,
|
254
261
|
model=response_model,
|
255
262
|
**response_args
|
256
263
|
)
|
@@ -267,6 +274,7 @@ async def chat_completion_stream_async(
|
|
267
274
|
reason_args=None,
|
268
275
|
response_args=None,
|
269
276
|
reason_prompt: str = "<Think>{}</Think>",
|
277
|
+
reason_system_prompt: Optional[str] = None,
|
270
278
|
created: int = int(time.time()),
|
271
279
|
_id: str = make_id_by_timestamp(),
|
272
280
|
max_tokens : Optional[int] = None
|
@@ -287,6 +295,7 @@ async def chat_completion_stream_async(
|
|
287
295
|
reason_stream = await reason_client.reason_stream(
|
288
296
|
messages,
|
289
297
|
reason_model,
|
298
|
+
reason_system_prompt,
|
290
299
|
**reason_args
|
291
300
|
)
|
292
301
|
|
@@ -342,6 +351,7 @@ class DeepAnythingClient:
|
|
342
351
|
show_model : str,
|
343
352
|
reason_args=None,
|
344
353
|
response_args=None,
|
354
|
+
reason_system_prompt: Optional[str] = None,
|
345
355
|
created : int = int(time.time()),
|
346
356
|
_id : str = make_id_by_timestamp(),
|
347
357
|
stream = False
|
@@ -355,6 +365,7 @@ class DeepAnythingClient:
|
|
355
365
|
show_model=show_model,
|
356
366
|
reason_args=reason_args,
|
357
367
|
response_args=response_args,
|
368
|
+
reason_system_prompt=reason_system_prompt,
|
358
369
|
created=created,
|
359
370
|
_id=_id,
|
360
371
|
stream=stream,
|
@@ -369,6 +380,7 @@ class DeepAnythingClient:
|
|
369
380
|
show_model : str,
|
370
381
|
reason_args=None,
|
371
382
|
response_args=None,
|
383
|
+
reason_system_prompt: Optional[str] = None,
|
372
384
|
created : int = int(time.time()),
|
373
385
|
_id : str = make_id_by_timestamp()
|
374
386
|
) -> Stream:
|
@@ -381,6 +393,7 @@ class DeepAnythingClient:
|
|
381
393
|
show_model=show_model,
|
382
394
|
reason_args=reason_args,
|
383
395
|
response_args=response_args,
|
396
|
+
reason_system_prompt=reason_system_prompt,
|
384
397
|
created=created,
|
385
398
|
_id=_id,
|
386
399
|
reason_prompt=self.reason_prompt
|
@@ -411,6 +424,7 @@ class AsyncDeepAnythingClient:
|
|
411
424
|
show_model: str,
|
412
425
|
reason_args=None,
|
413
426
|
response_args=None,
|
427
|
+
reason_system_prompt: Optional[str] = None,
|
414
428
|
created: int = int(time.time()),
|
415
429
|
_id: str = make_id_by_timestamp(),
|
416
430
|
stream=False
|
@@ -424,6 +438,7 @@ class AsyncDeepAnythingClient:
|
|
424
438
|
show_model=show_model,
|
425
439
|
reason_args=reason_args,
|
426
440
|
response_args=response_args,
|
441
|
+
reason_system_prompt=reason_system_prompt,
|
427
442
|
created=created,
|
428
443
|
_id=_id,
|
429
444
|
stream=stream,
|
@@ -438,6 +453,7 @@ class AsyncDeepAnythingClient:
|
|
438
453
|
show_model : str,
|
439
454
|
reason_args=None,
|
440
455
|
response_args=None,
|
456
|
+
reason_system_prompt: Optional[str] = None,
|
441
457
|
created : int = int(time.time()),
|
442
458
|
_id : str = make_id_by_timestamp()
|
443
459
|
) -> AsyncStream:
|
@@ -450,6 +466,7 @@ class AsyncDeepAnythingClient:
|
|
450
466
|
show_model=show_model,
|
451
467
|
reason_args=reason_args,
|
452
468
|
response_args=response_args,
|
469
|
+
reason_system_prompt=reason_system_prompt,
|
453
470
|
created=created,
|
454
471
|
_id=_id,
|
455
472
|
reason_prompt=self.reason_prompt
|
@@ -1,4 +1,7 @@
|
|
1
|
+
from typing import Optional
|
2
|
+
|
1
3
|
import openai
|
4
|
+
from openai import OpenAI
|
2
5
|
from openai.types.chat import chat_completion, chat_completion_chunk
|
3
6
|
from deepanything.Stream import Stream,AsyncStream
|
4
7
|
from deepanything import Utility
|
@@ -12,6 +15,7 @@ class ReasonClient:
|
|
12
15
|
self,
|
13
16
|
messages:list[dict],
|
14
17
|
model:str,
|
18
|
+
reason_system_prompt:Optional[str] = None,
|
15
19
|
stream = False,
|
16
20
|
**kwargs
|
17
21
|
) -> Stream or chat_completion.ChatCompletion:
|
@@ -26,6 +30,7 @@ class ReasonClient:
|
|
26
30
|
def reason_stream(self,
|
27
31
|
messages:list[dict],
|
28
32
|
model:str,
|
33
|
+
reason_system_prompt:Optional[str] = None,
|
29
34
|
**kwargs
|
30
35
|
) -> Stream:
|
31
36
|
raise NotImplementedError
|
@@ -38,6 +43,7 @@ class AsyncReasonClient:
|
|
38
43
|
self,
|
39
44
|
messages:list[dict],
|
40
45
|
model:str,
|
46
|
+
reason_system_prompt:Optional[str] = None,
|
41
47
|
stream = False,
|
42
48
|
**kwargs
|
43
49
|
) -> AsyncStream or chat_completion.ChatCompletion:
|
@@ -52,6 +58,7 @@ class AsyncReasonClient:
|
|
52
58
|
async def reason_stream(self,
|
53
59
|
messages:list[dict],
|
54
60
|
model:str,
|
61
|
+
reason_system_prompt:Optional[str] = None,
|
55
62
|
**kwargs
|
56
63
|
) -> AsyncStream:
|
57
64
|
raise NotImplementedError
|
@@ -70,6 +77,7 @@ class DeepseekReasonClient(ReasonClient):
|
|
70
77
|
def reason_stream(self,
|
71
78
|
messages: list[dict],
|
72
79
|
model: str,
|
80
|
+
reason_system_prompt:Optional[str] = None, # not used
|
73
81
|
**kwargs
|
74
82
|
) -> Stream:
|
75
83
|
stream = self.client.chat.completions.create(
|
@@ -108,6 +116,7 @@ class AsyncDeepseekReasonClient(AsyncReasonClient):
|
|
108
116
|
async def reason_stream(self,
|
109
117
|
messages: list[dict],
|
110
118
|
model: str,
|
119
|
+
reason_system_prompt:Optional[str] = None,
|
111
120
|
**kwargs
|
112
121
|
) -> AsyncStream:
|
113
122
|
stream = await self.client.chat.completions.create(
|
@@ -161,8 +170,12 @@ class OpenaiReasonClient(ReasonClient):
|
|
161
170
|
def reason_stream(self,
|
162
171
|
messages: list[dict],
|
163
172
|
model: str,
|
173
|
+
reason_system_prompt:Optional[str] = None,
|
164
174
|
**kwargs
|
165
175
|
) -> Stream:
|
176
|
+
if reason_system_prompt is not None:
|
177
|
+
messages = Utility.attend_message(messages,role="system",content=reason_system_prompt)
|
178
|
+
|
166
179
|
stream = self.client.chat.completions.create(
|
167
180
|
messages=messages,
|
168
181
|
model=model,
|
@@ -177,11 +190,16 @@ class OpenaiReasonClient(ReasonClient):
|
|
177
190
|
self,
|
178
191
|
messages:list[dict],
|
179
192
|
model:str,
|
193
|
+
reason_system_prompt:Optional[str] = None,
|
180
194
|
stream = False,
|
181
195
|
**kwargs
|
182
196
|
) -> Stream or chat_completion.ChatCompletion:
|
183
197
|
if stream:
|
184
198
|
return self.reason_stream(messages, model, **kwargs)
|
199
|
+
|
200
|
+
if reason_system_prompt is not None:
|
201
|
+
messages = Utility.attend_message(messages,role="system",content=reason_system_prompt)
|
202
|
+
|
185
203
|
completion = self.client.chat.completions.create(
|
186
204
|
messages=messages,
|
187
205
|
model=model,
|
@@ -207,9 +225,13 @@ class AsyncOpenaiReasonClient(AsyncReasonClient):
|
|
207
225
|
async def reason_stream(self,
|
208
226
|
messages: list[dict],
|
209
227
|
model: str,
|
228
|
+
reason_system_prompt:Optional[str] = None,
|
210
229
|
**kwargs
|
211
230
|
) -> AsyncStream:
|
212
231
|
|
232
|
+
if reason_system_prompt is not None:
|
233
|
+
messages = Utility.attend_message(messages,role="system",content=reason_system_prompt)
|
234
|
+
|
213
235
|
stream = await self.client.chat.completions.create(
|
214
236
|
messages=messages,
|
215
237
|
model=model,
|
@@ -226,12 +248,16 @@ class AsyncOpenaiReasonClient(AsyncReasonClient):
|
|
226
248
|
async def reason(self,
|
227
249
|
messages: list[dict],
|
228
250
|
model: str,
|
251
|
+
reason_system_prompt:Optional[str] = None,
|
229
252
|
stream = False,
|
230
253
|
**kwargs
|
231
254
|
) -> AsyncStream or chat_completion.ChatCompletion:
|
232
255
|
if stream:
|
233
256
|
return await self.reason_stream(messages, model, **kwargs)
|
234
257
|
|
258
|
+
if reason_system_prompt is not None:
|
259
|
+
messages = Utility.attend_message(messages,role="system",content=reason_system_prompt)
|
260
|
+
|
235
261
|
completion = await self.client.chat.completions.create(
|
236
262
|
messages=messages,
|
237
263
|
model=model,
|
@@ -10,12 +10,16 @@ from fastapi import FastAPI,Depends, HTTPException, status,Header,Request
|
|
10
10
|
from fastapi.responses import StreamingResponse,Response
|
11
11
|
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
12
12
|
from uvicorn.config import LOGGING_CONFIG
|
13
|
+
import logging
|
14
|
+
import logging.config
|
13
15
|
|
14
16
|
from deepanything.DeepAnythingClient import chat_completion_stream_async, chat_completion_async
|
15
17
|
from deepanything.ResponseClient import AsyncOpenaiResponseClient,AsyncResponseClient
|
16
18
|
from deepanything.Stream import AsyncStream
|
17
19
|
from deepanything.ReasonClient import AsyncDeepseekReasonClient,AsyncOpenaiReasonClient,AsyncReasonClient
|
18
20
|
from deepanything.Server import Types
|
21
|
+
from deepanything.metadatas import VERSION,PYTHON_RUNTIME
|
22
|
+
|
19
23
|
|
20
24
|
@dataclass
|
21
25
|
class ModelInfo:
|
@@ -25,12 +29,14 @@ class ModelInfo:
|
|
25
29
|
response_client : str
|
26
30
|
response_model : str
|
27
31
|
created : int = int(time.time())
|
28
|
-
reason_prompt : str = "<Think>{}</Think>"
|
32
|
+
reason_prompt : str = "<Think>{}</Think>",
|
33
|
+
reason_system_prompt : Optional[str] = None
|
29
34
|
|
30
35
|
class DeepAnythingServer:
|
31
36
|
app : FastAPI = FastAPI()
|
32
37
|
host : str
|
33
38
|
port : int
|
39
|
+
logger : logging.Logger
|
34
40
|
reason_clients : Dict[str,AsyncReasonClient] = {}
|
35
41
|
response_clients : Dict[str,AsyncResponseClient] = {}
|
36
42
|
models : Dict[str,ModelInfo] = {}
|
@@ -40,6 +46,7 @@ class DeepAnythingServer:
|
|
40
46
|
log_config : Dict[str,Any] = LOGGING_CONFIG
|
41
47
|
|
42
48
|
def __init__(self, host:str = None, port:int = None, config : Any or str = None):
|
49
|
+
print(f"DeepAnything Server {VERSION} on {PYTHON_RUNTIME}")
|
43
50
|
if config is not None:
|
44
51
|
if isinstance(config,str):
|
45
52
|
with open(config) as f:
|
@@ -54,7 +61,9 @@ class DeepAnythingServer:
|
|
54
61
|
self.app.add_api_route("/v1/models",self.get_models,methods=["GET"],response_model=Types.ModelsListResponse)
|
55
62
|
self.app.add_api_route("/v1/chat/completions",self.chat_completions,methods=["POST"])
|
56
63
|
|
64
|
+
|
57
65
|
def run(self):
|
66
|
+
self.logger.info(f"DeepAnything server is now running at http://{self.host}:{self.port}")
|
58
67
|
uvicorn.run(self.app,host=self.host,port=self.port,log_config=self.log_config)
|
59
68
|
|
60
69
|
@staticmethod
|
@@ -65,21 +74,26 @@ class DeepAnythingServer:
|
|
65
74
|
return args
|
66
75
|
|
67
76
|
def load_config(self,config_object : Dict) -> None:
|
77
|
+
print("Loading config")
|
68
78
|
self.host = config_object.get("host","0.0.0.0")
|
69
79
|
self.port = config_object.get("port",8000)
|
70
80
|
self.model_owner = config_object.get("model_owner","deepanything")
|
71
81
|
|
82
|
+
self.log_config = config_object.get("log",LOGGING_CONFIG)
|
83
|
+
if self.log_config == {}:
|
84
|
+
self.log_config = LOGGING_CONFIG
|
85
|
+
logging.config.dictConfig(self.log_config)
|
86
|
+
self.logger = logging.getLogger("deepanything")
|
87
|
+
|
72
88
|
self._load_reason_clients(config_object)
|
73
89
|
self._load_response_clients(config_object)
|
74
90
|
self._load_models(config_object)
|
75
91
|
|
76
92
|
self.api_keys = config_object.get("api_keys",[])
|
77
|
-
self.log_config = config_object.get("log",LOGGING_CONFIG)
|
78
|
-
if self.log_config == {}:
|
79
|
-
self.log_config = LOGGING_CONFIG
|
80
93
|
|
81
94
|
|
82
95
|
def _load_models(self, config_object):
|
96
|
+
self.logger.info("Loading models")
|
83
97
|
models: List[Dict] = config_object.get("models", [])
|
84
98
|
for _model in models:
|
85
99
|
name = _model["name"]
|
@@ -89,12 +103,18 @@ class DeepAnythingServer:
|
|
89
103
|
response_model = _model["response_model"]
|
90
104
|
created = _model.get("created", int(time.time()))
|
91
105
|
reason_prompt = _model.get("reason_prompt", "<Think>{}</Think>")
|
106
|
+
reason_system_prompt = _model.get("reason_system_prompt", None)
|
107
|
+
|
108
|
+
if name in self.models:
|
109
|
+
self.logger.error(f"Detected duplicate model : {name}")
|
92
110
|
|
93
111
|
if reason_client not in self.reason_clients:
|
94
|
-
|
112
|
+
self.logger.error(f"Reason client '{reason_model}' for '{name}' not found")
|
113
|
+
exit(0)
|
95
114
|
|
96
115
|
if response_client not in self.response_clients:
|
97
|
-
|
116
|
+
self.logger.error(f"Response client '{response_model}' for '{name}' not found")
|
117
|
+
exit(0)
|
98
118
|
|
99
119
|
self.models[name] = ModelInfo(
|
100
120
|
name=name,
|
@@ -103,10 +123,14 @@ class DeepAnythingServer:
|
|
103
123
|
response_client=response_client,
|
104
124
|
response_model=response_model,
|
105
125
|
created=created,
|
106
|
-
reason_prompt=reason_prompt
|
126
|
+
reason_prompt=reason_prompt,
|
127
|
+
reason_system_prompt=reason_system_prompt
|
107
128
|
)
|
108
129
|
|
130
|
+
self.logger.info(f"Loaded model : {name}")
|
131
|
+
|
109
132
|
def _load_response_clients(self, config_object):
|
133
|
+
self.logger.info("Loading response clients")
|
110
134
|
response_clients: List[Dict] = config_object.get("response_clients", [])
|
111
135
|
for client in response_clients:
|
112
136
|
name = client["name"]
|
@@ -114,12 +138,20 @@ class DeepAnythingServer:
|
|
114
138
|
api_key = client.get("api_key", "")
|
115
139
|
extract_args = client.get("extract_args", {})
|
116
140
|
|
141
|
+
if name in self.response_clients:
|
142
|
+
self.logger.error(f"Detected duplicate response clients : {name}")
|
143
|
+
exit(0)
|
144
|
+
|
117
145
|
if client["type"] == 'openai':
|
118
146
|
self.response_clients[name] = AsyncOpenaiResponseClient(base_url, api_key, **extract_args)
|
119
147
|
else:
|
120
|
-
|
148
|
+
self.logger.error(f"Unsupported response client type '{client['type']}'")
|
149
|
+
exit(0)
|
150
|
+
|
151
|
+
self.logger.info(f"Loaded response client : {name}")
|
121
152
|
|
122
153
|
def _load_reason_clients(self, config_object):
|
154
|
+
self.logger.info("Loading reason clients")
|
123
155
|
reason_clients: List[Dict] = config_object.get("reason_clients", [])
|
124
156
|
for client in reason_clients:
|
125
157
|
name = client["name"]
|
@@ -127,12 +159,20 @@ class DeepAnythingServer:
|
|
127
159
|
api_key = client.get("api_key", "")
|
128
160
|
extract_args = client.get("extract_args", {})
|
129
161
|
|
162
|
+
|
163
|
+
if name in self.response_clients:
|
164
|
+
self.logger.error(f"Detected duplicate response clients : {name}")
|
165
|
+
exit(0)
|
166
|
+
|
130
167
|
if client["type"] == 'deepseek':
|
131
168
|
self.reason_clients[name] = AsyncDeepseekReasonClient(base_url, api_key, **extract_args)
|
132
169
|
elif client["type"] == 'openai':
|
133
170
|
self.reason_clients[name] = AsyncOpenaiReasonClient(base_url, api_key, **extract_args)
|
134
171
|
else:
|
135
|
-
|
172
|
+
self.logger.error(f"Unsupported reason client type '{client['type']}'")
|
173
|
+
exit(0)
|
174
|
+
|
175
|
+
self.logger.info(f"Loaded reason client : {name}")
|
136
176
|
|
137
177
|
def add_reason_client(self,name:str,client:AsyncReasonClient):
|
138
178
|
self.reason_clients[name] = client
|
@@ -143,31 +183,31 @@ class DeepAnythingServer:
|
|
143
183
|
def add_model(self,name:str,model:ModelInfo):
|
144
184
|
self.models[name] = model
|
145
185
|
|
186
|
+
@staticmethod
|
187
|
+
def _extract_token(authorization:str):
|
188
|
+
if (authorization is None) or (not authorization.startswith("Bearer ")):
|
189
|
+
return None
|
190
|
+
return authorization[7:]
|
146
191
|
def _verify_authorization(self, authorization:Optional[str]):
|
192
|
+
token = DeepAnythingServer._extract_token(authorization)
|
193
|
+
|
147
194
|
if not self.api_keys:
|
148
|
-
return
|
195
|
+
return DeepAnythingServer._extract_token(authorization)
|
149
196
|
|
150
|
-
if authorization is None:
|
197
|
+
if authorization is None or token is None:
|
151
198
|
raise HTTPException(
|
152
199
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
153
200
|
detail="Expect token",
|
154
201
|
headers={"WWW-Authenticate": "Bearer"},
|
155
202
|
)
|
156
203
|
|
157
|
-
if not authorization.startswith("Bearer "):
|
158
|
-
raise HTTPException(
|
159
|
-
status_code=status.HTTP_401_UNAUTHORIZED,
|
160
|
-
detail="Invalid or expired token",
|
161
|
-
headers={"WWW-Authenticate": "Bearer"},
|
162
|
-
)
|
163
|
-
|
164
|
-
token =authorization[7:]
|
165
204
|
if token not in self.api_keys:
|
166
205
|
raise HTTPException(
|
167
206
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
168
207
|
detail="Invalid or expired token",
|
169
208
|
headers={"WWW-Authenticate": "Bearer"},
|
170
209
|
)
|
210
|
+
return token
|
171
211
|
|
172
212
|
async def chat_completions(
|
173
213
|
self,
|
@@ -175,7 +215,9 @@ class DeepAnythingServer:
|
|
175
215
|
query: Types.ChatCompletionQuery,
|
176
216
|
authorization: Optional[str] = Header(None)
|
177
217
|
):
|
178
|
-
self._verify_authorization(authorization)
|
218
|
+
token = self._verify_authorization(authorization)
|
219
|
+
|
220
|
+
self.logger.info(f"ChatCompletions : {token} -> {query.model}")
|
179
221
|
|
180
222
|
if query.model not in self.models:
|
181
223
|
raise HTTPException(
|
@@ -212,7 +254,8 @@ class DeepAnythingServer:
|
|
212
254
|
reason_prompt=model.reason_prompt,
|
213
255
|
response_args=args,
|
214
256
|
reason_args=args,
|
215
|
-
max_tokens=max_tokens
|
257
|
+
max_tokens=max_tokens,
|
258
|
+
|
216
259
|
),
|
217
260
|
request
|
218
261
|
)
|
@@ -6,6 +6,7 @@ import uuid
|
|
6
6
|
from openai.types.chat import chat_completion_chunk,chat_completion,chat_completion_message
|
7
7
|
from openai.types import completion_usage,completion_choice
|
8
8
|
|
9
|
+
|
9
10
|
def make_usage(
|
10
11
|
completion_tokens,
|
11
12
|
prompt_tokens,
|
@@ -207,4 +208,16 @@ def merge_usage(
|
|
207
208
|
)
|
208
209
|
|
209
210
|
def make_id_by_timestamp():
|
210
|
-
return "chatcmpl-" + str(uuid.uuid4())
|
211
|
+
return "chatcmpl-" + str(uuid.uuid4())
|
212
|
+
|
213
|
+
def attend_message(
|
214
|
+
messages : List,
|
215
|
+
role : Literal["developer", "system", "user", "assistant", "tool"],
|
216
|
+
content : Optional[str] = None,
|
217
|
+
reason_content : Optional[str] = None,
|
218
|
+
) -> List:
|
219
|
+
return messages + [make_chat_completion_message(
|
220
|
+
role=role,
|
221
|
+
content=content,
|
222
|
+
reasoning_content=reason_content
|
223
|
+
)]
|
@@ -12,7 +12,7 @@ def main():
|
|
12
12
|
args = parser.parse_args()
|
13
13
|
|
14
14
|
if args.config is not None:
|
15
|
-
with open(args.config) as f:
|
15
|
+
with open(args.config, encoding='utf-8') as f:
|
16
16
|
config = json.load(f)
|
17
17
|
server = DeepAnythingServer(host=args.host, port=args.port, config=config)
|
18
18
|
server.run()
|
@@ -1 +0,0 @@
|
|
1
|
-
VERSION = "v0.1.6"
|
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
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|