use claude

This commit is contained in:
Hongbo Wu
2024-04-29 17:11:20 +08:00
parent 0a2f9dba25
commit ea227b6462
3 changed files with 91 additions and 18 deletions

View File

@ -22,6 +22,7 @@
"@google-cloud/storage": "^7.0.1",
"@google-cloud/tasks": "^4.0.0",
"@graphql-tools/utils": "^9.1.1",
"@langchain/anthropic": "^0.1.16",
"@langchain/openai": "^0.0.14",
"@notionhq/client": "^2.2.14",
"@omnivore/content-handler": "1.0.0",

View File

@ -1,5 +1,6 @@
import { ChatAnthropic } from '@langchain/anthropic'
import { JsonOutputParser } from '@langchain/core/output_parsers'
import { PromptTemplate } from '@langchain/core/prompts'
import { ChatPromptTemplate, PromptTemplate } from '@langchain/core/prompts'
import { OpenAI } from '@langchain/openai'
import {
htmlToSpeechFile,
@ -234,7 +235,7 @@ const createUserProfile = async (
},
})
const contextualTemplate = PromptTemplate.fromTemplate(
const contextualTemplate = ChatPromptTemplate.fromTemplate(
digestDefinition.zeroShot.userPreferencesProfilePrompt
)
@ -362,29 +363,47 @@ const summarizeItems = async (
rankedCandidates: RankedItem[]
): Promise<RankedItem[]> => {
console.time('summarizeItems')
const llm = new OpenAI({
modelName: 'gpt-4-0125-preview',
configuration: {
apiKey: process.env.OPENAI_API_KEY,
},
// const llm = new OpenAI({
// modelName: 'gpt-4-0125-preview',
// configuration: {
// apiKey: process.env.OPENAI_API_KEY,
// },
// })
const llm = new ChatAnthropic({
apiKey: process.env.CLAUDE_API_KEY,
model: 'claude-3-sonnet-20240229',
})
const contextualTemplate = PromptTemplate.fromTemplate(
const contextualTemplate = ChatPromptTemplate.fromTemplate(
digestDefinition.summaryPrompt
)
const chain = contextualTemplate.pipe(llm)
// send all the ranked candidates to openAI at once in a batch
const summaries = await chain.batch(
rankedCandidates.map((item) => ({
title: item.libraryItem.title,
author: item.libraryItem.author ?? '',
content: item.libraryItem.readableContent, // markdown content
}))
const prompts = await Promise.all(
rankedCandidates.map(
async (item) =>
await contextualTemplate.format({
title: item.libraryItem.title,
author: item.libraryItem.author ?? '',
content: item.libraryItem.readableContent, // markdown content
})
)
)
// // send all the ranked candidates to openAI at once in a batch
// const summaries = await chain.batch(
// rankedCandidates.map((item) => ({
// title: item.libraryItem.title,
// author: item.libraryItem.author ?? '',
// content: item.libraryItem.readableContent, // markdown content
// }))
// )
const summaries = await llm.batch(prompts)
summaries.forEach(
(summary, index) => (rankedCandidates[index].summary = summary)
(summary, index) =>
(rankedCandidates[index].summary = summary.content.toString())
)
console.timeEnd('summarizeItems')

View File

@ -50,6 +50,20 @@
lodash "^4.17.21"
resize-observer-polyfill "^1.5.1"
"@anthropic-ai/sdk@^0.20.1":
version "0.20.7"
resolved "https://registry.yarnpkg.com/@anthropic-ai/sdk/-/sdk-0.20.7.tgz#b19b0e66ba070f928bbf583c06d76e6efdd93d5e"
integrity sha512-uyc+3WGLpe8ur6mSIKSab7P9JdBerTdmqb7popc/yROYLLCW/Ykyw4ZfjmN/cLmxjnAKnv5YUngzbPM0BJuGjg==
dependencies:
"@types/node" "^18.11.18"
"@types/node-fetch" "^2.6.4"
abort-controller "^3.0.0"
agentkeepalive "^4.2.1"
form-data-encoder "1.7.2"
formdata-node "^4.3.2"
node-fetch "^2.6.7"
web-streams-polyfill "^3.2.1"
"@anthropic-ai/sdk@^0.9.1":
version "0.9.1"
resolved "https://registry.yarnpkg.com/@anthropic-ai/sdk/-/sdk-0.9.1.tgz#b2d2b7bf05c90dce502c9a2e869066870f69ba88"
@ -3774,6 +3788,17 @@
dependencies:
lodash "^4.17.21"
"@langchain/anthropic@^0.1.16":
version "0.1.16"
resolved "https://registry.yarnpkg.com/@langchain/anthropic/-/anthropic-0.1.16.tgz#c2a9d3dd4e02df7118dd97cf2503c9bd1a4de5ad"
integrity sha512-vCbwkZ3pkMSKf67fBgNlslvuW9f3EZGBbO8Ic2etgX3xFl6L0WuMtfS26P1FCDpRwaKuC1BrCj2aLAeMzMq/Fg==
dependencies:
"@anthropic-ai/sdk" "^0.20.1"
"@langchain/core" "~0.1.56"
fast-xml-parser "^4.3.5"
zod "^3.22.4"
zod-to-json-schema "^3.22.4"
"@langchain/community@~0.0.33":
version "0.0.33"
resolved "https://registry.yarnpkg.com/@langchain/community/-/community-0.0.33.tgz#5568fe36b1e2f8947d49414d47e14a27da5b65c9"
@ -3803,6 +3828,24 @@
zod "^3.22.4"
zod-to-json-schema "^3.22.3"
"@langchain/core@~0.1.56":
version "0.1.61"
resolved "https://registry.yarnpkg.com/@langchain/core/-/core-0.1.61.tgz#9313363e04f1c6981a938b2909c44ce6fceb2736"
integrity sha512-C8OkAly+ugvXsL8TACCmFv9WTTcT4gvQaG6NbrXCOzibBCywfxxcTqEMOyg3zIKpxHEmR0DHqh0OiJRHocnsCg==
dependencies:
ansi-styles "^5.0.0"
camelcase "6"
decamelize "1.2.0"
js-tiktoken "^1.0.8"
langsmith "~0.1.7"
ml-distance "^4.0.0"
mustache "^4.2.0"
p-queue "^6.6.2"
p-retry "4"
uuid "^9.0.0"
zod "^3.22.4"
zod-to-json-schema "^3.22.3"
"@langchain/openai@^0.0.14":
version "0.0.14"
resolved "https://registry.yarnpkg.com/@langchain/openai/-/openai-0.0.14.tgz#27a6ba83f6b754391868b22f3b90cd440038acf0"
@ -15694,7 +15737,7 @@ fast-xml-parser@^4.2.2, fast-xml-parser@^4.3.0:
dependencies:
strnum "^1.0.5"
fast-xml-parser@^4.3.2:
fast-xml-parser@^4.3.2, fast-xml-parser@^4.3.5:
version "4.3.6"
resolved "https://registry.yarnpkg.com/fast-xml-parser/-/fast-xml-parser-4.3.6.tgz#190f9d99097f0c8f2d3a0e681a10404afca052ff"
integrity sha512-M2SovcRxD4+vC493Uc2GZVcZaj66CCJhWurC4viynVSTvrpErCShNcDz1lAho6n9REQKvL/ll4A4/fw6Y9z8nw==
@ -22822,6 +22865,11 @@ multimatch@5.0.0:
arrify "^2.0.1"
minimatch "^3.0.4"
mustache@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/mustache/-/mustache-4.2.0.tgz#e5892324d60a12ec9c2a73359edca52972bf6f64"
integrity sha512-71ippSywq5Yb7/tVYyGbkBggbU8H3u5Rz56fH60jGFgr8uHwxs+aSKeqmluIVzM0m0kB7xQjKS6qPfd0b2ZoqQ==
mute-stream@0.0.8, mute-stream@~0.0.4:
version "0.0.8"
resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.8.tgz#1630c42b2251ff81e2a283de96a5497ea92e5e0d"
@ -32217,6 +32265,11 @@ zod-to-json-schema@^3.22.3:
resolved "https://registry.yarnpkg.com/zod-to-json-schema/-/zod-to-json-schema-3.22.4.tgz#f8cc691f6043e9084375e85fb1f76ebafe253d70"
integrity sha512-2Ed5dJ+n/O3cU383xSY28cuVi0BCQhF8nYqWU5paEpl7fVdqdAmiLdqLyfblbNdfOFwFfi/mqU4O1pwc60iBhQ==
zod-to-json-schema@^3.22.4:
version "3.23.0"
resolved "https://registry.yarnpkg.com/zod-to-json-schema/-/zod-to-json-schema-3.23.0.tgz#4fc60e88d3c709eedbfaae3f92f8a7bf786469f2"
integrity sha512-az0uJ243PxsRIa2x1WmNE/pnuA05gUq/JB8Lwe1EDCCL/Fz9MgjYQ0fPlyc2Tcv6aF2ZA7WM5TWaRZVEFaAIag==
zod@^3.22.3, zod@^3.22.4:
version "3.22.4"
resolved "https://registry.yarnpkg.com/zod/-/zod-3.22.4.tgz#f31c3a9386f61b1f228af56faa9255e845cf3fff"