Skip to content

Commit

Permalink
Update CMake and enhance ChatGPTIPAProvider class
Browse files Browse the repository at this point in the history
- Added new include directories and library search paths in `FindLog4cplus.cmake`.
- Modified `find_package` and updated `LOG4CPLUS_LIBRARY` handling in `CMakeLists.txt`.
- Added new header files to the `HEADERS` list in `CMakeLists.txt`.
- Introduced header guard, namespace, and `ChatGPTConfiguration` struct in `ChatGPTConfiguration.h`.
- Enhanced `ChatGPTIPAProvider` with destructor, `initialize` method, and new member variables.
- Implemented `initialize` method and updated `processInput` in `ChatGPTIPAProvider.cpp`.
- Added `initialize` method to `IPAProvider` class.
- Modified `addIPAProvider` in `ProviderRegistry` to initialize providers.
  • Loading branch information
schnelle committed Dec 19, 2024
1 parent df4376f commit f56174a
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 14 deletions.
5 changes: 5 additions & 0 deletions source/w3cipa/CMake/FindLog4cplus.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ find_path(LOG4CPLUS_INCLUDE_DIR
/opt/local/include
/opt/csw/include
/opt/include
/include
/{<CONFIG>_OUTPUT_NAME}/include
$ENV{LOG4CPLUS_DIR}/include
$ENV{LOG4CPLUS_ROOT}/include
${LOG4CPLUS_DIR}/include
Expand All @@ -48,13 +50,16 @@ find_path(LOG4CPLUS_INCLUDE_DIR
find_library(LOG4CPLUS_LIBRARY
NAMES
log4cplus
log4cplusD
PATHS
/usr/local
/usr
/sw
/opt/local
/opt/csw
/opt
/lib
${W3CIPA_OPEN_SOURCE_SRC}/lib
$ENV{LOG4CPLUS_DIR}/lib
$ENV{LOG4CPLUS_ROOT}/lib
${LOG4CPLUS_DIR}/lib
Expand Down
7 changes: 4 additions & 3 deletions source/w3cipa/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions source/w3cipa/w3cipachatgptipaprovider/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <nlohmann/json.hpp>

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
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ namespace chatgpt {
class ChatGPTIPAProvider : public IPAProvider {
public:
ChatGPTIPAProvider();

virtual ~ChatGPTIPAProvider() {
}

void initialize() override;

const std::shared_ptr<ExternalIPAResponse> processInput(
const std::shared_ptr<IPARequest>& request) override;

Expand All @@ -51,7 +54,12 @@ class ChatGPTIPAProvider : public IPAProvider {
private:
/** Languages supported by this provider. */
std::list<Language> 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. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* [1] https://www.w3.org/Consortium/Legal/copyright-software
*/

#include <fstream>

#include <curl/curl.h>
#include <nlohmann/json.hpp>
#include <log4cplus/loggingmacros.h>
Expand All @@ -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 {
Expand Down Expand Up @@ -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<ModalityType> ChatGPTIPAProvider::getSupportedModalityTypes() const {
std::list<ModalityType> types = { TextModalityType() };
return types;
Expand All @@ -82,19 +99,20 @@ const std::shared_ptr<ExternalIPAResponse> 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);

// 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;
Expand All @@ -103,9 +121,8 @@ const std::shared_ptr<ExternalIPAResponse> 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<MultiModalDataCollection> multiModalInputs =
request->getMultiModalInputs();
std::shared_ptr<MultiModalData> input =
Expand All @@ -114,7 +131,7 @@ const std::shared_ptr<ExternalIPAResponse> ChatGPTIPAProvider::processInput(
std::dynamic_pointer_cast<TextMultiModalInput>(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;
Expand Down
2 changes: 1 addition & 1 deletion source/w3cipa/w3cipaframework/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<IPAProvider>& provider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ ProviderRegistry::~ProviderRegistry() {
}

void ProviderRegistry::addIPAProvider(const std::shared_ptr<IPAProvider>& provider) {
provider->initialize();
providers.push_back(provider);
}

Expand Down

0 comments on commit f56174a

Please sign in to comment.