#pragma once // Batch API for the Agent API: upload a JSONL file (purpose=batch), create a // batch against /v1/responses, poll it, download the output / error files. // 24h completion window is the only value. Batches and files are separate // services because the files also carry the output and error files. #include #include #include #include "llm/api_client.h" #include "vendors/perplexity/types.h" namespace molo::perplexity { struct BatchFileInfo { std::string id, filename, purpose; // purpose: batch | batch_output long bytes = 0, created_at = 0, expires_at = 0; bool deleted = false; // only set by remove() static BatchFileInfo parse(const Json& doc); }; struct BatchFileList { std::vector files; std::string first_id, last_id; bool has_more = false; bool truncated = false; // a walk cap stopped the listing }; struct BatchInfo { std::string id, endpoint, input_file_id, output_file_id, error_file_id, completion_window, status; long created_at = 0, in_progress_at = 0, expires_at = 0, finalizing_at = 0, completed_at = 0, failed_at = 0, expired_at = 0, cancelling_at = 0, cancelled_at = 0; long total = 0, completed = 0, failed = 0; // request_counts long input_tokens = 0, output_tokens = 0, total_tokens = 0; struct Error { std::string code, message; long line = 0; }; std::vector errors; std::map metadata; Json raw; static BatchInfo parse(const Json& doc); // validating, in_progress, finalizing, cancelling are active; the rest are final. bool finished() const; }; struct BatchList { std::vector batches; std::string first_id, last_id; bool has_more = false; bool truncated = false; }; // One request line of a batch input file: {"custom_id", "method", "url", "body"} // is the OpenAI-compatible shape; the exact line schema is not in the OpenAPI // document (see SPEC Unverified), so lines are only checked to be JSON objects. struct BatchInput { std::string filename = "batch.jsonl"; std::string jsonl; void validate() const; // <= 200 MB, <= 50,000 lines, each line a JSON object }; class BatchFiles : public llm::Service { public: explicit BatchFiles(std::shared_ptr client) : llm::Service(std::move(client)) {} BatchFileInfo upload(const BatchInput& input); // order: "", asc, desc; purpose: "", batch, batch_output; after: cursor id. BatchFileList list(int limit = 0, const std::string& after = "", const std::string& order = "", const std::string& purpose = ""); BatchFileInfo get(const std::string& file_id); // Destructive, no default. Batches already created from the file are not affected. BatchFileInfo remove(const std::string& file_id); std::string content(const std::string& file_id); }; class Batches : public llm::Service { public: explicit Batches(std::shared_ptr client) : llm::Service(std::move(client)) {} // idempotency_key: "" omits the header (<= 200 chars). metadata: <= 16 pairs, keys <= 64, values <= 512. BatchInfo create(const std::string& input_file_id, const std::map& metadata = {}, const std::string& idempotency_key = ""); BatchList list(int limit = 0, const std::string& after = ""); BatchInfo get(const std::string& batch_id); // Destructive, no default: moves the batch to cancelling. BatchInfo cancel(const std::string& batch_id); }; } // namespace molo::perplexity