generated from obsidianmd/obsidian-sample-plugin
/
formatChat.ts
43 lines (37 loc) · 1.11 KB
/
formatChat.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
import type OpenAI from 'openai'
import { OPEN_AI_CHAT_COMPLETION_OBJECT_TYPE } from '../constants'
import {
SYSTEM_MESSAGE_OBJECT_TYPE,
USER_MESSAGE_OBJECT_TYPE
} from '../../../constants'
import type { Conversation } from '../../conversation'
import type { UserPrompt, SystemMessage } from '../../../types'
const formatChat = (
conversation: Conversation
): OpenAI.Chat.ChatCompletionMessage[] => {
const messages = conversation.getConversationMessages()
return messages.map((message) => {
switch (message.message.object) {
case USER_MESSAGE_OBJECT_TYPE:
return {
role: 'user',
content: (message.message as UserPrompt).prompt
}
case OPEN_AI_CHAT_COMPLETION_OBJECT_TYPE:
return (
(message.message as OpenAI.Chat.ChatCompletion)?.choices?.[0]
?.message ?? {
role: 'assistant',
content: ''
}
)
case SYSTEM_MESSAGE_OBJECT_TYPE:
default:
return {
role: 'system',
content: (message.message as SystemMessage).output
}
}
})
}
export default formatChat