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: add algolia to cohere chat demo #1210

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
d815de1
ready to try to test
RohinBhargava Jul 26, 2024
f8b2038
add comment
RohinBhargava Jul 26, 2024
5ffdd78
add lockfile
RohinBhargava Jul 26, 2024
9f9a5ea
need to figure out how to populate url
RohinBhargava Jul 27, 2024
b443f49
pnpm-lock revert
RohinBhargava Jul 27, 2024
3ab5279
proper pnpm-lock
RohinBhargava Jul 27, 2024
3d2739e
main pnpm
Jul 29, 2024
f2382f7
reset pnpm-lock
Jul 29, 2024
14f82ee
lockfile
Jul 29, 2024
ee41049
merge from upstream
Jul 29, 2024
37326e8
update hack with getting the right docs url
Jul 29, 2024
21e7026
test
RohinBhargava Jul 29, 2024
be46882
remove kv
RohinBhargava Jul 29, 2024
a51b4b1
lockfile
RohinBhargava Jul 29, 2024
63daf56
prettier
RohinBhargava Jul 29, 2024
32797ca
proper import
RohinBhargava Jul 29, 2024
52d7dc1
Merge remote-tracking branch 'origin/main' into rohin/add-algolia-to-…
RohinBhargava Jul 30, 2024
dc97d0c
working example
RohinBhargava Jul 30, 2024
57a920a
prefer private docs
RohinBhargava Jul 30, 2024
0c43f54
pnpm format
RohinBhargava Jul 30, 2024
8835846
chat history added back, maximum of 10
RohinBhargava Jul 30, 2024
0a7107a
add comments
RohinBhargava Jul 30, 2024
e6bd21c
get rid of print
RohinBhargava Jul 30, 2024
3350fc2
Merge remote-tracking branch 'origin/main' into rohin/add-algolia-to-…
RohinBhargava Jul 30, 2024
76d4a0b
Merge branch 'ajiang/cohere-stream' into rohin/add-algolia-to-cohere-…
RohinBhargava Jul 30, 2024
7ffb3ee
remove algoliasearch dep
RohinBhargava Jul 30, 2024
1309cbb
comment out console log for CI
RohinBhargava Jul 30, 2024
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
6 changes: 5 additions & 1 deletion packages/ui/docs-bundle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@
"@fern-ui/ui": "workspace:*",
"@sentry/nextjs": "^7.112.2",
"@vercel/edge-config": "^1.1.0",
"@vercel/kv": "^2.0.0",
"@workos-inc/node": "^6.1.0",
"algoliasearch": "^4.22.1",
"cohere-ai": "^7.9.5",
"cssnano": "^6.0.3",
"esbuild": "0.20.2",
Expand All @@ -64,7 +66,8 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"sharp": "^0.33.3",
"url-join": "5.0.0"
"url-join": "5.0.0",
"uuid": "^9.0.0"
},
"devDependencies": {
"@fern-platform/configs": "workspace:*",
Expand All @@ -75,6 +78,7 @@
"@types/node-fetch": "2.6.9",
"@types/react": "^18.0.20",
"@types/react-dom": "^18.2.18",
"@types/uuid": "^9.0.1",
"autoprefixer": "^10.4.16",
"depcheck": "^1.4.3",
"env-cmd": "toddbluhm/env-cmd",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
import algolia from "algoliasearch";
import { Cohere, CohereClient } from "cohere-ai";
import { v4 } from "uuid";
import { FernRegistryClient } from "../../../../../../../fdr-sdk/src/client/generated";

export const runtime = "edge";

const cohere = new CohereClient({
token: process.env.COHERE_API_KEY,
});

if (!process.env.ALGOLIA_APP_ID || !process.env.ALGOLIA_ADMIN_API_KEY || !process.env.ALGOLIA_SEARCH_INDEX) {
RohinBhargava marked this conversation as resolved.
Show resolved Hide resolved
throw new Error("Missing Algolia environment variables");
}

if (!process.env.DOCS_URL) {
RohinBhargava marked this conversation as resolved.
Show resolved Hide resolved
throw new Error("Missing DOCS_URL environment variable");
}

const docsUrl = process.env.DOCS_URL;

const algoliaClient = algolia(process.env.ALGOLIA_APP_ID, process.env.ALGOLIA_ADMIN_API_KEY); // this can probably be hardcoded in cohere for app hack

const PREAMBLE = `
You are an expert AI assistant called Fernie that helps developers answer questions about Cohere's APIs and SDKs.
The user asking questions is a developer, technical writer, or product manager. Your tone is friendly and helpful, and you can provide code snippets.
Expand All @@ -28,10 +43,42 @@ export default async function handler(req: Request): Promise<Response> {

const body = await req.json();

let conversationId = body.conversationId;

if (!body.conversationId) {
conversationId = v4();
}

const frc = new FernRegistryClient({ environment: "production" });

const docsUrlResponse = await frc.docs.v2.read.getDocsForUrl({ url: docsUrl });
const privateDocsUrlResponse = await frc.docs.v2.read.getPrivateDocsForUrl({ url: docsUrl });

const docsSearchIndex = docsUrlResponse.ok ? docsUrlResponse.body.definition.algoliaSearchIndex : undefined;
const privateDocsSearchIndex = privateDocsUrlResponse.ok
? privateDocsUrlResponse.body.definition.algoliaSearchIndex
: undefined;

if (!docsSearchIndex) {
throw new Error("No Algolia search index found");
}

const index = algoliaClient.initIndex(docsSearchIndex);
const { hits } = await index.search(body.message);

if (privateDocsSearchIndex) {
const privateIndex = algoliaClient.initIndex(privateDocsSearchIndex);
const { hits: privateHits } = await privateIndex.search(body.message);
hits.push(...privateHits);
}

const response = await cohere.chatStream({
preamble: PREAMBLE,
conversationId: body.conversationId,
conversationId,
message: body.message,
documents: hits.map((hit) => ({
content: JSON.stringify(hit),
})),
});

const stream = convertAsyncIterableToStream(response).pipeThrough(getCohereStreamTransformer());
Expand All @@ -53,7 +100,7 @@ function convertAsyncIterableToStream<T>(iterable: AsyncIterable<T>): ReadableSt
});
}

function getCohereStreamTransformer() {
function getCohereStreamTransformer(): TransformStream<Cohere.StreamedChatResponse, Uint8Array> {
const encoder = new TextEncoder();
return new TransformStream<Cohere.StreamedChatResponse, Uint8Array>({
async transform(chunk, controller) {
Expand Down
Loading
Loading