camel.messages.conversion package

On this page

camel.messages.conversion package#

Subpackages#

Submodules#

camel.messages.conversion.alpaca module#

class camel.messages.conversion.alpaca.AlpacaItem(*, instruction: str, input: str, output: str)[source]#

Bases: BaseModel

Represents an instruction-response item in the Alpaca format.

Appropripate for both cases where input field is empty, or populated. Provides parsing from string format using the class method from_string().

Parameters:
  • instruction (str) – The instruction/question/prompt

  • input (str) – Input context or examples (put empty string if none)

  • output (str) – The response/answer to the instruction

classmethod from_string(text: str) AlpacaItem[source]#

Creates an AlpacaItem from a formatted string.

Parameters:

text

String in either of these formats: With input: ### Instruction: {instruction} ### Input: {input} ### Response: {response}

Without input: ### Instruction: {instruction} ### Response: {response}

Returns:

Parsed instance

Return type:

AlpacaItem

Raises:

ValueError – text doesn’t match expected format or sections missing

input: str#
instruction: str#
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_fields: ClassVar[Dict[str, FieldInfo]] = {'input': FieldInfo(annotation=str, required=True, description='Optional context or input for the task. For example, when the instruction is "Summarize the following article", the input is the article.'), 'instruction': FieldInfo(annotation=str, required=True, description='The instruction/question/prompt'), 'output': FieldInfo(annotation=str, required=True, description='The response/answer to the instruction')}#

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.

classmethod no_section_markers(value: str) str[source]#

Ensures fields don’t contain section markers like ‘### Response:’

output: str#
to_string() str[source]#

Converts the AlpacaItem to its string representation.

Returns:

Formatted string representation with sections markers

Return type:

str

camel.messages.conversion.conversation_models module#

class camel.messages.conversion.conversation_models.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.conversion.conversation_models.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#
class camel.messages.conversion.conversation_models.ToolCall(*, name: str, arguments: Dict[str, Any])[source]#

Bases: BaseModel

Represents a single tool/function call with validation

arguments: Dict[str, Any]#
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': [{'arguments': {'city': 'London', 'units': 'celsius'}, 'name': 'get_weather'}]}}#

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

model_fields: ClassVar[Dict[str, FieldInfo]] = {'arguments': FieldInfo(annotation=Dict[str, Any], required=True, description='The arguments to pass to the tool'), 'name': FieldInfo(annotation=str, required=True, description='The name of the tool to call', metadata=[MinLen(min_length=1), MaxLen(max_length=256)])}#

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.

name: str#
classmethod validate_arguments(v: Dict[str, Any]) Dict[str, Any][source]#

Validate argument structure and content

class camel.messages.conversion.conversation_models.ToolResponse(*, name: str, content: Any)[source]#

Bases: BaseModel

Represents a tool/function response with validation. This is a base class and default implementation for tool responses, for the purpose of converting between different formats.

content: Any#
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': [{'content': {'conditions': 'sunny', 'humidity': 65, 'temperature': 20}, 'name': 'get_weather'}]}}#

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

model_fields: ClassVar[Dict[str, FieldInfo]] = {'content': FieldInfo(annotation=Any, required=True, description='The response content from the tool. Must be JSON serializable literal or object'), 'name': FieldInfo(annotation=str, required=True, description='The name of the tool that was called', metadata=[MinLen(min_length=1), MaxLen(max_length=256)])}#

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.

name: str#
classmethod validate_content(v: Dict[str, Any]) Dict[str, Any][source]#

Validate response content structure

Module contents#

class camel.messages.conversion.AlpacaItem(*, instruction: str, input: str, output: str)[source]#

Bases: BaseModel

Represents an instruction-response item in the Alpaca format.

Appropripate for both cases where input field is empty, or populated. Provides parsing from string format using the class method from_string().

Parameters:
  • instruction (str) – The instruction/question/prompt

  • input (str) – Input context or examples (put empty string if none)

  • output (str) – The response/answer to the instruction

classmethod from_string(text: str) AlpacaItem[source]#

Creates an AlpacaItem from a formatted string.

Parameters:

text

String in either of these formats: With input: ### Instruction: {instruction} ### Input: {input} ### Response: {response}

Without input: ### Instruction: {instruction} ### Response: {response}

Returns:

Parsed instance

Return type:

AlpacaItem

Raises:

ValueError – text doesn’t match expected format or sections missing

input: str#
instruction: str#
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_fields: ClassVar[Dict[str, FieldInfo]] = {'input': FieldInfo(annotation=str, required=True, description='Optional context or input for the task. For example, when the instruction is "Summarize the following article", the input is the article.'), 'instruction': FieldInfo(annotation=str, required=True, description='The instruction/question/prompt'), 'output': FieldInfo(annotation=str, required=True, description='The response/answer to the instruction')}#

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.

classmethod no_section_markers(value: str) str[source]#

Ensures fields don’t contain section markers like ‘### Response:’

output: str#
to_string() str[source]#

Converts the AlpacaItem to its string representation.

Returns:

Formatted string representation with sections markers

Return type:

str

class camel.messages.conversion.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

class camel.messages.conversion.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.conversion.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#
class camel.messages.conversion.ToolCall(*, name: str, arguments: Dict[str, Any])[source]#

Bases: BaseModel

Represents a single tool/function call with validation

arguments: Dict[str, Any]#
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': [{'arguments': {'city': 'London', 'units': 'celsius'}, 'name': 'get_weather'}]}}#

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

model_fields: ClassVar[Dict[str, FieldInfo]] = {'arguments': FieldInfo(annotation=Dict[str, Any], required=True, description='The arguments to pass to the tool'), 'name': FieldInfo(annotation=str, required=True, description='The name of the tool to call', metadata=[MinLen(min_length=1), MaxLen(max_length=256)])}#

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.

name: str#
classmethod validate_arguments(v: Dict[str, Any]) Dict[str, Any][source]#

Validate argument structure and content

class camel.messages.conversion.ToolResponse(*, name: str, content: Any)[source]#

Bases: BaseModel

Represents a tool/function response with validation. This is a base class and default implementation for tool responses, for the purpose of converting between different formats.

content: Any#
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': [{'content': {'conditions': 'sunny', 'humidity': 65, 'temperature': 20}, 'name': 'get_weather'}]}}#

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

model_fields: ClassVar[Dict[str, FieldInfo]] = {'content': FieldInfo(annotation=Any, required=True, description='The response content from the tool. Must be JSON serializable literal or object'), 'name': FieldInfo(annotation=str, required=True, description='The name of the tool that was called', metadata=[MinLen(min_length=1), MaxLen(max_length=256)])}#

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.

name: str#
classmethod validate_content(v: Dict[str, Any]) Dict[str, Any][source]#

Validate response content structure