DocumentationAPI Reference📓 Tutorials🧑‍🍳 Cookbook🤝 Integrations💜 Discord🎨 Studio (Waitlist)
API Reference

OpenAIFunctionCaller invokes Python functions from ChatMessage(s)

Module haystack_experimental.components.tools.openai.function_caller

OpenAIFunctionCaller

OpenAIFunctionCaller processes a list of chat messages and call Python functions when needed.

The OpenAIFunctionCaller expects a list of ChatMessages and if there is a tool call with a function name and arguments, it runs the function and returns the result as a ChatMessage from role = 'function'

OpenAIFunctionCaller.__init__

def __init__(available_functions: Dict[str, Callable])

Initialize the OpenAIFunctionCaller component.

Arguments:

  • available_functions: A dictionary of available functions. This dictionary expects key value pairs of function name, and the function itself. For example, {"weather_function": weather_function}

OpenAIFunctionCaller.to_dict

def to_dict() -> Dict[str, Any]

Serializes the component to a dictionary.

Returns:

Dictionary with serialized data.

OpenAIFunctionCaller.from_dict

@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "OpenAIFunctionCaller"

Deserializes the component from a dictionary.

Arguments:

  • data: The dictionary to deserialize from.

Returns:

The deserialized component.

OpenAIFunctionCaller.run

@component.output_types(function_replies=List[ChatMessage],
                        assistant_replies=List[ChatMessage])
def run(messages: List[ChatMessage])

Evaluates messages and invokes available functions if the messages contain tool_calls.

Arguments:

  • messages: A list of messages generated from the OpenAIChatGenerator

Returns:

This component returns a list of messages in one of two outputs

  • function_replies: List of ChatMessages containing the result of a function invocation. This message comes from role = 'function'. If the function name was hallucinated or wrong, an assistant message explaining as such is returned
  • assistant_replies: List of ChatMessages containing a regular assistant reply. In this case, there were no tool_calls in the received messages