Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ChatGPT: refactor contract to work in the context of a confidential request #82

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,13 @@ import "suave-std/protocols/ChatGPT.sol";

contract Example {
function example() public {
ChatGPT chatgpt = new ChatGPT("apikey");
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?");

chatgpt.complete(messages);
chatgpt.complete(apiKey, messages);
}
}
```
Expand Down
11 changes: 2 additions & 9 deletions src/protocols/ChatGPT.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import "solady/src/utils/JSONParserLib.sol";
contract ChatGPT {
using JSONParserLib for *;

string apiKey;

enum Role {
User,
System
Expand All @@ -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++) {
Expand Down
13 changes: 7 additions & 6 deletions test/protocols/ChatGPT.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Loading