ChatMessage
@JsonInclude(JsonInclude.Include.NON_NULL) public class ChatMessage
io/agentscope/core/chat/completions/model/ChatMessage.java:54–153聊天消息,与模型服务通信并返回生成或向量化结果。位于 io.agentscope.core.chat.completions.model 包。核心成员包括 assistantWithToolCalls、toolResult、getRole、setRole、getContent、setContent。
Chat message representation for the Chat Completions API.
This DTO is compatible with OpenAI's chat message format and supports:
-
Basic text messages (user, assistant, system)
-
Tool call requests (assistant messages with tool_calls)
-
Tool results (tool messages with tool_call_id)
Example formats:
`// User message
{"role": "user", "content": "Hello"}
// Assistant message with tool call
{
"role": "assistant",
"content": "Let me check the weather",
"tool_calls": [{
"id": "call_abc",
"type": "function",
"function": {"name": "get_weather", "arguments": "{\"city\":\"Hangzhou\"}"}
}]
}
// Tool result message
{"role": "tool", "tool_call_id": "call_abc", "content": "{\"temp\": 25}"}
`
构造器摘要Constructor Summary
| 构造器Constructor | 说明Description |
|---|---|
public ChatMessage() | 初始化 `ChatMessage` 实例。 Default constructor for deserialization. |
public ChatMessage(String role, String content) | 创建简单文本消息。 Creates a simple text message. |
方法摘要Method Summary
| 方法Method | 说明Description |
|---|---|
public static ChatMessage assistantWithToolCalls(String content, List<ToolCall> toolCalls)static | 执行 `assistantWithToolCalls` 方法。 Creates an assistant message with tool calls. |
public static ChatMessage toolResult(String toolCallId, String name, String content)static | 创建工具结果消息。 Creates a tool result message. |
public String getRole() | 获取角色。 Returns the role. |
public void setRole(String role) | 设置角色。 Sets the role. |
public String getContent() | 获取内容。 Returns the content. |
public void setContent(String content) | 设置内容。 Sets the content. |
public List<ToolCall> getToolCalls() | 获取工具调用。 Returns the tool calls. |
public void setToolCalls(List<ToolCall> toolCalls) | 设置工具调用。 Sets the tool calls. |
public String getToolCallId() | 获取工具调用标识。 Returns the tool call id. |
public void setToolCallId(String toolCallId) | 设置工具调用标识。 Sets the tool call id. |
public String getName() | 获取名称。 Returns the name. |
public void setName(String name) | 设置名称。 Sets the name. |
构造器详情Constructor Detail
ChatMessage
public ChatMessage()
初始化 ChatMessage 实例。
Default constructor for deserialization.
io/agentscope/core/chat/completions/model/ChatMessage.java:73ChatMessage
public ChatMessage(String role, String content)
创建简单文本消息。
Creates a simple text message.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
role | String | 角色(类型 `String`)。Message role (user, assistant, system, tool) |
content | String | 内容(类型 `String`)。Message content |
io/agentscope/core/chat/completions/model/ChatMessage.java:81–84方法详情Method Detail
assistantWithToolCalls
public static ChatMessage assistantWithToolCalls(String content, List<ToolCall> toolCalls)
执行 assistantWithToolCalls 方法。
Creates an assistant message with tool calls.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
content | String | 内容(类型 `String`)。Optional text content |
toolCalls | List<ToolCall> | 工具调用的列出。List of tool calls |
ChatMessageio/agentscope/core/chat/completions/model/ChatMessage.java:93–97toolResult
public static ChatMessage toolResult(String toolCallId, String name, String content)
创建工具结果消息。
Creates a tool result message.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
toolCallId | String | 工具调用标识(类型 `String`)。ID of the tool call being responded to |
name | String | 工具的名称。Name of the tool |
content | String | 内容(类型 `String`)。Tool execution result |
ChatMessageio/agentscope/core/chat/completions/model/ChatMessage.java:107–112getRole
public String getRole()
获取角色。
Returns the role.
Stringio/agentscope/core/chat/completions/model/ChatMessage.java:114–116setRole
public void setRole(String role)
设置角色。
Sets the role.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
role | String | 角色(类型 `String`)。The role. |
io/agentscope/core/chat/completions/model/ChatMessage.java:118–120getContent
public String getContent()
获取内容。
Returns the content.
Stringio/agentscope/core/chat/completions/model/ChatMessage.java:122–124setContent
public void setContent(String content)
设置内容。
Sets the content.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
content | String | 内容(类型 `String`)。The content. |
io/agentscope/core/chat/completions/model/ChatMessage.java:126–128getToolCalls
public List<ToolCall> getToolCalls()
获取工具调用。
Returns the tool calls.
List<ToolCall>io/agentscope/core/chat/completions/model/ChatMessage.java:130–132setToolCalls
public void setToolCalls(List<ToolCall> toolCalls)
设置工具调用。
Sets the tool calls.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
toolCalls | List<ToolCall> | 工具调用(类型 `List`)。The tool calls. |
io/agentscope/core/chat/completions/model/ChatMessage.java:134–136getToolCallId
public String getToolCallId()
获取工具调用标识。
Returns the tool call id.
Stringio/agentscope/core/chat/completions/model/ChatMessage.java:138–140setToolCallId
public void setToolCallId(String toolCallId)
设置工具调用标识。
Sets the tool call id.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
toolCallId | String | 工具调用标识(类型 `String`)。The tool call id. |
io/agentscope/core/chat/completions/model/ChatMessage.java:142–144getName
public String getName()
获取名称。
Returns the name.
Stringio/agentscope/core/chat/completions/model/ChatMessage.java:146–148setName
public void setName(String name)
设置名称。
Sets the name.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
name | String | 名称(类型 `String`)。The name. |
io/agentscope/core/chat/completions/model/ChatMessage.java:150–152