camel.messages package

Contents

camel.messages package#

Submodules#

camel.messages.base module#

class camel.messages.base.BaseMessage(role_name: str, role_type: RoleType, meta_dict: Dict[str, str] | None, content: str, video_bytes: bytes | None = None, image_list: List[Image] | None = None, image_detail: Literal['auto', 'low', 'high'] = 'auto', video_detail: Literal['auto', 'low', 'high'] = 'low')[source]#

Bases: object

Base class for message objects used in CAMEL chat system.

Parameters:
  • role_name (str) – The name of the user or assistant role.

  • role_type (RoleType) – The type of role, either RoleType. ASSISTANT or RoleType.USER.

  • meta_dict (Optional[Dict[str, str]]) – Additional metadata dictionary for the message.

  • content (str) – The content of the message.

  • video_bytes (Optional[bytes]) – Optional bytes of a video associated with the message. Default is None.

  • image_list (Optional[List[Image.Image]]) – Optional list of PIL Image objects associated with the message. Default is None.

  • image_detail (Literal["auto", "low", "high"]) – Detail level of the images associated with the message. Default is “auto”.

  • video_detail (Literal["auto", "low", "high"]) – Detail level of the videos associated with the message. Default is “low”.

content: str#
create_new_instance(content: str) BaseMessage[source]#

Create a new instance of the BaseMessage with updated content.

Parameters:

content (str) – The new content value.

Returns:

The new instance of BaseMessage.

Return type:

BaseMessage

extract_text_and_code_prompts() Tuple[List[TextPrompt], List[CodePrompt]][source]#

Extract text and code prompts from the message content.

Returns:

A tuple containing a

list of text prompts and a list of code prompts extracted from the content.

Return type:

Tuple[List[TextPrompt], List[CodePrompt]]

classmethod from_sharegpt(message: ShareGPTMessage, function_format: FunctionCallFormatter[Any, Any] | None = None, role_mapping=None) BaseMessage[source]#

Convert ShareGPT message to BaseMessage or FunctionCallingMessage. Note tool calls and responses have an ‘assistant’ role in CAMEL

Parameters:
  • message (ShareGPTMessage) – ShareGPT message to convert.

  • function_format (FunctionCallFormatter, optional) – Function call formatter to use. (default: HermesFunctionFormatter().

  • role_mapping (Dict[str, List[str, RoleType]], optional) – Role mapping to use. Defaults to a CAMEL specific mapping.

Returns:

Converted message.

Return type:

BaseMessage

image_detail: Literal['auto', 'low', 'high'] = 'auto'#
image_list: List[Image] | None = None#
classmethod make_assistant_message(role_name: str, content: str, meta_dict: Dict[str, str] | None = None, video_bytes: bytes | None = None, image_list: List[Image] | None = None, image_detail: OpenAIVisionDetailType | str = OpenAIVisionDetailType.AUTO, video_detail: OpenAIVisionDetailType | str = OpenAIVisionDetailType.LOW) BaseMessage[source]#

Create a new assistant message.

Parameters:
  • role_name (str) – The name of the assistant role.

  • content (str) – The content of the message.

  • meta_dict (Optional[Dict[str, str]]) – Additional metadata dictionary for the message.

  • video_bytes (Optional[bytes]) – Optional bytes of a video associated with the message.

  • image_list (Optional[List[Image.Image]]) – Optional list of PIL Image objects associated with the message.

  • image_detail (Union[OpenAIVisionDetailType, str]) – Detail level of the images associated with the message.

  • video_detail (Union[OpenAIVisionDetailType, str]) – Detail level of the videos associated with the message.

Returns:

The new assistant message.

Return type:

BaseMessage

classmethod make_user_message(role_name: str, content: str, meta_dict: Dict[str, str] | None = None, video_bytes: bytes | None = None, image_list: List[Image] | None = None, image_detail: OpenAIVisionDetailType | str = OpenAIVisionDetailType.AUTO, video_detail: OpenAIVisionDetailType | str = OpenAIVisionDetailType.LOW) BaseMessage[source]#

Create a new user message.

Parameters:
  • role_name (str) – The name of the user role.

  • content (str) – The content of the message.

  • meta_dict (Optional[Dict[str, str]]) – Additional metadata dictionary for the message.

  • video_bytes (Optional[bytes]) – Optional bytes of a video associated with the message.

  • image_list (Optional[List[Image.Image]]) – Optional list of PIL Image objects associated with the message.

  • image_detail (Union[OpenAIVisionDetailType, str]) – Detail level of the images associated with the message.

  • video_detail (Union[OpenAIVisionDetailType, str]) – Detail level of the videos associated with the message.

Returns:

The new user message.

Return type:

BaseMessage

meta_dict: Dict[str, str] | None#
role_name: str#
role_type: RoleType#
to_dict() Dict[source]#

Converts the message to a dictionary.

Returns:

The converted dictionary.

Return type:

dict

to_openai_assistant_message() ChatCompletionAssistantMessageParam[source]#

Converts the message to an OpenAIAssistantMessage object.

Returns:

The converted OpenAIAssistantMessage

object.

Return type:

OpenAIAssistantMessage

to_openai_message(role_at_backend: OpenAIBackendRole) ChatCompletionSystemMessageParam | ChatCompletionUserMessageParam | ChatCompletionAssistantMessageParam | ChatCompletionToolMessageParam | ChatCompletionFunctionMessageParam[source]#

Converts the message to an OpenAIMessage object.

Parameters:

role_at_backend (OpenAIBackendRole) – The role of the message in OpenAI chat system.

Returns:

The converted OpenAIMessage object.

Return type:

OpenAIMessage

to_openai_system_message() ChatCompletionSystemMessageParam[source]#

Converts the message to an OpenAISystemMessage object.

Returns:

The converted OpenAISystemMessage

object.

Return type:

OpenAISystemMessage

to_openai_user_message() ChatCompletionUserMessageParam[source]#

Converts the message to an OpenAIUserMessage object.

Returns:

The converted OpenAIUserMessage object.

Return type:

OpenAIUserMessage

to_sharegpt(function_format: FunctionCallFormatter | None = None) ShareGPTMessage[source]#

Convert BaseMessage to ShareGPT message

Parameters:

function_format (FunctionCallFormatter) – Function call formatter to use. Defaults to Hermes.

video_bytes: bytes | None = None#
video_detail: Literal['auto', 'low', 'high'] = 'low'#

camel.messages.func_message module#

class camel.messages.func_message.FunctionCallingMessage(role_name: str, role_type: RoleType, meta_dict: Dict[str, str] | None, content: str, video_bytes: bytes | None = None, image_list: List[Image] | None = None, image_detail: Literal['auto', 'low', 'high'] = 'auto', video_detail: Literal['auto', 'low', 'high'] = 'low', func_name: str | None = None, args: Dict | None = None, result: Any | None = None)[source]#

Bases: BaseMessage

Class for message objects used specifically for function-related messages.

Parameters:
  • func_name (Optional[str]) – The name of the function used. (default: None)

  • args (Optional[Dict]) – The dictionary of arguments passed to the function. (default: None)

  • result (Optional[Any]) – The result of function execution. (default: None)

args: Dict | None = None#
func_name: str | None = None#
result: Any | None = None#
to_openai_assistant_message() ChatCompletionAssistantMessageParam[source]#

Converts the message to an OpenAIAssistantMessage object.

Returns:

The converted OpenAIAssistantMessage

object.

Return type:

OpenAIAssistantMessage

to_openai_function_message() ChatCompletionFunctionMessageParam[source]#

Converts the message to an OpenAIMessage object with the role being “function”.

Returns:

The converted OpenAIMessage object

with its role being “function”.

Return type:

OpenAIMessage

to_openai_message(role_at_backend: OpenAIBackendRole) ChatCompletionSystemMessageParam | ChatCompletionUserMessageParam | ChatCompletionAssistantMessageParam | ChatCompletionToolMessageParam | ChatCompletionFunctionMessageParam[source]#

Converts the message to an OpenAIMessage object.

Parameters:

role_at_backend (OpenAIBackendRole) – The role of the message in OpenAI chat system.

Returns:

The converted OpenAIMessage object.

Return type:

OpenAIMessage

to_sharegpt(function_format: FunctionCallFormatter[ToolCall, ToolResponse] | None = None) ShareGPTMessage[source]#

Convert FunctionCallingMessage to ShareGPT message.

Parameters:
  • (FunctionCallFormatter[ToolCall (function_format) – optional): The function formatter to use. Defaults to None.

  • ToolResponse] – optional): The function formatter to use. Defaults to None.

:param : optional): The function formatter to use. Defaults to None.

Module contents#

class camel.messages.BaseMessage(role_name: str, role_type: RoleType, meta_dict: Dict[str, str] | None, content: str, video_bytes: bytes | None = None, image_list: List[Image] | None = None, image_detail: Literal['auto', 'low', 'high'] = 'auto', video_detail: Literal['auto', 'low', 'high'] = 'low')[source]#

Bases: object

Base class for message objects used in CAMEL chat system.

Parameters:
  • role_name (str) – The name of the user or assistant role.

  • role_type (RoleType) – The type of role, either RoleType. ASSISTANT or RoleType.USER.

  • meta_dict (Optional[Dict[str, str]]) – Additional metadata dictionary for the message.

  • content (str) – The content of the message.

  • video_bytes (Optional[bytes]) – Optional bytes of a video associated with the message. Default is None.

  • image_list (Optional[List[Image.Image]]) – Optional list of PIL Image objects associated with the message. Default is None.

  • image_detail (Literal["auto", "low", "high"]) – Detail level of the images associated with the message. Default is “auto”.

  • video_detail (Literal["auto", "low", "high"]) – Detail level of the videos associated with the message. Default is “low”.

content: str#
create_new_instance(content: str) BaseMessage[source]#

Create a new instance of the BaseMessage with updated content.

Parameters:

content (str) – The new content value.

Returns:

The new instance of BaseMessage.

Return type:

BaseMessage

extract_text_and_code_prompts() Tuple[List[TextPrompt], List[CodePrompt]][source]#

Extract text and code prompts from the message content.

Returns:

A tuple containing a

list of text prompts and a list of code prompts extracted from the content.

Return type:

Tuple[List[TextPrompt], List[CodePrompt]]

classmethod from_sharegpt(message: ShareGPTMessage, function_format: FunctionCallFormatter[Any, Any] | None = None, role_mapping=None) BaseMessage[source]#

Convert ShareGPT message to BaseMessage or FunctionCallingMessage. Note tool calls and responses have an ‘assistant’ role in CAMEL

Parameters:
Returns:

Converted message.

Return type:

BaseMessage

image_detail: Literal['auto', 'low', 'high'] = 'auto'#
image_list: List[Image] | None = None#
classmethod make_assistant_message(role_name: str, content: str, meta_dict: Dict[str, str] | None = None, video_bytes: bytes | None = None, image_list: List[Image] | None = None, image_detail: OpenAIVisionDetailType | str = OpenAIVisionDetailType.AUTO, video_detail: OpenAIVisionDetailType | str = OpenAIVisionDetailType.LOW) BaseMessage[source]#

Create a new assistant message.

Parameters:
  • role_name (str) – The name of the assistant role.

  • content (str) – The content of the message.

  • meta_dict (Optional[Dict[str, str]]) – Additional metadata dictionary for the message.

  • video_bytes (Optional[bytes]) – Optional bytes of a video associated with the message.

  • image_list (Optional[List[Image.Image]]) – Optional list of PIL Image objects associated with the message.

  • image_detail (Union[OpenAIVisionDetailType, str]) – Detail level of the images associated with the message.

  • video_detail (Union[OpenAIVisionDetailType, str]) – Detail level of the videos associated with the message.

Returns:

The new assistant message.

Return type:

BaseMessage

classmethod make_user_message(role_name: str, content: str, meta_dict: Dict[str, str] | None = None, video_bytes: bytes | None = None, image_list: List[Image] | None = None, image_detail: OpenAIVisionDetailType | str = OpenAIVisionDetailType.AUTO, video_detail: OpenAIVisionDetailType | str = OpenAIVisionDetailType.LOW) BaseMessage[source]#

Create a new user message.

Parameters:
  • role_name (str) – The name of the user role.

  • content (str) – The content of the message.

  • meta_dict (Optional[Dict[str, str]]) – Additional metadata dictionary for the message.

  • video_bytes (Optional[bytes]) – Optional bytes of a video associated with the message.

  • image_list (Optional[List[Image.Image]]) – Optional list of PIL Image objects associated with the message.

  • image_detail (Union[OpenAIVisionDetailType, str]) – Detail level of the images associated with the message.

  • video_detail (Union[OpenAIVisionDetailType, str]) – Detail level of the videos associated with the message.

Returns:

The new user message.

Return type:

BaseMessage

meta_dict: Dict[str, str] | None#
role_name: str#
role_type: RoleType#
to_dict() Dict[source]#

Converts the message to a dictionary.

Returns:

The converted dictionary.

Return type:

dict

to_openai_assistant_message() ChatCompletionAssistantMessageParam[source]#

Converts the message to an OpenAIAssistantMessage object.

Returns:

The converted OpenAIAssistantMessage

object.

Return type:

OpenAIAssistantMessage

to_openai_message(role_at_backend: OpenAIBackendRole) ChatCompletionSystemMessageParam | ChatCompletionUserMessageParam | ChatCompletionAssistantMessageParam | ChatCompletionToolMessageParam | ChatCompletionFunctionMessageParam[source]#

Converts the message to an OpenAIMessage object.

Parameters:

role_at_backend (OpenAIBackendRole) – The role of the message in OpenAI chat system.

Returns:

The converted OpenAIMessage object.

Return type:

OpenAIMessage

to_openai_system_message() ChatCompletionSystemMessageParam[source]#

Converts the message to an OpenAISystemMessage object.

Returns:

The converted OpenAISystemMessage

object.

Return type:

OpenAISystemMessage

to_openai_user_message() ChatCompletionUserMessageParam[source]#

Converts the message to an OpenAIUserMessage object.

Returns:

The converted OpenAIUserMessage object.

Return type:

OpenAIUserMessage

to_sharegpt(function_format: FunctionCallFormatter | None = None) ShareGPTMessage[source]#

Convert BaseMessage to ShareGPT message

Parameters:

function_format (FunctionCallFormatter) – Function call formatter to use. Defaults to Hermes.

video_bytes: bytes | None = None#
video_detail: Literal['auto', 'low', 'high'] = 'low'#
class camel.messages.FunctionCallFormatter[source]#

Bases: ABC, Generic[CallT, ResponseT]

Abstract base class for function calling formats

abstract extract_tool_calls(message: str) List[CallT][source]#

Extract function call info from a message string

abstract extract_tool_response(message: str) ResponseT | None[source]#

Extract function response info from a message string

abstract format_tool_call(content: str, func_name: str, args: Dict[str, Any]) str[source]#

Format a function call into a message string

abstract format_tool_response(func_name: str, result: Any) str[source]#

Format a function response into a message string

class camel.messages.FunctionCallingMessage(role_name: str, role_type: RoleType, meta_dict: Dict[str, str] | None, content: str, video_bytes: bytes | None = None, image_list: List[Image] | None = None, image_detail: Literal['auto', 'low', 'high'] = 'auto', video_detail: Literal['auto', 'low', 'high'] = 'low', func_name: str | None = None, args: Dict | None = None, result: Any | None = None)[source]#

Bases: BaseMessage

Class for message objects used specifically for function-related messages.

Parameters:
  • func_name (Optional[str]) – The name of the function used. (default: None)

  • args (Optional[Dict]) – The dictionary of arguments passed to the function. (default: None)

  • result (Optional[Any]) – The result of function execution. (default: None)

args: Dict | None = None#
content: str#
func_name: str | None = None#
meta_dict: Dict[str, str] | None#
result: Any | None = None#
role_name: str#
role_type: RoleType#
to_openai_assistant_message() ChatCompletionAssistantMessageParam[source]#

Converts the message to an OpenAIAssistantMessage object.

Returns:

The converted OpenAIAssistantMessage

object.

Return type:

OpenAIAssistantMessage

to_openai_function_message() ChatCompletionFunctionMessageParam[source]#

Converts the message to an OpenAIMessage object with the role being “function”.

Returns:

The converted OpenAIMessage object

with its role being “function”.

Return type:

OpenAIMessage

to_openai_message(role_at_backend: OpenAIBackendRole) ChatCompletionSystemMessageParam | ChatCompletionUserMessageParam | ChatCompletionAssistantMessageParam | ChatCompletionToolMessageParam | ChatCompletionFunctionMessageParam[source]#

Converts the message to an OpenAIMessage object.

Parameters:

role_at_backend (OpenAIBackendRole) – The role of the message in OpenAI chat system.

Returns:

The converted OpenAIMessage object.

Return type:

OpenAIMessage

to_sharegpt(function_format: FunctionCallFormatter[ToolCall, ToolResponse] | None = None) ShareGPTMessage[source]#

Convert FunctionCallingMessage to ShareGPT message.

Parameters:
  • (FunctionCallFormatter[ToolCall (function_format) – optional): The function formatter to use. Defaults to None.

  • ToolResponse] – optional): The function formatter to use. Defaults to None.

:param : optional): The function formatter to use. Defaults to None.

class camel.messages.HermesFunctionFormatter[source]#

Bases: FunctionCallFormatter[HermesToolCall, HermesToolResponse]

Hermes-style function calling format implementation with validation

extract_tool_calls(message: str) List[HermesToolCall][source]#

Extracts all tool calls from the provided message string.

Parameters:

message (str) – The input message string containing potential tool calls.

Returns:

A list of parsed HermesToolCall objects.

Return type:

List[HermesToolCall]

extract_tool_response(message: str) HermesToolResponse | None[source]#

Extracts a single tool response from the provided message string.

Parameters:

message (str) – The input message string containing a potential tool response.

Returns:

A parsed HermesToolResponse object,

or None if no valid response is found.

Return type:

Optional[HermesToolResponse]

format_tool_call(content: str, func_name: str, args: Dict[str, Any]) str[source]#

Formats a tool call message with the given content, function name, and arguments.

Parameters:
  • content (str) – The content or message to be included in the tool call.

  • func_name (str) – The name of the function being called.

  • args (Dict[str, Any]) – A dictionary of arguments to be passed to the function.

Returns:

A formatted string representing the tool call in Hermes

format.

Return type:

str

format_tool_response(func_name: str, result: Any) str[source]#

Formats a tool response message with the given function name and result.

Parameters:
  • func_name (str) – The name of the function whose result is being returned.

  • result (Any) – The result to be included in the tool response.

Returns:

A formatted string representing the tool response in Hermes

format.

Return type:

str

camel.messages.OpenAIAssistantMessage#

alias of ChatCompletionAssistantMessageParam

camel.messages.OpenAIFunctionMessage#

alias of ChatCompletionFunctionMessageParam

camel.messages.OpenAISystemMessage#

alias of ChatCompletionSystemMessageParam

camel.messages.OpenAIUserMessage#

alias of ChatCompletionUserMessageParam

class camel.messages.ShareGPTConversation(root: RootModelRootType = PydanticUndefined)[source]#

Bases: RootModel

A full conversation in ShareGPT format with validation

model_computed_fields: ClassVar[Dict[str, ComputedFieldInfo]] = {}#

A dictionary of computed field names and their corresponding ComputedFieldInfo objects.

model_config: ClassVar[ConfigDict] = {}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

model_dump(**kwargs)[source]#

Usage docs: https://docs.pydantic.dev/2.9/concepts/serialization/#modelmodel_dump

Generate a dictionary representation of the model, optionally specifying which fields to include or exclude.

Parameters:
  • mode – The mode in which to_python should run. If mode is ‘json’, the output will only contain JSON serializable types. If mode is ‘python’, the output may contain non-JSON-serializable Python objects.

  • include – A set of fields to include in the output.

  • exclude – A set of fields to exclude from the output.

  • context – Additional context to pass to the serializer.

  • by_alias – Whether to use the field’s alias in the dictionary key if defined.

  • exclude_unset – Whether to exclude fields that have not been explicitly set.

  • exclude_defaults – Whether to exclude fields that are set to their default value.

  • exclude_none – Whether to exclude fields that have a value of None.

  • round_trip – If True, dumped values should be valid as input for non-idempotent types such as Json[T].

  • warnings – How to handle serialization errors. False/”none” ignores them, True/”warn” logs errors, “error” raises a [PydanticSerializationError][pydantic_core.PydanticSerializationError].

  • serialize_as_any – Whether to serialize fields with duck-typing serialization behavior.

Returns:

A dictionary representation of the model.

model_fields: ClassVar[Dict[str, FieldInfo]] = {'root': FieldInfo(annotation=List[ShareGPTMessage], required=True)}#

Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo] objects.

This replaces Model.__fields__ from Pydantic V1.

root: List[ShareGPTMessage]#
validate_conversation_flow() ShareGPTConversation[source]#

Validate the conversation follows logical message order

class camel.messages.ShareGPTMessage(*, from_: Literal['human', 'gpt', 'system', 'tool'], value: str)[source]#

Bases: BaseModel

A single message in ShareGPT format with enhanced validation

from_: Literal['human', 'gpt', 'system', 'tool']#
model_computed_fields: ClassVar[Dict[str, ComputedFieldInfo]] = {}#

A dictionary of computed field names and their corresponding ComputedFieldInfo objects.

model_config: ClassVar[ConfigDict] = {'extra': 'forbid', 'json_schema_extra': {'examples': [{'from': 'human', 'value': "What's the weather like today?"}]}, 'populate_by_name': True}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

model_fields: ClassVar[Dict[str, FieldInfo]] = {'from_': FieldInfo(annotation=Literal['human', 'gpt', 'system', 'tool'], required=True, alias='from', alias_priority=2, description='The role of the message sender'), 'value': FieldInfo(annotation=str, required=True, description='The content of the message', metadata=[MinLen(min_length=0), MaxLen(max_length=100000)])}#

Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo] objects.

This replaces Model.__fields__ from Pydantic V1.

value: str#