add ai worker

This commit is contained in:
Ishaan Dey
2024-05-13 23:00:02 -07:00
parent 02c1a6790a
commit 6e67578bf4
15 changed files with 3534 additions and 44 deletions

View File

@ -22,9 +22,7 @@ USER myuser
EXPOSE 3000
ARG CF_API_TOKEN
ARG CF_USER_ID
ENV CF_API_TOKEN=$CF_API_TOKEN
ENV CF_USER_ID=$CF_USER_ID
ARG CF_AI_KEY
ENV CF_AI_KEY=$CF_AI_KEY
CMD [ "node", "dist/index.js" ]

View File

@ -163,36 +163,12 @@ export const generateCode = async ({
instructions: string;
}) => {
return await fetch(
`https://api.cloudflare.com/client/v4/accounts/${process.env.CF_USER_ID}/ai/run/@cf/meta/llama-3-8b-instruct`,
`https://ai.ishaan1013.workers.dev/api?fileName=${fileName}&code=${code}&line=${line}&instructions=${instructions}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.CF_API_TOKEN}`,
Authorization: `${process.env.CF_AI_KEY}`,
},
body: JSON.stringify({
messages: [
{
role: "system",
content:
"You are an expert coding assistant. You read code from a file, and you suggest new code to add to the file. You may be given instructions on what to generate, which you should follow. You should generate code that is correct, efficient, and follows best practices. You should also generate code that is clear and easy to read. When you generate code, you should only return the code, and nothing else. You should not include backticks in the code you generate.",
},
{
role: "user",
content: `The file is called ${fileName}.`,
},
{
role: "user",
content: `Here are my instructions on what to generate: ${instructions}.`,
},
{
role: "user",
content: `Suggest me code to insert at line ${line} in my file. Give only the code, and NOTHING else. DO NOT include backticks in your response. My code file content is as follows
${code}`,
},
],
}),
}
);
};