Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
[lib] Add Conversation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maxijonson committed Aug 2, 2023
1 parent fd5e3bc commit de986d4
Show file tree
Hide file tree
Showing 4 changed files with 551 additions and 10 deletions.
42 changes: 42 additions & 0 deletions gpt-turbo.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"folders": [
{
"name": "lib",
"path": "packages/lib"
},
{
"name": "web",
"path": "packages/implementations/web"
},
{
"name": "stats",
"path": "packages/plugins/gpt-turbo-plugin-stats"
},
{
"name": "discord",
"path": "packages/implementations/discord"
},
{
"name": "cli",
"path": "packages/implementations/cli"
},
{
"name": "nest",
"path": "packages/implementations/nest"
},
{
"name": "root",
"path": "."
}
],
"settings": {
"jest.disabledWorkspaceFolders": [
"web",
"discord",
"stats",
"nest",
"cli",
"root"
]
}
}
15 changes: 9 additions & 6 deletions packages/lib/src/classes/ChatCompletionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,24 @@ export class ChatCompletionService {
options,
requestOptions
);
await this.moderateMessage(response);
await this.moderateMessage(response, requestOptions);
return this.history.addMessage(response);
}

/**
* @internal
* Should not be used directly by library consumers. Use `moderate` from the `Message` class instead.
*/
public async moderateMessage(message: Message) {
public async moderateMessage(
message: Message,
requestOptions?: ConversationRequestOptionsModel
) {
if (!this.config.isModerationEnabled) return;

await message.moderate(
this.config.apiKey,
this.requestOptions.getRequestOptions()
);
await message.moderate(this.config.apiKey, {
...this.requestOptions.getRequestOptions(),
...requestOptions,
});
await this.pluginService.onModeration(message);

const flags = message.flags ?? [];
Expand Down
12 changes: 9 additions & 3 deletions packages/lib/src/classes/Conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,10 @@ export class Conversation<

try {
await this.pluginService.onUserPrompt(userMessage);
await this.chatCompletionService.moderateMessage(userMessage);
await this.chatCompletionService.moderateMessage(
userMessage,
requestOptions
);
const assistantMessage =
await this.chatCompletionService.getAssistantResponse(
options,
Expand Down Expand Up @@ -246,7 +249,7 @@ export class Conversation<
if (previousUserMessageIndex < 0) break;
previousUserMessage = messages[previousUserMessageIndex];
}
if (previousUserMessage?.role !== "user") {
if (previousUserMessage.role !== "user") {
throw new Error(
`Could not find a previous user message to reprompt from (${id}).`
);
Expand Down Expand Up @@ -293,7 +296,10 @@ export class Conversation<

try {
await this.pluginService.onFunctionPrompt(functionMessage);
await this.chatCompletionService.moderateMessage(functionMessage);
await this.chatCompletionService.moderateMessage(
functionMessage,
requestOptions
);
const assistantMessage =
await this.chatCompletionService.getAssistantResponse(
options,
Expand Down
Loading

0 comments on commit de986d4

Please sign in to comment.