diff --git a/source/w3cipa/CMake/FindLog4cplus.cmake b/source/w3cipa/CMake/FindLog4cplus.cmake index 813d96b..c0e5fdf 100644 --- a/source/w3cipa/CMake/FindLog4cplus.cmake +++ b/source/w3cipa/CMake/FindLog4cplus.cmake @@ -36,6 +36,8 @@ find_path(LOG4CPLUS_INCLUDE_DIR /opt/local/include /opt/csw/include /opt/include + /include + /{_OUTPUT_NAME}/include $ENV{LOG4CPLUS_DIR}/include $ENV{LOG4CPLUS_ROOT}/include ${LOG4CPLUS_DIR}/include @@ -48,6 +50,7 @@ find_path(LOG4CPLUS_INCLUDE_DIR find_library(LOG4CPLUS_LIBRARY NAMES log4cplus + log4cplusD PATHS /usr/local /usr @@ -55,6 +58,8 @@ find_library(LOG4CPLUS_LIBRARY /opt/local /opt/csw /opt + /lib + ${W3CIPA_OPEN_SOURCE_SRC}/lib $ENV{LOG4CPLUS_DIR}/lib $ENV{LOG4CPLUS_ROOT}/lib ${LOG4CPLUS_DIR}/lib diff --git a/source/w3cipa/CMakeLists.txt b/source/w3cipa/CMakeLists.txt index 3c8bcb1..dbd6ded 100644 --- a/source/w3cipa/CMakeLists.txt +++ b/source/w3cipa/CMakeLists.txt @@ -54,7 +54,7 @@ endif() # find_package(PkgConfig REQUIRED) -find_package(Log4cplus REQUIRED ${W3CIPA_OPEN_SOURCE_SRC}) +find_package(Log4cplus MODULE REQUIRED) # On Windows we are relying on VCPKG to download CURL. if (WIN32) find_package(CURL MODULE REQUIRED) @@ -68,12 +68,13 @@ find_package(stduuid REQUIRED PATHS ${W3CIPA_OPEN_SOURCE_SRC}) # Add settings to the build envionment # include_directories(${LOG4CPLUS_INCLUDE_DIR}) -set(Log4cplus ${LOG4CPLUS_LIBRARIES}) -set(REQUIRED_LIBS ${REQUIRED_LIBS} ${Log4cplus}) +set(REQUIRED_LIBS ${REQUIRED_LIBS} ${LOG4CPLUS_LIBRARY}) set(REQUIRED_LIBS ${REQUIRED_LIBS} CURL::libcurl) set(REQUIRED_LIBS ${REQUIRED_LIBS} nlohmann_json::nlohmann_json) +message(STATUS "REQUIRED_LIBS: ${REQUIRED_LIBS}") + include_directories(${STDUUID_INCLUDE_DIR}) if (BUILD_W3CIPA_DOC) diff --git a/source/w3cipa/w3cipachatgptipaprovider/CMakeLists.txt b/source/w3cipa/w3cipachatgptipaprovider/CMakeLists.txt index c28b3f3..28e08c3 100644 --- a/source/w3cipa/w3cipachatgptipaprovider/CMakeLists.txt +++ b/source/w3cipa/w3cipachatgptipaprovider/CMakeLists.txt @@ -19,6 +19,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) set(HEADERS + include/w3c/voiceinteraction/ipa/reference/external/ipa/chatgpt/ChatGPTConfiguration.h include/w3c/voiceinteraction/ipa/reference/external/ipa/chatgpt/ChatGPTIPAProvider.h include/w3c/voiceinteraction/ipa/reference/external/ipa/chatgpt/ChatGPTMessage.h ) diff --git a/source/w3cipa/w3cipachatgptipaprovider/include/w3c/voiceinteraction/ipa/reference/external/ipa/chatgpt/ChatGPTConfiguration.h b/source/w3cipa/w3cipachatgptipaprovider/include/w3c/voiceinteraction/ipa/reference/external/ipa/chatgpt/ChatGPTConfiguration.h new file mode 100644 index 0000000..25a89af --- /dev/null +++ b/source/w3cipa/w3cipachatgptipaprovider/include/w3c/voiceinteraction/ipa/reference/external/ipa/chatgpt/ChatGPTConfiguration.h @@ -0,0 +1,46 @@ +/* + * IPA Reference Implementation: https://github.com/w3c/voiceinteraction + * + * Copyright (C) 2024 World Wide Web Consortium. All Rights Reserved. + * + * This work is distributed under the W3C Software and Document License [1] + * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even + * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * [1] https://www.w3.org/Consortium/Legal/copyright-software + */ + +#ifndef CHATGPTCONFIGURATION_H +#define CHATGPTCONFIGURATION_H + +#include + +namespace w3c { +namespace voiceinteraction { +namespace ipa { +namespace reference { +namespace external { +namespace ipa { +namespace chatgpt { + +struct ChatGPTConfiguration { + std::string endpoint; + std::string key; + std::string systemMessage; +}; + +void from_json(const nlohmann::json& j, ChatGPTConfiguration& config) { + j.at("endpoint").get_to(config.endpoint); + j.at("key").get_to(config.key); + j.at("systemMessage").get_to(config.systemMessage); +} + +} // chatgpt +} // ipa +} // external +} // namespace reference +} // ipa +} // voiceinteraction +} // w3c + +#endif // CHATGPTCONFIGURATION_H diff --git a/source/w3cipa/w3cipachatgptipaprovider/include/w3c/voiceinteraction/ipa/reference/external/ipa/chatgpt/ChatGPTIPAProvider.h b/source/w3cipa/w3cipachatgptipaprovider/include/w3c/voiceinteraction/ipa/reference/external/ipa/chatgpt/ChatGPTIPAProvider.h index bb003ec..ce5e036 100644 --- a/source/w3cipa/w3cipachatgptipaprovider/include/w3c/voiceinteraction/ipa/reference/external/ipa/chatgpt/ChatGPTIPAProvider.h +++ b/source/w3cipa/w3cipachatgptipaprovider/include/w3c/voiceinteraction/ipa/reference/external/ipa/chatgpt/ChatGPTIPAProvider.h @@ -34,9 +34,12 @@ namespace chatgpt { class ChatGPTIPAProvider : public IPAProvider { public: ChatGPTIPAProvider(); + virtual ~ChatGPTIPAProvider() { } + void initialize() override; + const std::shared_ptr processInput( const std::shared_ptr& request) override; @@ -51,7 +54,12 @@ class ChatGPTIPAProvider : public IPAProvider { private: /** Languages supported by this provider. */ std::list supportedLanguages; - + /** The ChatGPT endpoint */ + std::string endpoint; + /** The ChatGPT API key */ + std::string key; + /** The default system message. */ + std::string systemMessage; /** Id of this IP provider. */ const static std::string ID; /** Logger instance. */ diff --git a/source/w3cipa/w3cipachatgptipaprovider/src/w3c/voiceinteraction/ipa/reference/external/ipa/chatgpt/ChatGPTIPAProvider.cpp b/source/w3cipa/w3cipachatgptipaprovider/src/w3c/voiceinteraction/ipa/reference/external/ipa/chatgpt/ChatGPTIPAProvider.cpp index 9f5898d..a78770c 100644 --- a/source/w3cipa/w3cipachatgptipaprovider/src/w3c/voiceinteraction/ipa/reference/external/ipa/chatgpt/ChatGPTIPAProvider.cpp +++ b/source/w3cipa/w3cipachatgptipaprovider/src/w3c/voiceinteraction/ipa/reference/external/ipa/chatgpt/ChatGPTIPAProvider.cpp @@ -10,6 +10,8 @@ * [1] https://www.w3.org/Consortium/Legal/copyright-software */ +#include + #include #include #include @@ -20,6 +22,7 @@ #include "w3c/voiceinteraction/ipa/reference/TextMultiModalData.h" #include "w3c/voiceinteraction/ipa/reference/external/ipa/chatgpt/ChatGPTIPAProvider.h" +#include "w3c/voiceinteraction/ipa/reference/external/ipa/chatgpt/ChatGPTConfiguration.h" #include "w3c/voiceinteraction/ipa/reference/external/ipa/chatgpt/ChatGPTMessage.h" namespace w3c { @@ -58,6 +61,20 @@ ChatGPTIPAProvider::ChatGPTIPAProvider() { Language::ZH }; } +void ChatGPTIPAProvider::initialize() { + std::string configFile = "config"; + configFile += std::filesystem::path::preferred_separator; + configFile += "ChatGPTIPAProvider.json"; + std::ifstream file(configFile); + nlohmann::json json = nlohmann::json::parse(file); + ChatGPTConfiguration configuration = json; + endpoint = configuration.endpoint; + key = configuration.key; + systemMessage = configuration.systemMessage; + LOG4CPLUS_INFO_FMT(LOGGER, + LOG4CPLUS_TEXT("ChatGPT IPA provider initialized")); +} + const std::list ChatGPTIPAProvider::getSupportedModalityTypes() const { std::list types = { TextModalityType() }; return types; @@ -82,8 +99,9 @@ const std::shared_ptr ChatGPTIPAProvider::processInput( } // Set the header and API key struct curl_slist *headers = NULL; - headers = curl_slist_append(headers, - "Authorization: Bearer OPENAI-DEVELOPER-KEY"); + std::string authorization = "Authorization: Bearer "; + authorization += key; + headers = curl_slist_append(headers, authorization.c_str()); headers = curl_slist_append( headers, "Content-Type: application/json"); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); @@ -91,10 +109,10 @@ const std::shared_ptr ChatGPTIPAProvider::processInput( // TODO Remove this disabling of SSL verification curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); +// curl_easy_setopt(curl, CURLOPT_CAINFO, "config/cacert.pem"); // Set the URL to the OpenAI API endpoint - std::string apiUrl = "https://api.openai.com/v1/chat/completions"; - curl_easy_setopt(curl, CURLOPT_URL, apiUrl.c_str()); + curl_easy_setopt(curl, CURLOPT_URL, endpoint.c_str()); // Set the callback function for libcurl std::string response; @@ -103,9 +121,8 @@ const std::shared_ptr ChatGPTIPAProvider::processInput( // Set the payload ChatGPTJSONRequest req; - req.model = std::string("gpt-3.5-turbo"); - ChatGPTMessage systemMessage {"system", - "You are a standards maniac."}; + req.model = std::string("gpt-4o-mini"); + ChatGPTMessage actualSystemMessage{"system", systemMessage.c_str()}; std::shared_ptr multiModalInputs = request->getMultiModalInputs(); std::shared_ptr input = @@ -114,7 +131,7 @@ const std::shared_ptr ChatGPTIPAProvider::processInput( std::dynamic_pointer_cast(input); const std::string& text = textInput->getText(); ChatGPTMessage userMessage { "user", text }; - req.messages = std::vector({ systemMessage, userMessage }); + req.messages = std::vector({actualSystemMessage, userMessage}); req.temperature = 1; req.top_p = 1; req.max_tokens = 256; diff --git a/source/w3cipa/w3cipaframework/CMakeLists.txt b/source/w3cipa/w3cipaframework/CMakeLists.txt index 73a0a56..8b3cd75 100644 --- a/source/w3cipa/w3cipaframework/CMakeLists.txt +++ b/source/w3cipa/w3cipaframework/CMakeLists.txt @@ -117,7 +117,7 @@ if (BUILD_W3CIPA_DOC) COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_OUT} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} COMMENT "Generating API documentation with Doxygen" - VERBATIM ) + VERBATIM) endif() install(TARGETS ${PROJECT_NAME} LIBRARY DESTINATION lib) diff --git a/source/w3cipa/w3cipaframework/include/w3c/voiceinteraction/ipa/external/ipa/IPAProvider.h b/source/w3cipa/w3cipaframework/include/w3c/voiceinteraction/ipa/external/ipa/IPAProvider.h index a354e53..5883004 100644 --- a/source/w3cipa/w3cipaframework/include/w3c/voiceinteraction/ipa/external/ipa/IPAProvider.h +++ b/source/w3cipa/w3cipaframework/include/w3c/voiceinteraction/ipa/external/ipa/IPAProvider.h @@ -48,6 +48,11 @@ class IPAProvider { */ virtual ~IPAProvider(); + /** + * Initializes the IPA provider. + */ + virtual void initialize() = 0; + /** * Retrieves a list of languages that are supported by this IPA provider. * @return the supported languages of this IPA provider. diff --git a/source/w3cipa/w3cipaframework/include/w3c/voiceinteraction/ipa/external/ipa/ProviderRegistry.h b/source/w3cipa/w3cipaframework/include/w3c/voiceinteraction/ipa/external/ipa/ProviderRegistry.h index b892aae..423ec4f 100644 --- a/source/w3cipa/w3cipaframework/include/w3c/voiceinteraction/ipa/external/ipa/ProviderRegistry.h +++ b/source/w3cipa/w3cipaframework/include/w3c/voiceinteraction/ipa/external/ipa/ProviderRegistry.h @@ -53,7 +53,7 @@ class ProviderRegistry { virtual ~ProviderRegistry(); /** - * Adds the IPA provider to the known IPA providers. + * Adds the IPA provider to the known IPA providers and initializes it. * @param[in] provider the provider to add */ void addIPAProvider(const std::shared_ptr& provider); diff --git a/source/w3cipa/w3cipaframework/src/w3c/voiceinteraction/ipa/external/ipa/ProviderRegistry.cpp b/source/w3cipa/w3cipaframework/src/w3c/voiceinteraction/ipa/external/ipa/ProviderRegistry.cpp index 49d63d0..10bca17 100644 --- a/source/w3cipa/w3cipaframework/src/w3c/voiceinteraction/ipa/external/ipa/ProviderRegistry.cpp +++ b/source/w3cipa/w3cipaframework/src/w3c/voiceinteraction/ipa/external/ipa/ProviderRegistry.cpp @@ -26,6 +26,7 @@ ProviderRegistry::~ProviderRegistry() { } void ProviderRegistry::addIPAProvider(const std::shared_ptr& provider) { + provider->initialize(); providers.push_back(provider); }