Files
moloapp/src/meta/images.h
T

77 lines
2.7 KiB
C++

#pragma once
// Muse Image over the single-shot endpoints: POST /images/generations and
// POST /images/edits (JSON body with an `images` array, or multipart with
// image / image[N] parts). $0.01 per generated image, so the demo treats both
// as paid. Rules (docs: image-generation): n 1..10, aspect ratio at most 16:1,
// moderation is not enabled on muse-image-1.0 (any value is a 400), mask /
// input_fidelity / partial_images are rejected, built-in search is on unless
// tool_enablement turns a tool off. A `web_search` tool passed to Muse Image
// is an error; it does its own searching.
#include <functional>
#include <optional>
#include <string>
#include <vector>
#include "llm/api_client.h"
#include "meta/jsonx.h"
namespace molo::meta {
struct ImageRequest {
std::string model = "muse-image-1.0";
std::string prompt;
std::optional<int> n; // 1..10
std::string size; // "" | auto | WxH
std::string response_format; // b64_json (default) | url
std::string output_format; // e.g. webp
std::string reasoning_strength; // low | high (default high)
std::string user;
std::optional<bool> enable_image_search;
std::optional<bool> enable_web_search;
std::optional<bool> enable_shell;
std::vector<Json> images; // edits: {image_url} or {file_id}
Json extra;
void validate(bool edit) const;
Json to_json(bool edit, bool stream = false) const;
static Json image_url(const std::string& url);
static Json image_file(const std::string& file_id);
};
struct GeneratedImage {
std::string b64_json;
std::string url;
std::string revised_prompt;
};
struct ImageResult {
long created = 0;
std::vector<GeneratedImage> data;
std::string output_format;
Json usage;
static ImageResult from_json(const Json& j);
};
// Parses "WxH" and checks the 16:1 aspect limit; "auto" and "" pass.
bool valid_image_size(const std::string& size);
class ImagesService : public llm::Service {
public:
using EventSink = std::function<bool(const std::string& event, const Json& data)>;
explicit ImagesService(std::shared_ptr<llm::ApiClient> client)
: llm::Service(std::move(client)) {}
ImageResult generate(const ImageRequest& request);
ImageResult edit(const ImageRequest& request);
// Multipart edit (what the OpenAI SDK sends): one `image[N]` part per file.
ImageResult edit_multipart(const ImageRequest& request,
const std::vector<std::pair<std::string, std::string>>& files_mime_bytes);
// Streams image_generation.completed / image_edit.completed events.
void stream(const ImageRequest& request, bool edit, const EventSink& sink);
};
} // namespace molo::meta