Skip to content

Commit

Permalink
feat: migrate analyze_url to pipe3 backend (#12)
Browse files Browse the repository at this point in the history
* feat: migrate analyze_url to pipe3 backend

* fix: remove openai key

* fix: some issues

* feat: update analytics
  • Loading branch information
forrestchang committed Feb 1, 2023
1 parent 822ec49 commit 4319846
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 35 deletions.
47 changes: 15 additions & 32 deletions pages/api/analyze.ts
Expand Up @@ -13,48 +13,31 @@ const handler = async (req: NextRequest) => {
});
}

const prompt = `
Generate a list of thought provoking discussion questions about the URL, and return the answers of these questions with the evidence.
Please generate a JSON list object with the following properties: q and a. q and a should be string. q is the question. a is the answer.
The URL is: ${url}
`;

const payload = {
model: "text-davinci-003",
prompt,
temperature: 0.7,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
max_tokens: 500,
stream: false,
n: 1,
};

const res = await fetch("https://api.openai.com/v1/completions", {
const backendDomain = process.env.BACKEND_DOMAIN as string;
const apiUrl = `${backendDomain}/api/v1/readpilot/analyze_url`;

const res = await fetch(apiUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.OPENAI_API_KEY ?? ""}`,
"X-Api-Token": `${process.env.PIPE3_API_KEY}`,
},
method: "POST",
body: JSON.stringify(payload),
body: JSON.stringify({
url: url,
}),
});

if (!res.ok) {
return new Response(JSON.stringify({ msg: "OpenAI API error", data: [] }), {
if (res.status != 200) {
console.log(`[analyze.ts] Pipe3 API error: ${res}`);
return new Response(JSON.stringify({ msg: "Pipe3 API error", data: [] }), {
status: 500,
});
}

const { choices } = await res.json();
const { text } = choices[0];
const rawData = text.trimStart().replace(/^Output: /, "");
console.log(rawData);
const results = JSON.parse(rawData);
const respJson = await res.json();
const data = respJson.data;

return new Response(JSON.stringify({ data: results }));
return new Response(JSON.stringify({ data: data }));
};

export default handler;
14 changes: 11 additions & 3 deletions pages/index.tsx
Expand Up @@ -123,9 +123,17 @@ export default function Home() {
>
<Balancer>
Trusted by{" "}
<CountingNumbers value={7173} duration={1000} className="bg-gradient-to-r from-red-600 to-amber-600 bg-clip-text font-bold text-transparent" />{" "}
users, {" "}
<CountingNumbers value={9830} duration={1000} className="bg-gradient-to-r from-red-600 to-amber-600 bg-clip-text font-bold text-transparent" />{" "}
<CountingNumbers
value={7451}
duration={1000}
className="bg-gradient-to-r from-red-600 to-amber-600 bg-clip-text font-bold text-transparent"
/>{" "}
users,{" "}
<CountingNumbers
value={10219}
duration={1000}
className="bg-gradient-to-r from-red-600 to-amber-600 bg-clip-text font-bold text-transparent"
/>{" "}
links have been analyzed.
</Balancer>
</motion.p>
Expand Down

1 comment on commit 4319846

@vercel
Copy link

@vercel vercel bot commented on 4319846 Feb 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

readpilot – ./

readpilot.vercel.app
readpilot-forrestchang.vercel.app
readpilot-git-main-forrestchang.vercel.app

Please sign in to comment.