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

add you.com integration #514

Merged
merged 13 commits into from
Oct 26, 2023
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ npm run dev

Chat UI features a powerful Web Search feature. It works by:

1. Generating an appropriate Google query from the user prompt.
2. Performing Google search and extracting content from webpages.
1. Generating an appropriate search query from the user prompt.
2. Performing web search and extracting content from webpages.
3. Creating embeddings from texts using [transformers.js](https://huggingface.co/docs/transformers.js). Specifically, using [Xenova/gte-small](https://huggingface.co/Xenova/gte-small) model.
4. From these embeddings, find the ones that are closest to the user query using vector similarity search. Specifically, we use `inner product` distance.
5. Get the corresponding texts to those closest embeddings and perform [Retrieval-Augmented Generation](https://huggingface.co/papers/2005.11401) (i.e. expand user prompt by adding those texts so that a LLM can use this information).
Expand Down Expand Up @@ -122,7 +122,7 @@ PUBLIC_APP_DISCLAIMER=

### Web Search config

You can enable the web search by adding either `SERPER_API_KEY` ([serper.dev](https://serper.dev/)) or `SERPAPI_KEY` ([serpapi.com](https://serpapi.com/)) to your `.env.local`.
You can enable the web search by adding any of `YDC_API_KEY` ([docs.you.com](https://docs.you.com)) or `SERPER_API_KEY` ([serper.dev](https://serper.dev/)) or `SERPAPI_KEY` ([serpapi.com](https://serpapi.com/)) to your `.env.local`.
sam-h-bean marked this conversation as resolved.
Show resolved Hide resolved

### Custom models

Expand Down Expand Up @@ -209,7 +209,7 @@ The following is the default `webSearchQueryPromptTemplate`.
```prompt
{{userMessageToken}}
My question is: {{message.content}}.
Based on the conversation history (my previous questions are: {{previousMessages}}), give me an appropriate query to answer my question for google search. You should not say more than query. You should not say any words except the query. For the context, today is {{currentDate}}
Based on the conversation history (my previous questions are: {{previousMessages}}), give me an appropriate query to answer my question for web search. You should not say more than query. You should not say any words except the query. For the context, today is {{currentDate}}
{{userMessageEndToken}}
{{assistantMessageToken}}
```
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chat-ui",
"version": "0.6.0",
"version": "0.7.0",
sam-h-bean marked this conversation as resolved.
Show resolved Hide resolved
"private": true,
"packageManager": "npm@9.5.0",
"scripts": {
Expand Down
4 changes: 3 additions & 1 deletion src/lib/server/websearch/runWebSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from "$lib/server/websearch/sentenceSimilarity";
import type { Conversation } from "$lib/types/Conversation";
import type { MessageUpdate } from "$lib/types/MessageUpdate";
import { getWebSearchProvider } from "./searchWeb";

const MAX_N_PAGES_SCRAPE = 10 as const;
const MAX_N_PAGES_EMBED = 5 as const;
Expand Down Expand Up @@ -39,7 +40,8 @@ export async function runWebSearch(

try {
webSearch.searchQuery = await generateQuery(messages);
appendUpdate("Searching Google", [webSearch.searchQuery]);
const searchProvider = getWebSearchProvider();
appendUpdate(`Searching ${searchProvider}`, [webSearch.searchQuery]);
const results = await searchWeb(webSearch.searchQuery);
webSearch.results =
(results.organic_results &&
Expand Down
45 changes: 43 additions & 2 deletions src/lib/server/websearch/searchWeb.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
import { SERPAPI_KEY, SERPER_API_KEY } from "$env/static/private";
import { SERPAPI_KEY, SERPER_API_KEY, YDC_API_KEY } from "$env/static/private";

import { getJson } from "serpapi";
import type { GoogleParameters } from "serpapi";
import { YouWebSearch } from "../../types/WebSearch";

// get which SERP api is providing web results
export function getWebSearchProvider() {
if (YDC_API_KEY) {
return "You.com"
} else {
return "Google"
}
}

// Show result as JSON
export async function searchWeb(query: string) {
if (YDC_API_KEY) {
return await searchWebYouApi(query);
}
if (SERPER_API_KEY) {
return await searchWebSerper(query);
}
if (SERPAPI_KEY) {
sam-h-bean marked this conversation as resolved.
Show resolved Hide resolved
return await searchWebSerpApi(query);
}
throw new Error("No Serper.dev or SerpAPI key found");
throw new Error("No You.com or Serper.dev or SerpAPI key found");
}

export async function searchWebSerper(query: string) {
Expand Down Expand Up @@ -59,3 +72,31 @@ export async function searchWebSerpApi(query: string) {

return response;
}

export async function searchWebYouApi(query: string) {
const response = await fetch(`https://api.ydc-index.io/search?query=${query}`, {
method: "GET",
headers: {
"X-API-Key": YDC_API_KEY,
"Content-type": "application/json; charset=UTF-8",
},
});

if (!response.ok) {
throw new Error(
`You.com API returned error code ${response.status} - ${response.statusText}`
);
}

const data: YouWebSearch = await response.json();
const formattedResultsWithSnippets = data.hits.map(({ title, url, snippets }) => ({
title,
link: url,
text: snippets?.join("\n") || "",
hostname: new URL(url).hostname,
}))

return {
organic_results: formattedResultsWithSnippets,
};
}
12 changes: 12 additions & 0 deletions src/lib/types/WebSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,15 @@ export type WebSearchMessageSources = {
type: "sources";
sources: WebSearchSource[];
};

export interface YouWebSearch {
hits: YouSearchHit[];
latency: number;
}

interface YouSearchHit {
url: string;
title: string;
description: string;
snippets: string[];
}
Loading