-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add chat completion * chore: self mutation Signed-off-by: github-actions <[email protected]> --------- Signed-off-by: github-actions <[email protected]> Co-authored-by: github-actions <[email protected]>
- Loading branch information
Showing
12 changed files
with
308 additions
and
52 deletions.
There are no files selected for viewing
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
27 changes: 27 additions & 0 deletions
27
src/conversation-prompt/prompts/create-conversation-chat-completion-prompt.ts
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { ChatCompletionRequestMessage } from "openai"; | ||
import { renderAIPersona } from "./render-ai-persona"; | ||
import { renderConversationForChat } from "./render-conversation-for-chat"; | ||
import { AIPersona, Conversation } from "../../types"; | ||
|
||
const CONVERSATION_SUMMARY_SUFFIX = `The conversations starts with a detailed summary of a previous conversation. While answering questions, take this summary into account. Summary:`; | ||
|
||
export type CreateConversationChatCompletionPromptInput = { | ||
aiPersona: AIPersona; | ||
conversation: Conversation; | ||
}; | ||
|
||
export function createConversationChatCompletionPrompt({ | ||
aiPersona, | ||
conversation, | ||
}: CreateConversationChatCompletionPromptInput): Array<ChatCompletionRequestMessage> { | ||
const systemMessage = { | ||
role: "system" as const, | ||
content: renderAIPersona(aiPersona), | ||
}; | ||
|
||
if (conversation.summary) { | ||
systemMessage.content += `\n\n${CONVERSATION_SUMMARY_SUFFIX} ${conversation.summary}`; | ||
} | ||
|
||
return [systemMessage, ...renderConversationForChat(conversation)]; | ||
} |
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,10 +1,9 @@ | ||
import { AIPersona } from "../../types"; | ||
import { BOT_MENTION } from "../mention"; | ||
import { ASSISTANT_MENTION } from "../mention"; | ||
|
||
export const renderAIPersona = (aiPersona: AIPersona): string => | ||
`Instructions for ${BOT_MENTION}, this is how you should behave in a conversation, but this is not your personality:\n` + | ||
`Your name is "${aiPersona.name}". You are referenced in conversations as "${BOT_MENTION}".\n` + | ||
`Instructions for ${ASSISTANT_MENTION}, this is how you should behave in a conversation, but this is not your personality:\n` + | ||
`Your name is "${aiPersona.name}". You are referenced in conversations as "${ASSISTANT_MENTION}".\n` + | ||
aiPersona.instructions + | ||
`\n\nThis is your personality:\n` + | ||
aiPersona.personality + | ||
"\n"; | ||
aiPersona.personality; |
17 changes: 17 additions & 0 deletions
17
src/conversation-prompt/prompts/render-conversation-for-chat.ts
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { ChatCompletionRequestMessage } from "openai"; | ||
import { Conversation } from "../../types"; | ||
import { buildMention } from "../mention"; | ||
|
||
export const renderConversationForChat = ({ | ||
messages, | ||
}: Conversation): Array<ChatCompletionRequestMessage> => { | ||
return messages.map((message): ChatCompletionRequestMessage => { | ||
const author = message.author; | ||
|
||
return { | ||
role: author.type === "BOT" ? "assistant" : "user", | ||
name: buildMention(author).replace(/[<>@]/g, ""), | ||
content: message.text, | ||
}; | ||
}); | ||
}; |
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
128 changes: 128 additions & 0 deletions
128
test/conversation-prompt/prompts/create-conversation-chat-completion-prompt.test.ts
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 |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import { | ||
AIPersona, | ||
Author, | ||
ASSISTANT_MENTION, | ||
buildMention, | ||
} from "../../../src"; | ||
import { createConversationChatCompletionPrompt } from "../../../src/conversation-prompt/prompts/create-conversation-chat-completion-prompt"; | ||
|
||
describe("createConversationChatCompletionPrompt", () => { | ||
const aiPersona: AIPersona = { | ||
name: "Lenard", | ||
instructions: `When providing code examples, use triple backticks.`, | ||
personality: `You are a software engineer.`, | ||
}; | ||
const authors: Record<string, Author> = { | ||
bot: { type: "BOT" }, | ||
exampleUser1: { type: "USER", id: "EU01" }, | ||
exampleUser2: { type: "USER", id: "EU02" }, | ||
user1: { type: "USER", id: "U01" }, | ||
}; | ||
|
||
it("should work without summary", () => { | ||
expect( | ||
createConversationChatCompletionPrompt({ | ||
aiPersona, | ||
conversation: { | ||
messages: [ | ||
{ | ||
author: authors.user1, | ||
text: "hello!", | ||
}, | ||
{ | ||
author: authors.bot, | ||
text: "hello! how can I help you?", | ||
}, | ||
{ | ||
author: authors.user1, | ||
text: "can you write me fibonacci function in Typescript?", | ||
}, | ||
], | ||
}, | ||
}) | ||
).toMatchInlineSnapshot(` | ||
[ | ||
{ | ||
"content": "Instructions for <@assistant>, this is how you should behave in a conversation, but this is not your personality: | ||
Your name is "Lenard". You are referenced in conversations as "<@assistant>". | ||
When providing code examples, use triple backticks. | ||
This is your personality: | ||
You are a software engineer.", | ||
"role": "system", | ||
}, | ||
{ | ||
"content": "hello!", | ||
"name": "U01", | ||
"role": "user", | ||
}, | ||
{ | ||
"content": "hello! how can I help you?", | ||
"name": "assistant", | ||
"role": "assistant", | ||
}, | ||
{ | ||
"content": "can you write me fibonacci function in Typescript?", | ||
"name": "U01", | ||
"role": "user", | ||
}, | ||
] | ||
`); | ||
}); | ||
|
||
it("should work with summary", () => { | ||
expect( | ||
createConversationChatCompletionPrompt({ | ||
aiPersona, | ||
conversation: { | ||
summary: `${buildMention( | ||
authors.user1 | ||
)} asked ${ASSISTANT_MENTION} whether it knows Typescript.`, | ||
messages: [ | ||
{ | ||
author: authors.user1, | ||
text: "hello!", | ||
}, | ||
{ | ||
author: authors.bot, | ||
text: "hello! how can I help you?", | ||
}, | ||
{ | ||
author: authors.user1, | ||
text: "can you write me fibonacci function in Typescript?", | ||
}, | ||
], | ||
}, | ||
}) | ||
).toMatchInlineSnapshot(` | ||
[ | ||
{ | ||
"content": "Instructions for <@assistant>, this is how you should behave in a conversation, but this is not your personality: | ||
Your name is "Lenard". You are referenced in conversations as "<@assistant>". | ||
When providing code examples, use triple backticks. | ||
This is your personality: | ||
You are a software engineer. | ||
The conversations starts with a detailed summary of a previous conversation. While answering questions, take this summary into account. Summary: <@U01> asked <@assistant> whether it knows Typescript.", | ||
"role": "system", | ||
}, | ||
{ | ||
"content": "hello!", | ||
"name": "U01", | ||
"role": "user", | ||
}, | ||
{ | ||
"content": "hello! how can I help you?", | ||
"name": "assistant", | ||
"role": "assistant", | ||
}, | ||
{ | ||
"content": "can you write me fibonacci function in Typescript?", | ||
"name": "U01", | ||
"role": "user", | ||
}, | ||
] | ||
`); | ||
}); | ||
}); |
Oops, something went wrong.