Skip to content

Instantly share code, notes, and snippets.

@formysister
Last active November 10, 2023 09:53
Show Gist options
  • Save formysister/b23e94850eaa6cd7dd5cedf749c2c008 to your computer and use it in GitHub Desktop.
Save formysister/b23e94850eaa6cd7dd5cedf749c2c008 to your computer and use it in GitHub Desktop.
Ethereum + OpenAI integration using Chainlink
pragma solidity ^0.6.7;
import "@chainlink/contracts/src/v0.6/ChainlinkClient.sol";
import "@chainlink/contracts/src/v0.6/vendor/Ownable.sol";
contract ChatGPT is ChainlinkClient, Ownable {
uint256 private fee;
address private oracle;
bytes32 private jobId;
event ResponseReceived(
bytes32 indexed requestId,
uint256 indexed statusCode,
string indexed data
);
constructor(address _oracle, string memory _jobId, uint256 _fee) public {
setChainlinkToken(0xa36085F69e2889c224210F603D836748e7dC0088);
oracle = _oracle;
jobId = stringToBytes32(_jobId);
fee = _fee;
}
function requestChatGPT(string memory prompt) public returns (bytes32 requestId) {
Chainlink.Request memory request = buildChainlinkRequest(jobId, address(this), this.fulfill.selector);
request.add("get", "https://api.openai.com/v1/engines/davinci-codex/completions");
request.addHeader("Authorization", "Bearer YOUR_OPEN_AI_KEY");
request.addHeader("Content-Type", "application/json");
request.add("prompt", prompt);
return sendChainlinkRequestTo(oracle, request, fee);
}
function fulfill(bytes32 _requestId, uint256 _statusCode, string memory _data) public recordChainlinkFulfillment(_requestId) {
emit ResponseReceived(_requestId, _statusCode, _data);
}
function stringToBytes32(string memory source) private pure returns (bytes32 result) {
bytes memory tempEmptyStringTest = bytes(source);
if (tempEmptyStringTest.length == 0) {
return 0x0;
}
assembly {
result := mload(add(source, 32))
}
}
}
@formysister
Copy link
Author

Ethereum + OpenAI

Building a smart contract to interact with the ChatGPT API would involve using a decentralized oracle such as Chainlink to make HTTP requests from the smart contract to the OpenAI API. However, it's important to note that, as of the current time, OpenAI's use case policy does not allow for use in this manner.

That being said, here's a conceptual example of how you could set this up using Ethereum's Solidity language, Chainlink, and the OpenAI API. Please note that this is only a hypothetical example and should not be used as it violates OpenAI's use case policy.

  1. First, you would need to set up your smart contract in Solidity, which will interact with the Chainlink oracle to make HTTP requests.
  2. The smart contract above uses Chainlink to make HTTP GET requests to the OpenAI API. You would replace "Bearer YOUR_OPEN_AI_KEY" with your actual OpenAI API key.
  3. The requestChatGPT method is used to send a request to the OpenAI API. It takes a prompt as an argument, which is the initial text that you want the ChatGPT model to respond to.
  4. The fulfill method is used to handle the response from the OpenAI API. It emits a ResponseReceived event with the request ID, status code, and the response data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment