Skip to content

Commit d92f622

Browse files
committed
openai-chat fixes
1 parent b1a6f91 commit d92f622

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

components/openai/actions/chat/chat.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export default {
4343
images: {
4444
label: "Images",
4545
type: "string[]",
46-
description: "Provide one or more images to [OpenAI's vision model](https://platform.openai.com/docs/guides/vision). Each entry should be either a file URL or a path to a file in the `/tmp` directory (for example, `/tmp/myFile.jpg`) Compatible with the `gpt4-vision-preview` model",
46+
description: "Provide one or more images to [OpenAI's vision model](https://platform.openai.com/docs/guides/vision). Each entry should be either a file URL or a path to a file in the `/tmp` directory (for example, `/tmp/myFile.jpg`), or raw base64-encoded image data. Compatible with the `gpt4-vision-preview` model",
4747
optional: true,
4848
},
4949
audio: {

components/openai/actions/common/common.mjs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {
2-
ConfigurationError, getFileStream,
2+
ConfigurationError, getFileStreamAndMetadata,
33
} from "@pipedream/platform";
44
import constants from "../../common/constants.mjs";
55
import { parse } from "../../common/helpers.mjs";
@@ -85,27 +85,40 @@ export default {
8585
let content = [];
8686
if (this.images) {
8787
for (const image of this.images) {
88-
const stream = await getFileStream(image);
89-
const chunks = [];
90-
for await (const chunk of stream) {
91-
chunks.push(chunk);
88+
let base64Image = image;
89+
let imageType = "image/jpeg";
90+
if (image.startsWith("http") || image.includes("tmp/")) {
91+
const {
92+
stream, metadata,
93+
} = await getFileStreamAndMetadata(image);
94+
const chunks = [];
95+
for await (const chunk of stream) {
96+
chunks.push(chunk);
97+
}
98+
base64Image = Buffer.concat(chunks).toString("base64");
99+
if (metadata.contentType) imageType = metadata.contentType;
92100
}
93-
const base64Image = Buffer.concat(chunks).toString("base64");
94101
content.push({
95-
"type": "input_image",
96-
"image_url": `data:image/jpeg;base64,${base64Image}`,
102+
"type": "image_url",
103+
"image_url": {
104+
"url": base64Image.startsWith("data:")
105+
? base64Image
106+
: `data:${imageType};base64,${base64Image}`,
107+
},
97108
});
98109
}
99110
}
100111

101112
if (this.audio) {
102-
const stream = await getFileStream(this.audio);
113+
const {
114+
stream, metadata,
115+
} = await getFileStreamAndMetadata(this.audio);
103116
const chunks = [];
104117
for await (const chunk of stream) {
105118
chunks.push(chunk);
106119
}
107120
const fileContent = Buffer.concat(chunks).toString("base64");
108-
const extension = this.audio.match(/\.(\w+)$/)?.[1];
121+
const extension = metadata.name.split(".").pop();
109122
content.push({
110123
type: "input_audio",
111124
input_audio: {

0 commit comments

Comments
 (0)