Entropic 2.11.1
Local-first agentic inference engine
Loading...
Searching...
No Matches
entropic::ChatAdapter Class Referenceabstract

Concrete base class for chat format adapters (80% logic). More...

#include <entropic/inference/adapters/adapter_base.h>

Inheritance diagram for entropic::ChatAdapter:

Public Member Functions

 ChatAdapter (std::string tier_name, std::string identity_prompt)
 Construct adapter with tier identity.
 
virtual ThinkMarkers thinking_markers () const
 Parse tool calls from model output.
 
std::string strip_think_blocks (const std::string &content) const
 Strip this family's reasoning blocks from content (gh#108).
 
virtual ParseResult parse_tool_calls (const std::string &content) const =0
 
virtual Message format_tool_result (const ToolCall &tool_call, const std::string &result) const
 Format a tool result as a user message.
 
virtual std::string format_tools (const std::vector< std::string > &tool_jsons) const
 Format tool definitions for injection into system prompt.
 
bool is_response_complete (const std::string &content, const std::vector< ToolCall > &tool_calls) const
 Check if response represents task completion.
 
virtual std::string chat_format () const =0
 Chat format identifier (e.g.
 
virtual std::string format_system_with_vision (const std::string &base_system, bool has_vision) const
 Format system prompt with optional vision context.
 
virtual std::string format_content_parts (const std::vector< ContentPart > &parts) const
 Convert multimodal content parts to adapter-specific format.
 

Protected Member Functions

std::vector< ToolCallparse_tagged_tool_calls (const std::string &content) const
 Parse <tool_call>JSON</tool_call> tagged blocks.
 
std::optional< ToolCalltry_recover_json (const std::string &json_str) const
 Attempt JSON recovery on malformed tool call string.
 
std::optional< ToolCallparse_single_tool_call (const std::string &json_str) const
 Parse a single JSON tool call string.
 

Protected Attributes

std::string tier_name_
 Identity tier name.
 
std::string identity_prompt_
 Assembled identity prompt.
 
std::unordered_set< std::string > tool_prefixes_
 Known tool prefixes.
 

Detailed Description

Concrete base class for chat format adapters (80% logic).

Provides shared parsing primitives, think-block handling, JSON recovery, and system prompt assembly. Subclasses override parse_tool_calls() and optionally format_tools()/format_tool_result().

Version
1.8.2

Definition at line 129 of file adapter_base.h.

Constructor & Destructor Documentation

◆ ChatAdapter()

entropic::ChatAdapter::ChatAdapter ( std::string  tier_name,
std::string  identity_prompt 
)

Construct adapter with tier identity.

Parameters
tier_nameIdentity tier (e.g. "eng", "lead").
identity_promptAssembled identity prompt.
Version
1.8.2
Parameters
tier_nameIdentity tier name.
identity_promptAssembled identity prompt.
Version
1.8.2

Definition at line 103 of file adapter_base.cpp.

Member Function Documentation

◆ chat_format()

virtual std::string entropic::ChatAdapter::chat_format ( ) const
pure virtual

Chat format identifier (e.g.

"chatml").

Returns
Format string, or empty for GGUF-embedded template.
Version
1.8.2

Implemented in entropic::Gemma4Adapter, entropic::GenericAdapter, entropic::Nemotron3Adapter, entropic::Qwen35Adapter, and entropic::Qwen36Adapter.

◆ format_content_parts()

std::string ChatAdapter::format_content_parts ( const std::vector< ContentPart > &  parts) const
virtual

Convert multimodal content parts to adapter-specific format.

Default: OpenAI-format content array JSON.

Parameters
partsContent parts from a message.
Returns
JSON string in the format expected by the model's chat template.
Version
1.9.11

Default: OpenAI-format content array. Adapters override if the model expects a different image reference format.

Parameters
partsContent parts from a message.
Returns
JSON array string with text/image objects. @dg_internal
Version
1.9.11

Reimplemented in entropic::Qwen35Adapter, and entropic::Qwen36Adapter.

Definition at line 604 of file adapter_base.cpp.

◆ format_system_with_vision()

std::string ChatAdapter::format_system_with_vision ( const std::string &  base_system,
bool  has_vision 
) const
virtual

Format system prompt with optional vision context.

Default: return system prompt unchanged regardless of vision.

Parameters
base_systemBase system prompt text.
has_visionWhether the model has vision capability.
Returns
Formatted system prompt.
Version
1.9.11

Default implementation returns base_system unchanged. Vision-capable adapters override to append vision instructions.

Parameters
base_systemBase system prompt text.
has_visionWhether vision is available.
Returns
base_system unchanged. @dg_internal
Version
1.9.11

Reimplemented in entropic::Qwen35Adapter, and entropic::Qwen36Adapter.

Definition at line 591 of file adapter_base.cpp.

◆ format_tool_result()

Message entropic::ChatAdapter::format_tool_result ( const ToolCall tool_call,
const std::string &  result 
) const
virtual

Format a tool result as a user message.

Format tool result as user message (default).

Parameters
tool_callThe tool call that was executed.
resultTool execution result text.
Returns
Formatted message.
Version
1.8.2
Parameters
tool_callThe executed tool call.
resultExecution result text.
Returns
Formatted user message. @dg_internal
Version
1.8.2

Reimplemented in entropic::Nemotron3Adapter, entropic::Qwen35Adapter, and entropic::Qwen36Adapter.

Definition at line 121 of file adapter_base.cpp.

◆ format_tools()

std::string ChatAdapter::format_tools ( const std::vector< std::string > &  tool_jsons) const
virtual

Format tool definitions for injection into system prompt.

Default tool formatting: markdown headings + JSON schema.

Parameters
tool_jsonsTool definition JSON strings.
Returns
Adapter-formatted tool prompt string.
Version
2.0.4
Parameters
tool_jsonsTool definitions as JSON strings.
Returns
Formatted tool section string. @dg_internal
Version
1.8.2

Reimplemented in entropic::Nemotron3Adapter, entropic::Qwen35Adapter, and entropic::Qwen36Adapter.

Definition at line 535 of file adapter_base.cpp.

◆ is_response_complete()

bool entropic::ChatAdapter::is_response_complete ( const std::string &  content,
const std::vector< ToolCall > &  tool_calls 
) const

Check if response represents task completion.

Parameters
contentResponse content.
tool_callsParsed tool calls.
Returns
true if complete.
Version
1.8.2

Think-aware: think-only content without tool calls = still working.

Parameters
contentResponse content.
tool_callsParsed tool calls.
Returns
true if complete. @dg_internal
Version
1.8.2

Definition at line 145 of file adapter_base.cpp.

◆ parse_single_tool_call()

std::optional< ToolCall > ChatAdapter::parse_single_tool_call ( const std::string &  json_str) const
protected

Parse a single JSON tool call string.

Parameters
json_strJSON from tagged block.
Returns
Parsed ToolCall or nullopt.
Version
1.8.2
Parameters
json_strJSON string from tagged block.
Returns
Parsed ToolCall or nullopt. @dg_internal
Version
2.3.8

Definition at line 567 of file adapter_base.cpp.

◆ parse_tagged_tool_calls()

std::vector< ToolCall > entropic::ChatAdapter::parse_tagged_tool_calls ( const std::string &  content) const
protected

Parse <tool_call>JSON</tool_call> tagged blocks.

Version
1.8.2

Part of the ~80% the concrete base owns; concrete adapters override only their family specifics. Tolerates the asymmetric open tags real families emit (gemma4's pipe-prefixed <|tool_call> against a plain close).

Parameters
contentModel output content.
Returns
Vector of parsed tool calls; empty when no tagged block parses. @req REQ-INFER-012
Version
2.3.8

Definition at line 180 of file adapter_base.cpp.

◆ parse_tool_calls()

virtual ParseResult entropic::ChatAdapter::parse_tool_calls ( const std::string &  content) const
pure virtual

◆ strip_think_blocks()

std::string entropic::ChatAdapter::strip_think_blocks ( const std::string &  content) const

Strip this family's reasoning blocks from content (gh#108).

Uses thinking_markers(), so an adapter declares its markers once and both the buffered strip and the live StreamThinkFilter agree. Matching is plain substring, not regex — <|channel> contains a regex metacharacter and would need escaping.

An unclosed opening marker (generation hit its token budget mid-reasoning) erases to end of content and WARNs: no answer was ever produced, so returning the reasoning would be worse than nothing.

PUBLIC since v2.10.3 — was protected. The shared parse rule (response_parse.h) runs it on both the template and adapter branches, so it is an inference-layer concern now, not a subclass helper. Widening only; non-virtual, so no ABI change for existing consumers.

Parameters
contentModel output.
Returns
Content with reasoning blocks removed and trimmed.
Version
2.10.3

Driven by thinking_markers() so an adapter declares its delimiters once. Plain substring matching, not regex: gemma4's <|channel> contains a regex metacharacter and the previous std::regex form would have needed escaping to stay correct.

An opening marker with no close means the budget ran out mid-reasoning: the span is erased to end of content and a WARN is logged, because no answer was ever produced and returning the reasoning would be worse than nothing.

Parameters
contentModel output.
Returns
Content with this family's reasoning blocks removed and trimmed; unchanged when the family declares no markers. @req REQ-INFER-011
Version
2.11.0

Definition at line 426 of file adapter_base.cpp.

◆ thinking_markers()

virtual ThinkMarkers entropic::ChatAdapter::thinking_markers ( ) const
inlinevirtual

Parse tool calls from model output.

Parameters
contentRaw model output.
Returns
ParseResult with cleaned content and tool calls.
Version
1.8.2

This family's reasoning-block delimiters (gh#108).

Default is the <think> pair used by Qwen and Nemotron. Override in a family that wraps reasoning differently — Gemma-4 QAT does.

The single declaration point per family: both consumers read it — the buffered strip (strip_think_blocks) and the live StreamThinkFilter that wraps on_token in generate_streaming. Before v2.10.3 these were two independent hardcoded literals and gemma4 matched neither.

Returns
Marker pair; empty for a family that declares none, which makes both consumers no-ops. @req REQ-INFER-011
Version
2.10.3

Reimplemented in entropic::Gemma4Adapter.

Definition at line 167 of file adapter_base.h.

◆ try_recover_json()

std::optional< ToolCall > entropic::ChatAdapter::try_recover_json ( const std::string &  json_str) const
protected

Attempt JSON recovery on malformed tool call string.

Version
1.8.2

Member Data Documentation

◆ identity_prompt_

std::string entropic::ChatAdapter::identity_prompt_
protected

Assembled identity prompt.

Definition at line 269 of file adapter_base.h.

◆ tier_name_

std::string entropic::ChatAdapter::tier_name_
protected

Identity tier name.

Definition at line 268 of file adapter_base.h.

◆ tool_prefixes_

std::unordered_set<std::string> entropic::ChatAdapter::tool_prefixes_
mutableprotected

Known tool prefixes.

Definition at line 270 of file adapter_base.h.


The documentation for this class was generated from the following files: