Skip to content

Commit

Permalink
Add basic result spec
Browse files Browse the repository at this point in the history
  • Loading branch information
alchaplinsky committed Nov 24, 2023
1 parent 29a92e2 commit dff0ace
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 21 deletions.
45 changes: 26 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,28 @@ If bundler is not being used to manage dependencies, install the gem by executin
## Usage

Require it in you code:

```ruby
require 'gen_ai'
```



### Feature support
✅ - Supported | ❌ - Not supported | 🛠️ - Work in progress

✅ - Supported | ❌ - Not supported | 🛠️ - Work in progress

Language models capabilities

| Provider | Embedding | Completion | Conversation | Sentiment | Summarization |
| ---------------- | :-------: | :--------: | :----------: | :-------: | :-----------: |
| **OpenAI** |||| 🛠️ | 🛠️ |
| **Google Palm2** |||| 🛠️ | 🛠️ |

| **OpenAI** |||| 🛠️ | 🛠️ |
| **Google Palm2** |||| 🛠️ | 🛠️ |

Image generation model capabilities

| Provider | Generate | Variations | Edit | Upscale |
| ---------------- | :-------: | :--------: | :----------: | :-------: |
| **OpenAI** | | | | |
| **StabilityAI** | | | | |
| Provider | Generate | Variations | Edit | Upscale |
| --------------- | :------: | :--------: | :--: | :-----: |
| **OpenAI** | | | | |
| **StabilityAI** | | | | |

### Language

Expand Down Expand Up @@ -123,15 +122,18 @@ result = model.generate('A painting of a dog')
# => #<GenAI::Result:0x0000000110be6f20...>

result.value
# => Base64 encoded image
# => image binary

result.value(:base64)
# => image in base64

# Save image to file
File.open('dog.jpg', 'wb') do |f|
f.write(Base64.decode64(result.value))
f.write(result.value)
end
```
![dog](https://github.com/alchaplinsky/gen-ai/assets/695947/27a2af5d-530b-4966-94e8-6cdf628b6cac)

![dog](https://github.com/alchaplinsky/gen-ai/assets/695947/27a2af5d-530b-4966-94e8-6cdf628b6cac)

Get more **variations** of the same image

Expand All @@ -140,16 +142,19 @@ result = model.variations('./dog.jpg')
# => #<GenAI::Result:0x0000000116a1ec50...>

result.value
# => Base64 encoded image
# => image binary

result.value(:base64)
# => image in base64

# Save image to file
File.open('dog_variation.jpg', 'wb') do |f|
f.write(Base64.decode64(result.value))
f.write(result.value)
end

```
![dog_variation](https://github.com/alchaplinsky/gen-ai/assets/695947/977f5238-0114-4085-8e61-8f8b356ce308)

![dog_variation](https://github.com/alchaplinsky/gen-ai/assets/695947/977f5238-0114-4085-8e61-8f8b356ce308)

**Editing** existing images with additional prompt

Expand All @@ -158,18 +163,20 @@ result = model.edit('./llama.jpg', 'A cute llama wearing a beret', mask: './mask
# => #<GenAI::Result:0x0000000116a1ec50...>

result.value
# => Base64 encoded image
# => image binary

result.value(:base64)
# => image in base64

# Save image to file
File.open('dog_edited.jpg', 'wb') do |f|
f.write(Base64.decode64(result.value))
f.write(result.value)
end
```

![llama](https://github.com/alchaplinsky/gen-ai/assets/695947/9c862c6c-428e-463c-b935-ca749a6a33df)
![llama_edited](https://github.com/alchaplinsky/gen-ai/assets/695947/070d8e6a-07a0-4ed2-826f-8b9aabd183ae)


## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
Expand Down
11 changes: 9 additions & 2 deletions lib/gen_ai/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@ def initialize(provider:, model:, raw:, values:)
@values = values
end

def value
values.first
def value(format = :raw)
case format
when :raw
values.first
when :base64
Base64.encode64(values.first)
else
raise "Unsupported format: #{format}"
end
end

def prompt_tokens
Expand Down
35 changes: 35 additions & 0 deletions spec/result_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

RSpec.describe GenAI::Result do
subject do
described_class.new(provider: :open_ai, model: 'gpt-3.5-turbo', raw: {}, values: ['The capital of Turkey is Ankara.'])
end

describe '#value' do
it 'returns raw value' do
expect(subject.value).to eq('The capital of Turkey is Ankara.')
end

it 'returns base64 value' do
expect(subject.value(:base64)).to eq("VGhlIGNhcGl0YWwgb2YgVHVya2V5IGlzIEFua2FyYS4=\n")
end
end

describe '#prompt_tokens' do
it 'returns prompt tokens' do
expect(subject.prompt_tokens).to eq(nil)
end
end

describe '#completion_tokens' do
it 'returns completion tokens' do
expect(subject.completion_tokens).to eq(nil)
end
end

describe '#total_tokens' do
it 'returns total tokens' do
expect(subject.total_tokens).to eq(nil)
end
end
end

0 comments on commit dff0ace

Please sign in to comment.