-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
104 additions
and
90 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
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,29 +1,43 @@ | ||
import s from 'dedent' | ||
import { | ||
ChatCompletionAssistantMessageParam, | ||
ChatCompletionMessageParam, | ||
ChatCompletionSystemMessageParam, | ||
ChatCompletionToolMessageParam, | ||
ChatCompletionUserMessageParam, | ||
} from 'openai/resources/index.js' | ||
|
||
import { Message } from './types.js' | ||
export type Response = ChatCompletionAssistantMessageParam | ||
export const response = (content: string): Response => { | ||
return { | ||
role: 'assistant', | ||
content, | ||
} | ||
} | ||
|
||
export type Request = ChatCompletionUserMessageParam | ||
export const request = (content: string): Request => { | ||
return { | ||
role: 'user', | ||
content, | ||
} | ||
} | ||
|
||
const groupMessagePairs = <T>(messages: T[]): T[][] => { | ||
return messages.reduce((pairs: T[][], message: T, index: number) => { | ||
if (index % 2 === 0) { | ||
pairs.push([message]) | ||
} else { | ||
pairs[pairs.length - 1].push(message) | ||
} | ||
return pairs | ||
}, []) | ||
export type System = ChatCompletionSystemMessageParam | ||
export const system = (content: string): System => { | ||
return { | ||
role: 'system', | ||
content, | ||
} | ||
} | ||
|
||
/** | ||
* Chat conversation is stored as an array of [task, result, task, result...] pairs. | ||
* This function groups them and returns them as an array of steps. | ||
*/ | ||
export const getSteps = (conversation: Message[]): Message[] => { | ||
const steps = groupMessagePairs(conversation) | ||
return steps.map(([task, result]) => ({ | ||
role: 'user' as const, | ||
content: s` | ||
Step name: ${task.content} | ||
Step result: ${result.content} | ||
`, | ||
})) | ||
export type Tool = ChatCompletionToolMessageParam | ||
export const toolResult = (toolCallId: string, content: string): Tool => { | ||
return { | ||
role: 'tool', | ||
tool_call_id: toolCallId, | ||
content, | ||
} | ||
} | ||
|
||
export type Message = ChatCompletionMessageParam | ||
export type Conversation = [Request, ...Message[]] |
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
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