From 1ca035182f20afe8845dfbbc502615bf41963814 Mon Sep 17 00:00:00 2001 From: BlackyDrum Date: Sat, 19 Oct 2024 19:47:40 +0200 Subject: [PATCH 1/5] Add cpp-20 support --- .github/workflows/tests.yml | 12 +++++++++++- CMakeLists.txt | 2 +- include/ChromaDB/Client/Client.h | 1 + src/ChromaDB/Client/Client.cpp | 2 +- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index a05574c..7977b8a 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -34,8 +34,18 @@ jobs: - name: Initialize submodules run: git submodule update --init --recursive + - name: Install GCC 13 + run: | + sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y + sudo apt-get update + sudo apt-get install -y gcc-13 g++-13 + sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 60 + sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-13 60 + gcc --version + g++ --version + - name: Install dependencies - run: sudo apt-get update && sudo apt-get install -y cmake libssl-dev + run: sudo apt-get install -y cmake libssl-dev - name: Configure CMake run: cmake -S . -B build diff --git a/CMakeLists.txt b/CMakeLists.txt index 66300ee..d953997 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.10) project(chromadb VERSION 0.4.3) set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) -set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED True) if (MSVC) diff --git a/include/ChromaDB/Client/Client.h b/include/ChromaDB/Client/Client.h index aa5eb65..33b84b8 100644 --- a/include/ChromaDB/Client/Client.h +++ b/include/ChromaDB/Client/Client.h @@ -27,6 +27,7 @@ #include #include #include +#include namespace chromadb { diff --git a/src/ChromaDB/Client/Client.cpp b/src/ChromaDB/Client/Client.cpp index cdb51d0..5648631 100644 --- a/src/ChromaDB/Client/Client.cpp +++ b/src/ChromaDB/Client/Client.cpp @@ -418,7 +418,7 @@ namespace chromadb { std::vector Client::Query(const Collection& collection, const std::vector& queryTexts, const std::vector>& queryEmbeddings, size_t nResults, const std::vector& include, const nlohmann::json& where_document, const nlohmann::json& where) { if (collection.GetIsDeleted()) - throw ChromaInvalidCollectionException("Collection " + collection.GetName() + " has already been deleted"); + throw ChromaInvalidCollectionException(std::format("Collection {} has already been deleted", collection.GetName())); if (!((!queryEmbeddings.empty() && queryTexts.empty()) || (queryEmbeddings.empty() && !queryTexts.empty()) || (queryEmbeddings.empty() && queryTexts.empty()))) throw ChromaInvalidArgumentException("You must provide only one of queryEmbeddings or queryTexts"); From a4ee56efed0578bcc68dd92c3826272cfc3f44c0 Mon Sep 17 00:00:00 2001 From: BlackyDrum Date: Sat, 19 Oct 2024 19:51:39 +0200 Subject: [PATCH 2/5] Update cpp version in readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b307eee..adc047e 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ This guide will help you build and install the ChromaDB library and run an examp ### Prerequisites - CMake (version 3.10 or higher) -- A C++ compiler (supporting C++17) +- A C++ compiler (supporting C++20) - OpenSSL development libraries ### Building ChromaDB Library From 23a5b424c8f6b49773075c0c4c51eaf46fd21f68 Mon Sep 17 00:00:00 2001 From: BlackyDrum Date: Sat, 19 Oct 2024 20:14:58 +0200 Subject: [PATCH 3/5] Replace string concatenation with std:format --- include/ChromaDB/Client/ChromaApiClient.h | 1 + include/ChromaDB/Resources/Collection.h | 1 + src/ChromaDB/Client/ChromaApiClient.cpp | 2 +- src/ChromaDB/Client/Client.cpp | 36 +++++++++++------------ src/ChromaDB/Resources/Collection.cpp | 2 +- 5 files changed, 22 insertions(+), 20 deletions(-) diff --git a/include/ChromaDB/Client/ChromaApiClient.h b/include/ChromaDB/Client/ChromaApiClient.h index 9836816..79a0ea6 100644 --- a/include/ChromaDB/Client/ChromaApiClient.h +++ b/include/ChromaDB/Client/ChromaApiClient.h @@ -10,6 +10,7 @@ #include "Json/json.h" #include +#include namespace chromadb { diff --git a/include/ChromaDB/Resources/Collection.h b/include/ChromaDB/Resources/Collection.h index 8335a0e..7ada347 100644 --- a/include/ChromaDB/Resources/Collection.h +++ b/include/ChromaDB/Resources/Collection.h @@ -7,6 +7,7 @@ #include #include #include +#include namespace chromadb { diff --git a/src/ChromaDB/Client/ChromaApiClient.cpp b/src/ChromaDB/Client/ChromaApiClient.cpp index 891cae8..c92ef3e 100644 --- a/src/ChromaDB/Client/ChromaApiClient.cpp +++ b/src/ChromaDB/Client/ChromaApiClient.cpp @@ -5,7 +5,7 @@ namespace chromadb { ChromaApiClient::ChromaApiClient(const std::string& scheme, const std::string& host, const std::string& port, const std::string& authToken) : m_Scheme(scheme), m_Host(host), m_Port(port), m_AuthToken(authToken) { - m_BaseUrl = m_Scheme + "://" + m_Host + ":" + m_Port; + m_BaseUrl = std::format("{}://{}:{}", m_Scheme, m_Host, m_Port); } nlohmann::json ChromaApiClient::Get(const std::string& endpoint) diff --git a/src/ChromaDB/Client/Client.cpp b/src/ChromaDB/Client/Client.cpp index 5648631..c2509a0 100644 --- a/src/ChromaDB/Client/Client.cpp +++ b/src/ChromaDB/Client/Client.cpp @@ -12,7 +12,7 @@ namespace chromadb { { try { - m_ChromaApiClient.Get("/tenants/" + m_Tenant); + m_ChromaApiClient.Get(std::format("/tenants/{}", m_Tenant)); } catch (ChromaException) { @@ -21,7 +21,7 @@ namespace chromadb { try { - m_ChromaApiClient.Get("/databases/" + m_Database + "?tenant=" + m_Tenant); + m_ChromaApiClient.Get(std::format("/databases/{}?tenant={}", m_Database, m_Tenant)); } catch (ChromaException) { @@ -53,7 +53,7 @@ namespace chromadb { try { - m_ChromaApiClient.Post("/databases?tenant=" + m_Tenant, json); + m_ChromaApiClient.Post(std::format("/databases?tenant={}", m_Tenant), json); } catch (ChromaException& e) { @@ -115,7 +115,7 @@ namespace chromadb { try { - m_ChromaApiClient.Post("/collections?tenant=" + m_Tenant + "&database=" + m_Database, json); + m_ChromaApiClient.Post(std::format("/collections?tenant={}&database={}", m_Tenant, m_Database), json); } catch (ChromaException& e) { @@ -129,7 +129,7 @@ namespace chromadb { { try { - auto json = m_ChromaApiClient.Get("/collections/" + name + "?tenant=" + m_Tenant + "&database=" + m_Database); + auto json = m_ChromaApiClient.Get(std::format("/collections/{}?tenant={}&database={}", name, m_Tenant, m_Database)); std::unordered_map metadata; if (!json["metadata"].is_null()) @@ -159,7 +159,7 @@ namespace chromadb { { try { - auto json = m_ChromaApiClient.Get("/collections?tenant=" + m_Tenant + "&database=" + m_Database); + auto json = m_ChromaApiClient.Get(std::format("/collections?tenant={}&database={}", m_Tenant, m_Database)); std::vector collections; for (const auto& collection : json) @@ -183,7 +183,7 @@ namespace chromadb { { try { - return m_ChromaApiClient.Get("/count_collections?tenant=" + m_Tenant + "&database=" + m_Database); + return m_ChromaApiClient.Get(std::format("/count_collections?tenant={}&database={}", m_Tenant, m_Database)); } catch (ChromaException& e) { @@ -195,7 +195,7 @@ namespace chromadb { { try { - m_ChromaApiClient.Delete("/collections/" + collection.GetName() + "?tenant=" + m_Tenant + "&database=" + m_Database); + m_ChromaApiClient.Delete(std::format("/collections/{}?tenant={}&database={}", collection.GetName(), m_Tenant, m_Database)); collection.m_IsDeleted = true; } @@ -223,7 +223,7 @@ namespace chromadb { try { - m_ChromaApiClient.Put("/collections/" + oldCollection.GetId() + "?tenant=" + m_Tenant + "&database=" + m_Database, json); + m_ChromaApiClient.Put(std::format("/collections/{}?tenant={}?&database={}", oldCollection.GetId(), m_Tenant, m_Database), json); } catch (ChromaException& e) { @@ -252,7 +252,7 @@ namespace chromadb { try { - m_ChromaApiClient.Post("/collections/" + collection.GetId() + "/add", json); + m_ChromaApiClient.Post(std::format("/collections/{}/add", collection.GetId()), json); } catch (ChromaException& e) { @@ -279,7 +279,7 @@ namespace chromadb { try { - m_ChromaApiClient.Post("/collections/" + collection.GetId() + "/update", json); + m_ChromaApiClient.Post(std::format("/collections/{}/update", collection.GetId()), json); } catch (ChromaException& e) { @@ -306,7 +306,7 @@ namespace chromadb { try { - m_ChromaApiClient.Post("/collections/" + collection.GetId() + "/upsert", json); + m_ChromaApiClient.Post(std::format("/collections/{}/upsert", collection.GetId()), json); } catch (ChromaException& e) { @@ -318,7 +318,7 @@ namespace chromadb { { try { - return m_ChromaApiClient.Get("/collections/" + collection.GetId() + "/count"); + return m_ChromaApiClient.Get(std::format("/collections/{}/count", collection.GetId())); } catch (ChromaInvalidCollectionException& e) { @@ -346,7 +346,7 @@ namespace chromadb { try { - m_ChromaApiClient.Post("/collections/" + collection.GetId() + "/delete", json); + m_ChromaApiClient.Post(std::format("/collections/{}/delete", collection.GetId()), json); } catch (ChromaInvalidCollectionException& e) { @@ -381,7 +381,7 @@ namespace chromadb { try { - auto response = m_ChromaApiClient.Post("/collections/" + collection.GetId() + "/get", json); + auto response = m_ChromaApiClient.Post(std::format("/collections/{}/get", collection.GetId()), json); std::vector results; for (size_t i = 0; i < response["ids"].size(); i++) @@ -457,7 +457,7 @@ namespace chromadb { try { - auto response = m_ChromaApiClient.Post("/collections/" + collection.GetId() + "/query", json); + auto response = m_ChromaApiClient.Post(std::format("/collections/{}/query", collection.GetId()), json); std::vector queryResponses; for (size_t i = 0; i < response["ids"].size(); i++) @@ -513,7 +513,7 @@ namespace chromadb { Client::ValidationResult Client::Validate(const Collection& collection, const std::vector& ids, const std::vector>& embeddings, const std::vector>& metadata, const std::vector& documents, bool requireEmbeddingsOrDocuments) { if (collection.GetIsDeleted()) - throw ChromaInvalidCollectionException("Collection " + collection.GetName() + " has already been deleted"); + throw ChromaInvalidCollectionException(std::format("Collection {} has already been deleted", collection.GetName())); if (requireEmbeddingsOrDocuments) { @@ -557,7 +557,7 @@ namespace chromadb { duplicates.insert(id); } - throw ChromaInvalidArgumentException("Expected IDs to be unique, found duplicates for: " + Utils::Join(duplicates, ", ")); + throw ChromaInvalidArgumentException(std::format("Expected IDs to be unique, found duplicates for: {}", Utils::Join(duplicates, ", "))); } return { validatedIds, finalEmbeddings, metadata, documents }; diff --git a/src/ChromaDB/Resources/Collection.cpp b/src/ChromaDB/Resources/Collection.cpp index a541b92..0011637 100644 --- a/src/ChromaDB/Resources/Collection.cpp +++ b/src/ChromaDB/Resources/Collection.cpp @@ -10,7 +10,7 @@ namespace chromadb { void Collection::CheckDeleted() const { if (m_IsDeleted) - throw ChromaInvalidCollectionException("Collection " + m_Name + " has already been deleted"); + throw ChromaInvalidCollectionException(std::format("Collection {} has already been deleted", m_Name)); } } // namespace chromadb \ No newline at end of file From 447006846718c72a311121640d6f344b974de7df Mon Sep 17 00:00:00 2001 From: BlackyDrum Date: Sat, 19 Oct 2024 20:23:15 +0200 Subject: [PATCH 4/5] Rename function --- README.md | 6 +++--- include/ChromaDB/Embeddings/EmbeddingFunction.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index adc047e..4cbbda1 100644 --- a/README.md +++ b/README.md @@ -529,7 +529,7 @@ int main() - **path**: (Optional) The path of the endpoint for generating embeddings. Defaults to `/v1/embeddings`. ### Getting Additional Metadata from Embedding Requests -If you generated embeddings using an embedding function that calls a provider (e.g., OpenAI or Jina), you can retrieve additional information about the request using the `embeddingFunction->GetLastRequestAdditionalMetadata()` method. This function returns specific metadata for a provider. +If you generated embeddings using an embedding function that calls a provider (e.g., OpenAI or Jina), you can retrieve additional information about the request using the `embeddingFunction->GetRequestMetadata()` method. This function returns specific metadata for a provider. ```cpp #include "ChromaDB/ChromaDB.h" @@ -545,7 +545,7 @@ int main() client.AddEmbeddings(collection, ids, {}, {}, documents); - std::cout << embeddingFunction->GetLastRequestAdditionalMetadata() << std::endl; + std::cout << embeddingFunction->GetRequestMetadata() << std::endl; // Example output (specific for the Jina API): // { // "model": "jina-embeddings-v2-base-en", @@ -557,7 +557,7 @@ int main() // } // Access specific fields in the additional metadata - std::cout << embeddingFunction->GetLastRequestAdditionalMetadata()["usage"]["prompt_tokens"] << std::endl; // 8 + std::cout << embeddingFunction->GetRequestMetadata()["usage"]["prompt_tokens"] << std::endl; // 8 } ``` diff --git a/include/ChromaDB/Embeddings/EmbeddingFunction.h b/include/ChromaDB/Embeddings/EmbeddingFunction.h index 234f083..03633ad 100644 --- a/include/ChromaDB/Embeddings/EmbeddingFunction.h +++ b/include/ChromaDB/Embeddings/EmbeddingFunction.h @@ -20,7 +20,7 @@ namespace chromadb { virtual std::vector> Generate(const std::vector& documents) = 0; - nlohmann::json GetLastRequestAdditionalMetadata() const { return m_LastRequestAdditionalMetadata; } + nlohmann::json GetRequestMetadata() const { return m_LastRequestAdditionalMetadata; } protected: std::string m_BaseUrl; std::string m_Path; From 7c7a868f159b9b407a58d1910a302e9bda4040b2 Mon Sep 17 00:00:00 2001 From: BlackyDrum Date: Sat, 19 Oct 2024 20:32:01 +0200 Subject: [PATCH 5/5] Prepare 1.0.0 release --- CHANGELOG.md | 6 ++++++ CMakeLists.txt | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 147cbff..0cd6f02 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ All notable changes to this project will be documented in this file. +## [1.0.0] - 2024-10-19 +### Changed +- Updated the C++ standard from C++17 to C++20. +- Renamed the function `GetLastRequestAdditionalMetadata()` to `GetRequestMetadata()` for clarity. +- Replaced string concatenation with `std::format` for more readable formatting. + ## [0.4.3] - 2024-10-11 ### Changed - Replaced `push_back` with `emplace_back` diff --git a/CMakeLists.txt b/CMakeLists.txt index d953997..6921982 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.10) -project(chromadb VERSION 0.4.3) +project(chromadb VERSION 1.0.0) set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) set(CMAKE_CXX_STANDARD 20)