Skip to content

Commit

Permalink
fix: udpate the error message
Browse files Browse the repository at this point in the history
  • Loading branch information
JimmyLv committed Feb 28, 2023
1 parent 07e3ac6 commit ddb2a7c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
5 changes: 2 additions & 3 deletions pages/[...slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { useLocalStorage } from "react-use";
import Footer from "../components/Footer";
import Header from "../components/Header";
import SquigglyLines from "../components/SquigglyLines";
import { fetchWithTimeout } from "../utils/fetchWithTimeout";

export const Home: NextPage = () => {
const router = useRouter();
Expand Down Expand Up @@ -50,7 +49,7 @@ export const Home: NextPage = () => {
router.replace(curUrl);
}
setLoading(true);
const response = await fetchWithTimeout("/api/summarize", {
const response = await fetch("/api/summarize", {
method: "POST",
headers: {
"Content-Type": "application/json",
Expand All @@ -59,7 +58,7 @@ export const Home: NextPage = () => {
});

if (!response.ok) {
console.log("error", response.statusText);
console.log("error", response);
if (response.status === 501) {
toast.error("啊叻?视频字幕不见了?!");
} else {
Expand Down
11 changes: 6 additions & 5 deletions pages/api/summarize.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { omitBy } from "lodash";
import { OpenAIStream } from "../../utils/OpenAIStream";
import { getChunckedTranscripts, getSummaryPrompt } from "../../utils/prompt";

Expand All @@ -10,7 +9,7 @@ if (!process.env.OPENAI_API_KEY) {
throw new Error("Missing env var from OpenAI");
}

export default async function handler(req: Request) {
export default async function handler(req: Request, res: Response) {
const { url, apiKey } = (await req.json()) as {
url?: string;
apiKey?: string;
Expand All @@ -37,6 +36,8 @@ export default async function handler(req: Request) {
// @ts-ignore
const title = res.data?.title;
const subtitleUrl = res.data?.subtitle?.list?.[0]?.subtitle_url;
apiKey && console.log("========use user key========");
console.log("bvid_url", url);
console.log("subtitle_url", subtitleUrl);
if (!subtitleUrl) {
return new Response("No subtitle in the video", { status: 501 });
Expand All @@ -57,13 +58,13 @@ export default async function handler(req: Request) {
const prompt = getSummaryPrompt(title, text);

const payload = {
model: "text-davinci-003",
model: apiKey ? "text-davinci-003" : "text-curie-001",
prompt,
temperature: 0.5,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
max_tokens: 400,
max_tokens: 300,
stream: true,
n: 1,
};
Expand All @@ -72,6 +73,6 @@ export default async function handler(req: Request) {
return new Response(stream);
} catch (error: any) {
console.log(error);
return new Response(error, { status: 500 });
return new Response(error);
}
}
17 changes: 12 additions & 5 deletions utils/OpenAIStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@ export interface OpenAIStreamPayload {
n: number;
}

function checkApiKey(str :string) {
function checkApiKey(str: string) {
var pattern = /^sk-[A-Za-z0-9]{48}$/;
return pattern.test(str);
}

export async function OpenAIStream(payload: OpenAIStreamPayload, apiKey?: string) {
export async function OpenAIStream(
payload: OpenAIStreamPayload,
apiKey?: string
) {
const encoder = new TextEncoder();
const decoder = new TextDecoder();
const openai_api_key = apiKey || process.env.OPENAI_API_KEY || '';
const openai_api_key = apiKey || process.env.OPENAI_API_KEY || "";

if(!checkApiKey(openai_api_key)) {
throw new Error('OpenAI API Key Format Error')
if (!checkApiKey(openai_api_key)) {
throw new Error("OpenAI API Key Format Error");
}

const res = await fetch("https://api.openai.com/v1/completions", {
Expand All @@ -40,6 +43,10 @@ export async function OpenAIStream(payload: OpenAIStreamPayload, apiKey?: string
body: JSON.stringify(payload),
});

if (res.status !== 200) {
throw new Error("OpenAI API " + res.statusText);
}

let counter = 0;

const stream = new ReadableStream({
Expand Down

1 comment on commit ddb2a7c

@vercel
Copy link

@vercel vercel bot commented on ddb2a7c Feb 28, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.