artefactual.preprocessing.openai_parser#

Functions

is_openai_responses_api(outputs)

Detects if the output follows the signature of the new OpenAI Responses API.

process_openai_chat_completion(response, ...)

Processes top log probabilities from OpenAI Chat Completion (classic 'choices' format).

process_openai_responses_api(response)

Parses the response from the 'client.responses.create' API to extract the top log probabilities.

sampled_tokens_logprobs_chat_completion_api(...)

Retrieves the log probabilities of the sampled tokens from a response following the OpenAI Chat Completion format.

sampled_tokens_logprobs_responses_api(response)

Retrieves the log probabilities of the sampled tokens from a response following the OpenAI Responses API format.

artefactual.preprocessing.openai_parser.is_openai_responses_api(outputs)[source]#

Detects if the output follows the signature of the new OpenAI Responses API.

Return type:

bool

Args:

outputs (Any): The output object or dictionary to inspect.

Returns:

bool: True if the output matches the OpenAI Responses API signature, False otherwise.

artefactual.preprocessing.openai_parser.process_openai_chat_completion(response, iterations)[source]#

Processes top log probabilities from OpenAI Chat Completion (classic ‘choices’ format).

Return type:

list[dict[int, list[float]]]

Args:

response (Any): The response object or dictionary from OpenAI API (ChatCompletion). iterations (int): The number of iterations (choices) to process.

Returns:

list[dict[int, list[float]]]: A list of dictionaries, where each dictionary maps token indices to lists of log probabilities for a sequence.

artefactual.preprocessing.openai_parser.process_openai_responses_api(response)[source]#

Parses the response from the ‘client.responses.create’ API to extract the top log probabilities.

Structure expected: response.output -> [item] -> item.content -> [part] -> part.logprobs

Return type:

list[dict[int, list[float]]]

Args:

response (Any): The response object from the OpenAI Responses API.

Returns:

list[dict[int, list[float]]]: A list of dictionaries, where each dictionary maps token indices to lists of log probabilities for a sequence.

artefactual.preprocessing.openai_parser.sampled_tokens_logprobs_chat_completion_api(response)[source]#

Retrieves the log probabilities of the sampled tokens from a response following the OpenAI Chat Completion format.

Iterates over all choices and returns a list of 1D arrays (one per choice), consistent with the vLLM counterpart.

Return type:

list[ndarray[tuple[int, ...], dtype[TypeVar(_ScalarType_co, bound= generic, covariant=True)]]]

Args:

response (Any): The response object from the OpenAI Chat Completion API.

Returns:
list[NDArray]: A list of 1D numpy arrays, each containing the log probabilities

of the sampled tokens for one choice.

artefactual.preprocessing.openai_parser.sampled_tokens_logprobs_responses_api(response)[source]#

Retrieves the log probabilities of the sampled tokens from a response following the OpenAI Responses API format.

Iterates over all output items and returns a list of 1D arrays (one per output item), consistent with the vLLM counterpart.

Return type:

list[ndarray[tuple[int, ...], dtype[TypeVar(_ScalarType_co, bound= generic, covariant=True)]]]

Args:

response (Any): The response object from the OpenAI Responses API.

Returns:
list[NDArray]: A list of 1D numpy arrays, each containing the log probabilities

of the sampled tokens for one output item.