Skip to content

Commit

Permalink
chore: support convertAudioPartsToTextParts in google model
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangvvo committed Nov 7, 2024
1 parent 02f9d62 commit b025801
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
3 changes: 3 additions & 0 deletions javascript/src/anthropic/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ export interface AnthropicModelOptions {
baseURL?: string;
apiKey: string;
modelId: string;
/**
* If the AudioPart has a transcript, convert it to an TextPart
*/
convertAudioPartsToTextParts?: boolean;
}
21 changes: 16 additions & 5 deletions javascript/src/google/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import type {
ToolResultPart,
} from "../schemas/index.js";
import { mapAudioFormatToMimeType } from "../utils/audio.utils.js";
import { convertAudioPartsToTextParts } from "../utils/message.utils.js";
import type { InternalContentDelta } from "../utils/stream.utils.js";
import { ContentDeltaAccumulator } from "../utils/stream.utils.js";
import { calculateCost } from "../utils/usage.utils.js";
Expand All @@ -47,7 +48,10 @@ export class GoogleModel implements LanguageModel {

private genModel: GenerativeModel;

constructor(options: GoogleModelOptions, metadata?: LanguageModelMetadata) {
constructor(
public options: GoogleModelOptions,
metadata?: LanguageModelMetadata,
) {
this.provider = "google";
this.modelId = options.modelId;
if (metadata) this.metadata = metadata;
Expand All @@ -60,7 +64,7 @@ export class GoogleModel implements LanguageModel {

async generate(input: GoogleLanguageModelInput): Promise<ModelResponse> {
const result = await this.genModel.generateContent(
convertToGoogleParams(input),
convertToGoogleParams(input, this.options),
);

const candidate = result.response.candidates?.[0];
Expand All @@ -84,7 +88,7 @@ export class GoogleModel implements LanguageModel {
input: LanguageModelInput,
): AsyncGenerator<PartialModelResponse, ModelResponse> {
const { stream } = await this.genModel.generateContentStream(
convertToGoogleParams(input),
convertToGoogleParams(input, this.options),
);

let usage: ModelUsage | undefined;
Expand Down Expand Up @@ -127,6 +131,7 @@ export class GoogleModel implements LanguageModel {

export function convertToGoogleParams(
input: GoogleLanguageModelInput,
options: GoogleModelOptions,
): GenerateContentRequest {
const toolConfig = convertToGoogleToolConfig(input.toolChoice);

Expand All @@ -136,7 +141,7 @@ export function convertToGoogleParams(
...(!!input.systemPrompt && {
systemInstruction: input.systemPrompt,
}),
contents: convertToGoogleMessages(input.messages),
contents: convertToGoogleMessages(input.messages, options),
...(input.tools && {
tools: convertToGoogleTools(input.tools),
}),
Expand All @@ -151,7 +156,13 @@ export function convertToGoogleParams(
};
}

export function convertToGoogleMessages(messages: Message[]): Content[] {
export function convertToGoogleMessages(
messages: Message[],
options: GoogleModelOptions,
): Content[] {
if (options.convertAudioPartsToTextParts) {
messages = messages.map(convertAudioPartsToTextParts);
}
return messages.map((message): Content => {
const parts = message.content.map((part): GooglePart => {
switch (part.type) {
Expand Down
4 changes: 4 additions & 0 deletions javascript/src/google/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export interface GoogleModelOptions {
apiKey: string;
modelId: string;
/**
* If the AudioPart has a transcript, convert it to an TextPart
*/
convertAudioPartsToTextParts?: boolean;
}

0 comments on commit b025801

Please sign in to comment.