maitai-python 1.2.30__tar.gz → 1.2.31b1__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.
Files changed (58) hide show
  1. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/PKG-INFO +5 -3
  2. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/README.md +1 -1
  3. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai/_config.py +11 -3
  4. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai/_config_listener.py +7 -2
  5. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai/_maitai.py +12 -1
  6. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai/_maitai_async.py +10 -0
  7. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai/_utils.py +21 -0
  8. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai/agent/_completions.py +1 -0
  9. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai/agent/_completions_async.py +1 -0
  10. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai/chat/_completions.py +157 -63
  11. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai/chat/_completions_async.py +160 -66
  12. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai/models/agent/base.py +43 -0
  13. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai/models/agent/enum.py +0 -7
  14. maitai_python-1.2.31b1/maitai/models/agent/extended.py +702 -0
  15. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai/models/chat.py +67 -5
  16. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai/models/config.py +15 -2
  17. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai/models/inference.py +2 -0
  18. maitai_python-1.2.31b1/maitai/models/workflow_chunk.py +24 -0
  19. maitai_python-1.2.31b1/maitai/workflow/__init__.py +4 -0
  20. maitai_python-1.2.31b1/maitai/workflow/_completions.py +165 -0
  21. maitai_python-1.2.31b1/maitai/workflow/_completions_async.py +203 -0
  22. maitai_python-1.2.31b1/maitai/workflow/_local.py +376 -0
  23. maitai_python-1.2.31b1/maitai/workflow/_stream_http.py +164 -0
  24. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai_common/config/config_service.py +2 -3
  25. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai_common/version.py +1 -1
  26. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai_python.egg-info/PKG-INFO +5 -3
  27. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai_python.egg-info/SOURCES.txt +6 -0
  28. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai_python.egg-info/requires.txt +2 -0
  29. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/setup.py +3 -1
  30. maitai_python-1.2.30/maitai/models/agent/extended.py +0 -1505
  31. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai/__init__.py +0 -0
  32. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai/_context.py +0 -0
  33. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai/_evaluator.py +0 -0
  34. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai/_inference.py +0 -0
  35. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai/_maitai_client.py +0 -0
  36. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai/_pydantic.py +0 -0
  37. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai/_types.py +0 -0
  38. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai/agent/__init__.py +0 -0
  39. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai/chat/__init__.py +0 -0
  40. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai/exceptions.py +0 -0
  41. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai/models/__init__.py +0 -0
  42. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai/models/agent/__init__.py +0 -0
  43. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai/models/agent_action.py +0 -0
  44. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai/models/agent_routing.py +0 -0
  45. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai/models/application.py +0 -0
  46. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai/models/event.py +0 -0
  47. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai/models/key.py +0 -0
  48. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai/models/metric.py +0 -0
  49. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai/models/sentinel.py +0 -0
  50. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai/tools/__init__.py +0 -0
  51. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai/transcriptions/__init__.py +0 -0
  52. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai/transcriptions/_transcriptions.py +0 -0
  53. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai/transcriptions/_transcriptions_async.py +0 -0
  54. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai_common/__init__.py +0 -0
  55. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai_common/config/__init__.py +0 -0
  56. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai_python.egg-info/dependency_links.txt +0 -0
  57. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/maitai_python.egg-info/top_level.txt +0 -0
  58. {maitai_python-1.2.30 → maitai_python-1.2.31b1}/setup.cfg +0 -0
@@ -1,10 +1,10 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: maitai-python
3
- Version: 1.2.30
3
+ Version: 1.2.31b1
4
4
  Summary: Maitai SDK for Python
5
5
  Home-page: https://docs.trymaitai.ai
6
6
  Author: Maitai
7
- Author-email: support@trymaitai.ai
7
+ Author-email: support@trymaitai.com
8
8
  License: MIT
9
9
  Classifier: Programming Language :: Python :: 3
10
10
  Classifier: License :: OSI Approved :: MIT License
@@ -15,6 +15,8 @@ Requires-Dist: websocket-client
15
15
  Requires-Dist: pyhumps
16
16
  Requires-Dist: pydantic>=2.0.0
17
17
  Requires-Dist: groq
18
+ Requires-Dist: boto3
19
+ Requires-Dist: redis
18
20
  Dynamic: author
19
21
  Dynamic: author-email
20
22
  Dynamic: classifier
@@ -64,4 +66,4 @@ Maitai requires `openai` version `1.30.1` or later. If you are using an older ve
64
66
  ## Run Your Application
65
67
 
66
68
  Run your application, make sure it makes at least one Chat Completion Request, then head over
67
- to https://portal.trymaitai.ai and watch Maitai learn your application in real time.
69
+ to https://portal.trymaitai.com and watch Maitai learn your application in real time.
@@ -37,4 +37,4 @@ Maitai requires `openai` version `1.30.1` or later. If you are using an older ve
37
37
  ## Run Your Application
38
38
 
39
39
  Run your application, make sure it makes at least one Chat Completion Request, then head over
40
- to https://portal.trymaitai.ai and watch Maitai learn your application in real time.
40
+ to https://portal.trymaitai.com and watch Maitai learn your application in real time.
@@ -17,7 +17,7 @@ class Config(MaitaiClient):
17
17
  config_dir = os.path.expanduser(os.environ.get("MAITAI_CONFIG_DIR", "~/.maitai"))
18
18
  refresh_timer = None
19
19
 
20
- def __init__(self, api_key=None):
20
+ def __init__(self, api_key=None, server_side_only=False):
21
21
  super().__init__()
22
22
  self._api_key = None
23
23
  self._company_id = None
@@ -37,7 +37,11 @@ class Config(MaitaiClient):
37
37
  )
38
38
  self.refresh_interval = 30 * 60 # 30 minutes in seconds
39
39
  if api_key:
40
- self.initialize(api_key)
40
+ if server_side_only:
41
+ self._api_key = api_key
42
+ self.initialized = True
43
+ else:
44
+ self.initialize(api_key)
41
45
 
42
46
  def __del__(self):
43
47
  self.cleanup()
@@ -184,7 +188,11 @@ class Config(MaitaiClient):
184
188
 
185
189
  def _initialize_websocket(self):
186
190
  self.config_listener = ConfigListener(
187
- self, self.maitai_ws, "APPLICATION_CONFIG_CHANGE", self._company_id
191
+ self,
192
+ self.maitai_ws,
193
+ "APPLICATION_CONFIG_CHANGE",
194
+ self._company_id,
195
+ self.api_key,
188
196
  )
189
197
  self.config_listener.start()
190
198
 
@@ -13,11 +13,13 @@ logger = logging.getLogger("maitai")
13
13
 
14
14
 
15
15
  class ConfigListener:
16
- def __init__(self, config, path, type, key=None):
16
+ def __init__(self, config, path, type, key=None, api_key=None):
17
17
  self.config = config
18
18
  self.ws_url = f"{path}?type={type}"
19
19
  if key:
20
20
  self.ws_url += f"&key={key}"
21
+ if api_key:
22
+ self.ws_url += f"&api_key={api_key}"
21
23
 
22
24
  self.ws = None
23
25
  self.ws_thread = None
@@ -48,7 +50,10 @@ class ConfigListener:
48
50
 
49
51
  def on_error(self, ws, error):
50
52
  if not isinstance(error, (AttributeError, ConnectionError)):
51
- logger.error("Maitai config listener: websocket error", exc_info=error)
53
+ if error is not None and isinstance(error, Exception):
54
+ logger.error("Maitai config listener: websocket error", exc_info=error)
55
+ else:
56
+ logger.error(f"Maitai config listener: websocket error: {error}")
52
57
 
53
58
  def on_close(self, ws, close_status_code, close_msg):
54
59
  logger.info(
@@ -13,6 +13,7 @@ from maitai.chat._completions import Completions
13
13
  from maitai.models.chat import AzureParams, ClientParams
14
14
  from maitai.models.key import Key
15
15
  from maitai.transcriptions._transcriptions import Transcriptions
16
+ from maitai.workflow._completions import Completions as WorkflowCompletions
16
17
 
17
18
  logger = logging.getLogger("maitai")
18
19
 
@@ -45,6 +46,7 @@ class Maitai:
45
46
  default_query: Optional[Mapping[str, object]] = None,
46
47
  http_client: Optional[httpx.Client] = None,
47
48
  _strict_response_validation: bool = False,
49
+ server_side_only: bool = False,
48
50
  ):
49
51
  if not maitai_api_key:
50
52
  maitai_api_key = os.environ.get("MAITAI_API_KEY")
@@ -56,7 +58,7 @@ class Maitai:
56
58
  raise ValueError(
57
59
  "api_key is deprecated. Use provider specific keys instead. Supported variables: openai_api_key, groq_api_key, anthropic_api_key, cerebras_api_key, azure_api_key, sambanova_api_key, gemini_api_key, deepseek_api_key"
58
60
  )
59
- self.config = Config(maitai_api_key)
61
+ self.config = Config(maitai_api_key, server_side_only=server_side_only)
60
62
  if openai_api_key:
61
63
  if not self.config.auth_keys.openai_api_key:
62
64
  self.config.auth_keys.openai_api_key = Key(
@@ -175,6 +177,7 @@ class Maitai:
175
177
  )
176
178
  self.audio = Audio(self.config)
177
179
  self.agent = Agent(self.config)
180
+ self.workflow = Workflow(self.config)
178
181
  self.context = ContextManager(self.config)
179
182
 
180
183
 
@@ -205,3 +208,11 @@ class Agent:
205
208
  config: Config,
206
209
  ):
207
210
  self.completions = AgentCompletions(config)
211
+
212
+
213
+ class Workflow:
214
+ def __init__(
215
+ self,
216
+ config: Config,
217
+ ):
218
+ self.completions = WorkflowCompletions(config)
@@ -12,6 +12,7 @@ from maitai.chat._completions_async import AsyncCompletions
12
12
  from maitai.models.chat import AzureParams, ClientParams
13
13
  from maitai.models.key import Key
14
14
  from maitai.transcriptions._transcriptions_async import AsyncTranscriptions
15
+ from maitai.workflow._completions_async import AsyncCompletions as AsyncWorkflowCompletions
15
16
 
16
17
  logger = logging.getLogger("maitai")
17
18
 
@@ -174,6 +175,7 @@ class MaitaiAsync:
174
175
  )
175
176
  self.audio = AsyncAudio(self.config)
176
177
  self.agent = AsyncAgent(self.config)
178
+ self.workflow = AsyncWorkflow(self.config)
177
179
 
178
180
 
179
181
  class AsyncChat:
@@ -203,3 +205,11 @@ class AsyncAgent:
203
205
  config: Config,
204
206
  ):
205
207
  self.completions = AsyncAgentCompletions(config)
208
+
209
+
210
+ class AsyncWorkflow:
211
+ def __init__(
212
+ self,
213
+ config: Config,
214
+ ):
215
+ self.completions = AsyncWorkflowCompletions(config)
@@ -176,12 +176,19 @@ def get_chat_completion_params(
176
176
  ] = openai.NOT_GIVEN,
177
177
  top_logprobs: Union[Optional[int], openai.NotGiven] = openai.NOT_GIVEN,
178
178
  top_p: Union[Optional[float], openai.NotGiven] = openai.NOT_GIVEN,
179
+ top_k: Union[Optional[int], openai.NotGiven] = openai.NOT_GIVEN,
180
+ min_p: Union[Optional[float], openai.NotGiven] = openai.NOT_GIVEN,
181
+ repetition_penalty: Union[Optional[float], openai.NotGiven] = openai.NOT_GIVEN,
179
182
  user: Union[str, openai.NotGiven] = openai.NOT_GIVEN,
180
183
  parallel_tool_calls: Union[bool, openai.NotGiven] = openai.NOT_GIVEN,
181
184
  reasoning_effort: Union[str, openai.NotGiven, None] = openai.NOT_GIVEN,
185
+ reasoning: Union[dict, chat_types.ReasoningConfig, openai.NotGiven, None] = (
186
+ openai.NOT_GIVEN
187
+ ),
182
188
  extra_headers: Union[
183
189
  None, Headers, Dict[str, str], openai.NotGiven
184
190
  ] = openai.NOT_GIVEN,
191
+ extra_body: Union[dict, None] = None,
185
192
  ) -> Dict[str, Any]:
186
193
  params = {
187
194
  "messages": [msg for msg in messages],
@@ -197,6 +204,9 @@ def get_chat_completion_params(
197
204
  "max_tokens": max_tokens,
198
205
  "temperature": temperature,
199
206
  "top_p": top_p,
207
+ "top_k": top_k,
208
+ "min_p": min_p,
209
+ "repetition_penalty": repetition_penalty,
200
210
  "stop": stop,
201
211
  "logprobs": logprobs,
202
212
  "top_logprobs": top_logprobs,
@@ -217,6 +227,15 @@ def get_chat_completion_params(
217
227
  if value != openai.NOT_GIVEN:
218
228
  params[param_name] = value
219
229
 
230
+ if extra_body:
231
+ params["extra_body"] = extra_body
232
+
233
+ if reasoning != openai.NOT_GIVEN and reasoning is not None:
234
+ if hasattr(reasoning, "model_dump"):
235
+ params["reasoning"] = reasoning.model_dump(exclude_none=True)
236
+ else:
237
+ params["reasoning"] = reasoning
238
+
220
239
  if stream_options != openai.NOT_GIVEN and stream_options:
221
240
  params["stream_options"] = {
222
241
  "include_usage": stream_options.get("include_usage")
@@ -310,6 +329,8 @@ def chat_completion_chunk_to_response(
310
329
  def convert_params_to_groq(params: Dict[str, Any]) -> Dict[str, Any]:
311
330
  if "reasoning_effort" in params:
312
331
  del params["reasoning_effort"]
332
+ if "reasoning" in params:
333
+ del params["reasoning"]
313
334
  if "stream_options" in params:
314
335
  del params["stream_options"]
315
336
  if (
@@ -71,6 +71,7 @@ class Completions(MaitaiClient):
71
71
  "max_iterations": request.max_iterations,
72
72
  "high_performance": request.high_performance,
73
73
  "disable_chaining": request.disable_chaining,
74
+ "disable_strategy": request.disable_strategy,
74
75
  }
75
76
 
76
77
  # Remove None values from extra_body
@@ -71,6 +71,7 @@ class AsyncCompletions(MaitaiClient):
71
71
  "max_iterations": request.max_iterations,
72
72
  "high_performance": request.high_performance,
73
73
  "disable_chaining": request.disable_chaining,
74
+ "disable_strategy": request.disable_strategy,
74
75
  }
75
76
 
76
77
  # Remove None values from extra_body
@@ -37,7 +37,13 @@ from maitai.exceptions import (
37
37
  InferenceWarning,
38
38
  MaitaiConnectionError,
39
39
  )
40
- from maitai.models.chat import ChatCompletionParams, ClientParams, EvaluationContentType
40
+ from maitai.models.chat import (
41
+ ChatCompletionChunk,
42
+ ChatCompletionParams,
43
+ ClientParams,
44
+ EvaluationContentType,
45
+ ReasoningConfig,
46
+ )
41
47
  from maitai.models.config import InferenceLocations
42
48
  from maitai.models.inference import InferenceStreamResponse
43
49
  from maitai.models.metric import RequestTimingMetric
@@ -92,6 +98,7 @@ class Completions:
92
98
  fallback_config: Union[FallbackConfig, dict, None] = None,
93
99
  return_request: bool = False,
94
100
  push_timing_metrics: bool = True,
101
+ from_workflow: bool = False,
95
102
  # OpenAI Arguments
96
103
  messages: Iterable[openai_chat_types.ChatCompletionMessageParam],
97
104
  model: Union[str, openai_types.ChatModel, openai.NotGiven] = openai.NOT_GIVEN,
@@ -110,6 +117,9 @@ class Completions:
110
117
  n: Union[Optional[int], openai.NotGiven] = openai.NOT_GIVEN,
111
118
  presence_penalty: Union[Optional[float], openai.NotGiven] = openai.NOT_GIVEN,
112
119
  reasoning_effort: Union[str, openai.NotGiven, None] = openai.NOT_GIVEN,
120
+ reasoning: Union[
121
+ ReasoningConfig, dict, None, openai.NotGiven
122
+ ] = openai.NOT_GIVEN,
113
123
  response_format: Union[
114
124
  openai_chat_types.completion_create_params.ResponseFormat, openai.NotGiven
115
125
  ] = openai.NOT_GIVEN,
@@ -133,6 +143,9 @@ class Completions:
133
143
  ] = openai.NOT_GIVEN,
134
144
  top_logprobs: Union[Optional[int], openai.NotGiven] = openai.NOT_GIVEN,
135
145
  top_p: Union[Optional[float], openai.NotGiven] = openai.NOT_GIVEN,
146
+ top_k: Union[Optional[int], openai.NotGiven] = openai.NOT_GIVEN,
147
+ min_p: Union[Optional[float], openai.NotGiven] = openai.NOT_GIVEN,
148
+ repetition_penalty: Union[Optional[float], openai.NotGiven] = openai.NOT_GIVEN,
136
149
  user: Union[str, openai.NotGiven] = openai.NOT_GIVEN,
137
150
  extra_headers: Union[
138
151
  None, Headers, Dict[str, str], openai.NotGiven
@@ -166,30 +179,48 @@ class Completions:
166
179
  evaluation_enabled = maitai_config.evaluation_enabled
167
180
  if apply_corrections is None:
168
181
  apply_corrections = maitai_config.apply_corrections
182
+ defer_to_server = from_workflow and server_side_inference
169
183
  if model == openai.NOT_GIVEN:
170
- model = maitai_config.model
171
- if temperature == openai.NOT_GIVEN:
172
- temperature = maitai_config.temperature
184
+ if defer_to_server:
185
+ model = None
186
+ else:
187
+ model = maitai_config.model
173
188
  if safe_mode is None:
174
189
  safe_mode = maitai_config.safe_mode
175
190
  if stream == openai.NOT_GIVEN:
176
191
  stream = False
177
192
  if response_format == openai.NOT_GIVEN:
178
193
  response_format = {"type": "text"}
179
- if stop == openai.NOT_GIVEN and maitai_config.stop is not None:
180
- stop = maitai_config.stop
181
- if logprobs == openai.NOT_GIVEN:
182
- logprobs = maitai_config.logprobs
183
- if max_tokens == openai.NOT_GIVEN and maitai_config.max_tokens is not None:
184
- max_tokens = maitai_config.max_tokens
185
194
  if max_completion_tokens and max_completion_tokens != openai.NOT_GIVEN:
186
195
  max_tokens = max_completion_tokens
187
- if n == openai.NOT_GIVEN and maitai_config.n:
188
- n = maitai_config.n
189
- if frequency_penalty == openai.NOT_GIVEN:
190
- frequency_penalty = maitai_config.frequency_penalty
191
- if presence_penalty == openai.NOT_GIVEN:
192
- presence_penalty = maitai_config.presence_penalty
196
+ if not defer_to_server:
197
+ if temperature == openai.NOT_GIVEN:
198
+ temperature = maitai_config.temperature
199
+ if stop == openai.NOT_GIVEN and maitai_config.stop is not None:
200
+ stop = maitai_config.stop
201
+ if logprobs == openai.NOT_GIVEN:
202
+ logprobs = maitai_config.logprobs
203
+ if logprobs is True and top_logprobs == openai.NOT_GIVEN:
204
+ top_logprobs = 2
205
+ if max_tokens == openai.NOT_GIVEN and maitai_config.max_tokens is not None:
206
+ max_tokens = maitai_config.max_tokens
207
+ if n == openai.NOT_GIVEN and maitai_config.n:
208
+ n = maitai_config.n
209
+ if frequency_penalty == openai.NOT_GIVEN:
210
+ frequency_penalty = maitai_config.frequency_penalty
211
+ if presence_penalty == openai.NOT_GIVEN:
212
+ presence_penalty = maitai_config.presence_penalty
213
+ if top_p == openai.NOT_GIVEN and maitai_config.top_p is not None:
214
+ top_p = maitai_config.top_p
215
+ if top_k == openai.NOT_GIVEN and maitai_config.top_k is not None:
216
+ top_k = maitai_config.top_k
217
+ if min_p == openai.NOT_GIVEN and maitai_config.min_p is not None:
218
+ min_p = maitai_config.min_p
219
+ if (
220
+ repetition_penalty == openai.NOT_GIVEN
221
+ and maitai_config.repetition_penalty is not None
222
+ ):
223
+ repetition_penalty = maitai_config.repetition_penalty
193
224
  if timeout == openai.NOT_GIVEN and maitai_config.timeout > 0:
194
225
  timeout = maitai_config.timeout
195
226
  if fallback_config is None and maitai_config.fallback_config:
@@ -198,6 +229,13 @@ class Completions:
198
229
  "strategy": maitai_config.fallback_config.strategy,
199
230
  "timeout": maitai_config.fallback_config.timeout,
200
231
  }
232
+ if reasoning_effort is not openai.NOT_GIVEN:
233
+ warnings.warn(
234
+ "reasoning_effort is deprecated; use reasoning={'effort': <level>} instead. "
235
+ "See https://docs.trymaitai.com/sdk/reasoning",
236
+ DeprecationWarning,
237
+ stacklevel=2,
238
+ )
201
239
  completion_params = get_chat_completion_params(
202
240
  messages=messages,
203
241
  model=model,
@@ -217,10 +255,15 @@ class Completions:
217
255
  tools=tools,
218
256
  top_logprobs=top_logprobs,
219
257
  top_p=top_p,
258
+ top_k=top_k,
259
+ min_p=min_p,
260
+ repetition_penalty=repetition_penalty,
220
261
  user=user,
221
262
  parallel_tool_calls=parallel_tool_calls,
222
263
  extra_headers=extra_headers,
264
+ extra_body=extra_body,
223
265
  reasoning_effort=reasoning_effort,
266
+ reasoning=reasoning,
224
267
  )
225
268
  if server_side_inference:
226
269
  response_timeout = None
@@ -243,6 +286,9 @@ class Completions:
243
286
  "fallback_config": fallback_config,
244
287
  }
245
288
 
289
+ if from_workflow:
290
+ request_dict["from_workflow"] = True
291
+
246
292
  if self._client_params is not None:
247
293
  request_dict["client_params"] = self._client_params.model_dump()
248
294
 
@@ -289,9 +335,13 @@ class Completions:
289
335
  fallback_model = (
290
336
  fallback_config.get("model") if fallback_config else None
291
337
  )
292
- if "gpt" not in model and fallback_model and "gpt" in fallback_model:
338
+ if (
339
+ (model is None or "gpt" not in model)
340
+ and fallback_model
341
+ and "gpt" in fallback_model
342
+ ):
293
343
  model = fallback_model
294
- if "gpt" not in model:
344
+ if model is None or "gpt" not in model:
295
345
  raise e
296
346
  if safe_mode:
297
347
  raise e
@@ -305,34 +355,58 @@ class Completions:
305
355
  if "openai:" in model:
306
356
  model = model.replace("openai:", "")
307
357
  local_params = {
308
- "messages": messages,
309
- "model": model,
310
- "frequency_penalty": frequency_penalty,
311
- "function_call": function_call,
312
- "functions": functions,
313
- "logit_bias": logit_bias,
314
- "logprobs": logprobs,
315
- "max_tokens": max_tokens,
316
- "n": n,
317
- "presence_penalty": presence_penalty,
318
- "reasoning_effort": reasoning_effort,
319
- "response_format": response_format,
320
- "seed": seed,
321
- "stop": stop,
322
- "stream": stream,
323
- "stream_options": stream_options,
324
- "temperature": temperature,
325
- "tool_choice": tool_choice,
326
- "tools": tools,
327
- "top_logprobs": top_logprobs,
328
- "top_p": top_p,
329
- "user": user,
330
- "extra_headers": extra_headers,
331
- "extra_query": extra_query,
332
- "extra_body": extra_body,
333
- "timeout": timeout,
334
- "parallel_tool_calls": parallel_tool_calls,
358
+ k: v
359
+ for k, v in {
360
+ "messages": messages,
361
+ "model": model,
362
+ "frequency_penalty": frequency_penalty,
363
+ "function_call": function_call,
364
+ "functions": functions,
365
+ "logit_bias": logit_bias,
366
+ "logprobs": logprobs,
367
+ "max_tokens": max_tokens,
368
+ "n": n,
369
+ "presence_penalty": presence_penalty,
370
+ "reasoning_effort": reasoning_effort,
371
+ "response_format": response_format,
372
+ "seed": seed,
373
+ "stop": stop,
374
+ "stream": stream,
375
+ "stream_options": stream_options,
376
+ "temperature": temperature,
377
+ "tool_choice": tool_choice,
378
+ "tools": tools,
379
+ "top_logprobs": top_logprobs,
380
+ "top_p": top_p,
381
+ "user": user,
382
+ "extra_headers": extra_headers,
383
+ "extra_query": extra_query,
384
+ "extra_body": extra_body,
385
+ "timeout": timeout,
386
+ "parallel_tool_calls": parallel_tool_calls,
387
+ }.items()
388
+ if v is not openai.NOT_GIVEN
335
389
  }
390
+ if (
391
+ reasoning != openai.NOT_GIVEN
392
+ and reasoning is not None
393
+ and not groq_model
394
+ ):
395
+ rdict = (
396
+ reasoning.model_dump(exclude_none=True)
397
+ if isinstance(reasoning, ReasoningConfig)
398
+ else dict(reasoning)
399
+ )
400
+ rbody = {}
401
+ if rdict.get("effort") is not None and rdict.get("effort") != "":
402
+ rbody["effort"] = rdict["effort"]
403
+ if rdict.get("summary"):
404
+ rbody["summary"] = "auto"
405
+ if rbody:
406
+ eb = dict(local_params.get("extra_body") or {})
407
+ eb["reasoning"] = rbody
408
+ local_params["extra_body"] = eb
409
+ local_params.pop("reasoning_effort", None)
336
410
 
337
411
  if groq_model:
338
412
  if self._groq_client is None:
@@ -493,25 +567,45 @@ class Completions:
493
567
  push_timing_metrics: bool = True,
494
568
  ) -> Iterable[MaitaiChunk]:
495
569
  first = True
570
+ timing_metrics_sent = False
496
571
  try:
497
- for infer_resp in stream:
498
- if infer_resp.warning:
499
- warnings.warn(infer_resp.warning, InferenceWarning)
500
- if infer_resp.error:
501
- raise InferenceException(infer_resp.error)
502
- chunk = infer_resp.chat_completion_chunk
503
- if chunk is not None:
504
- if first:
505
- timing.time_first_chunk = time.time()
506
- timing.request_id = chunk.request_id
507
- first = False
508
- yield MaitaiChunk.model_validate(chunk.model_dump())
509
- if chunk.choices and chunk.choices[0].finish_reason:
510
- timing.time_request_end = time.time()
511
- if push_timing_metrics:
512
- self.inference.store_request_timing_data(timing)
513
- if chunk.choices and chunk.choices[0].finish_reason:
514
- return
572
+ try:
573
+ for infer_resp in stream:
574
+ if infer_resp.warning:
575
+ warnings.warn(infer_resp.warning, InferenceWarning)
576
+ if infer_resp.error:
577
+ raise InferenceException(infer_resp.error)
578
+ if infer_resp.chat_completion_response is not None:
579
+ cr = infer_resp.chat_completion_response
580
+ if cr.usage is not None:
581
+ syn = ChatCompletionChunk(
582
+ id=str(cr.id) if cr.id is not None else "",
583
+ model=cr.model or "",
584
+ object="chat.completion.chunk",
585
+ created=(
586
+ int(cr.created) if cr.created is not None else 0
587
+ ),
588
+ choices=[],
589
+ usage=cr.usage,
590
+ request_id=cr.request_id or "",
591
+ )
592
+ yield MaitaiChunk.model_validate(syn.model_dump())
593
+ continue
594
+ chunk = infer_resp.chat_completion_chunk
595
+ if chunk is not None:
596
+ if first:
597
+ timing.time_first_chunk = time.time()
598
+ timing.request_id = chunk.request_id
599
+ first = False
600
+ yield MaitaiChunk.model_validate(chunk.model_dump())
601
+ if chunk.choices and chunk.choices[0].finish_reason:
602
+ timing.time_request_end = time.time()
603
+ if push_timing_metrics and not timing_metrics_sent:
604
+ timing_metrics_sent = True
605
+ self.inference.store_request_timing_data(timing)
606
+ finally:
607
+ if not timing.time_request_end:
608
+ timing.time_request_end = time.time()
515
609
  except MaitaiConnectionError as e:
516
610
  if "gpt" not in retry_args.get("model", "") and "gpt" in (
517
611
  retry_args.get("fallback_model", "") or ""