-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
276 additions
and
50 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import React, {useEffect, useState} from 'react'; | ||
import axios from 'axios'; | ||
|
||
function Chatbot() { | ||
const [input, setInput] = useState(''); | ||
const [response, setResponse] = useState(''); | ||
const [pageText, setPageText] = useState(''); | ||
const [prompts, setPrompts] = useState<any>([]); | ||
useEffect(() => { | ||
setPageText(document.body.innerText); | ||
}, []); | ||
const handleInput = (event: { | ||
target: {value: React.SetStateAction<string>}; | ||
}) => { | ||
setInput(event.target.value); | ||
}; | ||
|
||
const handleSubmit = async (event: {preventDefault: () => void}) => { | ||
event.preventDefault(); | ||
prompts.push(input); | ||
try { | ||
let res: any = await fetch('/api/generate', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({prompt: input}), | ||
}); | ||
res = await res.json(); | ||
console.log(res); | ||
setResponse(res.result); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className='container'> | ||
<form onSubmit={handleSubmit}> | ||
<div className='form-group'> | ||
<label>Input:</label> | ||
<input | ||
className='form-control' | ||
type='text' | ||
value={input} | ||
onChange={handleInput} | ||
/> | ||
</div> | ||
<button className='btn btn-primary' type='submit'> | ||
Submit | ||
</button> | ||
</form> | ||
<div className='form-group'> | ||
<label>Response:</label> | ||
<textarea | ||
className='form-control' | ||
rows={5} | ||
value={response} | ||
readOnly></textarea> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
export default Chatbot; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import React from 'react'; | ||
import MoiLinkTree from '.'; | ||
export default { | ||
title: 'Components/UI/MoiLinkTree', | ||
component: MoiLinkTree, | ||
}; | ||
|
||
const Template = (args) => { | ||
return <MoiLinkTree {...args} />; | ||
}; | ||
|
||
export const Default = Template.bind({}); | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import {useRouter} from 'next/router'; | ||
import styled from 'styled-components'; | ||
export const Button = styled.button` | ||
background-color: #fefefe; | ||
color: #000; | ||
border: 1px solid #000; | ||
padding: 0.5rem; | ||
border-radius: 5px; | ||
min-width: 200px; | ||
`; | ||
const links: {label: string; url: string}[] = [ | ||
{ | ||
label: 'Store', | ||
url: 'https://moikaslookout.etsy.com', | ||
}, | ||
{ | ||
label: 'Github', | ||
url: 'https://github.com/moikapy', | ||
}, | ||
{ | ||
label: 'Twitter', | ||
url: 'https://twitter.com/moikaslookout', | ||
}, | ||
{ | ||
label: 'Discord', | ||
url: 'https://discord.gg/3Z5Y4Y4', | ||
}, | ||
{ | ||
label: 'Twitch', | ||
url: 'https://twitch.tv/moikapy', | ||
}, | ||
{ | ||
label: 'Youtube', | ||
url: 'https://youtube.com/moikapy', | ||
}, | ||
{ | ||
label: 'Instagram', | ||
url: 'https://instagram.com/moikapy', | ||
}, | ||
]; | ||
export default function MoiLinkTree() { | ||
const router = useRouter(); | ||
return ( | ||
<> | ||
<style jsx>{` | ||
.mlt { | ||
max-width: 800px; | ||
// background:#000; | ||
} | ||
`}</style> | ||
<div className='mlt d-flex flex-column align-items-center'> | ||
<h1 className=''>Welcome to Moika's Lookout!!</h1> | ||
<p className='text-capitalize'> | ||
A Simple Link Tree To Take you to all of our official links | ||
</p> | ||
{links.map((link) => { | ||
return ( | ||
<div className='m-2'> | ||
<Button | ||
onClick={() => { | ||
router.push(link?.url); | ||
}}> | ||
{link?.label} | ||
</Button> | ||
</div> | ||
); | ||
})} | ||
</div> | ||
</> | ||
); | ||
} |
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