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

Google fixes #415

Merged
merged 3 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixes
  • Loading branch information
mckaywrigley committed Apr 4, 2023
commit 4fd5210df6ed3fc06e7b6e1f864a9159fef0e8e3
26 changes: 20 additions & 6 deletions pages/api/google.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ChatBody, Message } from '@/types/chat';
import { GoogleSource } from '@/types/google';
import { Message } from '@/types/chat';
import { GoogleBody, GoogleSource } from '@/types/google';
import { OPENAI_API_HOST } from '@/utils/app/const';
import { cleanSourceText } from '@/utils/server/google';
import { Readability } from '@mozilla/readability';
Expand All @@ -9,14 +9,17 @@ import { NextApiRequest, NextApiResponse } from 'next';

const handler = async (req: NextApiRequest, res: NextApiResponse<any>) => {
try {
const { messages, key, model } = req.body as ChatBody;
const { messages, key, model, googleAPIKey, googleCSEId } =
req.body as GoogleBody;

const userMessage = messages[messages.length - 1];

const googleRes = await fetch(
`https://customsearch.googleapis.com/customsearch/v1?key=${
process.env.GOOGLE_API_KEY
}&cx=${process.env.GOOGLE_CSE_ID}&q=${userMessage.content.trim()}&num=5`,
googleAPIKey ? googleAPIKey : process.env.GOOGLE_API_KEY
}&cx=${
googleCSEId ? googleCSEId : process.env.GOOGLE_CSE_ID
}&q=${userMessage.content.trim()}&num=5`,
);

const googleData = await googleRes.json();
Expand All @@ -33,7 +36,16 @@ const handler = async (req: NextApiRequest, res: NextApiResponse<any>) => {
const sourcesWithText: any = await Promise.all(
sources.map(async (source) => {
try {
const res = await fetch(source.link);
const timeoutPromise = new Promise((_, reject) =>
setTimeout(() => reject(new Error('Request timed out')), 5000),
);

const res = (await Promise.race([
fetch(source.link),
timeoutPromise,
])) as any;

// if (res) {
const html = await res.text();

const virtualConsole = new jsdom.VirtualConsole();
Expand All @@ -56,9 +68,11 @@ const handler = async (req: NextApiRequest, res: NextApiResponse<any>) => {
text: sourceText.slice(0, 2000),
} as GoogleSource;
}
// }

return null;
} catch (error) {
console.error(error);
return null;
}
}),
Expand Down
17 changes: 16 additions & 1 deletion pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,21 @@ const Home: React.FC<HomeProps> = ({
};

const endpoint = getEndpoint(plugin);
let body;

if (!plugin) {
body = JSON.stringify(chatBody);
} else {
body = JSON.stringify({
...chatBody,
googleAPIKey: pluginKeys
.find((key) => key.pluginId === 'google-search')
?.requiredKeys.find((key) => key.key === 'GOOGLE_API_KEY')?.value,
googleCSEId: pluginKeys
.find((key) => key.pluginId === 'google-search')
?.requiredKeys.find((key) => key.key === 'GOOGLE_CSE_ID')?.value,
});
}

const controller = new AbortController();
const response = await fetch(endpoint, {
Expand All @@ -126,7 +141,7 @@ const Home: React.FC<HomeProps> = ({
'Content-Type': 'application/json',
},
signal: controller.signal,
body: JSON.stringify(chatBody),
body,
});

if (!response.ok) {
Expand Down
7 changes: 6 additions & 1 deletion types/google.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Message } from './chat';
import { ChatBody, Message } from './chat';

export interface GoogleBody extends ChatBody {
googleAPIKey: string;
googleCSEId: string;
}

export interface GoogleResponse {
message: Message;
Expand Down