AS AgentScopeAPI 手册 / API Reference

ChatMessage

@JsonInclude(JsonInclude.Include.NON_NULL) public class ChatMessage
源码Source 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.

源码Source io/agentscope/core/chat/completions/model/ChatMessage.java:73

ChatMessage

public ChatMessage(String role, String content)

创建简单文本消息。

Creates a simple text message.

参数Parameters
名称Name类型Type说明Description
roleString角色(类型 `String`)。Message role (user, assistant, system, tool)
contentString内容(类型 `String`)。Message content
源码Source io/agentscope/core/chat/completions/model/ChatMessage.java:81–84

方法详情Method Detail

assistantWithToolCalls

public static ChatMessage assistantWithToolCalls(String content, List<ToolCall> toolCalls)
static

执行 assistantWithToolCalls 方法。

Creates an assistant message with tool calls.

参数Parameters
名称Name类型Type说明Description
contentString内容(类型 `String`)。Optional text content
toolCallsList<ToolCall>工具调用的列出。List of tool calls
返回Returns ChatMessage
返回ChatMessage。New ChatMessage instance
源码Source io/agentscope/core/chat/completions/model/ChatMessage.java:93–97

toolResult

public static ChatMessage toolResult(String toolCallId, String name, String content)
static

创建工具结果消息。

Creates a tool result message.

参数Parameters
名称Name类型Type说明Description
toolCallIdString工具调用标识(类型 `String`)。ID of the tool call being responded to
nameString工具的名称。Name of the tool
contentString内容(类型 `String`)。Tool execution result
返回Returns ChatMessage
返回ChatMessage。New ChatMessage instance
源码Source io/agentscope/core/chat/completions/model/ChatMessage.java:107–112

getRole

public String getRole()

获取角色。

Returns the role.

返回Returns String
返回字符串结果。Returns a string.
源码Source io/agentscope/core/chat/completions/model/ChatMessage.java:114–116

setRole

public void setRole(String role)

设置角色。

Sets the role.

参数Parameters
名称Name类型Type说明Description
roleString角色(类型 `String`)。The role.
源码Source io/agentscope/core/chat/completions/model/ChatMessage.java:118–120

getContent

public String getContent()

获取内容。

Returns the content.

返回Returns String
返回字符串结果。Returns a string.
源码Source io/agentscope/core/chat/completions/model/ChatMessage.java:122–124

setContent

public void setContent(String content)

设置内容。

Sets the content.

参数Parameters
名称Name类型Type说明Description
contentString内容(类型 `String`)。The content.
源码Source io/agentscope/core/chat/completions/model/ChatMessage.java:126–128

getToolCalls

public List<ToolCall> getToolCalls()

获取工具调用。

Returns the tool calls.

返回Returns List<ToolCall>
返回列出。Returns list.
源码Source io/agentscope/core/chat/completions/model/ChatMessage.java:130–132

setToolCalls

public void setToolCalls(List<ToolCall> toolCalls)

设置工具调用。

Sets the tool calls.

参数Parameters
名称Name类型Type说明Description
toolCallsList<ToolCall>工具调用(类型 `List`)。The tool calls.
源码Source io/agentscope/core/chat/completions/model/ChatMessage.java:134–136

getToolCallId

public String getToolCallId()

获取工具调用标识。

Returns the tool call id.

返回Returns String
返回字符串结果。Returns a string.
源码Source io/agentscope/core/chat/completions/model/ChatMessage.java:138–140

setToolCallId

public void setToolCallId(String toolCallId)

设置工具调用标识。

Sets the tool call id.

参数Parameters
名称Name类型Type说明Description
toolCallIdString工具调用标识(类型 `String`)。The tool call id.
源码Source io/agentscope/core/chat/completions/model/ChatMessage.java:142–144

getName

public String getName()

获取名称。

Returns the name.

返回Returns String
返回字符串结果。Returns a string.
源码Source io/agentscope/core/chat/completions/model/ChatMessage.java:146–148

setName

public void setName(String name)

设置名称。

Sets the name.

参数Parameters
名称Name类型Type说明Description
nameString名称(类型 `String`)。The name.
源码Source io/agentscope/core/chat/completions/model/ChatMessage.java:150–152
基于官方固定版本源码静态生成Generated from pinned official sources · Python 2.0.11 · Java 2.0.4
签名与文档为静态抽取;如有疑问请以源码为准。Signatures and docs are extracted statically; verify against the source when in doubt.