forked from mongodb/chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConversationsService.ts
285 lines (241 loc) · 6.72 KB
/
ConversationsService.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import {
EmbeddedContent,
ObjectId,
References,
VerifiedAnswer,
WithScore,
} from "mongodb-rag-core";
import { FunctionCall } from "@azure/openai";
export type MessageBase = {
/**
The role of the message in the conversation.
*/
role: string;
/**
Message that occurs in the conversation.
*/
content: string;
/**
Custom data received from the client to include in the Message persisted to
the database.
*/
customData?: Record<string, unknown>;
/**
Arbitrary data about the message that should be sent to the client.
*/
metadata?: Record<string, unknown>;
};
export type SystemMessage = MessageBase & {
role: "system";
};
export type AssistantMessage = MessageBase & {
role: "assistant";
/**
Set to `true` if the user liked the response, `false` if the user didn't
like the response. No value if user didn't rate the response. Note that only
messages with `role: "assistant"` can be rated.
*/
rating?: boolean;
/**
An additional text comment provided by the user to clarify their
`rating` for the message. If `rating` is undefined, this should be
too.
*/
userComment?: string;
/**
Further reading links for the message.
*/
references?: References;
functionCall?: FunctionCall;
metadata?: AssistantMessageMetadata;
};
export type AssistantMessageMetadata = {
[k: string]: unknown;
/**
If the message came from the verified answers collection, contains the
metadata about the verified answer.
*/
verifiedAnswer?: VerifiedAnswerEventData;
};
export type VerifiedAnswerEventData = Pick<
VerifiedAnswer,
"_id" | "created" | "updated"
>;
export type FunctionMessage = MessageBase & {
role: "function";
name: string;
};
export type UserMessage = MessageBase & {
role: "user";
/**
The content of the message that the LLM will use to generate a response.
This is only used for the latest user message.
For previous user messages, the LLM will use the `content` field.
*/
contentForLlm?: string;
/**
The preprocessed content of the message.
For example, the query used for vector search.
*/
preprocessedContent?: string;
/**
Content found to help generate the message.
Useful to include for evaluation purposes.
For example, you might want to assess how faithful the LLM response
to the found content.
*/
contextContent?: Partial<WithScore<EmbeddedContent>>[];
/**
Whether preprocessor suggested not to answer based on the input.
*/
rejectQuery?: boolean;
/**
The vector representation of the message content.
*/
embedding?: number[];
};
/**
Message in the {@link Conversation}.
*/
export type SomeMessage =
| UserMessage
| AssistantMessage
| SystemMessage
| FunctionMessage;
/**
Message stored in the database.
*/
export type Message = SomeMessage & {
/**
Unique identifier for the message.
*/
id: ObjectId;
/**
The date the message was created.
*/
createdAt: Date;
};
export type ConversationCustomData = Record<string, unknown> | undefined;
/**
Conversation between the user and the chatbot as stored in the database.
*/
export interface Conversation<
CustomData extends ConversationCustomData = ConversationCustomData
> {
_id: ObjectId;
/** Messages in the conversation. */
messages: Message[];
/** The date the conversation was created. */
createdAt: Date;
/** The hostname that the request originated from. */
requestOrigin?: string;
/**
Custom data to include in the Conversation persisted to the database.
You can pass this data to the {@link ConversationsService.create()} method.
*/
customData?: CustomData;
}
export type CreateConversationParams = {
initialMessages?: SomeMessage[];
customData?: ConversationCustomData;
};
export type AddSystemMessageParams = Omit<SystemMessage, "id" | "createdAt">;
export type AddUserMessageParams = Omit<UserMessage, "id" | "createdAt"> & {
customData?: Record<string, unknown>;
};
export type AddFunctionMessageParams = Omit<
FunctionMessage,
"id" | "createdAt"
> & {
customData?: Record<string, unknown>;
};
export type AddAssistantMessageParams = Omit<
AssistantMessage,
"id" | "createdAt"
>;
export type AddSomeMessageParams =
| AddSystemMessageParams
| AddUserMessageParams
| AddAssistantMessageParams
| AddFunctionMessageParams;
export type AddConversationMessageParams = {
conversationId: ObjectId;
message: AddSomeMessageParams;
};
export type AddManyConversationMessagesParams = {
conversationId: ObjectId;
messages: AddSomeMessageParams[];
};
export interface FindByIdParams {
_id: ObjectId;
}
export interface RateMessageParams {
conversationId: ObjectId;
messageId: ObjectId;
rating: boolean;
}
export interface CommentMessageParams {
conversationId: ObjectId;
messageId: ObjectId;
comment: string;
}
/**
Static responses to send in pre-defined edge case scenarios.
*/
export interface ConversationConstants {
/**
Response message sent when the user sends a message that the chatbot
that doesn't match anything in the chatbot's knowledge base.
*/
NO_RELEVANT_CONTENT: string;
/**
Response message sent when the chatbot's LLM is not working.
*/
LLM_NOT_WORKING: string;
}
/**
Service for managing {@link Conversation}s.
*/
export interface ConversationsService {
conversationConstants: ConversationConstants;
/**
Create a new {@link Conversation}.
*/
create: (params?: CreateConversationParams) => Promise<Conversation>;
/**
Add a {@link Message} to a {@link Conversation}.
*/
addConversationMessage: (
params: AddConversationMessageParams
) => Promise<Message>;
/**
Add multiple {@link Message} objects to a {@link Conversation}.
*/
addManyConversationMessages: (
params: AddManyConversationMessagesParams
) => Promise<Message[]>;
findById: ({ _id }: FindByIdParams) => Promise<Conversation | null>;
/**
Rate a {@link Message} in a {@link Conversation}.
*/
rateMessage: ({
conversationId,
messageId,
rating,
}: RateMessageParams) => Promise<boolean>;
/**
Add a user comment to an assistant {@link Message}.
*/
commentMessage: ({
conversationId,
messageId,
comment,
}: CommentMessageParams) => Promise<boolean>;
}
export const defaultConversationConstants: ConversationConstants = {
NO_RELEVANT_CONTENT: `Unfortunately, I do not know how to respond to your message.
Please try to rephrase your message. Adding more details can help me respond with a relevant answer.`,
LLM_NOT_WORKING: `Unfortunately, my chat functionality is not working at the moment,
so I cannot respond to your message. Please try again later.
However, here are some links that might provide some helpful information for your message:`,
};