Skip to content

Commit

Permalink
overload function complete (#86)
Browse files Browse the repository at this point in the history
* add a function with more args

* add complete function with more args

* reorgnize

* remove repeat line

---------

Co-authored-by: Alex <[email protected]>
Co-authored-by: Alex <[email protected]>
  • Loading branch information
3 people authored Jun 5, 2024
1 parent 1826420 commit 09bcc1d
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/protocols/ChatGPT.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,35 @@ contract ChatGPT {
apiKey = _apiKey;
}

/// @notice complete a chat with the OpenAI ChatGPT.
/// @param messages the messages to complete the chat.
/// @param model the model of ChatGPT.
/// @param temperature the temperature of this request.
/// @return message the response from the OpenAI ChatGPT.
function complete(Message[] memory messages, string calldata model, string calldata temperature) public returns (string memory) {
bytes memory body;
body = abi.encodePacked('{"model": "',model);
body = abi.encodePacked(body,'", "messages": [');
for (uint256 i = 0; i < messages.length; i++) {
body = abi.encodePacked(
body,
'{"role": "',
messages[i].role == Role.User ? "user" : "system",
'", "content": "',
messages[i].content,
'"}'
);
if (i < messages.length - 1) {
body = abi.encodePacked(body, ",");
}
}
body = abi.encodePacked(body, '], "temperature":');
body = abi.encodePacked(body, temperature);
body = abi.encodePacked(body, '}');

return doGptRequest(body);
}

/// @notice complete a chat with the OpenAI ChatGPT.
/// @param messages the messages to complete the chat.
/// @return message the response from the OpenAI ChatGPT.
Expand All @@ -47,6 +76,10 @@ contract ChatGPT {
}
body = abi.encodePacked(body, '], "temperature": 0.7}');

return doGptRequest(body);
}

function doGptRequest(bytes memory body) private returns (string memory) {
Suave.HttpRequest memory request;
request.method = "POST";
request.url = "https://api.openai.com/v1/chat/completions";
Expand Down

0 comments on commit 09bcc1d

Please sign in to comment.