From 880d0bc4e0ea1cd937f1e0c9929fece560e2b5e1 Mon Sep 17 00:00:00 2001 From: Haythem Sellami <17862704+haythemsellami@users.noreply.github.com> Date: Sat, 4 May 2024 15:20:29 +0100 Subject: [PATCH 1/5] update ChatGPT protocol to work in teh context of confidential request --- src/protocols/ChatGPT.sol | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/protocols/ChatGPT.sol b/src/protocols/ChatGPT.sol index 7392215..df1a1b6 100644 --- a/src/protocols/ChatGPT.sol +++ b/src/protocols/ChatGPT.sol @@ -8,8 +8,6 @@ import "solady/src/utils/JSONParserLib.sol"; contract ChatGPT { using JSONParserLib for *; - string apiKey; - enum Role { User, System @@ -20,16 +18,11 @@ contract ChatGPT { string content; } - /// @notice constructor to create a ChatGPT instance. - /// @param _apiKey the API key to interact with the OpenAI ChatGPT. - constructor(string memory _apiKey) { - apiKey = _apiKey; - } - /// @notice complete a chat with the OpenAI ChatGPT. + /// @param apiKey ChatGPT API key /// @param messages the messages to complete the chat. /// @return message the response from the OpenAI ChatGPT. - function complete(Message[] memory messages) public returns (string memory) { + function complete(string calldata apiKey, Message[] memory messages) public returns (string memory) { bytes memory body; body = abi.encodePacked('{"model": "gpt-3.5-turbo", "messages": ['); for (uint256 i = 0; i < messages.length; i++) { From 7af721f112c23ca1bd77281c99c8bfb88027bc4f Mon Sep 17 00:00:00 2001 From: Haythem Sellami <17862704+haythemsellami@users.noreply.github.com> Date: Sat, 4 May 2024 15:20:36 +0100 Subject: [PATCH 2/5] update test --- test/protocols/ChatGPT.t.sol | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/test/protocols/ChatGPT.t.sol b/test/protocols/ChatGPT.t.sol index 313eea9..932d0bb 100644 --- a/test/protocols/ChatGPT.t.sol +++ b/test/protocols/ChatGPT.t.sol @@ -7,24 +7,25 @@ import "src/protocols/ChatGPT.sol"; contract ChatGPTTest is Test, SuaveEnabled { function testChatGPT() public { - ChatGPT chatgpt = getChatGPT(); + (ChatGPT chatgpt, string memory apiKey) = getChatGPT(); ChatGPT.Message[] memory messages = new ChatGPT.Message[](1); messages[0] = ChatGPT.Message(ChatGPT.Role.User, "Say this is a test!"); string memory expected = "This is a test!"; - string memory found = chatgpt.complete(messages); + string memory found = chatgpt.complete(apiKey, messages); assertEq(found, expected, "ChatGPT did not return the expected result"); } - function getChatGPT() public returns (ChatGPT chatgpt) { + function getChatGPT() public returns (ChatGPT chatgpt, string memory apiKey) { // NOTE: tried to do it with envOr but it did not worked - try vm.envString("CHATGPT_API_KEY") returns (string memory apiKey) { - if (bytes(apiKey).length == 0) { + try vm.envString("CHATGPT_API_KEY") returns (string memory apiKeyEnv) { + if (bytes(apiKeyEnv).length == 0) { vm.skip(true); } - chatgpt = new ChatGPT(apiKey); + chatgpt = new ChatGPT(); + apiKey = apiKeyEnv; } catch { vm.skip(true); } From 12b6a5cc7b9d81ba609d134ba433d71fab5d239d Mon Sep 17 00:00:00 2001 From: Haythem Sellami <17862704+haythemsellami@users.noreply.github.com> Date: Sat, 4 May 2024 15:26:51 +0100 Subject: [PATCH 3/5] chore: update README --- README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f27ce96..a8072c9 100644 --- a/README.md +++ b/README.md @@ -160,12 +160,16 @@ import "suave-std/protocols/ChatGPT.sol"; contract Example { function example() public { - ChatGPT chatgpt = new ChatGPT("apikey"); + //retreive the apiKey + bytes memory keyData = Suave.confidentialRetrieve(apiKeyRecord, API_KEY); + string memory apiKey = bytesToString(keyData); + + ChatGPT chatgpt = new ChatGPT(); ChatGPT.Message[] memory messages = new ChatGPT.Message[](1); messages[0] = ChatGPT.Message(ChatGPT.Role.User, "How do I write a Suapp with suave-std?"); - chatgpt.complete(messages); + chatgpt.complete(apiKey, messages); } } ``` From d8710a0e58bf93ada61c5c765f83916e56b83b67 Mon Sep 17 00:00:00 2001 From: Haythem Sellami <17862704+haythemsellami@users.noreply.github.com> Date: Sat, 4 May 2024 15:40:46 +0100 Subject: [PATCH 4/5] chore: update README --- README.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/README.md b/README.md index a8072c9..622f873 100644 --- a/README.md +++ b/README.md @@ -160,11 +160,8 @@ import "suave-std/protocols/ChatGPT.sol"; contract Example { function example() public { - //retreive the apiKey - bytes memory keyData = Suave.confidentialRetrieve(apiKeyRecord, API_KEY); - string memory apiKey = bytesToString(keyData); - ChatGPT chatgpt = new ChatGPT(); + string memory apiKey = "xxxx-xx" // in production, use confidential store ChatGPT.Message[] memory messages = new ChatGPT.Message[](1); messages[0] = ChatGPT.Message(ChatGPT.Role.User, "How do I write a Suapp with suave-std?"); From 240e532e875f753a62a114fda7f6a5ae50df1498 Mon Sep 17 00:00:00 2001 From: Haythem Sellami <17862704+haythemsellami@users.noreply.github.com> Date: Sat, 4 May 2024 15:46:18 +0100 Subject: [PATCH 5/5] chore: update README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 622f873..86b14c1 100644 --- a/README.md +++ b/README.md @@ -161,7 +161,7 @@ import "suave-std/protocols/ChatGPT.sol"; contract Example { function example() public { ChatGPT chatgpt = new ChatGPT(); - string memory apiKey = "xxxx-xx" // in production, use confidential store + string memory apiKey = "xxxx-xx"; // in production, use confidential store ChatGPT.Message[] memory messages = new ChatGPT.Message[](1); messages[0] = ChatGPT.Message(ChatGPT.Role.User, "How do I write a Suapp with suave-std?");