Package src/vendors/cohere (molo::cohere): Chat v2 with streaming, tools, thinking and citations; embed, rerank, classify, tokenize, detokenize, parse, transcription; datasets, embed jobs, batches, connectors, fine-tuning, models, check-api-key; legacy v1 chat/generate/summarize; CohereChatModel : ChatModel. All 42 operations of cohere-openapi.yaml are in one call table, verified by tools/check_spec.py. Demo routes under /ai/cohere, 51 unit tests and 2 route tests; live tests are opt-in. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
69 lines
1.8 KiB
C++
69 lines
1.8 KiB
C++
#pragma once
|
|
|
|
// Chat v2 response and the streaming accumulator. Stream events are the
|
|
// `type` values of StreamedChatResponseV2 (message-start ... message-end).
|
|
|
|
#include <map>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "vendors/cohere/types.h"
|
|
|
|
namespace molo::cohere {
|
|
|
|
struct ToolCallRecord {
|
|
std::string id;
|
|
std::string name;
|
|
std::string arguments; // JSON text, as sent by the model
|
|
bool arguments_valid() const;
|
|
Json parsed() const; // {} when invalid
|
|
};
|
|
|
|
struct ChatResult {
|
|
std::string id;
|
|
std::string finish_reason; // COMPLETE STOP_SEQUENCE MAX_TOKENS TOOL_CALL ERROR TIMEOUT
|
|
std::string text; // text parts joined
|
|
std::string thinking; // thinking parts joined
|
|
std::string tool_plan;
|
|
std::vector<ToolCallRecord> tool_calls;
|
|
std::vector<Json> citations;
|
|
TokenUsage usage;
|
|
Json logprobs; // array or null
|
|
Json raw;
|
|
|
|
static ChatResult parse(const Json& doc);
|
|
// The assistant message to put back into the history for the next turn.
|
|
Json history_message() const;
|
|
};
|
|
|
|
struct StreamDelta {
|
|
std::string type; // event type, e.g. "content-delta"
|
|
std::string text;
|
|
std::string thinking;
|
|
std::string tool_plan;
|
|
int index = 0;
|
|
bool tool_start = false; // tool-call-start: id and name are set
|
|
std::string tool_id;
|
|
std::string tool_name;
|
|
std::string args_delta;
|
|
Json citation;
|
|
std::string finish_reason; // message-end
|
|
bool has_usage = false;
|
|
TokenUsage usage;
|
|
};
|
|
|
|
class ChatStreamAccumulator {
|
|
public:
|
|
// Throws llm::ApiError (502 STREAM_ERROR) on an error event or a message-end error.
|
|
StreamDelta feed(const Json& event);
|
|
bool done() const { return done_; }
|
|
ChatResult result() const;
|
|
|
|
private:
|
|
ChatResult result_;
|
|
std::map<int, ToolCallRecord> tools_;
|
|
bool done_ = false;
|
|
};
|
|
|
|
} // namespace molo::cohere
|