diff --git a/package.json b/package.json index 81b8fbe..de03c80 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "apollo-server-core": "^3.10.0", "apollo-server-micro": "^3.10.2", "auto-bind": "^5.0.1", + "axios": "^1.2.3", "bignumber.js": "^9.1.0", "bootstrap": "^5.2.1", "dotenv": "^16.0.2", @@ -33,6 +34,7 @@ "mime": "^3.0.0", "next": "12.3.1", "next-pwa": "^5.5.4", + "openai": "^3.1.0", "orbit-db": "^0.28.6", "react": "18.2.0", "react-dom": "18.2.0", diff --git a/pages/api/generate.ts b/pages/api/generate.ts new file mode 100644 index 0000000..d07a2c1 --- /dev/null +++ b/pages/api/generate.ts @@ -0,0 +1,97 @@ +import {Configuration, OpenAIApi} from 'openai'; + +const configuration = new Configuration({ + apiKey: process.env.OPENAI_API_KEY, +}); +const openai = new OpenAIApi(configuration); +const MAX_TOKEN_LENGTH = 2048; +function truncatePrompt(prompt: string) { + if (prompt.length <= MAX_TOKEN_LENGTH) { + return prompt; + } else { + return prompt.substring(prompt.length - MAX_TOKEN_LENGTH, prompt.length); + } +} + +export default async function ( + req: { + body: { + prompt: string; + context: any; + }; + }, + res: { + status: (arg0: number) => { + (): any; + new (): any; + json: { + (arg0: { + error?: {message: string} | {message: string} | {message: string}; + result?: any; + }): void; + new (): any; + }; + }; + } +) { + if (!configuration.apiKey) { + res.status(500).json({ + error: { + message: + 'OpenAI API key not configured, please follow instructions in README.md', + }, + }); + return; + } + + const _text = req.body.prompt || ''; + if (_text.length === 0) { + res.status(400).json({ + error: { + message: 'Please enter message', + }, + }); + return; + } + + try { + const completion = await openai.createCompletion({ + model: 'text-davinci-003', + prompt: generatePrompt(_text), + temperature: 0.9, + max_tokens: 3500, + }); + + console.log(completion.data); + res.status(200).json({result: completion.data.choices[0].text}); + } catch (error: any) { + // Consider adjusting the error handling logic for your use case + if (error.response) { + console.error(error.response.status, error.response.data); + res.status(error.response.status).json(error.response.data); + } else { + console.error(`Error with OpenAI API request: ${error.message}`); + res.status(500).json({ + error: { + message: 'An error occurred during your request.', + }, + }); + } + } +} + +function generatePrompt(text: any) { + // const _t = truncatePrompt(text); + return [ + '!Only Responds If prompt Includes @AI!', + 'This is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly. Only Responds If prompt Includes @AI', + + 'Developer: Moikapy', + 'Twitter: @moikapy_', + 'Github: https://github.com/moikapy', + 'Twitch: https://www.twitch.tv/moikapy', + `Today's Date: ${new Date().toLocaleDateString()}`, + ...text, + '...awaiting response from bot...', + ].join('\n'); +} diff --git a/pages/index.tsx b/pages/index.tsx index 58a179f..b37023c 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -8,31 +8,11 @@ import H from '../src/components/common/H'; import {runTime} from '../dabu/helpers'; import Web3 from 'web3'; import TwitchEmbed from '@/src/components/blocks/TwitchEmbed'; +import Chatbot from '@/src/components/ui/Chatbot'; +import MoiLinkTree from '@/src/components/ui/MoiLinkTree'; var BN: any = Web3.utils.hexToNumberString; -// export async function getStaticProps(context: any, dabu: any) { -// const getLatestListed = async () => { -// // INIT Dabu -// var dabu = new DABU(process.env.AKKORO_ENV); -// console.log('dabu', dabu, await dabu.get_latest_nft_listing()); -// //Get Active Listings -// const latestListing: any = await dabu.get_latest_nft_listing(); -// return latestListing; -// }; -// const {scriptDuration: duration, res: latestListing} = await runTime( -// getLatestListed -// ); -// return { -// props: { -// latestListing: JSON.stringify(latestListing), -// scriptDuration: duration, -// }, -// // - At most once every 10 seconds -// revalidate: 15, // In seconds -// }; -// } - -export default function Dragon({latestListing}: any): JSX.Element { +export default function Index(): JSX.Element { return ( <> +
+ A Simple Link Tree To Take you to all of our official links +
+ {links.map((link) => { + return ( +