feat: add update all recommendations command
This commit is contained in:
@ -3,6 +3,16 @@ import { OPENAI_API_KEY } from "@lib/env.ts";
|
||||
|
||||
const openAI = OPENAI_API_KEY && new OpenAI(OPENAI_API_KEY);
|
||||
|
||||
function extractListFromResponse(response?: string): string[] {
|
||||
if (!response) return [];
|
||||
return response
|
||||
.split(/[\n,]/)
|
||||
.map((line) => line.trim())
|
||||
.filter((line) => !line.endsWith(":"))
|
||||
.map((line) => line.replace(/^[^(a-zA-Z)]*/, "").trim())
|
||||
.filter((line) => line.length > 0);
|
||||
}
|
||||
|
||||
export async function summarize(content: string) {
|
||||
if (!openAI) return;
|
||||
const chatCompletion = await openAI.createChatCompletion({
|
||||
@ -63,7 +73,11 @@ export async function extractAuthorName(content: string) {
|
||||
return author;
|
||||
}
|
||||
|
||||
export async function createKeywords(type: string, description: string) {
|
||||
export async function createKeywords(
|
||||
type: string,
|
||||
description: string,
|
||||
title = "unknown",
|
||||
) {
|
||||
if (!openAI) return;
|
||||
const chatCompletion = await openAI.createChatCompletion({
|
||||
model: "gpt-3.5-turbo",
|
||||
@ -71,18 +85,21 @@ export async function createKeywords(type: string, description: string) {
|
||||
{
|
||||
"role": "system",
|
||||
"content":
|
||||
`you create some keywords that can be used in a recommendation system. The keywords are based on a ${type} description. Create a range of keywords from very specific ones that describe the general vibe. Also include some that describe the genre`,
|
||||
`you create some keywords that can be used in a recommendation system. The keywords are based on a ${type} description. Create a range of keywords from very specific ones that describe the general vibe. Also include some that describe the genre. ${
|
||||
title ? `The name of the ${type} is ${title}` : ""
|
||||
}`,
|
||||
},
|
||||
{ "role": "user", "content": description.slice(0, 2000) },
|
||||
{
|
||||
"role": "user",
|
||||
"content":
|
||||
"only return a list of around 20 keywords seperated by commas",
|
||||
"content": "return a list of around 20 keywords seperated by commas",
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
return chatCompletion.choices[0].message.content?.toLowerCase().split(", ")
|
||||
const res = chatCompletion.choices[0].message.content?.toLowerCase();
|
||||
|
||||
return extractListFromResponse(res)
|
||||
.map((v) => v.replaceAll(" ", "-"));
|
||||
}
|
||||
|
||||
@ -101,6 +118,7 @@ export async function createTags(content: string) {
|
||||
],
|
||||
});
|
||||
|
||||
return chatCompletion.choices[0].message.content?.toLowerCase().split(", ")
|
||||
.map((v) => v.replaceAll(" ", "-"));
|
||||
const res = chatCompletion.choices[0].message.content?.toLowerCase();
|
||||
|
||||
return extractListFromResponse(res).map((v) => v.replaceAll(" ", "-"));
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ type RecommendationResource = {
|
||||
type: string;
|
||||
rating: number;
|
||||
tags?: string[];
|
||||
description?: string;
|
||||
keywords?: string[];
|
||||
author?: string;
|
||||
year?: number;
|
||||
@ -17,14 +18,14 @@ export async function createRecommendationResource(
|
||||
res: GenericResource,
|
||||
description?: string,
|
||||
) {
|
||||
const cacheId = `recommendations:${res.type}:${res.id}`;
|
||||
const cacheId = `recommendations:${res.type}:${res.id.replaceAll(":", "")}`;
|
||||
const resource: RecommendationResource = await cache.get(cacheId) || {
|
||||
id: res.id,
|
||||
type: res.type,
|
||||
rating: -1,
|
||||
};
|
||||
if (description && !resource.keywords) {
|
||||
const keywords = await openai.createKeywords(res.type, description);
|
||||
const keywords = await openai.createKeywords(res.type, description, res.id);
|
||||
if (keywords?.length) {
|
||||
resource.keywords = keywords;
|
||||
}
|
||||
@ -44,6 +45,10 @@ export async function createRecommendationResource(
|
||||
resource.author = author;
|
||||
}
|
||||
|
||||
if (description) {
|
||||
resource.description = description;
|
||||
}
|
||||
|
||||
if (date) {
|
||||
const d = typeof date === "string" ? new Date(date) : date;
|
||||
resource.year = d.getFullYear();
|
||||
@ -52,6 +57,10 @@ export async function createRecommendationResource(
|
||||
cache.set(cacheId, JSON.stringify(resource));
|
||||
}
|
||||
|
||||
export function getRecommendation(id: string, type: string) {
|
||||
return cache.get(`recommendations:${type}:${id}`);
|
||||
}
|
||||
|
||||
export async function getAllRecommendations() {
|
||||
const keys = await cache.keys("recommendations:movie:*");
|
||||
return Promise.all(keys.map((k) => cache.get(k))).then((res) =>
|
||||
|
Reference in New Issue
Block a user