Skip to content

mirascope.core.groq.call_response

This module contains the GroqCallResponse class.

Usage Documentation

Calls

GroqCallResponse

Bases: BaseCallResponse[ChatCompletion, GroqTool, ChatCompletionToolParam, AsyncGroqDynamicConfig | GroqDynamicConfig, ChatCompletionMessageParam, GroqCallParams, ChatCompletionUserMessageParam]

A convenience wrapper around the Groq ChatCompletion response.

When calling the Groq API using a function decorated with groq_call, the response will be an GroqCallResponse instance with properties that allow for more convenience access to commonly used attributes.

Example:

from mirascope.core import prompt_template
from mirascope.core.groq import groq_call


@groq_call("llama-3.1-8b-instant")
def recommend_book(genre: str) -> str:
    return f"Recommend a {genre} book"

response = recommend_book("fantasy")  # response is an `GroqCallResponse` instance
print(response.content)

content property

content: str

Returns the content of the chat completion for the 0th choice.

finish_reasons property

finish_reasons: list[str]

Returns the finish reasons of the response.

model property

model: str

Returns the name of the response model.

id property

id: str

Returns the id of the response.

usage property

usage: CompletionUsage | None

Returns the usage of the chat completion.

input_tokens property

input_tokens: int | None

Returns the number of input tokens.

output_tokens property

output_tokens: int | None

Returns the number of output tokens.

cost property

cost: float | None

Returns the cost of the call.

message_param property

message_param: SerializeAsAny[
    ChatCompletionAssistantMessageParam
]

Returns the assistants's response as a message parameter.

tools property

tools: list[GroqTool] | None

Returns any available tool calls as their GroqTool definition.

Raises:

Type Description
ValidationError

if a tool call doesn't match the tool's schema.

tool property

tool: GroqTool | None

Returns the 0th tool for the 0th choice message.

Raises:

Type Description
ValidationError

if the tool call doesn't match the tool's schema.

tool_message_params classmethod

tool_message_params(
    tools_and_outputs: list[tuple[GroqTool, str]]
) -> list[ChatCompletionToolMessageParam]

Returns the tool message parameters for tool call results.

Parameters:

Name Type Description Default
tools_and_outputs list[tuple[GroqTool, str]]

The list of tools and their outputs from which the tool message parameters should be constructed.

required

Returns:

Type Description
list[ChatCompletionToolMessageParam]

The list of constructed ChatCompletionToolMessageParam parameters.

Source code in mirascope/core/groq/call_response.py
@classmethod
def tool_message_params(
    cls, tools_and_outputs: list[tuple[GroqTool, str]]
) -> list[ChatCompletionToolMessageParam]:
    """Returns the tool message parameters for tool call results.

    Args:
        tools_and_outputs: The list of tools and their outputs from which the tool
            message parameters should be constructed.

    Returns:
        The list of constructed `ChatCompletionToolMessageParam` parameters.
    """
    return [
        ChatCompletionToolMessageParam(
            role="tool",
            content=output,
            tool_call_id=tool.tool_call.id,
            name=tool._name(),  # pyright: ignore [reportCallIssue]
        )
        for tool, output in tools_and_outputs
    ]