pulsatrix 1.1.0
Loading...
Searching...
No Matches
message.hpp
Go to the documentation of this file.
1
5#pragma once
6
7#include <cstdint>
8#include <optional>
9#include <string>
10#include <vector>
11
12#include "pulsatrix/json.hpp"
13
14namespace pulsatrix::orch {
15
18 enum class Type {
19 Text,
20 ToolUse,
22 Thinking,
23 Opaque,
24 };
25
28 std::string text;
30 std::string id;
32 std::string name;
36 bool is_error = false;
43 std::string origin;
44
45 [[nodiscard]] static ContentBlock Text(std::string text);
46 [[nodiscard]] static ContentBlock ToolUse(std::string id, std::string name, JsonValue input);
47 [[nodiscard]] static ContentBlock ToolResult(std::string tool_use_id, std::string content, bool is_error = false);
48};
49
51struct Message {
52 enum class Role { User, Assistant };
53
55 std::vector<ContentBlock> content;
56
57 [[nodiscard]] static Message User(std::string text);
58 [[nodiscard]] static Message Assistant(std::string text);
59
61 [[nodiscard]] std::string text() const;
63 [[nodiscard]] std::vector<const ContentBlock*> tool_uses() const;
64};
65
67struct ToolSpec {
68 std::string name;
69 std::string description;
74 bool strict = false;
75};
76
78enum class StopReason {
79 EndTurn,
80 ToolUse,
81 MaxTokens,
83 PauseTurn,
84 Refusal,
85 Other,
86};
87
89struct Usage {
90 int64_t input_tokens = 0;
91 int64_t output_tokens = 0;
94
95 Usage& operator+=(const Usage& other);
96};
97
100 std::string model;
101 std::string system;
102 std::vector<Message> messages;
103 std::vector<ToolSpec> tools;
104 int64_t max_tokens = 4096;
105 std::optional<double> temperature;
109 bool allow_tool_calls = true;
110};
111
121
123[[nodiscard]] bool IsValidToolName(const std::string& name);
124
125} // namespace pulsatrix::orch
One JSON value: null, a boolean, a number, a string, an array or an object.
Definition json.hpp:22
A small strict JSON value, parser and deterministic writer (RFC 8259).
Definition http.hpp:20
StopReason
Why the model stopped.
Definition message.hpp:78
@ EndTurn
it finished its answer
@ StopSequence
it produced a stop sequence
@ ToolUse
it called tools and waits for their results
@ Other
a reason this version doesn't know; see ChatResponse::stop_reason_text
@ PauseTurn
the provider paused a long turn; send the conversation back to continue
@ MaxTokens
it reached max_tokens
bool IsValidToolName(const std::string &name)
Whether name is a valid tool name (^[a-zA-Z0-9_-]{1,128}$).
A request to a provider.
Definition message.hpp:99
std::string system
Definition message.hpp:101
std::vector< Message > messages
Definition message.hpp:102
bool allow_tool_calls
Whether the model may call the tools. False sends them anyway, for the cache, and tells the model not...
Definition message.hpp:109
int64_t max_tokens
Definition message.hpp:104
std::string model
Definition message.hpp:100
std::optional< double > temperature
Definition message.hpp:105
std::vector< ToolSpec > tools
Definition message.hpp:103
A provider's answer.
Definition message.hpp:113
Message message
always an assistant message
Definition message.hpp:114
StopReason stop_reason
Definition message.hpp:115
std::string stop_reason_text
the provider's own word for it, for example end_turn
Definition message.hpp:116
Usage usage
Definition message.hpp:117
std::string request_id
for a support ticket, when the provider sends one
Definition message.hpp:119
std::string model
the model that answered, as the provider reports it
Definition message.hpp:118
One block of a message's content.
Definition message.hpp:17
static ContentBlock Text(std::string text)
bool is_error
Whether a tool result reports a failure.
Definition message.hpp:36
Type
Definition message.hpp:18
@ ToolUse
the model calls a tool: id, name and input
@ ToolResult
the answer to a ToolUse: id (the call's), text and is_error
@ Thinking
the model's reasoning: text, plus the provider's block in raw
@ Opaque
anything else the provider sent: raw only
JsonValue input
A tool call's input, a JSON object.
Definition message.hpp:34
Type type
Definition message.hpp:26
static ContentBlock ToolUse(std::string id, std::string name, JsonValue input)
std::string origin
Definition message.hpp:43
std::string text
Text, a tool result's content, or thinking.
Definition message.hpp:28
std::string name
A tool call's tool name.
Definition message.hpp:32
static ContentBlock ToolResult(std::string tool_use_id, std::string content, bool is_error=false)
std::string id
A tool call's id, or for a result the id of the call it answers.
Definition message.hpp:30
JsonValue raw
For Thinking and Opaque blocks, the block exactly as the provider sent it, and the provider's name in...
Definition message.hpp:42
One turn of a conversation.
Definition message.hpp:51
static Message Assistant(std::string text)
Role
Definition message.hpp:52
static Message User(std::string text)
std::vector< const ContentBlock * > tool_uses() const
The tool calls, in order.
std::vector< ContentBlock > content
Definition message.hpp:55
std::string text() const
The text blocks joined, without thinking or tool calls.
Role role
Definition message.hpp:54
A tool the model may call. ToolRegistry::specs() makes these from its tools.
Definition message.hpp:67
JsonValue input_schema
a JSON Schema object
Definition message.hpp:70
bool strict
Ask the provider to guarantee inputs that match the schema (Anthropic's strict tool use)....
Definition message.hpp:74
std::string name
letters, digits, _ and -, 1 to 128 characters
Definition message.hpp:68
std::string description
Definition message.hpp:69
Tokens used by one request, or summed over a tool loop.
Definition message.hpp:89
Usage & operator+=(const Usage &other)
int64_t cache_creation_input_tokens
Definition message.hpp:92
int64_t output_tokens
Definition message.hpp:91
int64_t input_tokens
Definition message.hpp:90
int64_t cache_read_input_tokens
Definition message.hpp:93