2023-08-09 02:01:48 -04:00
|
|
|
#frozen_string_literal: true
|
|
|
|
|
2024-01-04 08:44:07 -05:00
|
|
|
RSpec.describe DiscourseAi::AiBot::Tools::Image do
|
|
|
|
subject(:tool) { described_class.new({ prompts: prompts, seeds: [99, 32] }) }
|
|
|
|
|
2024-01-29 14:04:25 -05:00
|
|
|
let(:llm) { DiscourseAi::Completions::Llm.proxy("open_ai:gpt-3.5-turbo") }
|
2024-01-04 08:44:07 -05:00
|
|
|
let(:progress_blk) { Proc.new {} }
|
|
|
|
|
2023-10-23 02:00:58 -04:00
|
|
|
let(:bot_user) { User.find(DiscourseAi::AiBot::EntryPoint::GPT3_5_TURBO_ID) }
|
2024-01-04 08:44:07 -05:00
|
|
|
|
|
|
|
let(:prompts) { ["a pink cow", "a red cow"] }
|
2023-10-23 02:00:58 -04:00
|
|
|
|
|
|
|
before { SiteSetting.ai_bot_enabled = true }
|
2023-08-09 02:01:48 -04:00
|
|
|
|
|
|
|
describe "#process" do
|
|
|
|
it "can generate correct info" do
|
|
|
|
post = Fabricate(:post)
|
|
|
|
|
|
|
|
SiteSetting.ai_stability_api_url = "https://api.stability.dev"
|
|
|
|
SiteSetting.ai_stability_api_key = "abc"
|
|
|
|
|
|
|
|
image =
|
|
|
|
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="
|
|
|
|
|
2023-10-26 23:48:12 -04:00
|
|
|
artifacts = [{ base64: image, seed: 99 }]
|
|
|
|
|
|
|
|
WebMock
|
|
|
|
.stub_request(
|
|
|
|
:post,
|
|
|
|
"https://api.stability.dev/v1/generation/#{SiteSetting.ai_stability_engine}/text-to-image",
|
|
|
|
)
|
|
|
|
.with do |request|
|
|
|
|
json = JSON.parse(request.body, symbolize_names: true)
|
|
|
|
expect(prompts).to include(json[:text_prompts][0][:text])
|
|
|
|
true
|
|
|
|
end
|
|
|
|
.to_return(status: 200, body: { artifacts: artifacts }.to_json)
|
2023-08-09 02:01:48 -04:00
|
|
|
|
2024-01-04 08:44:07 -05:00
|
|
|
info = tool.invoke(bot_user, llm, &progress_blk).to_json
|
2023-08-09 02:01:48 -04:00
|
|
|
|
2023-10-31 04:12:25 -04:00
|
|
|
expect(JSON.parse(info)).to eq("prompts" => ["a pink cow", "a red cow"], "seeds" => [99, 99])
|
2024-01-04 08:44:07 -05:00
|
|
|
expect(tool.custom_raw).to include("upload://")
|
|
|
|
expect(tool.custom_raw).to include("[grid]")
|
|
|
|
expect(tool.custom_raw).to include("a pink cow")
|
|
|
|
expect(tool.custom_raw).to include("a red cow")
|
2023-08-09 02:01:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|