-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move fetching thread messages logic to separate file
- Loading branch information
Showing
3 changed files
with
37 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import OpenAI from "openai" | ||
import { MessageContentImageFile, MessageContentText, MessageListParams, ThreadMessage } from "openai/resources/beta/threads" | ||
import { Message } from "../types/interfaces"; | ||
|
||
type Conversation = Message[]; | ||
|
||
export async function fetchThreadConversation(openai: OpenAI, threadId: string, config: MessageListParams) { | ||
const pages = await openai.beta.threads.messages.list(threadId, config) | ||
let messages: ThreadMessage[] = [] | ||
|
||
for await (const page of pages.iterPages()) { | ||
messages = messages.concat(page.getPaginatedItems()) | ||
} | ||
|
||
function isMessageContentText(message: MessageContentText | MessageContentImageFile): message is MessageContentText { | ||
return message.type === "text" | ||
} | ||
|
||
const formattedMessages: Conversation = messages.map(message => { | ||
const textMessage: MessageContentText = message.content.filter(isMessageContentText)[0] | ||
return { | ||
role: message.role, | ||
content: textMessage.text.value | ||
} | ||
}) | ||
|
||
return formattedMessages | ||
} |
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