omnivore/packages/api/src/services/explain.ts
2024-04-22 15:52:28 -07:00

52 lines
1.2 KiB
TypeScript

import { OpenAI } from '@langchain/openai'
import { PromptTemplate } from '@langchain/core/prompts'
import { authTrx } from '../repository'
import { libraryItemRepository } from '../repository/library_item'
import { htmlToMarkdown } from '../utils/parser'
export const explainText = async (
userId: string,
text: string,
libraryItemId: string
): Promise<string> => {
const llm = new OpenAI({
modelName: 'gpt-4-0125-preview',
configuration: {
apiKey: process.env.OPENAI_API_KEY,
},
})
const libraryItem = await authTrx(
async (tx) =>
tx.withRepository(libraryItemRepository).findById(libraryItemId),
undefined,
userId
)
if (!libraryItem) {
throw 'No library item found'
}
const content = htmlToMarkdown(libraryItem.readableContent)
const contextualTemplate = PromptTemplate.fromTemplate(
`Create a brief, less than 300 character explanation of the provided
term. Use the article text for additional context.
Term: {text}
Article text: {content}
`
)
console.log('template: ', contextualTemplate)
const chain = contextualTemplate.pipe(llm)
const result = await chain.invoke({
text: text,
content,
})
console.log('result: ', result)
return result
}