Skip to content

Updated ChatCompletionMessageParam to support Structured Messages and… #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/lib/AbstractChatCompletionRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
type ChatCompletionMessageParam,
type CompletionCreateParams,
type ChatCompletionTool,
type ChatCompletionStructuredMessageText,
type ChatCompletionStructuredMessageImageURL,
} from 'together-ai/resources/chat/completions';
import { APIUserAbortError, TogetherError } from 'together-ai/error';
import {
Expand Down Expand Up @@ -214,15 +216,15 @@
return completion;
}

#getFinalContent(): string | null {
#getFinalContent(): string | (ChatCompletionStructuredMessageText | ChatCompletionStructuredMessageImageURL)[] | null {

Check failure on line 219 in src/lib/AbstractChatCompletionRunner.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `·string·|·(ChatCompletionStructuredMessageText·|·ChatCompletionStructuredMessageImageURL)[]·|·null·` with `⏎····|·string⏎····|·(ChatCompletionStructuredMessageText·|·ChatCompletionStructuredMessageImageURL)[]⏎····|·null`
return this.#getFinalMessage().content ?? null;
}

/**
* @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<string | null> {
async finalContent(): Promise<string | (ChatCompletionStructuredMessageText | ChatCompletionStructuredMessageImageURL)[] | null> {

Check failure on line 227 in src/lib/AbstractChatCompletionRunner.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `string·|·(ChatCompletionStructuredMessageText·|·ChatCompletionStructuredMessageImageURL)[]·|·null` with `⏎····string·|·(ChatCompletionStructuredMessageText·|·ChatCompletionStructuredMessageImageURL)[]·|·null⏎··`
await this.done();
return this.#getFinalContent();
}
Expand Down Expand Up @@ -270,7 +272,7 @@
return this.#getFinalFunctionCall();
}

#getFinalFunctionCallResult(): string | undefined {
#getFinalFunctionCallResult(): string | (ChatCompletionStructuredMessageText | ChatCompletionStructuredMessageImageURL)[] | undefined {

Check failure on line 275 in src/lib/AbstractChatCompletionRunner.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `·string·|·(ChatCompletionStructuredMessageText·|·ChatCompletionStructuredMessageImageURL)[]` with `⏎····|·string⏎····|·(ChatCompletionStructuredMessageText·|·ChatCompletionStructuredMessageImageURL)[]⏎···`
for (let i = this.messages.length - 1; i >= 0; i--) {
const message = this.messages[i];
if (isFunctionMessage(message) && message.content != null) {
Expand All @@ -292,7 +294,7 @@
return;
}

async finalFunctionCallResult(): Promise<string | undefined> {
async finalFunctionCallResult(): Promise<string | (ChatCompletionStructuredMessageText | ChatCompletionStructuredMessageImageURL)[] | undefined> {

Check failure on line 297 in src/lib/AbstractChatCompletionRunner.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `string·|·(ChatCompletionStructuredMessageText·|·ChatCompletionStructuredMessageImageURL)[]·|·undefined` with `⏎····string·|·(ChatCompletionStructuredMessageText·|·ChatCompletionStructuredMessageImageURL)[]·|·undefined⏎··`
await this.done();
return this.#getFinalFunctionCallResult();
}
Expand Down Expand Up @@ -674,12 +676,12 @@
functionCall: (functionCall: ChatCompletionMessage.FunctionCall) => void;
message: (message: ChatCompletionMessageParam) => void;
chatCompletion: (completion: ChatCompletion) => void;
finalContent: (contentSnapshot: string) => void;
finalContent: (contentSnapshot: string | (ChatCompletionStructuredMessageText | ChatCompletionStructuredMessageImageURL)[]) => void;

Check failure on line 679 in src/lib/AbstractChatCompletionRunner.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `contentSnapshot:·string·|·(ChatCompletionStructuredMessageText·|·ChatCompletionStructuredMessageImageURL)[]` with `⏎····contentSnapshot:⏎······|·string⏎······|·(ChatCompletionStructuredMessageText·|·ChatCompletionStructuredMessageImageURL)[],⏎··`
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;

Check failure on line 684 in src/lib/AbstractChatCompletionRunner.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `content:·string·|·(ChatCompletionStructuredMessageText·|·ChatCompletionStructuredMessageImageURL)[]` with `⏎····content:·string·|·(ChatCompletionStructuredMessageText·|·ChatCompletionStructuredMessageImageURL)[],⏎··`
error: (error: TogetherError) => void;
abort: (error: APIUserAbortError) => void;
end: () => void;
Expand Down
45 changes: 39 additions & 6 deletions src/resources/chat/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@ export namespace ChatCompletion {
export interface ChatCompletionAssistantMessageParam {
role: 'assistant';

content?: string | null;
content?:
| string
| Array<
| ChatCompletionsAPI.ChatCompletionStructuredMessageText
| ChatCompletionsAPI.ChatCompletionStructuredMessageImageURL
>
| null;

/**
* @deprecated
Expand Down Expand Up @@ -123,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
Expand Down Expand Up @@ -160,7 +172,13 @@ export interface ChatCompletionFunctionMessageParam {
}

export interface ChatCompletionMessage {
content: string | null;
content:
| string
| Array<
| ChatCompletionsAPI.ChatCompletionStructuredMessageText
| ChatCompletionsAPI.ChatCompletionStructuredMessageImageURL
>
| null;

role: 'assistant';

Expand Down Expand Up @@ -191,7 +209,12 @@ export type ChatCompletionMessageParam =
| ChatCompletionFunctionMessageParam;

export interface ChatCompletionSystemMessageParam {
content: string;
content:
| string
| Array<
| ChatCompletionsAPI.ChatCompletionStructuredMessageText
| ChatCompletionsAPI.ChatCompletionStructuredMessageImageURL
>;

role: 'system';

Expand All @@ -215,7 +238,12 @@ export namespace ChatCompletionTool {
}

export interface ChatCompletionToolMessageParam {
content?: string;
content?:
| string
| Array<
| ChatCompletionsAPI.ChatCompletionStructuredMessageText
| ChatCompletionsAPI.ChatCompletionStructuredMessageImageURL
>;

required?: unknown;

Expand Down Expand Up @@ -254,7 +282,12 @@ export interface ChatCompletionUsage {
}

export interface ChatCompletionUserMessageParam {
content: string;
content:
| string
| Array<
| ChatCompletionsAPI.ChatCompletionStructuredMessageText
| ChatCompletionsAPI.ChatCompletionStructuredMessageImageURL
>;

role: 'user';

Expand Down
Loading