Skip to content

Commit

Permalink
Support summarization of a prompt
Browse files Browse the repository at this point in the history
Useful for saving.
  • Loading branch information
ksylvest committed Jul 19, 2024
1 parent d52fbef commit b934644
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
omniai (1.6.5)
omniai (1.6.6)
event_stream_parser
http
zeitwerk
Expand Down
8 changes: 8 additions & 0 deletions lib/omniai/chat/content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ module OmniAI
class Chat
# A placeholder for parts of a message. Any subclass must implement the serializable interface.
class Content
# @return [String]
def self.summarize(content)
return content.map { |entry| summarize(entry) }.join("\n\n") if content.is_a?(Array)
return content if content.is_a?(String)

content.summarize
end

# @param context [Context] optional
#
# @return [String]
Expand Down
5 changes: 5 additions & 0 deletions lib/omniai/chat/media.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ def initialize(type)
@type = type
end

# @return [String]
def summarize
"[#{filename}]"
end

# @return [Boolean]
def text?
@type.match?(%r{^text/})
Expand Down
8 changes: 8 additions & 0 deletions lib/omniai/chat/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ def inspect
"#<#{self.class} role=#{@role.inspect} content=#{@content.inspect}>"
end

# @return [String]
def summarize
<<~TEXT
#{@role}
#{Content.summarize(@content)}
TEXT
end

# Usage:
#
# Message.deserialize({ role: :user, content: 'Hello!' }) # => #<Message ...>
Expand Down
5 changes: 5 additions & 0 deletions lib/omniai/chat/prompt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ def inspect
"#<#{self.class.name} messages=#{@messages.inspect}>"
end

# @return [String]
def summarize
@messages.map(&:summarize).join("\n\n")
end

# Usage:
#
# prompt.serialize # => [{ content: "What is the capital of Canada?", role: :user }]
Expand Down
5 changes: 5 additions & 0 deletions lib/omniai/chat/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ def inspect
"#<#{self.class} text=#{@text.inspect}>"
end

# @return [String]
def summarize
@text
end

# @param data [Hash]
def self.deserialize(data, context: nil)
deserialize = context&.deserializers&.[](:text)
Expand Down
5 changes: 5 additions & 0 deletions lib/omniai/chat/url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ def initialize(uri, type = nil)
@uri = uri
end

# @return [String]
def summarize
"[#{filename}]"
end

# @return [String]
def inspect
"#<#{self.class} uri=#{@uri.inspect}>"
Expand Down
2 changes: 1 addition & 1 deletion lib/omniai/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module OmniAI
VERSION = '1.6.5'
VERSION = '1.6.6'
end
22 changes: 22 additions & 0 deletions spec/omniai/chat/content_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,28 @@
RSpec.describe OmniAI::Chat::Content do
subject(:content) { described_class.new }

describe '.summarize' do
subject(:summarize) { described_class.summarize(content) }

context 'with a string' do
let(:content) { 'Hello!' }

it { expect(summarize).to eq('Hello!') }
end

context 'with a content' do
let(:content) { OmniAI::Chat::Text.new('Hello!') }

it { expect(summarize).to eq('Hello!') }
end

context 'with an array' do
let(:content) { [OmniAI::Chat::Text.new('Hello!')] }

it { expect(summarize).to eq('Hello!') }
end
end

describe '#serialize' do
subject(:serialize) { content.serialize }

Expand Down
8 changes: 8 additions & 0 deletions spec/omniai/chat/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@
it { expect(file.inspect).to eql("#<OmniAI::Chat::File io=#{io.inspect}>") }
end

describe '#filename' do
it { expect(file.filename).to eql(File.basename(io)) }
end

describe '#summarize' do
it { expect(file.summarize).to eql("[#{file.filename}]") }
end

describe '#fetch!' do
it { expect(file.fetch!).to eql('Hello!') }
end
Expand Down
4 changes: 4 additions & 0 deletions spec/omniai/chat/prompt_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
it { expect(prompt.inspect).to eql('#<OmniAI::Chat::Prompt messages=[]>') }
end

describe '#summarize' do
it { expect(prompt.summarize).to eql('') }
end

describe '#message' do
context 'without some text or a block' do
it { expect { prompt.message }.to raise_error(ArgumentError, 'content or block is required') }
Expand Down
10 changes: 9 additions & 1 deletion spec/omniai/chat/url_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@
let(:type) { 'image/png' }
let(:uri) { 'https://localhost/hamster.png' }

describe '#url' do
describe '#inspect' do
it { expect(url.inspect).to eql('#<OmniAI::Chat::URL uri="https://localhost/hamster.png">') }
end

describe '#filename' do
it { expect(url.filename).to eql('hamster.png') }
end

describe '#summarize' do
it { expect(url.summarize).to eql('[hamster.png]') }
end

describe '#fetch!' do
before do
stub_request(:get, 'https://localhost/hamster.png')
Expand Down

0 comments on commit b934644

Please sign in to comment.