Class: OmniAI::Anthropic::Chat

Inherits:
Chat
  • Object
show all
Defined in:
lib/omniai/anthropic/chat.rb,
lib/omniai/anthropic/chat/response/stream.rb,
lib/omniai/anthropic/chat/response/completion.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.choice.message.content # '...'

Defined Under Namespace

Modules: Model, Response

Constant Summary collapse

DEFAULT_MODEL =
Model::CLAUDE_SONNET
MEDIA_SERIALIZER =

Examples:

media = Media.new(...)
MEDIA_SERIALIZER.call(media)

Returns:

  • (Hash)
lambda do |media, *|
  {
    type: media.kind, # i.e. 'image' / 'video' / 'audio' / ...
    source: {
      type: 'base64',
      media_type: media.type, # i.e. 'image/jpeg' / 'video/ogg' / 'audio/mpeg' / ...
      data: media.data,
    },
  }
end
CONTEXT =

Returns:

  • (Context)
Context.build do |context|
  context.serializers[:file] = MEDIA_SERIALIZER
  context.serializers[:url] = MEDIA_SERIALIZER
end

Instance Method Summary collapse

Instance Method Details

#messagesArray<Hash>

Returns:

  • (Array<Hash>)


65
66
67
68
# File 'lib/omniai/anthropic/chat.rb', line 65

def messages
  messages = @prompt.messages.filter(&:user?)
  messages.map { |message| message.serialize(context: CONTEXT) }
end

#pathString

Returns:

  • (String)


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

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

#payloadHash

Returns:

  • (Hash)


53
54
55
56
57
58
59
60
61
62
# File 'lib/omniai/anthropic/chat.rb', line 53

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)


71
72
73
74
# File 'lib/omniai/anthropic/chat.rb', line 71

def system
  messages = @prompt.messages.filter(&:system?)
  messages.map(&:content).join("\n\n") if messages.any?
end