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

overload function complete #86

Merged
merged 4 commits into from
Jun 5, 2024
Merged
Changes from 3 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
34 changes: 34 additions & 0 deletions src/protocols/ChatGPT.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,36 @@ 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.
/// @param messages the messages to complete the chat.
dugubuyan marked this conversation as resolved.
Show resolved Hide resolved
/// @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 +77,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
Loading