Skip to content

Commit c4e7070

Browse files
committed
feat: parse in chat completion
1 parent fe0e47b commit c4e7070

1 file changed

Lines changed: 167 additions & 0 deletions

File tree

portkey_ai/api_resources/apis/chat_complete.py

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
)
1616

1717
import httpx
18+
from portkey_ai._vendor.openai.types.chat.parsed_chat_completion import (
19+
ParsedChatCompletion,
20+
)
1821
from portkey_ai.api_resources.client import AsyncPortkey, Portkey
1922
from portkey_ai.api_resources.types.chat_complete_type import (
2023
ChatCompletionChunk,
@@ -297,6 +300,88 @@ def delete(
297300

298301
return data
299302

303+
def parse(
304+
self,
305+
*,
306+
messages: Iterable[Any],
307+
model: Optional[str] = "portkey-default",
308+
audio: Union[Optional[Any], NotGiven] = NOT_GIVEN,
309+
response_format: Union[Any, NotGiven] = NOT_GIVEN,
310+
frequency_penalty: Union[Optional[float], NotGiven] = NOT_GIVEN,
311+
function_call: Union[Any, NotGiven] = NOT_GIVEN,
312+
functions: Union[Iterable[Any], NotGiven] = NOT_GIVEN,
313+
logit_bias: Union[Optional[Dict[str, int]], NotGiven] = NOT_GIVEN,
314+
logprobs: Union[Optional[bool], NotGiven] = NOT_GIVEN,
315+
max_completion_tokens: Union[Optional[int], NotGiven] = NOT_GIVEN,
316+
max_tokens: Union[Optional[int], NotGiven] = NOT_GIVEN,
317+
metadata: Union[Optional[Metadata], NotGiven] = NOT_GIVEN,
318+
modalities: Union[Optional[List[Any]], NotGiven] = NOT_GIVEN,
319+
n: Union[Optional[int], NotGiven] = NOT_GIVEN,
320+
parallel_tool_calls: Union[bool, NotGiven] = NOT_GIVEN,
321+
prediction: Union[Any, NotGiven] = NOT_GIVEN,
322+
presence_penalty: Union[Optional[float], NotGiven] = NOT_GIVEN,
323+
prompt_cache_key: Union[str, NotGiven] = NOT_GIVEN,
324+
reasoning_effort: Union[Any, NotGiven] = NOT_GIVEN,
325+
safety_identifier: Union[str, NotGiven] = NOT_GIVEN,
326+
seed: Union[Optional[int], NotGiven] = NOT_GIVEN,
327+
service_tier: Union[
328+
Literal["auto", "default", "flex", "scale", "priority"], NotGiven
329+
] = NOT_GIVEN,
330+
stop: Union[Optional[str], List[str], None] | NotGiven = NOT_GIVEN,
331+
store: Union[Optional[bool], NotGiven] = NOT_GIVEN,
332+
stream_options: Union[Any, NotGiven] = NOT_GIVEN,
333+
temperature: Union[Optional[float], NotGiven] = NOT_GIVEN,
334+
tool_choice: Union[Any, NotGiven] = NOT_GIVEN,
335+
tools: Union[Iterable[Any], NotGiven] = NOT_GIVEN,
336+
top_logprobs: Union[Optional[int], NotGiven] = NOT_GIVEN,
337+
top_p: Union[Optional[float], NotGiven] = NOT_GIVEN,
338+
user: Union[str, NotGiven] = NOT_GIVEN,
339+
verbosity: Union[Literal["low", "medium", "high"], NotGiven] = NOT_GIVEN,
340+
web_search_options: Union[Any, NotGiven] = NOT_GIVEN,
341+
extra_headers: Optional[Headers] = None,
342+
extra_query: Optional[Query] = None,
343+
extra_body: Optional[Body] = None,
344+
) -> ParsedChatCompletion:
345+
response = self.openai_client.chat.completions.parse(
346+
messages=messages,
347+
model=model, # type: ignore[arg-type]
348+
audio=audio,
349+
response_format=response_format,
350+
frequency_penalty=frequency_penalty,
351+
function_call=function_call,
352+
functions=functions,
353+
logit_bias=logit_bias,
354+
logprobs=logprobs,
355+
max_completion_tokens=max_completion_tokens,
356+
max_tokens=max_tokens,
357+
metadata=metadata,
358+
modalities=modalities,
359+
n=n,
360+
parallel_tool_calls=parallel_tool_calls,
361+
prediction=prediction,
362+
presence_penalty=presence_penalty,
363+
prompt_cache_key=prompt_cache_key,
364+
reasoning_effort=reasoning_effort,
365+
safety_identifier=safety_identifier,
366+
seed=seed,
367+
service_tier=service_tier,
368+
stop=stop,
369+
store=store,
370+
stream_options=stream_options,
371+
temperature=temperature,
372+
tool_choice=tool_choice,
373+
tools=tools,
374+
top_logprobs=top_logprobs,
375+
top_p=top_p,
376+
user=user,
377+
verbosity=verbosity,
378+
web_search_options=web_search_options,
379+
extra_headers=extra_headers,
380+
extra_query=extra_query,
381+
extra_body=extra_body,
382+
)
383+
return response
384+
300385

301386
class AsyncCompletions(AsyncAPIResource):
302387
def __init__(self, client: AsyncPortkey) -> None:
@@ -542,6 +627,88 @@ async def delete(
542627

543628
return data
544629

630+
async def parse(
631+
self,
632+
*,
633+
messages: Iterable[Any],
634+
model: Optional[str] = "portkey-default",
635+
audio: Union[Optional[Any], NotGiven] = NOT_GIVEN,
636+
response_format: Union[Any, NotGiven] = NOT_GIVEN,
637+
frequency_penalty: Union[Optional[float], NotGiven] = NOT_GIVEN,
638+
function_call: Union[Any, NotGiven] = NOT_GIVEN,
639+
functions: Union[Iterable[Any], NotGiven] = NOT_GIVEN,
640+
logit_bias: Union[Optional[Dict[str, int]], NotGiven] = NOT_GIVEN,
641+
logprobs: Union[Optional[bool], NotGiven] = NOT_GIVEN,
642+
max_completion_tokens: Union[Optional[int], NotGiven] = NOT_GIVEN,
643+
max_tokens: Union[Optional[int], NotGiven] = NOT_GIVEN,
644+
metadata: Union[Optional[Metadata], NotGiven] = NOT_GIVEN,
645+
modalities: Union[Optional[List[Any]], NotGiven] = NOT_GIVEN,
646+
n: Union[Optional[int], NotGiven] = NOT_GIVEN,
647+
parallel_tool_calls: Union[bool, NotGiven] = NOT_GIVEN,
648+
prediction: Union[Any, NotGiven] = NOT_GIVEN,
649+
presence_penalty: Union[Optional[float], NotGiven] = NOT_GIVEN,
650+
prompt_cache_key: Union[str, NotGiven] = NOT_GIVEN,
651+
reasoning_effort: Union[Any, NotGiven] = NOT_GIVEN,
652+
safety_identifier: Union[str, NotGiven] = NOT_GIVEN,
653+
seed: Union[Optional[int], NotGiven] = NOT_GIVEN,
654+
service_tier: Union[
655+
Literal["auto", "default", "flex", "scale", "priority"], NotGiven
656+
] = NOT_GIVEN,
657+
stop: Union[Optional[str], List[str], None] | NotGiven = NOT_GIVEN,
658+
store: Union[Optional[bool], NotGiven] = NOT_GIVEN,
659+
stream_options: Union[Any, NotGiven] = NOT_GIVEN,
660+
temperature: Union[Optional[float], NotGiven] = NOT_GIVEN,
661+
tool_choice: Union[Any, NotGiven] = NOT_GIVEN,
662+
tools: Union[Iterable[Any], NotGiven] = NOT_GIVEN,
663+
top_logprobs: Union[Optional[int], NotGiven] = NOT_GIVEN,
664+
top_p: Union[Optional[float], NotGiven] = NOT_GIVEN,
665+
user: Union[str, NotGiven] = NOT_GIVEN,
666+
verbosity: Union[Literal["low", "medium", "high"], NotGiven] = NOT_GIVEN,
667+
web_search_options: Union[Any, NotGiven] = NOT_GIVEN,
668+
extra_headers: Optional[Headers] = None,
669+
extra_query: Optional[Query] = None,
670+
extra_body: Optional[Body] = None,
671+
) -> ParsedChatCompletion:
672+
response = await self.openai_client.chat.completions.parse(
673+
messages=messages,
674+
model=model, # type: ignore[arg-type]
675+
audio=audio,
676+
response_format=response_format,
677+
frequency_penalty=frequency_penalty,
678+
function_call=function_call,
679+
functions=functions,
680+
logit_bias=logit_bias,
681+
logprobs=logprobs,
682+
max_completion_tokens=max_completion_tokens,
683+
max_tokens=max_tokens,
684+
metadata=metadata,
685+
modalities=modalities,
686+
n=n,
687+
parallel_tool_calls=parallel_tool_calls,
688+
prediction=prediction,
689+
presence_penalty=presence_penalty,
690+
prompt_cache_key=prompt_cache_key,
691+
reasoning_effort=reasoning_effort,
692+
safety_identifier=safety_identifier,
693+
seed=seed,
694+
service_tier=service_tier,
695+
stop=stop,
696+
store=store,
697+
stream_options=stream_options,
698+
temperature=temperature,
699+
tool_choice=tool_choice,
700+
tools=tools,
701+
top_logprobs=top_logprobs,
702+
top_p=top_p,
703+
user=user,
704+
verbosity=verbosity,
705+
web_search_options=web_search_options,
706+
extra_headers=extra_headers,
707+
extra_query=extra_query,
708+
extra_body=extra_body,
709+
)
710+
return response
711+
545712
def _get_config_string(self, config: Union[Mapping, str]) -> str:
546713
return config if isinstance(config, str) else json.dumps(config)
547714

0 commit comments

Comments
 (0)