Last active
November 10, 2023 09:53
-
-
Save formysister/b23e94850eaa6cd7dd5cedf749c2c008 to your computer and use it in GitHub Desktop.
Ethereum + OpenAI integration using Chainlink
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
"Bearer YOUR_OPEN_AI_KEY"
with your actual OpenAI API key.requestChatGPT
method is used to send a request to the OpenAI API. It takes aprompt
as an argument, which is the initial text that you want the ChatGPT model to respond to.fulfill
method is used to handle the response from the OpenAI API. It emits aResponseReceived
event with the request ID, status code, and the response data.