Class: OmniAI::Anthropic::Chat

Inherits:
Chat
  • Object
show all
Defined in:
lib/omniai/anthropic/chat.rb,
lib/omniai/anthropic/chat/stream.rb,
lib/omniai/anthropic/chat/text_serializer.rb,
lib/omniai/anthropic/chat/tool_serializer.rb,
lib/omniai/anthropic/chat/media_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/payload_serializer.rb,
lib/omniai/anthropic/chat/function_serializer.rb,
lib/omniai/anthropic/chat/tool_call_serializer.rb,
lib/omniai/anthropic/chat/tool_call_result_serializer.rb

Overview

An Anthropic chat implementation.

Usage:

completion = OmniAI::Anthropic::Chat.process!(client: client) do |prompt|
  prompt.system('You are an expert in the field of AI.')
  prompt.user('What are the biggest risks of AI?')
end
completion.text # '...'

Defined Under Namespace

Modules: ChoiceSerializer, ContentSerializer, FunctionSerializer, MediaSerializer, MessageSerializer, Model, PayloadSerializer, TextSerializer, ToolCallResultSerializer, ToolCallSerializer, ToolSerializer Classes: Stream

Constant Summary collapse

DEFAULT_MODEL =
Model::CLAUDE_SONNET
CONTEXT =

Returns:

  • (Context)
Context.build do |context|
  context.serializers[:tool] = ToolSerializer.method(:serialize)

  context.serializers[:file] = MediaSerializer.method(:serialize)
  context.serializers[:url] = MediaSerializer.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[:payload] = PayloadSerializer.method(:deserialize)
end

Instance Method Summary collapse

Instance Method Details

#messagesArray<Hash>

Returns:

  • (Array<Hash>)


78
79
80
81
# File 'lib/omniai/anthropic/chat.rb', line 78

def messages
  messages = @prompt.messages.reject(&:system?)
  messages.map { |message| message.serialize(context:) }
end

#pathString

Returns:

  • (String)


92
93
94
# File 'lib/omniai/anthropic/chat.rb', line 92

def path
  "/#{Client::VERSION}/messages"
end

#payloadHash

Returns:

  • (Hash)


66
67
68
69
70
71
72
73
74
75
# File 'lib/omniai/anthropic/chat.rb', line 66

def payload
  OmniAI::Anthropic.config.chat_options.merge({
    model: @model,
    messages:,
    system:,
    stream: @stream.nil? ? nil : !@stream.nil?,
    temperature: @temperature,
    tools: tools_payload,
  }).compact
end

#systemString?

Returns:

  • (String, nil)


84
85
86
87
88
89
# File 'lib/omniai/anthropic/chat.rb', line 84

def system
  messages = @prompt.messages.filter(&:system?)
  return if messages.empty?

  messages.filter(&:text?).map(&:text).join("\n\n")
end