@@ -94,8 +94,17 @@ import FormData from "form-data";
94
94
// src/openai-wrapper.ts
95
95
import OpenAI from "openai" ;
96
96
var apiKey = process . env [ "OPENAI_API_KEY" ] ;
97
- var config = { apiKey } ;
98
97
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 } ;
99
108
if ( azureOpenAiApiKey ) {
100
109
config = {
101
110
apiKey : azureOpenAiApiKey ,
@@ -109,6 +118,7 @@ var openaiImage;
109
118
if ( azureOpenAiApiKey ) {
110
119
if ( ! apiKey ) {
111
120
openaiImage = new OpenAI ( {
121
+ // Azureは東海岸しかDALL-Eが無いので新規に作る。TODO: ここだけ東海岸にする
112
122
apiKey : azureOpenAiApiKey ,
113
123
baseURL : `https://${ process . env [ "AZURE_OPENAI_API_INSTANCE_NAME" ] } .openai.azure.com/openai` ,
114
124
defaultQuery : { "api-version" : process . env [ "AZURE_OPENAI_API_VERSION" ] ?? "2023-08-01-preview" } ,
@@ -118,10 +128,6 @@ if (azureOpenAiApiKey) {
118
128
openaiImage = new OpenAI ( { apiKey } ) ;
119
129
}
120
130
}
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 } ) ;
125
131
var plugins = /* @__PURE__ */ new Map ( ) ;
126
132
var functions = [ ] ;
127
133
function registerChatPlugin ( plugin ) {
@@ -211,7 +217,6 @@ async function createChatCompletion(messages, functions2 = void 0) {
211
217
let tools = false ;
212
218
let currentOpenAi = openai ;
213
219
let currentModel = model ;
214
- const visionModel = process . env [ "OPENAI_VISION_MODEL_NAME" ] ;
215
220
if ( visionModel ) {
216
221
messages . some ( ( message ) => {
217
222
if ( typeof message . content !== "string" ) {
@@ -702,6 +707,7 @@ ${(typeof messages[1].content === "string" ? messages[1].content : messages[1].c
702
707
}
703
708
704
709
// src/botservice.ts
710
+ import sharp from "sharp" ;
705
711
if ( ! global . FormData ) {
706
712
global . FormData = FormData3 ;
707
713
}
@@ -796,8 +802,30 @@ async function getBase64Image(url) {
796
802
matterMostLog . error ( `Fech Image URL HTTP error! status: ${ response . status } ` ) ;
797
803
return "" ;
798
804
}
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 } ` ;
801
829
const base64 = buffer . toString ( "base64" ) ;
802
830
const dataURL = "data:" + mimeType + ";base64," + base64 ;
803
831
return dataURL ;
0 commit comments