Skip to content

Commit

Permalink
issue #54 Add ChatGPT config and initialization support
Browse files Browse the repository at this point in the history
Updated `FindLog4cplus.cmake` to include additional search paths.
Modified `CMakeLists.txt` to include new ChatGPT headers and copy
`ChatGPTIPAProvider.json` to the config directory. Added new JSON
config file `ChatGPTIPAProvider.json` for ChatGPT API settings.
Added `ChatGPTConfiguration.h` for parsing JSON config. Updated
`ChatGPTIPAProvider` class to load config values and use them in
`processInput`. Updated `IPAProvider` to include a pure virtual
`initialize` method. Modified `ProviderRegistry` to call `initialize`
on IPA providers when added to the registry.
  • Loading branch information
schnelle committed Nov 19, 2024
1 parent 0e2cb6d commit 77d527e
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 14 deletions.
7 changes: 6 additions & 1 deletion source/w3cipa/CMake/FindLog4cplus.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,14 @@ find_path(LOG4CPLUS_INCLUDE_DIR
HINTS
${PC_LOG4CPLUS_INCLUDE_DIR}
${PC_LOG4CPLUS_INCLUDE_DIRS}
${OPEN_SRC_INSTALL_PREFIX}/include

)

find_library(LOG4CPLUS_LIBRARY
NAMES
log4cplus
log4cplusd
PATHS
/usr/local
/usr
Expand All @@ -62,6 +65,8 @@ find_library(LOG4CPLUS_LIBRARY
HINTS
${PC_LOG4CPLUS_LIBDIR}
${PC_LOG4CPLUS_LIBRARY_DIRS}
${OPEN_SRC_INSTALL_PREFIX}/lib
${OPEN_SRC_INSTALL_PREFIX}/bin
)

if(LOG4CPLUS_LIBRARY)
Expand All @@ -75,4 +80,4 @@ include(FindPackageHandleStandardArgs)
# all listed variables are TRUE
FIND_PACKAGE_HANDLE_STANDARD_ARGS(Log4cplus DEFAULT_MSG LOG4CPLUS_LIBRARIES LOG4CPLUS_INCLUDE_DIR)

MARK_AS_ADVANCED(LOG4CPLUS_INCLUDE_DIR LOG4CPLUS_LIBRARIES)
MARK_AS_ADVANCED(LOG4CPLUS_INCLUDE_DIR LOG4CPLUS_LIBRARIES)
7 changes: 7 additions & 0 deletions source/w3cipa/w3cipachatgptipaprovider/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED 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 All @@ -39,4 +40,10 @@ install(TARGETS ${PROJECT_NAME}
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib)

#
# Add configuration files
#
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/ChatGPTIPAProvider.json
${PROJECT_CONFIG_DIR}/ChatGPTIPAProvider.json COPYONLY)


Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"endpoint": "https://api.openai.com/v1/chat/completions",
"key": "OPENAI-DEVELOPER-KEY",
"systemMessage": "You are a standards maniac."
}
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 @@ -37,6 +37,8 @@ class ChatGPTIPAProvider : public IPAProvider {
virtual ~ChatGPTIPAProvider() {
}

void initialize() override;

const std::shared_ptr<ExternalClientResponse> processInput(
const std::shared_ptr<ClientRequest>& request) override;

Expand All @@ -48,10 +50,15 @@ class ChatGPTIPAProvider : public IPAProvider {

const std::list<Language>& getSupportedLanguages() const override;

private:
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 @@ -13,14 +13,15 @@
#include <curl/curl.h>
#include <nlohmann/json.hpp>
#include <log4cplus/loggingmacros.h>

#include <fstream>

#include <w3c/voiceinteraction/ipa/TextModalityType.h>

#include "w3c/voiceinteraction/ipa/reference/TextMultiModalInput.h"
#include "w3c/voiceinteraction/ipa/reference/TextMultiModalOutput.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 @@ -59,6 +60,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 @@ -83,8 +98,10 @@ const std::shared_ptr<ExternalClientResponse> ChatGPTIPAProvider::processInput(
}
// Set the header and API key
struct curl_slist *headers = NULL;
std::string authorization = "Authorization: Bearer ";
authorization += key;
headers = curl_slist_append(headers,
"Authorization: Bearer OPENAI-DEVELOPER-KEY");
authorization.c_str());
headers = curl_slist_append(
headers, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
Expand All @@ -94,8 +111,7 @@ const std::shared_ptr<ExternalClientResponse> ChatGPTIPAProvider::processInput(
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);

// 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 @@ -105,8 +121,7 @@ const std::shared_ptr<ExternalClientResponse> ChatGPTIPAProvider::processInput(
// Set the payload
ChatGPTJSONRequest req;
req.model = std::string("gpt-3.5-turbo");
ChatGPTMessage systemMessage {"system",
"You are a standards maniac."};
ChatGPTMessage actualSystemMessage {"system", systemMessage.c_str()};
std::shared_ptr<MultiModalInputs> multiModalInputs =
request->getMultiModalInputs();
std::shared_ptr<MultiModalInput> input =
Expand All @@ -115,7 +130,7 @@ const std::shared_ptr<ExternalClientResponse> ChatGPTIPAProvider::processInput(
std::dynamic_pointer_cast<TextMultiModalInput>(input);
const std::string& text = textInput->getTextInput();
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: 0 additions & 2 deletions source/w3cipa/w3cipademo/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ target_link_libraries(${PROJECT_NAME} w3cipaframework)
#
# Add configuration files
#
set(PROJECT_CONFIG_DIR ${CMAKE_CURRENT_BINARY_DIR}/config)
file(MAKE_DIRECTORY ${PROJECT_CONFIG_DIR})
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/log4cplus.properties
${PROJECT_CONFIG_DIR}/log4cplus.properties COPYONLY)

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,7 +26,8 @@ ProviderRegistry::~ProviderRegistry() {
}

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

const std::list<std::shared_ptr<IPAProvider>> ProviderRegistry::getIPAProviders(
Expand Down

0 comments on commit 77d527e

Please sign in to comment.