Skip to content

Commit 67c190f

Browse files
committed
📦 feat(package.json, botservice.ts): added image processing library sharp and implemented image resizing and format conversion
To conform to the image formats and sizes supported by the OpenAI vision model, we use the sharp library to resize and convert the format of images. This ensures that images uploaded by users meet the requirements of the vision model, preventing errors.
1 parent 8f5b7d1 commit 67c190f

File tree

5 files changed

+543
-31
lines changed

5 files changed

+543
-31
lines changed

dist/botservice.mjs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,17 @@ import FormData from "form-data";
9494
// src/openai-wrapper.ts
9595
import OpenAI from "openai";
9696
var apiKey = process.env["OPENAI_API_KEY"];
97-
var config = { apiKey };
9897
var azureOpenAiApiKey = process.env["AZURE_OPENAI_API_KEY"];
98+
var model = process.env["OPENAI_MODEL_NAME"] ?? "gpt-3.5-turbo";
99+
var MAX_TOKENS = Number(process.env["OPENAI_MAX_TOKENS"] ?? 2e3);
100+
var temperature = Number(process.env["OPENAI_TEMPERATURE"] ?? 1);
101+
var visionModel = process.env["OPENAI_VISION_MODEL_NAME"] ?? "gpt-4-vision-preview";
102+
if (!apiKey && !azureOpenAiApiKey) {
103+
openAILog.error("OPENAI_API_KEY or AZURE_OPENAI_API_KEY is not set");
104+
process.exit(1);
105+
}
106+
openAILog.debug({ model, max_tokens: MAX_TOKENS, temperature });
107+
var config = { apiKey };
99108
if (azureOpenAiApiKey) {
100109
config = {
101110
apiKey: azureOpenAiApiKey,
@@ -109,6 +118,7 @@ var openaiImage;
109118
if (azureOpenAiApiKey) {
110119
if (!apiKey) {
111120
openaiImage = new OpenAI({
121+
// Azureは東海岸しかDALL-Eが無いので新規に作る。TODO: ここだけ東海岸にする
112122
apiKey: azureOpenAiApiKey,
113123
baseURL: `https://${process.env["AZURE_OPENAI_API_INSTANCE_NAME"]}.openai.azure.com/openai`,
114124
defaultQuery: { "api-version": process.env["AZURE_OPENAI_API_VERSION"] ?? "2023-08-01-preview" },
@@ -118,10 +128,6 @@ if (azureOpenAiApiKey) {
118128
openaiImage = new OpenAI({ apiKey });
119129
}
120130
}
121-
var model = process.env["OPENAI_MODEL_NAME"] ?? "gpt-3.5-turbo";
122-
var MAX_TOKENS = Number(process.env["OPENAI_MAX_TOKENS"] ?? 2e3);
123-
var temperature = Number(process.env["OPENAI_TEMPERATURE"] ?? 1);
124-
openAILog.debug({ model, max_tokens: MAX_TOKENS, temperature });
125131
var plugins = /* @__PURE__ */ new Map();
126132
var functions = [];
127133
function registerChatPlugin(plugin) {
@@ -211,7 +217,6 @@ async function createChatCompletion(messages, functions2 = void 0) {
211217
let tools = false;
212218
let currentOpenAi = openai;
213219
let currentModel = model;
214-
const visionModel = process.env["OPENAI_VISION_MODEL_NAME"];
215220
if (visionModel) {
216221
messages.some((message) => {
217222
if (typeof message.content !== "string") {
@@ -702,6 +707,7 @@ ${(typeof messages[1].content === "string" ? messages[1].content : messages[1].c
702707
}
703708

704709
// src/botservice.ts
710+
import sharp from "sharp";
705711
if (!global.FormData) {
706712
global.FormData = FormData3;
707713
}
@@ -796,8 +802,30 @@ async function getBase64Image(url) {
796802
matterMostLog.error(`Fech Image URL HTTP error! status: ${response.status}`);
797803
return "";
798804
}
799-
const buffer = Buffer.from(await response.arrayBuffer());
800-
const mimeType = response.headers.get("content-type");
805+
let buffer = Buffer.from(await response.arrayBuffer());
806+
let { width = 0, height = 0, format = "" } = await sharp(buffer).metadata();
807+
if (!["png", "jpeg", "webp", "gif"].includes(format)) {
808+
matterMostLog.warn(`Unsupported image format: ${format}. Converting to JPEG.`);
809+
buffer = await sharp(buffer).jpeg().toBuffer();
810+
format = "jpeg";
811+
}
812+
const shortEdge = 768;
813+
const longEdge = 1024;
814+
if (width > longEdge || height > longEdge) {
815+
const resizeRatio = longEdge / Math.max(width, height);
816+
width *= resizeRatio;
817+
height *= resizeRatio;
818+
}
819+
if (Math.min(width, height) > shortEdge) {
820+
const resizeRatio = shortEdge / Math.min(width, height);
821+
width *= resizeRatio;
822+
height *= resizeRatio;
823+
}
824+
buffer = await sharp(buffer).resize({
825+
width: Math.round(width),
826+
height: Math.round(height)
827+
}).toBuffer();
828+
const mimeType = `image/${format}`;
801829
const base64 = buffer.toString("base64");
802830
const dataURL = "data:" + mimeType + ";base64," + base64;
803831
return dataURL;

0 commit comments

Comments
 (0)