mellea.helpers.openai_compatible_helpers
A file for helper functions that deal with OpenAI API compatible helpers.
Functions
FUNC extract_model_tool_requests
extract_model_tool_requests(tools: dict[str, AbstractMelleaTool], response: dict[str, Any]) -> dict[str, ModelToolCall] | None
Extract tool calls from the dict representation of an OpenAI-like chat response object.
Args:
tools: Mapping of tool name toAbstractMelleaToolfor lookup.response: Dict representation of an OpenAI-compatible chat completion message (must contain a"message"key).
Returns:
- Mapping of tool name to
ModelToolCallfor each requested tool call, or Noneif no tool calls were found.
FUNC chat_completion_delta_merge
chat_completion_delta_merge(chunks: list[dict], force_all_tool_calls_separate: bool = False) -> dict
Merge a list of deltas from ChatCompletionChunks into a single dict representing the ChatCompletion choice.
Args:
chunks: The list of dicts that represent the message deltas.force_all_tool_calls_separate: IfTrue, tool calls in separate message deltas will not be merged even if their index values are the same. Use when providers do not return the correct index value for tool calls; all tool calls must then be fully populated in a single delta.
Returns:
- A single merged dict representing the assembled
ChatCompletionchoice, - with
finish_reason,index, and amessagesub-dict containing content,role, andtool_calls.
FUNC message_to_openai_message
message_to_openai_message(msg: Message, formatter: Formatter | None = None) -> dict
Serialise a Mellea Message to the format required by OpenAI-compatible API providers.
Args:
msg: TheMessageobject to serialise.formatter: Optional formatter used to render the message content (including documents) through the template system. WhenNone, uses the rawmsg.contentstring without document rendering.
Returns:
- A dict with
"role"and"content"fields. When the message carries - images,
"content"is a list of text and image-URL dicts; otherwise it - is a plain string.
FUNC messages_to_docs
messages_to_docs(msgs: list[Message]) -> list[dict[str, str]]
Extract all Document objects from a list of Message objects.
Args:
msgs: List ofMessageobjects whose_docsattributes are inspected.
Returns:
- A list of dicts, each with a
"text"key and optional"title"and "doc_id"keys, suitable for passing to an OpenAI-compatible RAG API.
FUNC build_completion_usage
build_completion_usage(output: ModelOutputThunk) -> CompletionUsage | None
Build a normalized usage object from a model output, if available.
Args:
output: Model output object whosegeneration.usagemapping contains token counts.
Returns:
- A
CompletionUsageobject when usage metadata is present on the - output, otherwise
None.
FUNC has_tool_calls
has_tool_calls(output: ModelOutputThunk) -> bool
Check if a model output has tool calls.
Args:
output: Model output thunk that may expose atool_callsmapping.
Returns:
Trueif the output has non-empty tool calls,Falseotherwise.
FUNC build_tool_calls
build_tool_calls(output: ModelOutputThunk) -> list[ToolCallDict] | None
Build OpenAI-compatible tool calls from a model output, if available.
Args:
output: Model output thunk that may expose atool_callsmapping.
Returns:
- List of
ToolCallDictobjects when tool calls are present, - otherwise
None.
Classes
CLASS ToolCallFunction
Function details in a tool call.
CLASS ToolCallDict
OpenAI-compatible tool call dictionary with ID and function.
CLASS CompletionUsage
Token usage statistics for a completion request.