Skip to content

Commit 15b53ce

Browse files
committed
Adding an optional display (header) file name to OpenAIService.uploadFile function. If not specified a full path is used as a file name instead.
1 parent 5a2ffca commit 15b53ce

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

openai-client/src/main/scala/io/cequence/openaiscala/service/OpenAIServiceImpl.scala

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ private class OpenAIServiceImpl(
197197
): Future[ImageInfo] =
198198
execPOSTMultipart(
199199
Command.images_edits,
200-
fileParams = Seq(Tag.image -> image) ++ mask.map((Tag.mask ->_)),
200+
fileParams = Seq((Tag.image, image, None)) ++ mask.map((Tag.mask, _, None)),
201201
bodyParams = Seq(
202202
Tag.prompt -> Some(prompt),
203203
Tag.n -> settings.n,
@@ -215,7 +215,7 @@ private class OpenAIServiceImpl(
215215
): Future[ImageInfo] =
216216
execPOSTMultipart(
217217
Command.images_variations,
218-
fileParams = Seq(Tag.image -> image),
218+
fileParams = Seq((Tag.image, image, None)),
219219
bodyParams = Seq(
220220
Tag.n -> settings.n,
221221
Tag.size -> settings.size.map(_.toString),
@@ -254,7 +254,7 @@ private class OpenAIServiceImpl(
254254
): Future[TranscriptResponse] =
255255
execPOSTMultipartWithStatusString(
256256
Command.audio_transcriptions,
257-
fileParams = Seq(Tag.file -> file),
257+
fileParams = Seq((Tag.file, file, None)),
258258
bodyParams = Seq(
259259
Tag.prompt -> prompt,
260260
Tag.model -> Some(settings.model),
@@ -271,7 +271,7 @@ private class OpenAIServiceImpl(
271271
): Future[TranscriptResponse] =
272272
execPOSTMultipartWithStatusString(
273273
Command.audio_translations,
274-
fileParams = Seq(Tag.file -> file),
274+
fileParams = Seq((Tag.file, file, None)),
275275
bodyParams = Seq(
276276
Tag.prompt -> prompt,
277277
Tag.model -> Some(settings.model),
@@ -323,11 +323,12 @@ private class OpenAIServiceImpl(
323323

324324
override def uploadFile(
325325
file: File,
326-
settings: UploadFileSettings = DefaultSettings.UploadFile
326+
displayFileName: Option[String],
327+
settings: UploadFileSettings
327328
): Future[FileInfo] =
328329
execPOSTMultipart(
329330
Command.files,
330-
fileParams = Seq(Tag.file -> file),
331+
fileParams = Seq((Tag.file, file, displayFileName)),
331332
bodyParams = Seq(
332333
Tag.purpose -> Some(settings.purpose)
333334
)
@@ -478,7 +479,7 @@ private class OpenAIServiceImpl(
478479

479480
override def createModeration(
480481
input: String,
481-
settings: CreateModerationSettings = DefaultSettings.CreateModeration
482+
settings: CreateModerationSettings
482483
): Future[ModerationResponse] =
483484
execPOST(
484485
Command.moderations,

openai-client/src/main/scala/io/cequence/openaiscala/service/ws/WSRequestHelper.scala

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,28 @@ trait WSRequestHelper extends WSHelper {
8686
// POST //
8787
//////////
8888

89+
/**
90+
* @param fileParams - the third param in a tuple is a display (header) file name
91+
*/
8992
protected def execPOSTMultipart(
9093
endPoint: PEP,
9194
endPointParam: Option[String] = None,
9295
params: Seq[(PT, Option[Any])] = Nil,
93-
fileParams: Seq[(PT, File)] = Nil,
96+
fileParams: Seq[(PT, File, Option[String])] = Nil,
9497
bodyParams: Seq[(PT, Option[Any])] = Nil
9598
): Future[JsValue] =
9699
execPOSTMultipartWithStatus(
97100
endPoint, endPointParam, params, fileParams, bodyParams
98101
).map(handleErrorResponse)
99102

103+
/**
104+
* @param fileParams - the third param in a tuple is a display (header) file name
105+
*/
100106
protected def execPOSTMultipartWithStatus(
101107
endPoint: PEP,
102108
endPointParam: Option[String] = None,
103109
params: Seq[(PT, Option[Any])] = Nil,
104-
fileParams: Seq[(PT, File)] = Nil,
110+
fileParams: Seq[(PT, File, Option[String])] = Nil,
105111
bodyParams: Seq[(PT, Option[Any])] = Nil,
106112
acceptableStatusCodes: Seq[Int] = defaultAcceptableStatusCodes
107113
): Future[RichJsResponse] = {
@@ -113,11 +119,14 @@ trait WSRequestHelper extends WSHelper {
113119
execPOSTJsonAux(request, formData, Some(endPoint), acceptableStatusCodes)
114120
}
115121

122+
/**
123+
* @param fileParams - the third param in a tuple is a display (header) file name
124+
*/
116125
protected def execPOSTMultipartWithStatusString(
117126
endPoint: PEP,
118127
endPointParam: Option[String] = None,
119128
params: Seq[(PT, Option[Any])] = Nil,
120-
fileParams: Seq[(PT, File)] = Nil,
129+
fileParams: Seq[(PT, File, Option[String])] = Nil,
121130
bodyParams: Seq[(PT, Option[Any])] = Nil,
122131
acceptableStatusCodes: Seq[Int] = defaultAcceptableStatusCodes
123132
): Future[RichStringResponse] = {
@@ -131,15 +140,16 @@ trait WSRequestHelper extends WSHelper {
131140

132141
// create a multipart form data holder contain classic data (key-value) parts as well as file parts
133142
private def createMultipartFormData(
134-
fileParams: Seq[(PT, File)] = Nil,
143+
fileParams: Seq[(PT, File, Option[String])] = Nil,
135144
bodyParams: Seq[(PT, Option[Any])] = Nil
136145
) = MultipartFormData(
137146
dataParts = bodyParams.collect { case (key, Some(value)) =>
138147
(key.toString, Seq(value.toString))
139148
}.toMap,
140149

141-
// TODO: we can potentially use here header-file-names as well (if provided as function's params)
142-
files = fileParams.map { case (key, file) => FilePart(key.toString, file.getPath) }
150+
files = fileParams.map { case (key, file, headerFileName) =>
151+
FilePart(key.toString, file.getPath, headerFileName)
152+
}
143153
)
144154

145155
protected def execPOST(

openai-core/src/main/scala/io/cequence/openaiscala/service/OpenAIService.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ trait OpenAIService extends OpenAIServiceConsts {
213213
*/
214214
def uploadFile(
215215
file: File,
216+
displayFileName: Option[String] = None,
216217
settings: UploadFileSettings = DefaultSettings.UploadFile
217218
): Future[FileInfo]
218219

0 commit comments

Comments
 (0)