-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.ts
87 lines (70 loc) · 2.22 KB
/
actions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"use server";
import weaviate from "weaviate-client";
import { CohereClient } from "cohere-ai";
import { EmojiType } from "./types";
import { kv } from "@vercel/kv";
const client = await weaviate.connectToWCS(process.env.WCS_URL!!, {
authCredentials: new weaviate.ApiKey(process.env.WCS_API_KEY!!),
headers: {
"X-OpenAI-Api-Key": process.env.OPENAI_APIKEY!!,
},
});
const cohere = new CohereClient({
token: process.env.COHERE_APIKEY!!,
});
export async function findQuotesByArgument(searchTerm: string, alpha: number) {
const kvStorePrefix = "emojis__";
const cachedResult = await kv.get<EmojiType[]>(
kvStorePrefix + searchTerm + alpha.toString()
);
// if (cachedResult) {
// return cachedResult;
// }
const collection = await client.collections.get<Omit<EmojiType, "distance">>(
"Emojis"
);
const { objects } = await collection.query.hybrid(searchTerm, {
limit: 50,
alpha: alpha,
returnMetadata: ["score", "explainScore"],
});
let quotesAndAuthorsArray: EmojiType[] = objects.map((quote) => ({
...quote.properties,
distance: quote.metadata?.score!!,
}));
quotesAndAuthorsArray = quotesAndAuthorsArray.filter((quote, index) => {
return (
quotesAndAuthorsArray.findIndex(
(q) => q.representation === quote.representation
) === index
);
});
const rerankResults = await cohere.rerank({
documents: quotesAndAuthorsArray.map((quote) => ({
text: quote.name,
...quote,
})),
query: searchTerm,
topN: 30,
model: "rerank-multilingual-v3.0",
});
const rerankedQuotesAndAuthorsArray = rerankResults.results.map(
(result) => quotesAndAuthorsArray[result.index]
);
// const rerankedQuotesAndAuthorsArray = quotesAndAuthorsArray;
console.log(rerankedQuotesAndAuthorsArray.map((q) => q.representation));
await kv.set(
kvStorePrefix + searchTerm + alpha.toString(),
JSON.stringify(rerankedQuotesAndAuthorsArray)
);
return rerankedQuotesAndAuthorsArray;
}
const filterDuplicates = (quotes: EmojiType[]) => {
const filteredQuotes = quotes.filter((quote, index) => {
return (
quotes.findIndex((q) => q.representation === quote.representation) ===
index
);
});
return filteredQuotes;
};