Class: OmniAI::Anthropic::Chat
- Inherits:
-
Chat
- Object
- Chat
- OmniAI::Anthropic::Chat
- Defined in:
- lib/omniai/anthropic/chat.rb,
lib/omniai/anthropic/chat/stream.rb,
lib/omniai/anthropic/chat/url_serializer.rb,
lib/omniai/anthropic/chat/file_serializer.rb,
lib/omniai/anthropic/chat/text_serializer.rb,
lib/omniai/anthropic/chat/tool_serializer.rb,
lib/omniai/anthropic/chat/choice_serializer.rb,
lib/omniai/anthropic/chat/content_serializer.rb,
lib/omniai/anthropic/chat/message_serializer.rb,
lib/omniai/anthropic/chat/function_serializer.rb,
lib/omniai/anthropic/chat/response_serializer.rb,
lib/omniai/anthropic/chat/thinking_serializer.rb,
lib/omniai/anthropic/chat/tool_call_serializer.rb,
lib/omniai/anthropic/chat/tool_call_result_serializer.rb
Overview
Defined Under Namespace
Modules: ChoiceSerializer, ContentSerializer, FileSerializer, FunctionSerializer, MessageSerializer, Model, ResponseSerializer, TextSerializer, ThinkingSerializer, ToolCallResultSerializer, ToolCallSerializer, ToolSerializer, URLSerializer Classes: Stream
Constant Summary collapse
- DEFAULT_MODEL =
Model::CLAUDE_SONNET
- CONTEXT =
Context.build do |context| context.serializers[:tool] = ToolSerializer.method(:serialize) context.serializers[:file] = FileSerializer.method(:serialize) context.serializers[:url] = URLSerializer.method(:serialize) context.serializers[:choice] = ChoiceSerializer.method(:serialize) context.deserializers[:choice] = ChoiceSerializer.method(:deserialize) context.serializers[:tool_call] = ToolCallSerializer.method(:serialize) context.deserializers[:tool_call] = ToolCallSerializer.method(:deserialize) context.serializers[:tool_call_result] = ToolCallResultSerializer.method(:serialize) context.deserializers[:tool_call_result] = ToolCallResultSerializer.method(:deserialize) context.serializers[:function] = FunctionSerializer.method(:serialize) context.deserializers[:function] = FunctionSerializer.method(:deserialize) context.serializers[:message] = MessageSerializer.method(:serialize) context.deserializers[:message] = MessageSerializer.method(:deserialize) context.deserializers[:content] = ContentSerializer.method(:deserialize) context.deserializers[:response] = ResponseSerializer.method(:deserialize) context.serializers[:thinking] = ThinkingSerializer.method(:serialize) context.deserializers[:thinking] = ThinkingSerializer.method(:deserialize) end
Instance Method Summary collapse
- #messages ⇒ Array<Hash>
- #path ⇒ String
-
#payload ⇒ Hash
NOTE: Anthropic requires temperature=1 (default) when thinking is enabled, so temperature is omitted from the payload when thinking_config is present.
- #system ⇒ String?
-
#thinking_config ⇒ Hash?
Translates unified thinking option to Anthropic’s native format.
-
#thinking_max_tokens ⇒ Integer?
Returns max_tokens ensuring it’s greater than budget_tokens when thinking is enabled.
Instance Method Details
#messages ⇒ Array<Hash>
140 141 142 143 |
# File 'lib/omniai/anthropic/chat.rb', line 140 def = @prompt..reject(&:system?) .map { || .serialize(context:) } end |
#path ⇒ String
155 156 157 |
# File 'lib/omniai/anthropic/chat.rb', line 155 def path "/#{Client::VERSION}/messages" end |
#payload ⇒ Hash
NOTE: Anthropic requires temperature=1 (default) when thinking is enabled, so temperature is omitted from the payload when thinking_config is present.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/omniai/anthropic/chat.rb', line 92 def payload OmniAI::Anthropic.config. .merge({ model: @model, messages:, system:, stream: stream? || nil, temperature: thinking_config ? nil : @temperature, tools: tools_payload, thinking: thinking_config, }) .merge({ max_tokens: thinking_max_tokens, output_config: }.compact) .compact end |
#system ⇒ String?
146 147 148 149 150 151 152 |
# File 'lib/omniai/anthropic/chat.rb', line 146 def system parts = @prompt..filter(&:system?).filter(&:text?).map(&:text) parts << formatting if formatting? return if parts.empty? parts.join("\n\n") end |
#thinking_config ⇒ Hash?
Translates unified thinking option to Anthropic’s native format. Example: ‘thinking: { budget_tokens: 10000 }` becomes `{ type: “enabled”, budget_tokens: 10000 }` Example: `thinking: { effort: nil }` becomes `{ type: “adaptive” }` (Claude decides) Example: `thinking: { effort: “medium” }` becomes `{ type: “adaptive” }` + output_config
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/omniai/anthropic/chat.rb', line 112 def thinking_config return @thinking_config if defined?(@thinking_config) thinking = @options[:thinking] @thinking_config = case thinking when true then { type: "enabled", budget_tokens: 10_000 } when Hash if thinking.key?(:effort) { type: "adaptive" } else { type: "enabled" }.merge(thinking) end end end |
#thinking_max_tokens ⇒ Integer?
Returns max_tokens ensuring it’s greater than budget_tokens when thinking is enabled.
130 131 132 133 134 135 136 137 |
# File 'lib/omniai/anthropic/chat.rb', line 130 def thinking_max_tokens budget = thinking_config&.dig(:budget_tokens) return unless budget base = @options[:max_tokens] || OmniAI::Anthropic.config.[:max_tokens] || 0 # Ensure max_tokens > budget_tokens (default to budget + 8000 for response) [base, budget + 8_000].max end |