🤖OpenAI API Integration
Connecting OpenAI to the Blockchain via Chainlink Functions
Integrating Chainlink Functions to call the ChatGPT API in order to research company information, involves breaking down the implementation into several steps. These are the steps I took:
Steps to Integrate Chainlink Functions with ChatGPT API
Setup Chainlink Functions:
Use Chainlink's oracle services to enable smart contracts to make external API calls.
Deploy a Chainlink node if necessary or use a third-party node.
Create a Smart Contract to Request Data:
Write a Solidity smart contract that requests company information via Chainlink.
Define the request structure and handle the response.
Configure Chainlink Node:
Configure the node to interact with the ChatGPT API.
Ensure the API key and request parameters are securely handled.
Train and Configure ChatGPT Model:
Prepare a prompt for the ChatGPT API to fetch the required company information.
Fine-tune the model or adjust the prompt to consistently get the desired information.
Integrate the Data into the dApp:
Update the frontend to display the fetched data.
Ensure the data flow from the smart contract to the frontend is smooth.
Detailed Steps and Code
1. Smart Contract to Request Data
Create a Solidity contract that requests data from Chainlink Functions:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@chainlink/contracts/src/v0.8/ChainlinkClient.sol";
contract CompanyInfo is ChainlinkClient {
using Chainlink for Chainlink.Request;
uint256 public companyData;
address private oracle;
bytes32 private jobId;
uint256 private fee;
event DataFulfilled(bytes32 indexed requestId, uint256 indexed data);
constructor() {
setPublicChainlinkToken();
oracle = 0x...; // Chainlink Oracle address
jobId = "b0..."; // Chainlink Job ID
fee = 0.1 * 10 ** 18; // 0.1 LINK
}
function requestCompanyInfo(string memory companyName) public returns (bytes32 requestId) {
Chainlink.Request memory request = buildChainlinkRequest(jobId, address(this), this.fulfill.selector);
request.add("companyName", companyName);
return sendChainlinkRequestTo(oracle, request, fee);
}
function fulfill(bytes32 _requestId, uint256 _data) public recordChainlinkFulfillment(_requestId) {
emit DataFulfilled(_requestId, _data);
companyData = _data;
}
}2. Configure Chainlink Node
On the Chainlink node, configure the external adapter to call the ChatGPT API. This involves creating an adapter that formats the request to the API and parses the response.
3. ChatGPT API Request Configuration
Configure the ChatGPT API request to fetch company information:
{
"model": "gpt-4",
"messages": [
{
"role": "system",
"content": "You are an assistant that provides detailed company information including mission, values, political donations, political leanings, and adherence to DEI standards."
},
{
"role": "user",
"content": "Provide detailed information about [Company Name]."
}
]
}4. Frontend Integration
Updating the React frontend to display the company information:
import React, { useState, useEffect } from 'react';
import Web3 from 'web3';
import CompanyInfoContract from './contracts/CompanyInfo.json';
const CompanyInfo = () => {
const [companyData, setCompanyData] = useState(null);
const [companyName, setCompanyName] = useState('');
useEffect(() => {
const fetchData = async () => {
const web3 = new Web3(Web3.givenProvider);
const networkId = await web3.eth.net.getId();
const deployedNetwork = CompanyInfoContract.networks[networkId];
const instance = new web3.eth.Contract(
CompanyInfoContract.abi,
deployedNetwork && deployedNetwork.address,
);
if (companyName) {
const data = await instance.methods.requestCompanyInfo(companyName).call();
setCompanyData(data);
}
};
fetchData();
}, [companyName]);
return (
<div>
<input
type="text"
value={companyName}
onChange={(e) => setCompanyName(e.target.value)}
placeholder="Enter company name"
/>
{companyData && (
<div>
<h2>Company Information</h2>
<p>{companyData}</p>
</div>
)}
</div>
);
};
export default CompanyInfo;By integrating Chainlink Functions with ChatGPT, we can enhance the "I on You Reviews" dApp to provide detailed company information. The steps involve creating a smart contract, configuring the Chainlink node, making the ChatGPT API request, and updating the frontend to display the data. We will be storing business data and company information on IPFS
Last updated