Skip to content

Commit

Permalink
Merge pull request openai#11 from akkadaska/feature/codeinterpreter-o…
Browse files Browse the repository at this point in the history
…utput-integration

Integrate codeInterpreter output: fix file download link and display images in chat
  • Loading branch information
ibigio committed May 3, 2024
2 parents 1fa3266 + 2131970 commit ce8e942
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
14 changes: 14 additions & 0 deletions app/api/files/[fileId]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { openai } from "@/app/openai";

// download file by file ID
export async function GET(_request, { params: { fileId } }) {
const [file, fileContent] = await Promise.all([
openai.files.retrieve(fileId),
openai.files.content(fileId),
]);
return new Response(fileContent.body, {
headers: {
"Content-Disposition": `attachment; filename="${file.filename}"`,
},
});
}
7 changes: 7 additions & 0 deletions app/components/chat.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@
align-self: flex-start;
border-radius: 15px;
max-width: 80%;
overflow-wrap: break-word;
}

.assistantMessage img {
max-width: 100%;
margin: 8px 0px 8px 0px;
border-radius: 8px;
}

.userMessage {
Expand Down
34 changes: 33 additions & 1 deletion app/components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,19 @@ const Chat = ({

// textDelta - append text to last assistant message
const handleTextDelta = (delta) => {
appendToLastMessage(delta.value);
if (delta.value != null) {
appendToLastMessage(delta.value);
};
if (delta.annotations != null) {
annotateLastMessage(delta.annotations);
}
};

// imageFileDone - show image in chat
const handleImageFileDone = (image) => {
appendToLastMessage(`\n![${image.file_id}](/api/files/${image.file_id})\n`);
}

// toolCallCreated - log new tool call
const toolCallCreated = (toolCall) => {
if (toolCall.type != "code_interpreter") return;
Expand Down Expand Up @@ -183,6 +193,9 @@ const Chat = ({
stream.on("textCreated", handleTextCreated);
stream.on("textDelta", handleTextDelta);

// image
stream.on("imageFileDone", handleImageFileDone);

// code interpreter
stream.on("toolCallCreated", toolCallCreated);
stream.on("toolCallDelta", toolCallDelta);
Expand Down Expand Up @@ -216,6 +229,25 @@ const Chat = ({
setMessages((prevMessages) => [...prevMessages, { role, text }]);
};

const annotateLastMessage = (annotations) => {
setMessages((prevMessages) => {
const lastMessage = prevMessages[prevMessages.length - 1];
const updatedLastMessage = {
...lastMessage,
};
annotations.forEach((annotation) => {
if (annotation.type === 'file_path') {
updatedLastMessage.text = updatedLastMessage.text.replaceAll(
annotation.text,
`/api/files/${annotation.file_path.file_id}`
);
}
})
return [...prevMessages.slice(0, -1), updatedLastMessage];
});

}

return (
<div className={styles.chatContainer}>
<div className={styles.messages}>
Expand Down

0 comments on commit ce8e942

Please sign in to comment.