-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: start using @wisegpt/gpt-conversation-prompt library
- Loading branch information
Showing
11 changed files
with
201 additions
and
223 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export const BOT_USER_ID = "bot-user-id"; | ||
|
||
export type User = { | ||
userId: string; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { CreateCompletionRequest } from "openai"; | ||
import { | ||
AIPersona, | ||
ModelConfiguration, | ||
} from "@wisegpt/gpt-conversation-prompt"; | ||
|
||
export const SEPARATOR_TOKEN = "<|endofstatement|>"; | ||
|
||
export type Persona = { | ||
name: string; | ||
basePrompt: string; | ||
baseCompletionRequest: CreateCompletionRequest; | ||
export type Persona = AIPersona & { | ||
configName: string; | ||
modelConfiguration: ModelConfiguration; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,41 @@ | ||
import { ConversationPromptService } from "@wisegpt/gpt-conversation-prompt"; | ||
import { Configuration, OpenAIApi } from "openai"; | ||
import config from "../../config"; | ||
import { Message } from "../../domain/conversation/conversation.dto"; | ||
import { getPersonaByConfigName } from "../../domain/persona"; | ||
import { | ||
Persona, | ||
SEPARATOR_TOKEN, | ||
} from "../../domain/persona/base-persona/base-persona.dto"; | ||
BOT_USER_ID, | ||
Message, | ||
} from "../../domain/conversation/conversation.dto"; | ||
import { getPersonaByConfigName } from "../../domain/persona"; | ||
import { Persona } from "../../domain/persona/base-persona/base-persona.dto"; | ||
import { OpenAiSecretsService } from "../secrets/open-ai-secrets.service"; | ||
|
||
type AIResponse = { | ||
text: string; | ||
usage: { | ||
promptTokens: number; | ||
completionTokens: number; | ||
totalTokens: number; | ||
}; | ||
}; | ||
|
||
export class OpenAIService { | ||
constructor( | ||
private readonly openAiSecretsService = new OpenAiSecretsService(), | ||
private readonly persona: Persona = getPersonaByConfigName( | ||
config.conversation.persona | ||
config.conversation.personaConfigName | ||
) | ||
) {} | ||
|
||
async askForResponse(messages: Message[]): Promise<AIResponse> { | ||
const prompt = | ||
this.persona.basePrompt + | ||
messages | ||
.map( | ||
(message) => | ||
`${ | ||
message.author.userId === config.bot.userId | ||
? config.bot.name | ||
: "Human" | ||
}: ${message.text}` | ||
) | ||
.join(` ${SEPARATOR_TOKEN}\n`) + | ||
`\n${config.bot.name}: `; | ||
|
||
// TODO: take out secret retrieval to outside of this class | ||
async askForResponse(messages: Message[]) { | ||
const { apiKey } = await this.openAiSecretsService.retrieve(); | ||
const api = new OpenAIApi(new Configuration({ apiKey })); | ||
|
||
const { data } = await api.createCompletion({ | ||
...this.persona.baseCompletionRequest, | ||
prompt, | ||
const openAIApi = new OpenAIApi(new Configuration({ apiKey })); | ||
const conversationPromptService = new ConversationPromptService(openAIApi); | ||
|
||
return conversationPromptService.conversationCompletion({ | ||
prompt: { | ||
conversation: { | ||
messages: messages.map(({ text, author }) => ({ | ||
text, | ||
author: | ||
author.userId === BOT_USER_ID | ||
? { type: "BOT" } | ||
: { type: "USER", id: author.userId }, | ||
})), | ||
}, | ||
aiPersona: this.persona, | ||
}, | ||
modelConfiguration: this.persona.modelConfiguration, | ||
}); | ||
|
||
const text = data.choices?.[0].text!.trim().replace(SEPARATOR_TOKEN, ""); | ||
|
||
const { | ||
prompt_tokens: promptTokens, | ||
completion_tokens: completionTokens, | ||
total_tokens: totalTokens, | ||
} = data.usage!; | ||
|
||
return { text, usage: { promptTokens, completionTokens, totalTokens } }; | ||
} | ||
} |