2023-09-08 13:33:29 +02:00
import { OpenAI } from "https://deno.land/x/openai@1.4.2/mod.ts" ;
2023-08-01 21:35:21 +02:00
import { OPENAI_API_KEY } from "@lib/env.ts" ;
const openAI = OPENAI_API_KEY && new OpenAI ( OPENAI_API_KEY ) ;
export async function summarize ( content : string ) {
if ( ! openAI ) return ;
const chatCompletion = await openAI . createChatCompletion ( {
model : "gpt-3.5-turbo" ,
messages : [
{ "role" : "system" , "content" : "You are a helpful assistant." } ,
{ "role" : "user" , "content" : content . slice ( 0 , 2000 ) } ,
{
"role" : "user" ,
"content" :
"Please summarize the article in one sentence as short as possible" ,
} ,
] ,
} ) ;
return chatCompletion . choices [ 0 ] . message . content ;
}
export async function shortenTitle ( content : string ) {
if ( ! openAI ) return ;
const chatCompletion = await openAI . createChatCompletion ( {
model : "gpt-3.5-turbo" ,
messages : [
{ "role" : "system" , "content" : "You are a helpful assistant." } ,
{ "role" : "user" , "content" : content . slice ( 0 , 2000 ) } ,
{
"role" : "user" ,
"content" :
"Please shorten the provided website title as much as possible, don't rewrite it, just remove unneccesary informations. Please remove for example any mention of the name of the website." ,
} ,
] ,
} ) ;
return chatCompletion . choices [ 0 ] . message . content ;
}
export async function extractAuthorName ( content : string ) {
if ( ! openAI ) return ;
const chatCompletion = await openAI . createChatCompletion ( {
model : "gpt-3.5-turbo" ,
messages : [
{ "role" : "system" , "content" : "You are a helpful assistant." } ,
{ "role" : "user" , "content" : content . slice ( 0 , 2000 ) } ,
{
"role" : "user" ,
"content" :
"If you are able to extract the name of the author from the text please respond with the name, otherwise respond with 'not found'" ,
} ,
] ,
} ) ;
const author = chatCompletion . choices [ 0 ] . message . content ;
2023-08-02 15:56:33 +02:00
if (
author ? . toLowerCase ( ) . includes ( "not" ) &&
author ? . toLowerCase ( ) . includes ( "found" )
) return "" ;
return author ;
2023-08-01 21:35:21 +02:00
}
2023-09-08 13:33:29 +02:00
export async function createKeywords ( type : string , description : string ) {
if ( ! openAI ) return ;
const chatCompletion = await openAI . createChatCompletion ( {
model : "gpt-3.5-turbo" ,
messages : [
{
"role" : "system" ,
"content" :
` you create some general vibey keywords to use in a recommendation system based on a ${ type } description ` ,
} ,
{ "role" : "user" , "content" : description . slice ( 0 , 2000 ) } ,
{
"role" : "user" ,
"content" : "return a list of keywords seperated by commas" ,
} ,
] ,
} ) ;
return chatCompletion . choices [ 0 ] . message . content ? . toLowerCase ( ) . split ( ", " )
. map ( ( v ) = > v . replaceAll ( " " , "-" ) ) ;
}
2023-08-01 21:35:21 +02:00
export async function createTags ( content : string ) {
if ( ! openAI ) return ;
const chatCompletion = await openAI . createChatCompletion ( {
model : "gpt-3.5-turbo" ,
messages : [
{ "role" : "system" , "content" : "You are a helpful assistant." } ,
{ "role" : "user" , "content" : content . slice ( 0 , 2000 ) } ,
{
"role" : "user" ,
"content" :
"Please respond with a list of genres the corresponding article falls into, dont include any other informations, just a comma seperated list of top 5 categories. Please respond only with tags that make sense even if there are less than five." ,
} ,
] ,
} ) ;
return chatCompletion . choices [ 0 ] . message . content ? . toLowerCase ( ) . split ( ", " )
. map ( ( v ) = > v . replaceAll ( " " , "-" ) ) ;
}