-
Notifications
You must be signed in to change notification settings - Fork 1
/
docbot.ts
176 lines (155 loc) · 5.55 KB
/
docbot.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import path from 'path'
import type { NextApiRequest, NextApiResponse } from 'next'
import cors from 'cors'
import { OpenAIChat } from 'langchain/llms'
import { ChatVectorDBQAChain } from 'langchain/chains'
import { HNSWLib } from 'langchain/vectorstores'
import { OpenAIEmbeddings } from 'langchain/embeddings'
const WCPayDocsStore = path.resolve('./public', `vectorstores/wcpay-docs`)
function getTernaryString(value: boolean | undefined) {
if (value === undefined) {
return 'unknown'
}
return value ? 'yes' : 'no'
}
function getLanguageFromLocale(locale: string) {
const formattedLocale = locale.substring(0, 2).toLowerCase()
try {
return new Intl.Locale(formattedLocale).language
} catch (e) {
return 'unknown'
}
}
function getCountryFromCode(countryCode: string) {
try {
return new Intl.Locale('en', { region: countryCode }).region
} catch (e) {
return 'unknown'
}
}
function getDepositsScheduleString(
depositsState: WCPayQARequestProps['deposits']
) {
if (depositsState?.interval === 'weekly') {
return `weekly on ${depositsState?.weekly_anchor}`
}
if (depositsState?.interval === 'monthly') {
if (depositsState?.monthly_anchor === 31) {
return 'monthly on the last day of month'
}
return `monthly on day of month ${
depositsState?.monthly_anchor || 'unknown'
}`
}
return depositsState?.interval || 'unknown'
}
async function generateAnswer({
question,
store = WCPayDocsStore,
locale,
country,
deposits,
has_active_loan,
has_previous_loans,
isLive,
supportedCurrencies,
instantDepositsEligible,
hasOverdueRequirements,
hasPendingRequirements,
currency,
depositDestination,
chatHistory,
}: WCPayQARequestProps): Promise<WCPayQAResponseProps> {
// Prepare the prompt
const systemMessage = `
You are a friendly WooCommerce Payments support engineer who will answer questions for merchants.
Format your response using markdown, including markdown-compatible links to documentation relevant to your answer where appropriate.
Don't make any assumptions about the merchant's account but tailor your answer to the account details.
Always consider if the merchant is eligible for a feature before answering questions about it.
If you don't know the answer, just say that you don't know, don't try to make up an answer.
Pay extra attention to the merchant's eligibility for specific features, such as instant deposits, in-person payments and built-in subscriptions.
`.replace(/\n\s+/g, ' ')
const userMessage = `
Today is ${new Date().toDateString()}.
You're talking to a merchant and you have the following details about their WooCommerce Payments account:
Currencies accepted by this account: ${
supportedCurrencies?.join(', ') || 'unknown'
}.
Account is a live account (not a test account): ${getTernaryString(isLive)}.
Account has an active Capital loan: ${getTernaryString(has_active_loan)}.
Account has had previous Capital loans: ${getTernaryString(
has_previous_loans
)}.
Account has overdue requirements: ${getTernaryString(hasOverdueRequirements)}.
Account has pending requirements: ${getTernaryString(hasPendingRequirements)}.
Has the account completed the 7 day new account waiting period: ${getTernaryString(
deposits?.completed_waiting_period
)}.
Account has the following deposit schedule set (when the available balance deposit will be dispatched to the merchant's bank): ${getDepositsScheduleString(
deposits
)}.
Account has the following deposit pending period (the number of days payments received will be held before being included in the available balance): ${
deposits?.delay_days || 'unknown'
} days.
${instantDepositsEligible ? 'Account is eligible for instant deposits.' : ''}
Account deposits received via bank account or debit card: ${
depositDestination || 'unknown'
}.
`
// Translate the prompt to the user's language
if (locale && getLanguageFromLocale(locale) !== 'unknown') {
question += ` (Respond in language: ${getLanguageFromLocale(locale || '')})`
}
question += ` (Account country: ${getCountryFromCode(country || '')})`
question += ` (Account currency: ${currency || 'unknown'})`
// Initialize the LLM to use to answer the question
const model = new OpenAIChat({
modelName: 'gpt-3.5-turbo',
temperature: 0.1, // Low temperature results in less creativity, more factual
prefixMessages: [
{ role: 'system', content: systemMessage },
{
role: 'user',
content: userMessage,
},
],
cache: false,
})
// Load the vectorstore
const vectorStore = await HNSWLib.load(store, new OpenAIEmbeddings())
// Create the chain
const chain = ChatVectorDBQAChain.fromLLM(model, vectorStore)
chain.returnSourceDocuments = true
// Ask it a question
console.log(question)
const start = Date.now()
type modelResponse = {
text: string
sourceDocuments: SourceDocument[]
}
console.log('ChatHistory length: ' + chatHistory?.length)
const modelResponse = (await chain.call({
question,
chat_history: chatHistory,
})) as modelResponse
const end = Date.now()
const answerDuration = end - start
console.log(`Answered in ${answerDuration}ms`)
return {
answer: modelResponse.text.trim(),
sources: modelResponse.sourceDocuments,
answerDuration,
}
}
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
cors()(req, res, async () => {
const body: WCPayQARequestProps = req.body
const answer = await generateAnswer({
...body,
})
res.status(200).json({ ...answer })
})
}