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>)


69
70
71
72
# File 'lib/omniai/anthropic/chat.rb', line 69

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

#pathString

Returns:

  • (String)


83
84
85
# File 'lib/omniai/anthropic/chat.rb', line 83

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

#payloadHash

Returns:

  • (Hash)


57
58
59
60
61
62
63
64
65
66
# File 'lib/omniai/anthropic/chat.rb', line 57

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)


75
76
77
78
79
80
# File 'lib/omniai/anthropic/chat.rb', line 75

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

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