Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 157 additions & 0 deletions engine/common/assistant.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
#pragma once

#include <string>
#include "common/assistant_tool.h"
#include "common/thread_tool_resources.h"
#include "common/variant_map.h"
#include "utils/result.hpp"

namespace OpenAi {
// Deprecated. After jan's migration, we should remove this struct
struct JanAssistant : JsonSerializable {
std::string id;

std::string name;

std::string object = "assistant";

uint32_t created_at;

Json::Value tools;

Json::Value model;

std::string instructions;

~JanAssistant() = default;

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

json["id"] = id;
json["name"] = name;
json["object"] = object;
json["created_at"] = created_at;

json["tools"] = tools;
json["model"] = model;
json["instructions"] = instructions;

return json;
} catch (const std::exception& e) {
return cpp::fail(std::string("ToJson failed: ") + e.what());
}
}

static cpp::result<JanAssistant, std::string> FromJson(Json::Value&& json) {
if (json.empty()) {
return cpp::fail("Empty JSON");
}

JanAssistant assistant;
if (json.isMember("assistant_id")) {
assistant.id = json["assistant_id"].asString();
} else {
assistant.id = json["id"].asString();
}

if (json.isMember("assistant_name")) {
assistant.name = json["assistant_name"].asString();
} else {
assistant.name = json["name"].asString();
}
assistant.object = "assistant";
assistant.created_at = 0; // Jan does not have this
if (json.isMember("tools")) {
assistant.tools = json["tools"];
}
if (json.isMember("model")) {
assistant.model = json["model"];
}
assistant.instructions = json["instructions"].asString();

return assistant;
}
};

struct Assistant {
/**
* The identifier, which can be referenced in API endpoints.
*/
std::string id;

/**
* The object type, which is always assistant.
*/
std::string object = "assistant";

/**
* The Unix timestamp (in seconds) for when the assistant was created.
*/
uint64_t created_at;

/**
* The name of the assistant. The maximum length is 256 characters.
*/
std::optional<std::string> name;

/**
* The description of the assistant. The maximum length is 512 characters.
*/
std::optional<std::string> description;

/**
* ID of the model to use. You can use the List models API to see all of
* your available models, or see our Model overview for descriptions of them.
*/
std::string model;

/**
* The system instructions that the assistant uses. The maximum length is
* 256,000 characters.
*/
std::optional<std::string> instructions;

/**
* A list of tool enabled on the assistant. There can be a maximum of 128
* tools per assistant. Tools can be of types code_interpreter, file_search,
* or function.
*/
std::vector<std::unique_ptr<AssistantTool>> tools;

/**
* A set of resources that are used by the assistant's tools. The resources
* are specific to the type of tool. For example, the code_interpreter tool
* requires a list of file IDs, while the file_search tool requires a list
* of vector store IDs.
*/
std::optional<std::variant<ThreadCodeInterpreter, ThreadFileSearch>>
tool_resources;

/**
* 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.
*/
Cortex::VariantMap metadata;

/**
* What sampling temperature to use, between 0 and 2. Higher values like
* 0.8 will make the output more random, while lower values like 0.2 will
* make it more focused and deterministic.
*/
std::optional<float> temperature;

/**
* An alternative to sampling with temperature, called nucleus sampling,
* where the model considers the results of the tokens with top_p
* probability mass. So 0.1 means only the tokens comprising the top 10%
* probability mass are considered.
*
* We generally recommend altering this or temperature but not both.
*/
std::optional<float> top_p;
};
} // namespace OpenAi
91 changes: 91 additions & 0 deletions engine/common/assistant_tool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#pragma once

#include <optional>
#include <string>

namespace OpenAi {
struct AssistantTool {
std::string type;

AssistantTool(const std::string& type) : type{type} {}

virtual ~AssistantTool() = default;
};

struct AssistantCodeInterpreterTool : public AssistantTool {
AssistantCodeInterpreterTool() : AssistantTool{"code_interpreter"} {}

~AssistantCodeInterpreterTool() = default;
};

struct AssistantFileSearchTool : public AssistantTool {
AssistantFileSearchTool() : AssistantTool("file_search") {}

~AssistantFileSearchTool() = default;

/**
* The ranking options for the file search. If not specified,
* the file search tool will use the auto ranker and a score_threshold of 0.
*
* See the file search tool documentation for more information.
*/
struct RankingOption {
/**
* The ranker to use for the file search. If not specified will use the auto ranker.
*/
std::string ranker;

/**
* The score threshold for the file search. All values must be a
* floating point number between 0 and 1.
*/
float score_threshold;
};

/**
* Overrides for the file search tool.
*/
struct FileSearch {
/**
* The maximum number of results the file search tool should output.
* The default is 20 for gpt-4* models and 5 for gpt-3.5-turbo.
* This number should be between 1 and 50 inclusive.
*
* Note that the file search tool may output fewer than max_num_results results.
* See the file search tool documentation for more information.
*/
int max_num_result;
};
};

struct AssistantFunctionTool : public AssistantTool {
AssistantFunctionTool() : AssistantTool("function") {}

~AssistantFunctionTool() = default;

struct Function {
/**
* A description of what the function does, used by the model to choose
* when and how to call the function.
*/
std::string description;

/**
* The name of the function to be called. Must be a-z, A-Z, 0-9, or contain
* underscores and dashes, with a maximum length of 64.
*/
std::string name;

// TODO: namh handle parameters

/**
* Whether to enable strict schema adherence when generating the function call.
* If set to true, the model will follow the exact schema defined in the parameters
* field. Only a subset of JSON Schema is supported when strict is true.
*
* Learn more about Structured Outputs in the function calling guide.
*/
std::optional<bool> strict;
};
};
} // namespace OpenAi
23 changes: 23 additions & 0 deletions engine/common/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <json/reader.h>
#include <json/value.h>
#include <json/writer.h>
#include "common/assistant.h"
#include "common/thread_tool_resources.h"
#include "common/variant_map.h"
#include "json_serializable.h"
Expand Down Expand Up @@ -47,6 +48,9 @@ struct Thread : JsonSerializable {
*/
Cortex::VariantMap metadata;

// For supporting Jan
std::optional<std::vector<JanAssistant>> assistants;

static cpp::result<Thread, std::string> FromJson(const Json::Value& json) {
Thread thread;

Expand Down Expand Up @@ -90,6 +94,25 @@ struct Thread : JsonSerializable {
}
}

if (json.isMember("title") && !json["title"].isNull()) {
thread.metadata["title"] = json["title"].asString();
}

if (json.isMember("assistants") && json["assistants"].isArray()) {
std::vector<JanAssistant> assistants;
for (Json::ArrayIndex i = 0; i < json["assistants"].size(); ++i) {
Json::Value assistant_json = json["assistants"][i];
auto assistant_result =
JanAssistant::FromJson(std::move(assistant_json));
if (assistant_result.has_error()) {
return cpp::fail("Failed to parse assistant: " +
assistant_result.error());
}
assistants.push_back(std::move(assistant_result.value()));
}
thread.assistants = std::move(assistants);
}

return thread;
}

Expand Down
3 changes: 0 additions & 3 deletions engine/config/model_config.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
#pragma once

#include <json/json.h>
#include <cmath>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <limits>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
#include "utils/format_utils.h"
#include "utils/remote_models_utils.h"
#include "yaml-cpp/yaml.h"

namespace config {

Expand Down
Loading