From 95b53e24d46a57579a7d5e01d2bb5fd74a596bf9 Mon Sep 17 00:00:00 2001 From: mpatankar Date: Fri, 3 Jan 2025 14:37:07 -0500 Subject: [PATCH 1/5] Updated ChatCompletionMessageParam to support Structured Messages and Image URLs --- src/resources/chat/completions.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/resources/chat/completions.ts b/src/resources/chat/completions.ts index 26fcf5c6..772cc880 100644 --- a/src/resources/chat/completions.ts +++ b/src/resources/chat/completions.ts @@ -67,7 +67,7 @@ export namespace ChatCompletion { export interface ChatCompletionAssistantMessageParam { role: 'assistant'; - content?: string | null; + content?: string | Array | null; /** * @deprecated @@ -191,7 +191,7 @@ export type ChatCompletionMessageParam = | ChatCompletionFunctionMessageParam; export interface ChatCompletionSystemMessageParam { - content: string; + content: string | Array; role: 'system'; @@ -215,7 +215,7 @@ export namespace ChatCompletionTool { } export interface ChatCompletionToolMessageParam { - content?: string; + content?: string | Array; required?: unknown; @@ -254,7 +254,7 @@ export interface ChatCompletionUsage { } export interface ChatCompletionUserMessageParam { - content: string; + content: string | Array; role: 'user'; From 03bc73b83db9a11a49cc7fb5f6e603c2f57cb5e7 Mon Sep 17 00:00:00 2001 From: mpatankar Date: Fri, 3 Jan 2025 17:13:01 -0500 Subject: [PATCH 2/5] Updated content union formatting to pass linter --- src/resources/chat/completions.ts | 51 ++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/src/resources/chat/completions.ts b/src/resources/chat/completions.ts index 772cc880..cc0d334d 100644 --- a/src/resources/chat/completions.ts +++ b/src/resources/chat/completions.ts @@ -67,7 +67,13 @@ export namespace ChatCompletion { export interface ChatCompletionAssistantMessageParam { role: 'assistant'; - content?: string | Array | null; + content?: + | string + | Array< + | ChatCompletionsAPI.ChatCompletionStructuredMessageText + | ChatCompletionsAPI.ChatCompletionStructuredMessageImageURL + > + | null; /** * @deprecated @@ -191,7 +197,12 @@ export type ChatCompletionMessageParam = | ChatCompletionFunctionMessageParam; export interface ChatCompletionSystemMessageParam { - content: string | Array; + content: + | string + | Array< + | ChatCompletionsAPI.ChatCompletionStructuredMessageText + | ChatCompletionsAPI.ChatCompletionStructuredMessageImageURL + >; role: 'system'; @@ -215,7 +226,12 @@ export namespace ChatCompletionTool { } export interface ChatCompletionToolMessageParam { - content?: string | Array; + content?: + | string + | Array< + | ChatCompletionsAPI.ChatCompletionStructuredMessageText + | ChatCompletionsAPI.ChatCompletionStructuredMessageImageURL + >; required?: unknown; @@ -254,7 +270,12 @@ export interface ChatCompletionUsage { } export interface ChatCompletionUserMessageParam { - content: string | Array; + content: + | string + | Array< + | ChatCompletionsAPI.ChatCompletionStructuredMessageText + | ChatCompletionsAPI.ChatCompletionStructuredMessageImageURL + >; role: 'user'; @@ -275,12 +296,12 @@ export interface CompletionCreateParamsBase { * [See all of Together AI's chat models](https://docs.together.ai/docs/serverless-models#chat-models) */ model: - | 'Qwen/Qwen2.5-72B-Instruct-Turbo' - | 'Qwen/Qwen2.5-7B-Instruct-Turbo' - | 'meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo' - | 'meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo' - | 'meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo' - | (string & {}); + | 'Qwen/Qwen2.5-72B-Instruct-Turbo' + | 'Qwen/Qwen2.5-7B-Instruct-Turbo' + | 'meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo' + | 'meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo' + | 'meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo' + | (string & {}); /** * If true, the response will contain the prompt. Can be used with `logprobs` to @@ -412,11 +433,11 @@ export namespace CompletionCreateParams { * format. */ content: - | string - | Array< - | ChatCompletionsAPI.ChatCompletionStructuredMessageText - | ChatCompletionsAPI.ChatCompletionStructuredMessageImageURL - >; + | string + | Array< + | ChatCompletionsAPI.ChatCompletionStructuredMessageText + | ChatCompletionsAPI.ChatCompletionStructuredMessageImageURL + >; /** * The role of the messages author. Choice between: system, user, or assistant. From c9c17078afa07a7a67955cf4c09774d7b38dcc7c Mon Sep 17 00:00:00 2001 From: mpatankar Date: Fri, 3 Jan 2025 17:36:07 -0500 Subject: [PATCH 3/5] Added spaces to content union for formatting --- src/resources/chat/completions.ts | 54 +++++++++++++++---------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/src/resources/chat/completions.ts b/src/resources/chat/completions.ts index cc0d334d..6dcb483f 100644 --- a/src/resources/chat/completions.ts +++ b/src/resources/chat/completions.ts @@ -68,12 +68,12 @@ export interface ChatCompletionAssistantMessageParam { role: 'assistant'; content?: - | string - | Array< - | ChatCompletionsAPI.ChatCompletionStructuredMessageText - | ChatCompletionsAPI.ChatCompletionStructuredMessageImageURL - > - | null; + | string + | Array< + | ChatCompletionsAPI.ChatCompletionStructuredMessageText + | ChatCompletionsAPI.ChatCompletionStructuredMessageImageURL + > + | null; /** * @deprecated @@ -198,11 +198,11 @@ export type ChatCompletionMessageParam = export interface ChatCompletionSystemMessageParam { content: - | string - | Array< - | ChatCompletionsAPI.ChatCompletionStructuredMessageText - | ChatCompletionsAPI.ChatCompletionStructuredMessageImageURL - >; + | string + | Array< + | ChatCompletionsAPI.ChatCompletionStructuredMessageText + | ChatCompletionsAPI.ChatCompletionStructuredMessageImageURL + >; role: 'system'; @@ -227,11 +227,11 @@ export namespace ChatCompletionTool { export interface ChatCompletionToolMessageParam { content?: - | string - | Array< - | ChatCompletionsAPI.ChatCompletionStructuredMessageText - | ChatCompletionsAPI.ChatCompletionStructuredMessageImageURL - >; + | string + | Array< + | ChatCompletionsAPI.ChatCompletionStructuredMessageText + | ChatCompletionsAPI.ChatCompletionStructuredMessageImageURL + >; required?: unknown; @@ -271,11 +271,11 @@ export interface ChatCompletionUsage { export interface ChatCompletionUserMessageParam { content: - | string - | Array< - | ChatCompletionsAPI.ChatCompletionStructuredMessageText - | ChatCompletionsAPI.ChatCompletionStructuredMessageImageURL - >; + | string + | Array< + | ChatCompletionsAPI.ChatCompletionStructuredMessageText + | ChatCompletionsAPI.ChatCompletionStructuredMessageImageURL + >; role: 'user'; @@ -296,12 +296,12 @@ export interface CompletionCreateParamsBase { * [See all of Together AI's chat models](https://docs.together.ai/docs/serverless-models#chat-models) */ model: - | 'Qwen/Qwen2.5-72B-Instruct-Turbo' - | 'Qwen/Qwen2.5-7B-Instruct-Turbo' - | 'meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo' - | 'meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo' - | 'meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo' - | (string & {}); + | 'Qwen/Qwen2.5-72B-Instruct-Turbo' + | 'Qwen/Qwen2.5-7B-Instruct-Turbo' + | 'meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo' + | 'meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo' + | 'meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo' + | (string & {}); /** * If true, the response will contain the prompt. Can be used with `logprobs` to From 939d7c35c8824fb2b2178450895aa4e69e2168cb Mon Sep 17 00:00:00 2001 From: mpatankar Date: Fri, 3 Jan 2025 17:53:10 -0500 Subject: [PATCH 4/5] Added Union types to ChatCompletionMessage --- src/resources/chat/completions.ts | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/resources/chat/completions.ts b/src/resources/chat/completions.ts index 6dcb483f..ba318aaa 100644 --- a/src/resources/chat/completions.ts +++ b/src/resources/chat/completions.ts @@ -166,7 +166,13 @@ export interface ChatCompletionFunctionMessageParam { } export interface ChatCompletionMessage { - content: string | null; + content: + | string + | Array< + | ChatCompletionsAPI.ChatCompletionStructuredMessageText + | ChatCompletionsAPI.ChatCompletionStructuredMessageImageURL + > + | null; role: 'assistant'; @@ -433,11 +439,11 @@ export namespace CompletionCreateParams { * format. */ content: - | string - | Array< - | ChatCompletionsAPI.ChatCompletionStructuredMessageText - | ChatCompletionsAPI.ChatCompletionStructuredMessageImageURL - >; + | string + | Array< + | ChatCompletionsAPI.ChatCompletionStructuredMessageText + | ChatCompletionsAPI.ChatCompletionStructuredMessageImageURL + >; /** * The role of the messages author. Choice between: system, user, or assistant. From 8f871c2c363cd92de337c69591bcc484f1e049f9 Mon Sep 17 00:00:00 2001 From: mpatankar Date: Fri, 3 Jan 2025 18:36:06 -0500 Subject: [PATCH 5/5] Added Union types to Completion Choices --- src/lib/AbstractChatCompletionRunner.ts | 14 ++++++++------ src/resources/chat/completions.ts | 8 +++++++- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/lib/AbstractChatCompletionRunner.ts b/src/lib/AbstractChatCompletionRunner.ts index 28875e70..67052d48 100644 --- a/src/lib/AbstractChatCompletionRunner.ts +++ b/src/lib/AbstractChatCompletionRunner.ts @@ -6,6 +6,8 @@ import { type ChatCompletionMessageParam, type CompletionCreateParams, type ChatCompletionTool, + type ChatCompletionStructuredMessageText, + type ChatCompletionStructuredMessageImageURL, } from 'together-ai/resources/chat/completions'; import { APIUserAbortError, TogetherError } from 'together-ai/error'; import { @@ -214,7 +216,7 @@ export abstract class AbstractChatCompletionRunner< return completion; } - #getFinalContent(): string | null { + #getFinalContent(): string | (ChatCompletionStructuredMessageText | ChatCompletionStructuredMessageImageURL)[] | null { return this.#getFinalMessage().content ?? null; } @@ -222,7 +224,7 @@ export abstract class AbstractChatCompletionRunner< * @returns a promise that resolves with the content of the final ChatCompletionMessage, or rejects * if an error occurred or the stream ended prematurely without producing a ChatCompletionMessage. */ - async finalContent(): Promise { + async finalContent(): Promise { await this.done(); return this.#getFinalContent(); } @@ -270,7 +272,7 @@ export abstract class AbstractChatCompletionRunner< return this.#getFinalFunctionCall(); } - #getFinalFunctionCallResult(): string | undefined { + #getFinalFunctionCallResult(): string | (ChatCompletionStructuredMessageText | ChatCompletionStructuredMessageImageURL)[] | undefined { for (let i = this.messages.length - 1; i >= 0; i--) { const message = this.messages[i]; if (isFunctionMessage(message) && message.content != null) { @@ -292,7 +294,7 @@ export abstract class AbstractChatCompletionRunner< return; } - async finalFunctionCallResult(): Promise { + async finalFunctionCallResult(): Promise { await this.done(); return this.#getFinalFunctionCallResult(); } @@ -674,12 +676,12 @@ export interface AbstractChatCompletionRunnerEvents { functionCall: (functionCall: ChatCompletionMessage.FunctionCall) => void; message: (message: ChatCompletionMessageParam) => void; chatCompletion: (completion: ChatCompletion) => void; - finalContent: (contentSnapshot: string) => void; + finalContent: (contentSnapshot: string | (ChatCompletionStructuredMessageText | ChatCompletionStructuredMessageImageURL)[]) => void; finalMessage: (message: ChatCompletionMessageParam) => void; finalChatCompletion: (completion: ChatCompletion) => void; finalFunctionCall: (functionCall: ChatCompletionMessage.FunctionCall) => void; functionCallResult: (content: string) => void; - finalFunctionCallResult: (content: string) => void; + finalFunctionCallResult: (content: string | (ChatCompletionStructuredMessageText | ChatCompletionStructuredMessageImageURL)[]) => void; error: (error: TogetherError) => void; abort: (error: APIUserAbortError) => void; end: () => void; diff --git a/src/resources/chat/completions.ts b/src/resources/chat/completions.ts index ba318aaa..e4684db3 100644 --- a/src/resources/chat/completions.ts +++ b/src/resources/chat/completions.ts @@ -129,7 +129,13 @@ export namespace ChatCompletionChunk { export interface Delta { role: 'system' | 'user' | 'assistant' | 'function' | 'tool'; - content?: string | null; + content?: + | string + | Array< + | ChatCompletionsAPI.ChatCompletionStructuredMessageText + | ChatCompletionsAPI.ChatCompletionStructuredMessageImageURL + > + | null; /** * @deprecated