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

integration: spider search & fetch for faster retrieval of sources #10

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
46 changes: 30 additions & 16 deletions app/api/getAnswer/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
TogetherAIStreamPayload,
} from "@/utils/TogetherAIStream";
import Together from "together-ai";
import { Spider } from "@spider-cloud/spider-client";

const together = new Together({
apiKey: process.env["TOGETHER_API_KEY"],
Expand All @@ -23,22 +24,35 @@ export async function POST(request: Request) {
let finalResults = await Promise.all(
sources.map(async (result: any) => {
try {
// Fetch the source URL, or abort if it's been 3 seconds
const response = await fetchWithTimeout(result.url);
const html = await response.text();
const virtualConsole = new jsdom.VirtualConsole();
const dom = new JSDOM(html, { virtualConsole });

const doc = dom.window.document;
const parsed = new Readability(doc).parse();
let parsedContent = parsed
? cleanedText(parsed.textContent)
: "Nothing found";

return {
...result,
fullContent: parsedContent,
};
if (sources[0].source === "spider") {
console.log("fetching with spider", result.url);
const spider = new Spider({
apiKey: process.env.SPIDER_API_KEY,
});
const spiderResult = await spider.scrapeUrl(result.url, { return_format: "markdown", metadata: false, readability: true });
return {
...result,
fullContent: spiderResult[0].content.toString(),
};
} else {
console.log("fetching", result.url)
// Fetch the source URL, or abort if it's been 3 seconds
const response = await fetchWithTimeout(result.url);
const html = await response.text();
const virtualConsole = new jsdom.VirtualConsole();
const dom = new JSDOM(html, { virtualConsole });

const doc = dom.window.document;
const parsed = new Readability(doc).parse();
let parsedContent = parsed
? cleanedText(parsed.textContent)
: "Nothing found";

return {
...result,
fullContent: parsedContent,
};
}
} catch (e) {
console.log(`error parsing ${result.name}, error: ${e}`);
return {
Expand Down
21 changes: 20 additions & 1 deletion app/api/getSources/route.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { NextResponse } from "next/server";
import { z } from "zod";
import { Spider } from "@spider-cloud/spider-client";

let excludedSites = ["youtube.com"];

let searchEngine: "bing" | "serper" = "serper";
type SearchEngine = "bing" | "serper" | "spider";

let searchEngine: SearchEngine = "serper";

export async function POST(request: Request) {
let { question } = await request.json();
Expand Down Expand Up @@ -77,6 +80,22 @@ export async function POST(request: Request) {
url: result.link,
}));

return NextResponse.json(results);
} else if (searchEngine === "spider") {
const SPIDER_API_KEY = process.env["SPIDER_API_KEY"];
if (!SPIDER_API_KEY) {
throw new Error("SPIDER_API_KEY is required");
}

const app = new Spider({ apiKey: SPIDER_API_KEY });
const response = await app.search(question, { fetch_page_content: false, limit: 5 });

let results = response.content.map((result: { title: string, url: string }) => ({
name: result.title,
url: result.url,
source: "spider",
}));

return NextResponse.json(results);
}
}
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"dependencies": {
"@mozilla/readability": "^0.5.0",
"@spider-cloud/spider-client": "^0.0.27",
"eventsource-parser": "^1.1.2",
"jsdom": "^24.1.0",
"next": "14.2.3",
Expand Down