Got a cool example to share? Get listed here! Submit it now.

useChainlinkFunctions()

Collection of community submitted examples for Chainlink Functions

Get inspired from quick examples

Chainlink Functions, a new self-service platform that allows anyone to write serverless code to fetch any data from any API and run custom compute on Chainlink's network. Learn from examples created by the community or contribute your own!
Learn more about Chainlink Functions

Prompt AI for a response

Submitted by:
Patrick Collins
Ask OpenAI (or any AI model you want to interact with) for information on-chain.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 const prompt = args[0] if ( !secrets.openaiKey ) { throw Error( "Need to set OPENAI_KEY environment variable" ) } // example request: // curl https://api.openai.com/v1/completions -H "Content-Type: application/json" -H "Authorization: Bearer YOUR_API_KEY" -d '{"model": "text-davinci-003", "prompt": "Say this is a test", "temperature": 0, "max_tokens": 7} // example response: // {"id":"cmpl-6jFdLbY08kJobPRfCZL4SVzQ6eidJ","object":"text_completion","created":1676242875,"model":"text-davinci-003","choices":[{"text":"\n\nThis is indeed a test","index":0,"logprobs":null,"finish_reason":"length"}],"usage":{"prompt_tokens":5,"completion_tokens":7,"total_tokens":12}} const openAIRequest = Functions.makeHttpRequest({ url: "https://api.openai.com/v1/completions", method: "POST", headers: { 'Authorization': `Bearer ${secrets.openaiKey}` }, data: { "model": "text-davinci-003", "prompt": prompt, "temperature": 0, "max_tokens": 7 } }) const [openAiResponse] = await Promise.all([ openAIRequest ]) console.log("raw response", openAiResponse) const result = openAiResponse.data.choices[0].text return Functions.encodeString(result)