Skip to content

Commit 55ca36d

Browse files
committed
feat: add run
1 parent c4b370f commit 55ca36d

File tree

103 files changed

+3716
-214
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+3716
-214
lines changed

Diff for: engine/common/additional_message.h

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#pragma once
2+
3+
#include <optional>
4+
#include <variant>
5+
#include "common/message_attachment.h"
6+
#include "common/message_attachment_factory.h"
7+
#include "common/message_content.h"
8+
#include "common/message_content_factory.h"
9+
#include "common/message_role.h"
10+
#include "common/variant_map.h"
11+
12+
namespace OpenAi {
13+
struct AdditionalMessage {
14+
AdditionalMessage() = default;
15+
16+
AdditionalMessage(const AdditionalMessage&) = delete;
17+
18+
AdditionalMessage& operator=(const AdditionalMessage&) = delete;
19+
20+
AdditionalMessage(AdditionalMessage&& other) noexcept
21+
: role{std::move(other.role)},
22+
content{std::move(other.content)},
23+
attachments{std::move(other.attachments)},
24+
metadata{std::move(other.metadata)} {}
25+
26+
AdditionalMessage& operator=(AdditionalMessage&& other) noexcept {
27+
if (this != &other) {
28+
role = std::move(other.role);
29+
content = std::move(other.content);
30+
attachments = std::move(other.attachments);
31+
metadata = std::move(other.metadata);
32+
}
33+
34+
return *this;
35+
}
36+
37+
/**
38+
* The role of the entity that is creating the message.
39+
* Allowed values include: User or Assistant.
40+
*/
41+
Role role;
42+
43+
std::variant<std::string, std::vector<std::unique_ptr<OpenAi::Content>>>
44+
content;
45+
46+
/**
47+
* A list of files attached to the message, and the tools they were added to.
48+
*/
49+
std::optional<std::vector<Attachment>> attachments;
50+
51+
/**
52+
* Set of 16 key-value pairs that can be attached to an object. This can be useful
53+
* for storing additional information about the object in a structured format.
54+
* Keys can be a maximum of 64 characters long and values can be a maximum of
55+
* 512 characters long.
56+
*/
57+
std::optional<Cortex::VariantMap> metadata;
58+
59+
static cpp::result<AdditionalMessage, std::string> FromJson(
60+
Json::Value&& json) {
61+
try {
62+
AdditionalMessage msg;
63+
if (json.isMember("role") && json["role"].isString()) {
64+
msg.role = RoleFromString(json["role"].asString());
65+
}
66+
if (!json.isMember("content")) {
67+
return cpp::fail("content is mandatory");
68+
}
69+
if (json["content"].isString()) {
70+
msg.content = std::move(json["content"].asString());
71+
} else if (json["content"].isArray()) {
72+
auto result = ParseContents(std::move(json["content"]));
73+
if (result.has_error()) {
74+
return cpp::fail("Failed to parse content array: " + result.error());
75+
}
76+
if (result.value().empty()) {
77+
return cpp::fail("Content array cannot be empty");
78+
}
79+
msg.content = std::move(result.value());
80+
} else {
81+
return cpp::fail("content must be either a string or an array");
82+
}
83+
84+
if (json.isMember("attachments")) {
85+
msg.attachments =
86+
ParseAttachments(std::move(json["attachments"])).value();
87+
}
88+
if (json.isMember("metadata") && json["metadata"].isObject() &&
89+
!json["metadata"].empty()) {
90+
auto res = Cortex::ConvertJsonValueToMap(json["metadata"]);
91+
if (res.has_error()) {
92+
CTL_WRN("Failed to convert metadata to map: " + res.error());
93+
} else {
94+
msg.metadata = res.value();
95+
}
96+
}
97+
return msg;
98+
} catch (const std::exception& e) {
99+
return cpp::fail("FromJson failed: " + std::string(e.what()));
100+
}
101+
}
102+
};
103+
} // namespace OpenAi

Diff for: engine/common/api-dto/delete_success_response.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ struct DeleteSuccessResponse : JsonSerializable {
88
std::string object;
99
bool deleted;
1010

11-
cpp::result<Json::Value, std::string> ToJson() override {
11+
cpp::result<Json::Value, std::string> ToJson() const override {
1212
Json::Value json;
1313
json["id"] = id;
1414
json["object"] = object;

Diff for: engine/common/assistant.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ struct JanAssistant : JsonSerializable {
2929

3030
~JanAssistant() = default;
3131

32-
cpp::result<Json::Value, std::string> ToJson() override {
32+
cpp::result<Json::Value, std::string> ToJson() const override {
3333
try {
3434
Json::Value json;
3535

@@ -201,7 +201,7 @@ struct Assistant : JsonSerializable {
201201

202202
std::variant<std::string, Json::Value> response_format;
203203

204-
cpp::result<Json::Value, std::string> ToJson() override {
204+
cpp::result<Json::Value, std::string> ToJson() const override {
205205
try {
206206
Json::Value root;
207207

Diff for: engine/common/assistant_code_interpreter_tool.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct AssistantCodeInterpreterTool : public AssistantTool {
2323
return std::move(tool);
2424
}
2525

26-
cpp::result<Json::Value, std::string> ToJson() override {
26+
cpp::result<Json::Value, std::string> ToJson() const override {
2727
Json::Value json;
2828
json["type"] = type;
2929
return json;

Diff for: engine/common/assistant_file_search_tool.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ struct FileSearchRankingOption : public JsonSerializable {
4242
return option;
4343
}
4444

45-
cpp::result<Json::Value, std::string> ToJson() override {
45+
cpp::result<Json::Value, std::string> ToJson() const override {
4646
Json::Value json;
4747
json["ranker"] = ranker;
4848
json["score_threshold"] = score_threshold;
@@ -99,7 +99,7 @@ struct AssistantFileSearch : public JsonSerializable {
9999
}
100100
}
101101

102-
cpp::result<Json::Value, std::string> ToJson() override {
102+
cpp::result<Json::Value, std::string> ToJson() const override {
103103
Json::Value root;
104104
root["max_num_results"] = max_num_results;
105105
root["ranking_options"] = ranking_options.ToJson().value();
@@ -137,7 +137,7 @@ struct AssistantFileSearchTool : public AssistantTool {
137137
}
138138
}
139139

140-
cpp::result<Json::Value, std::string> ToJson() override {
140+
cpp::result<Json::Value, std::string> ToJson() const override {
141141
try {
142142
Json::Value root;
143143
root["type"] = type;

Diff for: engine/common/assistant_function_tool.h

+3-7
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@ struct AssistantFunction : public JsonSerializable {
6464
return cpp::fail("Function name can't be empty");
6565
}
6666

67-
if (!json.isMember("description")) {
68-
return cpp::fail("Function description is mandatory");
69-
}
70-
7167
if (!json.isMember("parameters")) {
7268
return cpp::fail("Function parameters are mandatory");
7369
}
@@ -76,14 +72,14 @@ struct AssistantFunction : public JsonSerializable {
7672
if (json.isMember("strict")) {
7773
is_strict = json["strict"].asBool();
7874
}
79-
AssistantFunction function{json["description"].asString(),
75+
AssistantFunction function{json.get("description", "").asString(),
8076
json["name"].asString(), json["parameters"],
8177
is_strict};
8278
function.parameters = json["parameters"];
8379
return function;
8480
}
8581

86-
cpp::result<Json::Value, std::string> ToJson() override {
82+
cpp::result<Json::Value, std::string> ToJson() const override {
8783
Json::Value json;
8884
json["description"] = description;
8985
json["name"] = name;
@@ -120,7 +116,7 @@ struct AssistantFunctionTool : public AssistantTool {
120116
return AssistantFunctionTool{function_res.value()};
121117
}
122118

123-
cpp::result<Json::Value, std::string> ToJson() override {
119+
cpp::result<Json::Value, std::string> ToJson() const override {
124120
Json::Value root;
125121
root["type"] = type;
126122
root["function"] = function.ToJson().value();

Diff for: engine/common/cortex/sync_queue.h

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#pragma once
2+
3+
#include <json/value.h>
4+
#include <condition_variable>
5+
#include <mutex>
6+
#include <queue>
7+
8+
// Status and result
9+
using InferResult = std::pair<Json::Value, Json::Value>;
10+
11+
struct SyncQueue {
12+
void push(InferResult&& p) {
13+
std::unique_lock<std::mutex> l(mtx);
14+
q.push(p);
15+
cond.notify_one();
16+
}
17+
18+
InferResult wait_and_pop() {
19+
std::unique_lock<std::mutex> l(mtx);
20+
cond.wait(l, [this] { return !q.empty(); });
21+
auto res = q.front();
22+
q.pop();
23+
return res;
24+
}
25+
26+
std::mutex mtx;
27+
std::condition_variable cond;
28+
std::queue<InferResult> q;
29+
};

0 commit comments

Comments
 (0)