Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate analyze_url to pipe3 backend #12

Merged
merged 4 commits into from Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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