Skip to content

Commit

Permalink
Fixing issue on how options were being applied to the request. (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
glecaros authored Aug 14, 2024
1 parent b16639f commit 5d138c9
Showing 1 changed file with 25 additions and 30 deletions.
55 changes: 25 additions & 30 deletions sdk/js/packages/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
getClient,
isKeyCredential,
KeyCredential,
operationOptionsToRequestParameters,
randomUUID,
RequestParameters,
TokenCredential,
Expand Down Expand Up @@ -111,6 +112,7 @@ export class AIChatProtocolClient {
messages: AIChatMessage[],
options: AIChatCompletionOperationOptions,
): RequestParameters {
const request = operationOptionsToRequestParameters(options);
if (messages.some((message) => message.files && message.files.length > 0)) {
const binaryParts = messages
.map((message, index) => {
Expand All @@ -130,36 +132,31 @@ export class AIChatProtocolClient {
})
.flat();
const boundary = `---Part-${randomUUID()}`;
return {
contentType: `multipart/form-data; boundary=${boundary}`,
body: [
{
dispositionType: "form-data; name=json",
contentType: "application/json",
body: {
messages: messages.map((message) => ({
...message,
files: undefined,
})),
context: options.context,
sessionState: options.sessionState,
},
request.contentType = `multipart/form-data; boundary=${boundary}`;
request.body = [
{
dispositionType: "form-data; name=json",
contentType: "application/json",
body: {
messages: messages.map((message) => ({
...message,
files: undefined,
})),
context: options.context,
sessionState: options.sessionState,
},
...binaryParts.filter((part) => part !== undefined),
],
};
} else {
return {
headers: {
"Content-Type": "application/json",
},
body: {
messages: messages,
context: options.context,
sessionState: options.sessionState,
},
...binaryParts.filter((part) => part !== undefined),
];
} else {
request.contentType = "application/json";
request.body = {
messages: messages,
context: options.context,
sessionState: options.sessionState,
};
}
return request;
}

/**
Expand All @@ -174,9 +171,7 @@ export class AIChatProtocolClient {
options: AIChatCompletionOperationOptions = {},
): Promise<AIChatCompletion> {
const request = this.prepareRequest(messages, options);
const response = await this.client
.path(this.basePath)
.post(request, options);
const response = await this.client.path(this.basePath).post(request);
const { status, body } = response;
if (!/2\d\d/.test(status)) {
handleFailedRequest(status, body);
Expand All @@ -197,7 +192,7 @@ export class AIChatProtocolClient {
): Promise<AsyncIterable<AIChatCompletionDelta>> {
const request = this.prepareRequest(messages, options);
const response = await asStream(
this.client.path(`${this.basePath}/stream`).post(request, options),
this.client.path(`${this.basePath}/stream`).post(request),
);
if (!/2\d\d/.test(response.status)) {
const body = await getStreamContent(response.body);
Expand Down

0 comments on commit 5d138c9

Please sign in to comment.