Files
moloapp/src/meta/chat_completions.h
T

115 lines
4.5 KiB
C++

#pragma once
// POST /chat/completions: the OpenAI-compatible messages-array endpoint for
// the Muse Spark and Muse Wonder text models. It does not carry reasoning
// across turns (reasoning_content is redacted for external keys); use the
// Responses API for that. Rules enforced before any traffic (docs:
// protocols/chat-completions): n is always 1, stop / logit_bias / prediction /
// verbosity / web_search_options / modalities are not offered, tool_choice is
// only "auto", reasoning_effort "none" is rejected, response_format and tools
// pass the schema pre-flight.
#include <functional>
#include <optional>
#include <string>
#include <vector>
#include "llm/api_client.h"
#include "meta/jsonx.h"
#include "meta/usage.h"
namespace molo::meta {
class ChatMessage {
public:
static Json developer(const std::string& text);
static Json system(const std::string& text); // same level as developer
static Json user(const std::string& text);
static Json user_parts(const std::vector<Json>& parts);
static Json assistant(const std::string& text);
static Json assistant_tool_calls(const std::string& text, const std::vector<Json>& tool_calls);
static Json tool_result(const std::string& tool_call_id, const std::string& content);
// Content parts.
static Json text_part(const std::string& text);
static Json image_part(const std::string& url, const std::string& detail = "");
static Json video_part(const std::string& url, double fps = 0);
static Json file_part(const std::string& filename, const std::string& file_data_url,
const std::string& file_id = "");
static Json audio_part(const std::string& format, const std::string& base64_data);
// {id, type:"function", function:{name, arguments}}
static Json tool_call(const std::string& id, const std::string& name,
const std::string& arguments_json);
// Tool definition: {type:"function", function:{name, description, parameters, strict}}
static Json function_tool(const std::string& name, const std::string& description,
const Json& parameters, bool strict = false);
};
struct ChatCompletionRequest {
std::string model;
std::vector<Json> messages;
std::vector<Json> tools;
Json response_format; // null, or {type: json_object | json_schema, json_schema:{...}}
std::string reasoning_effort; // minimal..max
std::optional<int> max_completion_tokens;
std::optional<double> temperature;
std::optional<double> top_p;
std::optional<double> frequency_penalty;
std::optional<double> presence_penalty;
std::optional<long> seed;
std::optional<bool> parallel_tool_calls;
std::string safety_identifier;
std::string prompt_cache_key;
std::string prompt_cache_retention;
std::string service_tier;
bool include_usage = true; // stream_options.include_usage when streaming
std::vector<std::pair<std::string, std::string>> metadata;
Json extra;
void validate() const;
Json to_json(bool stream = false) const;
static Json format_json_object();
static Json format_json_schema(const std::string& name, const Json& schema, bool strict = false,
const std::string& description = "");
};
struct ChatToolCallState {
long index = 0;
std::string id, name, arguments;
};
// Folds chat.completion.chunk objects into text, reasoning_content, tool calls,
// finish_reason and usage.
class ChatChunkAccumulator {
public:
void feed(const Json& chunk);
const std::string& text() const { return text_; }
const std::string& reasoning() const { return reasoning_; }
const std::string& finish_reason() const { return finish_reason_; }
const std::string& id() const { return id_; }
const std::string& model() const { return model_; }
const std::vector<ChatToolCallState>& tool_calls() const { return calls_; }
bool has_usage() const { return has_usage_; }
MetaUsage usage() const { return usage_; }
private:
std::string text_, reasoning_, finish_reason_, id_, model_;
std::vector<ChatToolCallState> calls_;
MetaUsage usage_;
bool has_usage_ = false;
};
class ChatCompletionsService : public llm::Service {
public:
using ChunkSink = std::function<bool(const Json&)>; // false: stop
explicit ChatCompletionsService(std::shared_ptr<llm::ApiClient> client)
: llm::Service(std::move(client)) {}
Json create(const ChatCompletionRequest& request);
ChatChunkAccumulator stream(const ChatCompletionRequest& request,
const ChunkSink& sink = nullptr);
};
} // namespace molo::meta