Skip to content

Commit

Permalink
feat: add run
Browse files Browse the repository at this point in the history
  • Loading branch information
namchuai committed Jan 6, 2025
1 parent c4b370f commit 10fc488
Show file tree
Hide file tree
Showing 98 changed files with 3,315 additions and 151 deletions.
103 changes: 103 additions & 0 deletions engine/common/additional_message.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#pragma once

#include <optional>
#include <variant>
#include "common/message_attachment.h"
#include "common/message_attachment_factory.h"
#include "common/message_content.h"
#include "common/message_content_factory.h"
#include "common/message_role.h"
#include "common/variant_map.h"

namespace OpenAi {
struct AdditionalMessage {
AdditionalMessage() = default;

AdditionalMessage(const AdditionalMessage&) = delete;

AdditionalMessage& operator=(const AdditionalMessage&) = delete;

AdditionalMessage(AdditionalMessage&& other) noexcept
: role{std::move(other.role)},
content{std::move(other.content)},
attachments{std::move(other.attachments)},
metadata{std::move(other.metadata)} {}

AdditionalMessage& operator=(AdditionalMessage&& other) noexcept {
if (this != &other) {
role = std::move(other.role);
content = std::move(other.content);
attachments = std::move(other.attachments);
metadata = std::move(other.metadata);
}

return *this;
}

/**
* The role of the entity that is creating the message.
* Allowed values include: User or Assistant.
*/
Role role;

std::variant<std::string, std::vector<std::unique_ptr<OpenAi::Content>>>
content;

/**
* A list of files attached to the message, and the tools they were added to.
*/
std::optional<std::vector<Attachment>> attachments;

/**
* Set of 16 key-value pairs that can be attached to an object. This can be useful
* for storing additional information about the object in a structured format.
* Keys can be a maximum of 64 characters long and values can be a maximum of
* 512 characters long.
*/
std::optional<Cortex::VariantMap> metadata;

static cpp::result<AdditionalMessage, std::string> FromJson(
Json::Value&& json) {
try {
AdditionalMessage msg;
if (json.isMember("role") && json["role"].isString()) {
msg.role = RoleFromString(json["role"].asString());
}
if (!json.isMember("content")) {
return cpp::fail("content is mandatory");
}
if (json["content"].isString()) {
msg.content = std::move(json["content"].asString());
} else if (json["content"].isArray()) {
auto result = ParseContents(std::move(json["content"]));
if (result.has_error()) {
return cpp::fail("Failed to parse content array: " + result.error());
}
if (result.value().empty()) {
return cpp::fail("Content array cannot be empty");
}
msg.content = std::move(result.value());
} else {
return cpp::fail("content must be either a string or an array");
}

if (json.isMember("attachments")) {
msg.attachments =
ParseAttachments(std::move(json["attachments"])).value();
}
if (json.isMember("metadata") && json["metadata"].isObject() &&
!json["metadata"].empty()) {
auto res = Cortex::ConvertJsonValueToMap(json["metadata"]);
if (res.has_error()) {
CTL_WRN("Failed to convert metadata to map: " + res.error());
} else {
msg.metadata = res.value();
}
}
return msg;
} catch (const std::exception& e) {
return cpp::fail("FromJson failed: " + std::string(e.what()));
}
}
};
} // namespace OpenAi
2 changes: 1 addition & 1 deletion engine/common/api-dto/delete_success_response.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct DeleteSuccessResponse : JsonSerializable {
std::string object;
bool deleted;

cpp::result<Json::Value, std::string> ToJson() override {
cpp::result<Json::Value, std::string> ToJson() const override {
Json::Value json;
json["id"] = id;
json["object"] = object;
Expand Down
4 changes: 2 additions & 2 deletions engine/common/assistant.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct JanAssistant : JsonSerializable {

~JanAssistant() = default;

cpp::result<Json::Value, std::string> ToJson() override {
cpp::result<Json::Value, std::string> ToJson() const override {
try {
Json::Value json;

Expand Down Expand Up @@ -201,7 +201,7 @@ struct Assistant : JsonSerializable {

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

cpp::result<Json::Value, std::string> ToJson() override {
cpp::result<Json::Value, std::string> ToJson() const override {
try {
Json::Value root;

Expand Down
2 changes: 1 addition & 1 deletion engine/common/assistant_code_interpreter_tool.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct AssistantCodeInterpreterTool : public AssistantTool {
return std::move(tool);
}

cpp::result<Json::Value, std::string> ToJson() override {
cpp::result<Json::Value, std::string> ToJson() const override {
Json::Value json;
json["type"] = type;
return json;
Expand Down
6 changes: 3 additions & 3 deletions engine/common/assistant_file_search_tool.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct FileSearchRankingOption : public JsonSerializable {
return option;
}

cpp::result<Json::Value, std::string> ToJson() override {
cpp::result<Json::Value, std::string> ToJson() const override {
Json::Value json;
json["ranker"] = ranker;
json["score_threshold"] = score_threshold;
Expand Down Expand Up @@ -99,7 +99,7 @@ struct AssistantFileSearch : public JsonSerializable {
}
}

cpp::result<Json::Value, std::string> ToJson() override {
cpp::result<Json::Value, std::string> ToJson() const override {
Json::Value root;
root["max_num_results"] = max_num_results;
root["ranking_options"] = ranking_options.ToJson().value();
Expand Down Expand Up @@ -137,7 +137,7 @@ struct AssistantFileSearchTool : public AssistantTool {
}
}

cpp::result<Json::Value, std::string> ToJson() override {
cpp::result<Json::Value, std::string> ToJson() const override {
try {
Json::Value root;
root["type"] = type;
Expand Down
10 changes: 3 additions & 7 deletions engine/common/assistant_function_tool.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ struct AssistantFunction : public JsonSerializable {
return cpp::fail("Function name can't be empty");
}

if (!json.isMember("description")) {
return cpp::fail("Function description is mandatory");
}

if (!json.isMember("parameters")) {
return cpp::fail("Function parameters are mandatory");
}
Expand All @@ -76,14 +72,14 @@ struct AssistantFunction : public JsonSerializable {
if (json.isMember("strict")) {
is_strict = json["strict"].asBool();
}
AssistantFunction function{json["description"].asString(),
AssistantFunction function{json.get("description", "").asString(),
json["name"].asString(), json["parameters"],
is_strict};
function.parameters = json["parameters"];
return function;
}

cpp::result<Json::Value, std::string> ToJson() override {
cpp::result<Json::Value, std::string> ToJson() const override {
Json::Value json;
json["description"] = description;
json["name"] = name;
Expand Down Expand Up @@ -120,7 +116,7 @@ struct AssistantFunctionTool : public AssistantTool {
return AssistantFunctionTool{function_res.value()};
}

cpp::result<Json::Value, std::string> ToJson() override {
cpp::result<Json::Value, std::string> ToJson() const override {
Json::Value root;
root["type"] = type;
root["function"] = function.ToJson().value();
Expand Down
29 changes: 29 additions & 0 deletions engine/common/cortex/sync_queue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <json/value.h>
#include <condition_variable>
#include <mutex>
#include <queue>

// Status and result
using InferResult = std::pair<Json::Value, Json::Value>;

struct SyncQueue {
void push(InferResult&& p) {
std::unique_lock<std::mutex> l(mtx);
q.push(p);
cond.notify_one();
}

InferResult wait_and_pop() {
std::unique_lock<std::mutex> l(mtx);
cond.wait(l, [this] { return !q.empty(); });
auto res = q.front();
q.pop();
return res;
}

std::mutex mtx;
std::condition_variable cond;
std::queue<InferResult> q;
};
Loading

0 comments on commit 10fc488

Please sign in to comment.