Message Module¶
erniebot_agent.memory.messages
¶
Message
¶
Base class of the message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
role |
str
|
character of the message. |
required |
content |
str
|
content of the message. |
required |
token_count |
Optional[int]
|
number of tokens of the message content. Defaults to None. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
role |
str
|
character of the message. |
content |
str
|
content of the message. |
token_count |
Optional[int]
|
number of tokens of the message content. |
Examples:
>>> Message("user", "hello")
<role: user, content: hello>
>>> Message("user", "hello", token_count=5)
<role: user, content: hello, token_count: 5>
Source code in erniebot-agent/src/erniebot_agent/memory/messages.py
to_dict()
¶
Transfer the message to a dict, which is used to chat with models by ERNIB Bot SDK.
Source code in erniebot-agent/src/erniebot_agent/memory/messages.py
SystemMessage
¶
Definition of system message, such that the feature of the Agent can be customized.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content |
str
|
the content of the message. |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
role |
str
|
character of the message. |
content |
str
|
content of the message. |
token_count |
Optional[int]
|
number of tokens of the message content. |
Examples:
>>> from erniebot_agent.messages import SystemMessage
>>> SystemMessage("you are an assistant useful for ocr.")
<role: system, content: you are an assistant useful for ocr.>
>>> SystemMessage("you are an assistant useful for ocr.").to_dict()
{'role': 'system', 'content': 'you are an assistant useful for ocr.'}
>>> SystemMessage("you are an assistant useful for ocr.").token_count
2
>>> SystemMessage("you are an assistant useful for ocr.").token_count = 3
>>> SystemMessage("you are an assistant useful for ocr.").token_count
3
Source code in erniebot-agent/src/erniebot_agent/memory/messages.py
HumanMessage
¶
The definition of the message created by a human.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content |
str
|
the content of the message. |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
role |
str
|
character of the message. |
content |
str
|
content of the message. |
token_count |
Optional[int]
|
number of tokens of the message content. |
Examples:
>>> from erniebot_agent.messages import HumanMessage
>>> HumanMessage("I want to order a pizza.")
<role: user, content: I want to order a pizza.>
>>> prompt = "What is the text in this image?"
>>> files = [await file_manager.create_file_from_path(file_path="ocr_img.jpg", file_type="remote")]
>>> message = await HumanMessage.create_with_files(
prompt, files, include_file_urls=True)
>>> message
<role: user, content: W h ha t.<file>File-local-xxxx</file><url>{url}</url>.>
Source code in erniebot-agent/src/erniebot_agent/memory/messages.py
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | |
create_with_files(text, files, *, include_file_urls=False)
async
classmethod
¶
create a Human Message with file input
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text |
str
|
content of the message. |
required |
files |
List[File]
|
The file that the message contains. |
required |
include_file_urls |
bool
|
Whehter to include file URLs in the content of message. |
False
|
Returns:
| Type | Description |
|---|---|
Self
|
A HumanMessage object that contains file in the content. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
Only |
Source code in erniebot-agent/src/erniebot_agent/memory/messages.py
AIMessage
¶
The definition of the message from an assistant.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content |
str
|
the content of the message. |
required |
function_call |
Optional[FunctionCall]
|
The function that agent calls. Defaults to None. |
None
|
token_usage |
Optional[TokenUsage]
|
the token usage calculate by ERNIE. Defaults to None. |
None
|
search_info |
Optional[SearchInfo]
|
|
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
role |
str
|
character of the message. |
content |
str
|
content of the message. |
token_count |
Optional[int]
|
number of tokens of the message content. |
function_call |
Optional[FunctionCall]
|
The function that agent calls. |
query_tokens_count |
int
|
the number of tokens in the query. |
search_info |
Optional[SearchInfo]
|
The SearchInfo in the chat model's response. |
Examples:
>>> human_message = HumanMessage(content="What is the text in this image?")
>>> ai_message = AIMessage(
function_call={"name": "OCR", "thoughts": "The user want to know the text in the image,
I need to use the OCR tool",
"arguments": "{"imgae_byte_str": file-remote-xxxx, "lang": "en"}"},
token_usage={"prompt_tokens": 10, "completion_tokens": 20},
search_info={}]}
)
>>> human_message.content
"What is the text in this image?"
>>> ai_message.function_call
{"name": "OCR",
"thoughts": "The user want to know the text in the image, I need to use the OCR tool",
"arguments": "{"imgae_byte_str": file-remote-xxxx, "lang": "en"}"}
Source code in erniebot-agent/src/erniebot_agent/memory/messages.py
FunctionMessage
¶
The definition of a message that calls tools, containing the result of a function call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name |
str
|
the name of the function. |
required |
content |
str
|
the content of the message. |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
the name of the function. |
role |
str
|
character of the message. |
content |
str
|
content of the message. |
token_count |
Optional[int]
|
number of tokens of the message content. |
Examples:
>>> function_message = FunctionMessage(name="OCR", content="The text in the image is: 1234567")
>>> function_message.name
"OCR"
>>> function_message.content
"The text in the image is: 1234567"
>>> function_message.role
"function"
>>> function_message.token_count
0