From 85ef0a96eb5f237731def152184e6aaa169c6fa6 Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Wed, 25 Sep 2024 17:10:21 -0700 Subject: [PATCH 1/9] Add responseLogProbs and logProbs parameters to generateContentReq --- src/methods/generate-content.test.ts | 2 ++ src/models/generative-model.test.ts | 12 ++++++++++-- src/models/generative-model.ts | 11 +++++++++++ types/requests.ts | 13 +++++++++++-- 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/methods/generate-content.test.ts b/src/methods/generate-content.test.ts index 964ce8f..0920f43 100644 --- a/src/methods/generate-content.test.ts +++ b/src/methods/generate-content.test.ts @@ -44,6 +44,8 @@ const fakeRequestParams: GenerateContentRequest = { ], presencePenalty: 0.5, frequencyPenalty: 0.1, + responseLogProbs: true, + logProbs: 2, }; describe("generateContent()", () => { diff --git a/src/models/generative-model.test.ts b/src/models/generative-model.test.ts index 3ab0387..5f83681 100644 --- a/src/models/generative-model.test.ts +++ b/src/models/generative-model.test.ts @@ -73,6 +73,8 @@ describe("GenerativeModel", () => { ], presencePenalty: 0.6, frequencyPenalty: 0.5, + responseLogProbs: true, + logProbs: 2, tools: [{ functionDeclarations: [{ name: "myfunc" }] }], toolConfig: { functionCallingConfig: { mode: FunctionCallingMode.NONE }, @@ -122,7 +124,9 @@ describe("GenerativeModel", () => { value.includes("testField") && value.includes(HarmBlockThreshold.BLOCK_LOW_AND_ABOVE) && value.includes("presencePenalty") && - value.includes("frequencyPenalty") + value.includes("frequencyPenalty") && + value.includes("responseLogProbs") && + value.includes("logProbs") ); }), match((value) => { @@ -218,6 +222,8 @@ describe("GenerativeModel", () => { ], presencePenalty: 0.6, frequencyPenalty: 0.5, + responseLogProbs: true, + logProbs: 2, contents: [{ role: "user", parts: [{ text: "hello" }] }], tools: [{ functionDeclarations: [{ name: "otherfunc" }] }], toolConfig: { functionCallingConfig: { mode: FunctionCallingMode.AUTO } }, @@ -238,7 +244,9 @@ describe("GenerativeModel", () => { !value.includes("testField") && value.includes(HarmCategory.HARM_CATEGORY_HARASSMENT) && value.includes("presencePenalty") && - value.includes("frequencyPenalty") + value.includes("frequencyPenalty") && + value.includes("responseLogProbs") && + value.includes("logProbs") ); }), {}, diff --git a/src/models/generative-model.ts b/src/models/generative-model.ts index 2c584b4..50e6e3f 100644 --- a/src/models/generative-model.ts +++ b/src/models/generative-model.ts @@ -65,6 +65,8 @@ export class GenerativeModel { cachedContent: CachedContent; presencePenalty?: number; frequencyPenalty?: number; + responseLogProbs?: boolean; + logProbs?: number; constructor( public apiKey: string, @@ -88,6 +90,8 @@ export class GenerativeModel { this.cachedContent = modelParams.cachedContent; this.presencePenalty = modelParams.presencePenalty; this.frequencyPenalty = modelParams.frequencyPenalty; + this.responseLogProbs = modelParams.responseLogProbs; + this.logProbs = modelParams.logProbs; } /** @@ -119,6 +123,8 @@ export class GenerativeModel { cachedContent: this.cachedContent?.name, presencePenalty: this.presencePenalty, frequencyPenalty: this.frequencyPenalty, + responseLogProbs: this.responseLogProbs, + logProbs: this.logProbs, ...formattedParams, }, generativeModelRequestOptions, @@ -156,6 +162,8 @@ export class GenerativeModel { cachedContent: this.cachedContent?.name, presencePenalty: this.presencePenalty, frequencyPenalty: this.frequencyPenalty, + responseLogProbs: this.responseLogProbs, + logProbs: this.logProbs, ...formattedParams, }, generativeModelRequestOptions, @@ -204,6 +212,9 @@ export class GenerativeModel { cachedContent: this.cachedContent, presencePenalty: this.presencePenalty, frequencyPenalty: this.frequencyPenalty, + responseLogProbs: this.responseLogProbs, + logProbs: this.logProbs, + }); const generativeModelRequestOptions: SingleRequestOptions = { ...this._requestOptions, diff --git a/types/requests.ts b/types/requests.ts index 4b8068d..dad722b 100644 --- a/types/requests.ts +++ b/types/requests.ts @@ -35,12 +35,21 @@ export interface BaseParams { * Presence penalty applied to the next token's logprobs if the token has * already been seen in the response. */ - presencePenalty?: number + presencePenalty?: number; /** * Frequency penalty applied to the next token's logprobs, multiplied by the * number of times each token has been seen in the respponse so far. */ - frequencyPenalty?: number + frequencyPenalty?: number; + /** + * If True, export the logprobs results in response. + */ + responseLogProbs?: boolean; + /** + * Valid if responseLogProbs is set to True. This will set the number of top + * logprobs to return at each decoding step in the logProbsResult. + */ + logProbs?: number; } /** From 0cb13e15e507e8f571f30f0a1aee3ff049463bc5 Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Wed, 25 Sep 2024 17:17:43 -0700 Subject: [PATCH 2/9] update docs & test --- .changeset/funny-pillows-whisper.md | 5 +++++ common/api-review/generative-ai.api.md | 6 ++++++ .../main/generative-ai.baseparams.logprobs.md | 13 +++++++++++++ docs/reference/main/generative-ai.baseparams.md | 2 ++ .../generative-ai.baseparams.responselogprobs.md | 13 +++++++++++++ .../main/generative-ai.generativemodel.logprobs.md | 11 +++++++++++ .../reference/main/generative-ai.generativemodel.md | 2 ++ ...enerative-ai.generativemodel.responselogprobs.md | 11 +++++++++++ src/models/generative-model.test.ts | 2 +- src/models/generative-model.ts | 1 - 10 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 .changeset/funny-pillows-whisper.md create mode 100644 docs/reference/main/generative-ai.baseparams.logprobs.md create mode 100644 docs/reference/main/generative-ai.baseparams.responselogprobs.md create mode 100644 docs/reference/main/generative-ai.generativemodel.logprobs.md create mode 100644 docs/reference/main/generative-ai.generativemodel.responselogprobs.md diff --git a/.changeset/funny-pillows-whisper.md b/.changeset/funny-pillows-whisper.md new file mode 100644 index 0000000..2dfb8ff --- /dev/null +++ b/.changeset/funny-pillows-whisper.md @@ -0,0 +1,5 @@ +--- +"@google/generative-ai": minor +--- + +Added the responseLogProbs and logProbs parameters diff --git a/common/api-review/generative-ai.api.md b/common/api-review/generative-ai.api.md index 0a3e847..fac73cd 100644 --- a/common/api-review/generative-ai.api.md +++ b/common/api-review/generative-ai.api.md @@ -9,7 +9,9 @@ export interface BaseParams { frequencyPenalty?: number; // (undocumented) generationConfig?: GenerationConfig; + logProbs?: number; presencePenalty?: number; + responseLogProbs?: boolean; // (undocumented) safetySettings?: SafetySetting[]; } @@ -467,10 +469,14 @@ export class GenerativeModel { // (undocumented) generationConfig: GenerationConfig; // (undocumented) + logProbs?: number; + // (undocumented) model: string; // (undocumented) presencePenalty?: number; // (undocumented) + responseLogProbs?: boolean; + // (undocumented) safetySettings: SafetySetting[]; startChat(startChatParams?: StartChatParams): ChatSession; // (undocumented) diff --git a/docs/reference/main/generative-ai.baseparams.logprobs.md b/docs/reference/main/generative-ai.baseparams.logprobs.md new file mode 100644 index 0000000..476c99a --- /dev/null +++ b/docs/reference/main/generative-ai.baseparams.logprobs.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [BaseParams](./generative-ai.baseparams.md) > [logProbs](./generative-ai.baseparams.logprobs.md) + +## BaseParams.logProbs property + +Valid if responseLogProbs is set to True. This will set the number of top logprobs to return at each decoding step in the logProbsResult. + +**Signature:** + +```typescript +logProbs?: number; +``` diff --git a/docs/reference/main/generative-ai.baseparams.md b/docs/reference/main/generative-ai.baseparams.md index ed893b3..72b183a 100644 --- a/docs/reference/main/generative-ai.baseparams.md +++ b/docs/reference/main/generative-ai.baseparams.md @@ -18,6 +18,8 @@ export interface BaseParams | --- | --- | --- | --- | | [frequencyPenalty?](./generative-ai.baseparams.frequencypenalty.md) | | number | _(Optional)_ Frequency penalty applied to the next token's logprobs, multiplied by the number of times each token has been seen in the respponse so far. | | [generationConfig?](./generative-ai.baseparams.generationconfig.md) | | [GenerationConfig](./generative-ai.generationconfig.md) | _(Optional)_ | +| [logProbs?](./generative-ai.baseparams.logprobs.md) | | number | _(Optional)_ Valid if responseLogProbs is set to True. This will set the number of top logprobs to return at each decoding step in the logProbsResult. | | [presencePenalty?](./generative-ai.baseparams.presencepenalty.md) | | number | _(Optional)_ Presence penalty applied to the next token's logprobs if the token has already been seen in the response. | +| [responseLogProbs?](./generative-ai.baseparams.responselogprobs.md) | | boolean | _(Optional)_ If True, export the logprobs results in response. | | [safetySettings?](./generative-ai.baseparams.safetysettings.md) | | [SafetySetting](./generative-ai.safetysetting.md)\[\] | _(Optional)_ | diff --git a/docs/reference/main/generative-ai.baseparams.responselogprobs.md b/docs/reference/main/generative-ai.baseparams.responselogprobs.md new file mode 100644 index 0000000..cec8ab4 --- /dev/null +++ b/docs/reference/main/generative-ai.baseparams.responselogprobs.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [BaseParams](./generative-ai.baseparams.md) > [responseLogProbs](./generative-ai.baseparams.responselogprobs.md) + +## BaseParams.responseLogProbs property + +If True, export the logprobs results in response. + +**Signature:** + +```typescript +responseLogProbs?: boolean; +``` diff --git a/docs/reference/main/generative-ai.generativemodel.logprobs.md b/docs/reference/main/generative-ai.generativemodel.logprobs.md new file mode 100644 index 0000000..32e184f --- /dev/null +++ b/docs/reference/main/generative-ai.generativemodel.logprobs.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [GenerativeModel](./generative-ai.generativemodel.md) > [logProbs](./generative-ai.generativemodel.logprobs.md) + +## GenerativeModel.logProbs property + +**Signature:** + +```typescript +logProbs?: number; +``` diff --git a/docs/reference/main/generative-ai.generativemodel.md b/docs/reference/main/generative-ai.generativemodel.md index 0b6e550..06b21bc 100644 --- a/docs/reference/main/generative-ai.generativemodel.md +++ b/docs/reference/main/generative-ai.generativemodel.md @@ -26,8 +26,10 @@ export declare class GenerativeModel | [cachedContent](./generative-ai.generativemodel.cachedcontent.md) | | [CachedContent](./generative-ai.cachedcontent.md) | | | [frequencyPenalty?](./generative-ai.generativemodel.frequencypenalty.md) | | number | _(Optional)_ | | [generationConfig](./generative-ai.generativemodel.generationconfig.md) | | [GenerationConfig](./generative-ai.generationconfig.md) | | +| [logProbs?](./generative-ai.generativemodel.logprobs.md) | | number | _(Optional)_ | | [model](./generative-ai.generativemodel.model.md) | | string | | | [presencePenalty?](./generative-ai.generativemodel.presencepenalty.md) | | number | _(Optional)_ | +| [responseLogProbs?](./generative-ai.generativemodel.responselogprobs.md) | | boolean | _(Optional)_ | | [safetySettings](./generative-ai.generativemodel.safetysettings.md) | | [SafetySetting](./generative-ai.safetysetting.md)\[\] | | | [systemInstruction?](./generative-ai.generativemodel.systeminstruction.md) | | [Content](./generative-ai.content.md) | _(Optional)_ | | [toolConfig?](./generative-ai.generativemodel.toolconfig.md) | | [ToolConfig](./generative-ai.toolconfig.md) | _(Optional)_ | diff --git a/docs/reference/main/generative-ai.generativemodel.responselogprobs.md b/docs/reference/main/generative-ai.generativemodel.responselogprobs.md new file mode 100644 index 0000000..3aebc99 --- /dev/null +++ b/docs/reference/main/generative-ai.generativemodel.responselogprobs.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [GenerativeModel](./generative-ai.generativemodel.md) > [responseLogProbs](./generative-ai.generativemodel.responselogprobs.md) + +## GenerativeModel.responseLogProbs property + +**Signature:** + +```typescript +responseLogProbs?: boolean; +``` diff --git a/src/models/generative-model.test.ts b/src/models/generative-model.test.ts index 5f83681..9fdfa6a 100644 --- a/src/models/generative-model.test.ts +++ b/src/models/generative-model.test.ts @@ -245,7 +245,7 @@ describe("GenerativeModel", () => { value.includes(HarmCategory.HARM_CATEGORY_HARASSMENT) && value.includes("presencePenalty") && value.includes("frequencyPenalty") && - value.includes("responseLogProbs") && + value.includes("responseLogProbs") && value.includes("logProbs") ); }), diff --git a/src/models/generative-model.ts b/src/models/generative-model.ts index 50e6e3f..33c7a3e 100644 --- a/src/models/generative-model.ts +++ b/src/models/generative-model.ts @@ -214,7 +214,6 @@ export class GenerativeModel { frequencyPenalty: this.frequencyPenalty, responseLogProbs: this.responseLogProbs, logProbs: this.logProbs, - }); const generativeModelRequestOptions: SingleRequestOptions = { ...this._requestOptions, From f4ac9fe67671557d912ea8b62ae7dc521433789e Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Wed, 25 Sep 2024 17:38:11 -0700 Subject: [PATCH 3/9] Update docs and add avglogprobs and logprobsresult as output --- .changeset/weak-stingrays-serve.md | 5 +++ common/api-review/generative-ai.api.md | 20 ++++++++++ .../generative-ai.candidate.logprobability.md | 13 +++++++ .../reference/main/generative-ai.candidate.md | 22 +++++++++++ .../main/generative-ai.candidate.token.md | 13 +++++++ .../main/generative-ai.candidate.tokenid.md | 13 +++++++ ...ai.generatecontentcandidate.avglogprobs.md | 13 +++++++ ...generatecontentcandidate.logprobsresult.md | 13 +++++++ .../generative-ai.generatecontentcandidate.md | 2 + ...tive-ai.logprobsresult.chosencandidates.md | 13 +++++++ .../main/generative-ai.logprobsresult.md | 21 ++++++++++ ...erative-ai.logprobsresult.topcandidates.md | 13 +++++++ docs/reference/main/generative-ai.md | 3 ++ .../generative-ai.topcandidates.candidates.md | 13 +++++++ .../main/generative-ai.topcandidates.md | 20 ++++++++++ types/responses.ts | 39 +++++++++++++++++++ 16 files changed, 236 insertions(+) create mode 100644 .changeset/weak-stingrays-serve.md create mode 100644 docs/reference/main/generative-ai.candidate.logprobability.md create mode 100644 docs/reference/main/generative-ai.candidate.md create mode 100644 docs/reference/main/generative-ai.candidate.token.md create mode 100644 docs/reference/main/generative-ai.candidate.tokenid.md create mode 100644 docs/reference/main/generative-ai.generatecontentcandidate.avglogprobs.md create mode 100644 docs/reference/main/generative-ai.generatecontentcandidate.logprobsresult.md create mode 100644 docs/reference/main/generative-ai.logprobsresult.chosencandidates.md create mode 100644 docs/reference/main/generative-ai.logprobsresult.md create mode 100644 docs/reference/main/generative-ai.logprobsresult.topcandidates.md create mode 100644 docs/reference/main/generative-ai.topcandidates.candidates.md create mode 100644 docs/reference/main/generative-ai.topcandidates.md diff --git a/.changeset/weak-stingrays-serve.md b/.changeset/weak-stingrays-serve.md new file mode 100644 index 0000000..4541d8e --- /dev/null +++ b/.changeset/weak-stingrays-serve.md @@ -0,0 +1,5 @@ +--- +"@google/generative-ai": minor +--- + +Add GenerateContentResponse's avgLogProbs and logProbsResult diff --git a/common/api-review/generative-ai.api.md b/common/api-review/generative-ai.api.md index fac73cd..ca1c8f7 100644 --- a/common/api-review/generative-ai.api.md +++ b/common/api-review/generative-ai.api.md @@ -64,6 +64,13 @@ export interface CachedContentBase { tools?: Tool[]; } +// @public +export interface Candidate { + logProbability: number; + token: string; + tokenID: number; +} + // @public export class ChatSession { constructor(apiKey: string, model: string, params?: StartChatParams, _requestOptions?: RequestOptions); @@ -373,6 +380,7 @@ export interface FunctionResponsePart { // @public export interface GenerateContentCandidate { + avgLogProbs?: number; // (undocumented) citationMetadata?: CitationMetadata; // (undocumented) @@ -383,6 +391,7 @@ export interface GenerateContentCandidate { finishReason?: FinishReason; // (undocumented) index: number; + logProbsResult?: LogprobsResult; // (undocumented) safetyRatings?: SafetyRating[]; } @@ -583,6 +592,12 @@ export interface InlineDataPart { text?: never; } +// @public +export interface LogprobsResult { + chosenCandidates: Candidate[]; + topCandidates: TopCandidates[]; +} + // @public export interface ModelParams extends BaseParams { // (undocumented) @@ -736,6 +751,11 @@ export interface ToolConfig { functionCallingConfig: FunctionCallingConfig; } +// @public +export interface TopCandidates { + candidates: Candidate[]; +} + // @public export interface UsageMetadata { cachedContentTokenCount?: number; diff --git a/docs/reference/main/generative-ai.candidate.logprobability.md b/docs/reference/main/generative-ai.candidate.logprobability.md new file mode 100644 index 0000000..93a65a5 --- /dev/null +++ b/docs/reference/main/generative-ai.candidate.logprobability.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [Candidate](./generative-ai.candidate.md) > [logProbability](./generative-ai.candidate.logprobability.md) + +## Candidate.logProbability property + +The candidate's log probability. + +**Signature:** + +```typescript +logProbability: number; +``` diff --git a/docs/reference/main/generative-ai.candidate.md b/docs/reference/main/generative-ai.candidate.md new file mode 100644 index 0000000..32bc071 --- /dev/null +++ b/docs/reference/main/generative-ai.candidate.md @@ -0,0 +1,22 @@ + + +[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [Candidate](./generative-ai.candidate.md) + +## Candidate interface + +Candidate for the logprobs token and score. + +**Signature:** + +```typescript +export interface Candidate +``` + +## Properties + +| Property | Modifiers | Type | Description | +| --- | --- | --- | --- | +| [logProbability](./generative-ai.candidate.logprobability.md) | | number | The candidate's log probability. | +| [token](./generative-ai.candidate.token.md) | | string | The candidate's token string value. | +| [tokenID](./generative-ai.candidate.tokenid.md) | | number | The candidate's token id value. | + diff --git a/docs/reference/main/generative-ai.candidate.token.md b/docs/reference/main/generative-ai.candidate.token.md new file mode 100644 index 0000000..c8925d9 --- /dev/null +++ b/docs/reference/main/generative-ai.candidate.token.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [Candidate](./generative-ai.candidate.md) > [token](./generative-ai.candidate.token.md) + +## Candidate.token property + +The candidate's token string value. + +**Signature:** + +```typescript +token: string; +``` diff --git a/docs/reference/main/generative-ai.candidate.tokenid.md b/docs/reference/main/generative-ai.candidate.tokenid.md new file mode 100644 index 0000000..c1088ed --- /dev/null +++ b/docs/reference/main/generative-ai.candidate.tokenid.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [Candidate](./generative-ai.candidate.md) > [tokenID](./generative-ai.candidate.tokenid.md) + +## Candidate.tokenID property + +The candidate's token id value. + +**Signature:** + +```typescript +tokenID: number; +``` diff --git a/docs/reference/main/generative-ai.generatecontentcandidate.avglogprobs.md b/docs/reference/main/generative-ai.generatecontentcandidate.avglogprobs.md new file mode 100644 index 0000000..a04a5b0 --- /dev/null +++ b/docs/reference/main/generative-ai.generatecontentcandidate.avglogprobs.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [GenerateContentCandidate](./generative-ai.generatecontentcandidate.md) > [avgLogProbs](./generative-ai.generatecontentcandidate.avglogprobs.md) + +## GenerateContentCandidate.avgLogProbs property + +Average log probability score of the candidate. + +**Signature:** + +```typescript +avgLogProbs?: number; +``` diff --git a/docs/reference/main/generative-ai.generatecontentcandidate.logprobsresult.md b/docs/reference/main/generative-ai.generatecontentcandidate.logprobsresult.md new file mode 100644 index 0000000..4cb08df --- /dev/null +++ b/docs/reference/main/generative-ai.generatecontentcandidate.logprobsresult.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [GenerateContentCandidate](./generative-ai.generatecontentcandidate.md) > [logProbsResult](./generative-ai.generatecontentcandidate.logprobsresult.md) + +## GenerateContentCandidate.logProbsResult property + +Log-likelihood scores for the response tokens and top tokens. + +**Signature:** + +```typescript +logProbsResult?: LogprobsResult; +``` diff --git a/docs/reference/main/generative-ai.generatecontentcandidate.md b/docs/reference/main/generative-ai.generatecontentcandidate.md index 35f0c5a..a938440 100644 --- a/docs/reference/main/generative-ai.generatecontentcandidate.md +++ b/docs/reference/main/generative-ai.generatecontentcandidate.md @@ -16,10 +16,12 @@ export interface GenerateContentCandidate | Property | Modifiers | Type | Description | | --- | --- | --- | --- | +| [avgLogProbs?](./generative-ai.generatecontentcandidate.avglogprobs.md) | | number | _(Optional)_ Average log probability score of the candidate. | | [citationMetadata?](./generative-ai.generatecontentcandidate.citationmetadata.md) | | [CitationMetadata](./generative-ai.citationmetadata.md) | _(Optional)_ | | [content](./generative-ai.generatecontentcandidate.content.md) | | [Content](./generative-ai.content.md) | | | [finishMessage?](./generative-ai.generatecontentcandidate.finishmessage.md) | | string | _(Optional)_ | | [finishReason?](./generative-ai.generatecontentcandidate.finishreason.md) | | [FinishReason](./generative-ai.finishreason.md) | _(Optional)_ | | [index](./generative-ai.generatecontentcandidate.index.md) | | number | | +| [logProbsResult?](./generative-ai.generatecontentcandidate.logprobsresult.md) | | [LogprobsResult](./generative-ai.logprobsresult.md) | _(Optional)_ Log-likelihood scores for the response tokens and top tokens. | | [safetyRatings?](./generative-ai.generatecontentcandidate.safetyratings.md) | | [SafetyRating](./generative-ai.safetyrating.md)\[\] | _(Optional)_ | diff --git a/docs/reference/main/generative-ai.logprobsresult.chosencandidates.md b/docs/reference/main/generative-ai.logprobsresult.chosencandidates.md new file mode 100644 index 0000000..8bb0c7d --- /dev/null +++ b/docs/reference/main/generative-ai.logprobsresult.chosencandidates.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [LogprobsResult](./generative-ai.logprobsresult.md) > [chosenCandidates](./generative-ai.logprobsresult.chosencandidates.md) + +## LogprobsResult.chosenCandidates property + +Length = total number of decoding steps. The chosen candidates may or may not be in topCandidates. + +**Signature:** + +```typescript +chosenCandidates: Candidate[]; +``` diff --git a/docs/reference/main/generative-ai.logprobsresult.md b/docs/reference/main/generative-ai.logprobsresult.md new file mode 100644 index 0000000..d29531e --- /dev/null +++ b/docs/reference/main/generative-ai.logprobsresult.md @@ -0,0 +1,21 @@ + + +[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [LogprobsResult](./generative-ai.logprobsresult.md) + +## LogprobsResult interface + +Logprobs Result + +**Signature:** + +```typescript +export interface LogprobsResult +``` + +## Properties + +| Property | Modifiers | Type | Description | +| --- | --- | --- | --- | +| [chosenCandidates](./generative-ai.logprobsresult.chosencandidates.md) | | [Candidate](./generative-ai.candidate.md)\[\] | Length = total number of decoding steps. The chosen candidates may or may not be in topCandidates. | +| [topCandidates](./generative-ai.logprobsresult.topcandidates.md) | | [TopCandidates](./generative-ai.topcandidates.md)\[\] | Length = total number of decoding steps. | + diff --git a/docs/reference/main/generative-ai.logprobsresult.topcandidates.md b/docs/reference/main/generative-ai.logprobsresult.topcandidates.md new file mode 100644 index 0000000..a89e210 --- /dev/null +++ b/docs/reference/main/generative-ai.logprobsresult.topcandidates.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [LogprobsResult](./generative-ai.logprobsresult.md) > [topCandidates](./generative-ai.logprobsresult.topcandidates.md) + +## LogprobsResult.topCandidates property + +Length = total number of decoding steps. + +**Signature:** + +```typescript +topCandidates: TopCandidates[]; +``` diff --git a/docs/reference/main/generative-ai.md b/docs/reference/main/generative-ai.md index d0b15bf..d50a8e9 100644 --- a/docs/reference/main/generative-ai.md +++ b/docs/reference/main/generative-ai.md @@ -40,6 +40,7 @@ | [BatchEmbedContentsResponse](./generative-ai.batchembedcontentsresponse.md) | Response from calling [GenerativeModel.batchEmbedContents()](./generative-ai.generativemodel.batchembedcontents.md). | | [CachedContent](./generative-ai.cachedcontent.md) | Describes CachedContent interface for sending to the server (if creating) or received from the server (using getters or list methods). | | [CachedContentBase](./generative-ai.cachedcontentbase.md) | | +| [Candidate](./generative-ai.candidate.md) | Candidate for the logprobs token and score. | | [CitationMetadata](./generative-ai.citationmetadata.md) | Citation metadata that may be found on a [GenerateContentCandidate](./generative-ai.generatecontentcandidate.md). | | [CitationSource](./generative-ai.citationsource.md) | A single citation source. | | [CodeExecutionResult](./generative-ai.codeexecutionresult.md) | Result of executing the ExecutableCode. Only generated when using code execution, and always follows a Part containing the ExecutableCode. | @@ -74,6 +75,7 @@ | [GenerationConfig](./generative-ai.generationconfig.md) | Config options for content-related requests | | [GenerativeContentBlob](./generative-ai.generativecontentblob.md) | Interface for sending an image. | | [InlineDataPart](./generative-ai.inlinedatapart.md) | Content part interface if the part represents an image. | +| [LogprobsResult](./generative-ai.logprobsresult.md) | Logprobs Result | | [ModelParams](./generative-ai.modelparams.md) | Params passed to [GoogleGenerativeAI.getGenerativeModel()](./generative-ai.googlegenerativeai.getgenerativemodel.md). | | [PromptFeedback](./generative-ai.promptfeedback.md) | If the prompt was blocked, this will be populated with blockReason and the relevant safetyRatings. | | [RequestOptions](./generative-ai.requestoptions.md) | Params passed to getGenerativeModel() or GoogleAIFileManager(). | @@ -85,6 +87,7 @@ | [StartChatParams](./generative-ai.startchatparams.md) | Params for [GenerativeModel.startChat()](./generative-ai.generativemodel.startchat.md). | | [TextPart](./generative-ai.textpart.md) | Content part interface if the part represents a text string. | | [ToolConfig](./generative-ai.toolconfig.md) | Tool config. This config is shared for all tools provided in the request. | +| [TopCandidates](./generative-ai.topcandidates.md) | Candidates with top log probabilities at each decoding step | | [UsageMetadata](./generative-ai.usagemetadata.md) | Metadata on the generation request's token usage. | ## Variables diff --git a/docs/reference/main/generative-ai.topcandidates.candidates.md b/docs/reference/main/generative-ai.topcandidates.candidates.md new file mode 100644 index 0000000..e64ec37 --- /dev/null +++ b/docs/reference/main/generative-ai.topcandidates.candidates.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [TopCandidates](./generative-ai.topcandidates.md) > [candidates](./generative-ai.topcandidates.candidates.md) + +## TopCandidates.candidates property + +Sorted by log probability in descending order. + +**Signature:** + +```typescript +candidates: Candidate[]; +``` diff --git a/docs/reference/main/generative-ai.topcandidates.md b/docs/reference/main/generative-ai.topcandidates.md new file mode 100644 index 0000000..6525b90 --- /dev/null +++ b/docs/reference/main/generative-ai.topcandidates.md @@ -0,0 +1,20 @@ + + +[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [TopCandidates](./generative-ai.topcandidates.md) + +## TopCandidates interface + +Candidates with top log probabilities at each decoding step + +**Signature:** + +```typescript +export interface TopCandidates +``` + +## Properties + +| Property | Modifiers | Type | Description | +| --- | --- | --- | --- | +| [candidates](./generative-ai.topcandidates.candidates.md) | | [Candidate](./generative-ai.candidate.md)\[\] | Sorted by log probability in descending order. | + diff --git a/types/responses.ts b/types/responses.ts index 2acf210..97dc980 100644 --- a/types/responses.ts +++ b/types/responses.ts @@ -87,6 +87,41 @@ export interface GenerateContentResponse { usageMetadata?: UsageMetadata; } +/** + * Logprobs Result + * @public + */ +export interface LogprobsResult { + /** Length = total number of decoding steps. */ + topCandidates: TopCandidates[]; + /** + * Length = total number of decoding steps. + * The chosen candidates may or may not be in topCandidates. + */ + chosenCandidates: Candidate[]; +} + +/** + * Candidate for the logprobs token and score. + * @public + */ +export interface Candidate { + /** The candidate's token string value. */ + token: string; + /** The candidate's token id value. */ + tokenID: number; + /** The candidate's log probability. */ + logProbability: number; +} + +/** + * Candidates with top log probabilities at each decoding step + */ +export interface TopCandidates { + /** Sorted by log probability in descending order. */ + candidates: Candidate[]; +} + /** * Metadata on the generation request's token usage. * @public @@ -124,6 +159,10 @@ export interface GenerateContentCandidate { finishMessage?: string; safetyRatings?: SafetyRating[]; citationMetadata?: CitationMetadata; + /** Average log probability score of the candidate. */ + avgLogProbs?: number; + /** Log-likelihood scores for the response tokens and top tokens. */ + logProbsResult?: LogprobsResult; } /** From 1e222752c6a972ebd45b02c199770bd2372a2252 Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Thu, 26 Sep 2024 10:35:50 -0700 Subject: [PATCH 4/9] update variable names in responses.ts --- types/responses.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/types/responses.ts b/types/responses.ts index 97dc980..54db37f 100644 --- a/types/responses.ts +++ b/types/responses.ts @@ -98,14 +98,14 @@ export interface LogprobsResult { * Length = total number of decoding steps. * The chosen candidates may or may not be in topCandidates. */ - chosenCandidates: Candidate[]; + chosenCandidates: LogProbsCandidate[]; } /** * Candidate for the logprobs token and score. * @public */ -export interface Candidate { +export interface LogProbsCandidate { /** The candidate's token string value. */ token: string; /** The candidate's token id value. */ @@ -119,7 +119,7 @@ export interface Candidate { */ export interface TopCandidates { /** Sorted by log probability in descending order. */ - candidates: Candidate[]; + candidates: LogProbsCandidate[]; } /** @@ -160,9 +160,9 @@ export interface GenerateContentCandidate { safetyRatings?: SafetyRating[]; citationMetadata?: CitationMetadata; /** Average log probability score of the candidate. */ - avgLogProbs?: number; + avgLogprobs?: number; /** Log-likelihood scores for the response tokens and top tokens. */ - logProbsResult?: LogprobsResult; + logprobsResult?: LogprobsResult; } /** From 3c959e958fcf5b5da4eb528203515767a8e7fe81 Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Thu, 26 Sep 2024 11:26:20 -0700 Subject: [PATCH 5/9] Move parameters to GenerationConfig --- src/methods/generate-content.test.ts | 4 --- src/models/generative-model.test.ts | 21 +-------------- src/models/generative-model.ts | 20 --------------- types/requests.ts | 38 ++++++++++++++-------------- 4 files changed, 20 insertions(+), 63 deletions(-) diff --git a/src/methods/generate-content.test.ts b/src/methods/generate-content.test.ts index 0920f43..5f49dc1 100644 --- a/src/methods/generate-content.test.ts +++ b/src/methods/generate-content.test.ts @@ -42,10 +42,6 @@ const fakeRequestParams: GenerateContentRequest = { threshold: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE, }, ], - presencePenalty: 0.5, - frequencyPenalty: 0.1, - responseLogProbs: true, - logProbs: 2, }; describe("generateContent()", () => { diff --git a/src/models/generative-model.test.ts b/src/models/generative-model.test.ts index 9fdfa6a..d3fbae4 100644 --- a/src/models/generative-model.test.ts +++ b/src/models/generative-model.test.ts @@ -71,10 +71,6 @@ describe("GenerativeModel", () => { threshold: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE, }, ], - presencePenalty: 0.6, - frequencyPenalty: 0.5, - responseLogProbs: true, - logProbs: 2, tools: [{ functionDeclarations: [{ name: "myfunc" }] }], toolConfig: { functionCallingConfig: { mode: FunctionCallingMode.NONE }, @@ -96,9 +92,6 @@ describe("GenerativeModel", () => { genModel.generationConfig?.responseSchema.properties.testField.type, ).to.equal(SchemaType.STRING); expect(genModel.safetySettings?.length).to.equal(1); - expect(genModel.presencePenalty).to.equal(0.6); - expect(genModel.frequencyPenalty).to.equal(0.5); - expect(genModel.tools?.length).to.equal(1); expect(genModel.toolConfig?.functionCallingConfig.mode).to.equal( FunctionCallingMode.NONE, ); @@ -122,11 +115,7 @@ describe("GenerativeModel", () => { value.includes("be friendly") && value.includes("temperature") && value.includes("testField") && - value.includes(HarmBlockThreshold.BLOCK_LOW_AND_ABOVE) && - value.includes("presencePenalty") && - value.includes("frequencyPenalty") && - value.includes("responseLogProbs") && - value.includes("logProbs") + value.includes(HarmBlockThreshold.BLOCK_LOW_AND_ABOVE) ); }), match((value) => { @@ -220,10 +209,6 @@ describe("GenerativeModel", () => { threshold: HarmBlockThreshold.BLOCK_NONE, }, ], - presencePenalty: 0.6, - frequencyPenalty: 0.5, - responseLogProbs: true, - logProbs: 2, contents: [{ role: "user", parts: [{ text: "hello" }] }], tools: [{ functionDeclarations: [{ name: "otherfunc" }] }], toolConfig: { functionCallingConfig: { mode: FunctionCallingMode.AUTO } }, @@ -243,10 +228,6 @@ describe("GenerativeModel", () => { value.includes("newTestField") && !value.includes("testField") && value.includes(HarmCategory.HARM_CATEGORY_HARASSMENT) && - value.includes("presencePenalty") && - value.includes("frequencyPenalty") && - value.includes("responseLogProbs") && - value.includes("logProbs") ); }), {}, diff --git a/src/models/generative-model.ts b/src/models/generative-model.ts index 33c7a3e..7cd3fe6 100644 --- a/src/models/generative-model.ts +++ b/src/models/generative-model.ts @@ -63,10 +63,6 @@ export class GenerativeModel { toolConfig?: ToolConfig; systemInstruction?: Content; cachedContent: CachedContent; - presencePenalty?: number; - frequencyPenalty?: number; - responseLogProbs?: boolean; - logProbs?: number; constructor( public apiKey: string, @@ -88,10 +84,6 @@ export class GenerativeModel { modelParams.systemInstruction, ); this.cachedContent = modelParams.cachedContent; - this.presencePenalty = modelParams.presencePenalty; - this.frequencyPenalty = modelParams.frequencyPenalty; - this.responseLogProbs = modelParams.responseLogProbs; - this.logProbs = modelParams.logProbs; } /** @@ -121,10 +113,6 @@ export class GenerativeModel { toolConfig: this.toolConfig, systemInstruction: this.systemInstruction, cachedContent: this.cachedContent?.name, - presencePenalty: this.presencePenalty, - frequencyPenalty: this.frequencyPenalty, - responseLogProbs: this.responseLogProbs, - logProbs: this.logProbs, ...formattedParams, }, generativeModelRequestOptions, @@ -160,10 +148,6 @@ export class GenerativeModel { toolConfig: this.toolConfig, systemInstruction: this.systemInstruction, cachedContent: this.cachedContent?.name, - presencePenalty: this.presencePenalty, - frequencyPenalty: this.frequencyPenalty, - responseLogProbs: this.responseLogProbs, - logProbs: this.logProbs, ...formattedParams, }, generativeModelRequestOptions, @@ -210,10 +194,6 @@ export class GenerativeModel { toolConfig: this.toolConfig, systemInstruction: this.systemInstruction, cachedContent: this.cachedContent, - presencePenalty: this.presencePenalty, - frequencyPenalty: this.frequencyPenalty, - responseLogProbs: this.responseLogProbs, - logProbs: this.logProbs, }); const generativeModelRequestOptions: SingleRequestOptions = { ...this._requestOptions, diff --git a/types/requests.ts b/types/requests.ts index dad722b..197571e 100644 --- a/types/requests.ts +++ b/types/requests.ts @@ -31,25 +31,6 @@ import { export interface BaseParams { safetySettings?: SafetySetting[]; generationConfig?: GenerationConfig; - /** - * Presence penalty applied to the next token's logprobs if the token has - * already been seen in the response. - */ - presencePenalty?: number; - /** - * Frequency penalty applied to the next token's logprobs, multiplied by the - * number of times each token has been seen in the respponse so far. - */ - frequencyPenalty?: number; - /** - * If True, export the logprobs results in response. - */ - responseLogProbs?: boolean; - /** - * Valid if responseLogProbs is set to True. This will set the number of top - * logprobs to return at each decoding step in the logProbsResult. - */ - logProbs?: number; } /** @@ -121,6 +102,25 @@ export interface GenerationConfig { * this is limited to `application/json`. */ responseSchema?: ResponseSchema; + /** + * Presence penalty applied to the next token's logprobs if the token has + * already been seen in the response. + */ + presencePenalty?: number; + /** + * Frequency penalty applied to the next token's logprobs, multiplied by the + * number of times each token has been seen in the respponse so far. + */ + frequencyPenalty?: number; + /** + * If True, export the logprobs results in response. + */ + responseLogProbs?: boolean; + /** + * Valid if responseLogProbs is set to True. This will set the number of top + * logprobs to return at each decoding step in the logProbsResult. + */ + logProbs?: number; } /** From ed2b7fcd2b6e92f96ffddcf4ace45621b80f43ff Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Thu, 26 Sep 2024 11:39:20 -0700 Subject: [PATCH 6/9] Update test cases for new parameters to test generationConfig --- src/models/generative-model.test.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/models/generative-model.test.ts b/src/models/generative-model.test.ts index d3fbae4..46d989e 100644 --- a/src/models/generative-model.test.ts +++ b/src/models/generative-model.test.ts @@ -64,6 +64,10 @@ describe("GenerativeModel", () => { }, }, }, + presencePenalty: 0.6, + frequencyPenalty: 0.5, + responseLogProbs: true, + logProbs: 2, }, safetySettings: [ { @@ -91,6 +95,18 @@ describe("GenerativeModel", () => { expect( genModel.generationConfig?.responseSchema.properties.testField.type, ).to.equal(SchemaType.STRING); + expect(genModel.generationConfig?.presencePenalty).to.equal( + 0.6, + ); + expect(genModel.generationConfig?.frequencyPenalty).to.equal( + 0.5, + ); + expect(genModel.generationConfig?.responseLogProbs).to.equal( + true + ); + expect(genModel.generationConfig?.logProbs).to.equal( + 2 + ); expect(genModel.safetySettings?.length).to.equal(1); expect(genModel.toolConfig?.functionCallingConfig.mode).to.equal( FunctionCallingMode.NONE, From 34d67686930c371c5e2508380b7c233ffc18c198 Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Thu, 26 Sep 2024 12:24:37 -0700 Subject: [PATCH 7/9] Updated generatecontentresponse testcase --- .changeset/cyan-pants-move.md | 2 +- .changeset/funny-pillows-whisper.md | 5 -- .changeset/weak-stingrays-serve.md | 5 -- common/api-review/generative-ai.api.md | 38 ++++---- .../main/generative-ai.baseparams.md | 4 - .../reference/main/generative-ai.candidate.md | 22 ----- ...ai.generatecontentcandidate.avglogprobs.md | 6 +- ...generatecontentcandidate.logprobsresult.md | 6 +- .../generative-ai.generatecontentcandidate.md | 4 +- ...e-ai.generationconfig.frequencypenalty.md} | 4 +- ...enerative-ai.generationconfig.logprobs.md} | 4 +- .../main/generative-ai.generationconfig.md | 4 + ...ve-ai.generationconfig.presencepenalty.md} | 4 +- ...e-ai.generationconfig.responselogprobs.md} | 4 +- ...ive-ai.generativemodel.frequencypenalty.md | 11 --- .../generative-ai.generativemodel.logprobs.md | 11 --- .../main/generative-ai.generativemodel.md | 4 - ...tive-ai.generativemodel.presencepenalty.md | 11 --- ...ive-ai.generativemodel.responselogprobs.md | 11 --- ...ve-ai.logprobscandidate.logprobability.md} | 4 +- .../main/generative-ai.logprobscandidate.md | 22 +++++ ... generative-ai.logprobscandidate.token.md} | 4 +- ...enerative-ai.logprobscandidate.tokenid.md} | 4 +- ...tive-ai.logprobsresult.chosencandidates.md | 2 +- .../main/generative-ai.logprobsresult.md | 2 +- docs/reference/main/generative-ai.md | 2 +- .../generative-ai.topcandidates.candidates.md | 2 +- .../main/generative-ai.topcandidates.md | 2 +- src/methods/generate-content.test.ts | 27 ++++++ src/models/generative-model.test.ts | 18 ++-- .../unary-success-logprobs.json | 86 +++++++++++++++++++ types/responses.ts | 6 +- 32 files changed, 190 insertions(+), 151 deletions(-) delete mode 100644 .changeset/funny-pillows-whisper.md delete mode 100644 .changeset/weak-stingrays-serve.md delete mode 100644 docs/reference/main/generative-ai.candidate.md rename docs/reference/main/{generative-ai.baseparams.frequencypenalty.md => generative-ai.generationconfig.frequencypenalty.md} (63%) rename docs/reference/main/{generative-ai.baseparams.logprobs.md => generative-ai.generationconfig.logprobs.md} (65%) rename docs/reference/main/{generative-ai.baseparams.presencepenalty.md => generative-ai.generationconfig.presencepenalty.md} (61%) rename docs/reference/main/{generative-ai.baseparams.responselogprobs.md => generative-ai.generationconfig.responselogprobs.md} (56%) delete mode 100644 docs/reference/main/generative-ai.generativemodel.frequencypenalty.md delete mode 100644 docs/reference/main/generative-ai.generativemodel.logprobs.md delete mode 100644 docs/reference/main/generative-ai.generativemodel.presencepenalty.md delete mode 100644 docs/reference/main/generative-ai.generativemodel.responselogprobs.md rename docs/reference/main/{generative-ai.candidate.logprobability.md => generative-ai.logprobscandidate.logprobability.md} (54%) create mode 100644 docs/reference/main/generative-ai.logprobscandidate.md rename docs/reference/main/{generative-ai.candidate.token.md => generative-ai.logprobscandidate.token.md} (57%) rename docs/reference/main/{generative-ai.candidate.tokenid.md => generative-ai.logprobscandidate.tokenid.md} (56%) create mode 100644 test-utils/mock-responses/unary-success-logprobs.json diff --git a/.changeset/cyan-pants-move.md b/.changeset/cyan-pants-move.md index e12e83b..ccf111c 100644 --- a/.changeset/cyan-pants-move.md +++ b/.changeset/cyan-pants-move.md @@ -2,4 +2,4 @@ "@google/generative-ai": minor --- -Add `frequencyPenalty` and `presencePenalty` parameters support for `generateContent()` +Add `frequencyPenalty`, `presencePenalty`, `responseLogprobs`, and `logProbs` parameters support for `generationConfig`. Added `avgLogprobs` and `logprobsResult` to `GenerateContentResponse`. Updated test cases. diff --git a/.changeset/funny-pillows-whisper.md b/.changeset/funny-pillows-whisper.md deleted file mode 100644 index 2dfb8ff..0000000 --- a/.changeset/funny-pillows-whisper.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"@google/generative-ai": minor ---- - -Added the responseLogProbs and logProbs parameters diff --git a/.changeset/weak-stingrays-serve.md b/.changeset/weak-stingrays-serve.md deleted file mode 100644 index 4541d8e..0000000 --- a/.changeset/weak-stingrays-serve.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"@google/generative-ai": minor ---- - -Add GenerateContentResponse's avgLogProbs and logProbsResult diff --git a/common/api-review/generative-ai.api.md b/common/api-review/generative-ai.api.md index ca1c8f7..19779e4 100644 --- a/common/api-review/generative-ai.api.md +++ b/common/api-review/generative-ai.api.md @@ -6,12 +6,8 @@ // @public export interface BaseParams { - frequencyPenalty?: number; // (undocumented) generationConfig?: GenerationConfig; - logProbs?: number; - presencePenalty?: number; - responseLogProbs?: boolean; // (undocumented) safetySettings?: SafetySetting[]; } @@ -64,13 +60,6 @@ export interface CachedContentBase { tools?: Tool[]; } -// @public -export interface Candidate { - logProbability: number; - token: string; - tokenID: number; -} - // @public export class ChatSession { constructor(apiKey: string, model: string, params?: StartChatParams, _requestOptions?: RequestOptions); @@ -380,7 +369,7 @@ export interface FunctionResponsePart { // @public export interface GenerateContentCandidate { - avgLogProbs?: number; + avgLogprobs?: number; // (undocumented) citationMetadata?: CitationMetadata; // (undocumented) @@ -391,7 +380,7 @@ export interface GenerateContentCandidate { finishReason?: FinishReason; // (undocumented) index: number; - logProbsResult?: LogprobsResult; + logprobsResult?: LogprobsResult; // (undocumented) safetyRatings?: SafetyRating[]; } @@ -440,8 +429,12 @@ export interface GenerateContentStreamResult { export interface GenerationConfig { // (undocumented) candidateCount?: number; + frequencyPenalty?: number; + logProbs?: number; // (undocumented) maxOutputTokens?: number; + presencePenalty?: number; + responseLogProbs?: boolean; responseMimeType?: string; responseSchema?: ResponseSchema; // (undocumented) @@ -471,21 +464,13 @@ export class GenerativeModel { cachedContent: CachedContent; countTokens(request: CountTokensRequest | string | Array, requestOptions?: SingleRequestOptions): Promise; embedContent(request: EmbedContentRequest | string | Array, requestOptions?: SingleRequestOptions): Promise; - // (undocumented) - frequencyPenalty?: number; generateContent(request: GenerateContentRequest | string | Array, requestOptions?: SingleRequestOptions): Promise; generateContentStream(request: GenerateContentRequest | string | Array, requestOptions?: SingleRequestOptions): Promise; // (undocumented) generationConfig: GenerationConfig; // (undocumented) - logProbs?: number; - // (undocumented) model: string; // (undocumented) - presencePenalty?: number; - // (undocumented) - responseLogProbs?: boolean; - // (undocumented) safetySettings: SafetySetting[]; startChat(startChatParams?: StartChatParams): ChatSession; // (undocumented) @@ -592,9 +577,16 @@ export interface InlineDataPart { text?: never; } +// @public +export interface LogprobsCandidate { + logProbability: number; + token: string; + tokenID: number; +} + // @public export interface LogprobsResult { - chosenCandidates: Candidate[]; + chosenCandidates: LogprobsCandidate[]; topCandidates: TopCandidates[]; } @@ -753,7 +745,7 @@ export interface ToolConfig { // @public export interface TopCandidates { - candidates: Candidate[]; + candidates: LogprobsCandidate[]; } // @public diff --git a/docs/reference/main/generative-ai.baseparams.md b/docs/reference/main/generative-ai.baseparams.md index 72b183a..ea3fb2b 100644 --- a/docs/reference/main/generative-ai.baseparams.md +++ b/docs/reference/main/generative-ai.baseparams.md @@ -16,10 +16,6 @@ export interface BaseParams | Property | Modifiers | Type | Description | | --- | --- | --- | --- | -| [frequencyPenalty?](./generative-ai.baseparams.frequencypenalty.md) | | number | _(Optional)_ Frequency penalty applied to the next token's logprobs, multiplied by the number of times each token has been seen in the respponse so far. | | [generationConfig?](./generative-ai.baseparams.generationconfig.md) | | [GenerationConfig](./generative-ai.generationconfig.md) | _(Optional)_ | -| [logProbs?](./generative-ai.baseparams.logprobs.md) | | number | _(Optional)_ Valid if responseLogProbs is set to True. This will set the number of top logprobs to return at each decoding step in the logProbsResult. | -| [presencePenalty?](./generative-ai.baseparams.presencepenalty.md) | | number | _(Optional)_ Presence penalty applied to the next token's logprobs if the token has already been seen in the response. | -| [responseLogProbs?](./generative-ai.baseparams.responselogprobs.md) | | boolean | _(Optional)_ If True, export the logprobs results in response. | | [safetySettings?](./generative-ai.baseparams.safetysettings.md) | | [SafetySetting](./generative-ai.safetysetting.md)\[\] | _(Optional)_ | diff --git a/docs/reference/main/generative-ai.candidate.md b/docs/reference/main/generative-ai.candidate.md deleted file mode 100644 index 32bc071..0000000 --- a/docs/reference/main/generative-ai.candidate.md +++ /dev/null @@ -1,22 +0,0 @@ - - -[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [Candidate](./generative-ai.candidate.md) - -## Candidate interface - -Candidate for the logprobs token and score. - -**Signature:** - -```typescript -export interface Candidate -``` - -## Properties - -| Property | Modifiers | Type | Description | -| --- | --- | --- | --- | -| [logProbability](./generative-ai.candidate.logprobability.md) | | number | The candidate's log probability. | -| [token](./generative-ai.candidate.token.md) | | string | The candidate's token string value. | -| [tokenID](./generative-ai.candidate.tokenid.md) | | number | The candidate's token id value. | - diff --git a/docs/reference/main/generative-ai.generatecontentcandidate.avglogprobs.md b/docs/reference/main/generative-ai.generatecontentcandidate.avglogprobs.md index a04a5b0..05a3ae0 100644 --- a/docs/reference/main/generative-ai.generatecontentcandidate.avglogprobs.md +++ b/docs/reference/main/generative-ai.generatecontentcandidate.avglogprobs.md @@ -1,13 +1,13 @@ -[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [GenerateContentCandidate](./generative-ai.generatecontentcandidate.md) > [avgLogProbs](./generative-ai.generatecontentcandidate.avglogprobs.md) +[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [GenerateContentCandidate](./generative-ai.generatecontentcandidate.md) > [avgLogprobs](./generative-ai.generatecontentcandidate.avglogprobs.md) -## GenerateContentCandidate.avgLogProbs property +## GenerateContentCandidate.avgLogprobs property Average log probability score of the candidate. **Signature:** ```typescript -avgLogProbs?: number; +avgLogprobs?: number; ``` diff --git a/docs/reference/main/generative-ai.generatecontentcandidate.logprobsresult.md b/docs/reference/main/generative-ai.generatecontentcandidate.logprobsresult.md index 4cb08df..17b561d 100644 --- a/docs/reference/main/generative-ai.generatecontentcandidate.logprobsresult.md +++ b/docs/reference/main/generative-ai.generatecontentcandidate.logprobsresult.md @@ -1,13 +1,13 @@ -[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [GenerateContentCandidate](./generative-ai.generatecontentcandidate.md) > [logProbsResult](./generative-ai.generatecontentcandidate.logprobsresult.md) +[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [GenerateContentCandidate](./generative-ai.generatecontentcandidate.md) > [logprobsResult](./generative-ai.generatecontentcandidate.logprobsresult.md) -## GenerateContentCandidate.logProbsResult property +## GenerateContentCandidate.logprobsResult property Log-likelihood scores for the response tokens and top tokens. **Signature:** ```typescript -logProbsResult?: LogprobsResult; +logprobsResult?: LogprobsResult; ``` diff --git a/docs/reference/main/generative-ai.generatecontentcandidate.md b/docs/reference/main/generative-ai.generatecontentcandidate.md index a938440..e0d669c 100644 --- a/docs/reference/main/generative-ai.generatecontentcandidate.md +++ b/docs/reference/main/generative-ai.generatecontentcandidate.md @@ -16,12 +16,12 @@ export interface GenerateContentCandidate | Property | Modifiers | Type | Description | | --- | --- | --- | --- | -| [avgLogProbs?](./generative-ai.generatecontentcandidate.avglogprobs.md) | | number | _(Optional)_ Average log probability score of the candidate. | +| [avgLogprobs?](./generative-ai.generatecontentcandidate.avglogprobs.md) | | number | _(Optional)_ Average log probability score of the candidate. | | [citationMetadata?](./generative-ai.generatecontentcandidate.citationmetadata.md) | | [CitationMetadata](./generative-ai.citationmetadata.md) | _(Optional)_ | | [content](./generative-ai.generatecontentcandidate.content.md) | | [Content](./generative-ai.content.md) | | | [finishMessage?](./generative-ai.generatecontentcandidate.finishmessage.md) | | string | _(Optional)_ | | [finishReason?](./generative-ai.generatecontentcandidate.finishreason.md) | | [FinishReason](./generative-ai.finishreason.md) | _(Optional)_ | | [index](./generative-ai.generatecontentcandidate.index.md) | | number | | -| [logProbsResult?](./generative-ai.generatecontentcandidate.logprobsresult.md) | | [LogprobsResult](./generative-ai.logprobsresult.md) | _(Optional)_ Log-likelihood scores for the response tokens and top tokens. | +| [logprobsResult?](./generative-ai.generatecontentcandidate.logprobsresult.md) | | [LogprobsResult](./generative-ai.logprobsresult.md) | _(Optional)_ Log-likelihood scores for the response tokens and top tokens. | | [safetyRatings?](./generative-ai.generatecontentcandidate.safetyratings.md) | | [SafetyRating](./generative-ai.safetyrating.md)\[\] | _(Optional)_ | diff --git a/docs/reference/main/generative-ai.baseparams.frequencypenalty.md b/docs/reference/main/generative-ai.generationconfig.frequencypenalty.md similarity index 63% rename from docs/reference/main/generative-ai.baseparams.frequencypenalty.md rename to docs/reference/main/generative-ai.generationconfig.frequencypenalty.md index 2eeb104..5bee962 100644 --- a/docs/reference/main/generative-ai.baseparams.frequencypenalty.md +++ b/docs/reference/main/generative-ai.generationconfig.frequencypenalty.md @@ -1,8 +1,8 @@ -[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [BaseParams](./generative-ai.baseparams.md) > [frequencyPenalty](./generative-ai.baseparams.frequencypenalty.md) +[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [GenerationConfig](./generative-ai.generationconfig.md) > [frequencyPenalty](./generative-ai.generationconfig.frequencypenalty.md) -## BaseParams.frequencyPenalty property +## GenerationConfig.frequencyPenalty property Frequency penalty applied to the next token's logprobs, multiplied by the number of times each token has been seen in the respponse so far. diff --git a/docs/reference/main/generative-ai.baseparams.logprobs.md b/docs/reference/main/generative-ai.generationconfig.logprobs.md similarity index 65% rename from docs/reference/main/generative-ai.baseparams.logprobs.md rename to docs/reference/main/generative-ai.generationconfig.logprobs.md index 476c99a..bdf938f 100644 --- a/docs/reference/main/generative-ai.baseparams.logprobs.md +++ b/docs/reference/main/generative-ai.generationconfig.logprobs.md @@ -1,8 +1,8 @@ -[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [BaseParams](./generative-ai.baseparams.md) > [logProbs](./generative-ai.baseparams.logprobs.md) +[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [GenerationConfig](./generative-ai.generationconfig.md) > [logProbs](./generative-ai.generationconfig.logprobs.md) -## BaseParams.logProbs property +## GenerationConfig.logProbs property Valid if responseLogProbs is set to True. This will set the number of top logprobs to return at each decoding step in the logProbsResult. diff --git a/docs/reference/main/generative-ai.generationconfig.md b/docs/reference/main/generative-ai.generationconfig.md index c6e5fa1..509604d 100644 --- a/docs/reference/main/generative-ai.generationconfig.md +++ b/docs/reference/main/generative-ai.generationconfig.md @@ -17,7 +17,11 @@ export interface GenerationConfig | Property | Modifiers | Type | Description | | --- | --- | --- | --- | | [candidateCount?](./generative-ai.generationconfig.candidatecount.md) | | number | _(Optional)_ | +| [frequencyPenalty?](./generative-ai.generationconfig.frequencypenalty.md) | | number | _(Optional)_ Frequency penalty applied to the next token's logprobs, multiplied by the number of times each token has been seen in the respponse so far. | +| [logProbs?](./generative-ai.generationconfig.logprobs.md) | | number | _(Optional)_ Valid if responseLogProbs is set to True. This will set the number of top logprobs to return at each decoding step in the logProbsResult. | | [maxOutputTokens?](./generative-ai.generationconfig.maxoutputtokens.md) | | number | _(Optional)_ | +| [presencePenalty?](./generative-ai.generationconfig.presencepenalty.md) | | number | _(Optional)_ Presence penalty applied to the next token's logprobs if the token has already been seen in the response. | +| [responseLogProbs?](./generative-ai.generationconfig.responselogprobs.md) | | boolean | _(Optional)_ If True, export the logprobs results in response. | | [responseMimeType?](./generative-ai.generationconfig.responsemimetype.md) | | string | _(Optional)_ Output response mimetype of the generated candidate text. Supported mimetype: text/plain: (default) Text output. application/json: JSON response in the candidates. | | [responseSchema?](./generative-ai.generationconfig.responseschema.md) | | [ResponseSchema](./generative-ai.responseschema.md) | _(Optional)_ Output response schema of the generated candidate text. Note: This only applies when the specified responseMIMEType supports a schema; currently this is limited to application/json. | | [stopSequences?](./generative-ai.generationconfig.stopsequences.md) | | string\[\] | _(Optional)_ | diff --git a/docs/reference/main/generative-ai.baseparams.presencepenalty.md b/docs/reference/main/generative-ai.generationconfig.presencepenalty.md similarity index 61% rename from docs/reference/main/generative-ai.baseparams.presencepenalty.md rename to docs/reference/main/generative-ai.generationconfig.presencepenalty.md index 8e21278..06d61dc 100644 --- a/docs/reference/main/generative-ai.baseparams.presencepenalty.md +++ b/docs/reference/main/generative-ai.generationconfig.presencepenalty.md @@ -1,8 +1,8 @@ -[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [BaseParams](./generative-ai.baseparams.md) > [presencePenalty](./generative-ai.baseparams.presencepenalty.md) +[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [GenerationConfig](./generative-ai.generationconfig.md) > [presencePenalty](./generative-ai.generationconfig.presencepenalty.md) -## BaseParams.presencePenalty property +## GenerationConfig.presencePenalty property Presence penalty applied to the next token's logprobs if the token has already been seen in the response. diff --git a/docs/reference/main/generative-ai.baseparams.responselogprobs.md b/docs/reference/main/generative-ai.generationconfig.responselogprobs.md similarity index 56% rename from docs/reference/main/generative-ai.baseparams.responselogprobs.md rename to docs/reference/main/generative-ai.generationconfig.responselogprobs.md index cec8ab4..9ca1c74 100644 --- a/docs/reference/main/generative-ai.baseparams.responselogprobs.md +++ b/docs/reference/main/generative-ai.generationconfig.responselogprobs.md @@ -1,8 +1,8 @@ -[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [BaseParams](./generative-ai.baseparams.md) > [responseLogProbs](./generative-ai.baseparams.responselogprobs.md) +[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [GenerationConfig](./generative-ai.generationconfig.md) > [responseLogProbs](./generative-ai.generationconfig.responselogprobs.md) -## BaseParams.responseLogProbs property +## GenerationConfig.responseLogProbs property If True, export the logprobs results in response. diff --git a/docs/reference/main/generative-ai.generativemodel.frequencypenalty.md b/docs/reference/main/generative-ai.generativemodel.frequencypenalty.md deleted file mode 100644 index 72b29ae..0000000 --- a/docs/reference/main/generative-ai.generativemodel.frequencypenalty.md +++ /dev/null @@ -1,11 +0,0 @@ - - -[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [GenerativeModel](./generative-ai.generativemodel.md) > [frequencyPenalty](./generative-ai.generativemodel.frequencypenalty.md) - -## GenerativeModel.frequencyPenalty property - -**Signature:** - -```typescript -frequencyPenalty?: number; -``` diff --git a/docs/reference/main/generative-ai.generativemodel.logprobs.md b/docs/reference/main/generative-ai.generativemodel.logprobs.md deleted file mode 100644 index 32e184f..0000000 --- a/docs/reference/main/generative-ai.generativemodel.logprobs.md +++ /dev/null @@ -1,11 +0,0 @@ - - -[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [GenerativeModel](./generative-ai.generativemodel.md) > [logProbs](./generative-ai.generativemodel.logprobs.md) - -## GenerativeModel.logProbs property - -**Signature:** - -```typescript -logProbs?: number; -``` diff --git a/docs/reference/main/generative-ai.generativemodel.md b/docs/reference/main/generative-ai.generativemodel.md index 06b21bc..546e68d 100644 --- a/docs/reference/main/generative-ai.generativemodel.md +++ b/docs/reference/main/generative-ai.generativemodel.md @@ -24,12 +24,8 @@ export declare class GenerativeModel | --- | --- | --- | --- | | [apiKey](./generative-ai.generativemodel.apikey.md) | | string | | | [cachedContent](./generative-ai.generativemodel.cachedcontent.md) | | [CachedContent](./generative-ai.cachedcontent.md) | | -| [frequencyPenalty?](./generative-ai.generativemodel.frequencypenalty.md) | | number | _(Optional)_ | | [generationConfig](./generative-ai.generativemodel.generationconfig.md) | | [GenerationConfig](./generative-ai.generationconfig.md) | | -| [logProbs?](./generative-ai.generativemodel.logprobs.md) | | number | _(Optional)_ | | [model](./generative-ai.generativemodel.model.md) | | string | | -| [presencePenalty?](./generative-ai.generativemodel.presencepenalty.md) | | number | _(Optional)_ | -| [responseLogProbs?](./generative-ai.generativemodel.responselogprobs.md) | | boolean | _(Optional)_ | | [safetySettings](./generative-ai.generativemodel.safetysettings.md) | | [SafetySetting](./generative-ai.safetysetting.md)\[\] | | | [systemInstruction?](./generative-ai.generativemodel.systeminstruction.md) | | [Content](./generative-ai.content.md) | _(Optional)_ | | [toolConfig?](./generative-ai.generativemodel.toolconfig.md) | | [ToolConfig](./generative-ai.toolconfig.md) | _(Optional)_ | diff --git a/docs/reference/main/generative-ai.generativemodel.presencepenalty.md b/docs/reference/main/generative-ai.generativemodel.presencepenalty.md deleted file mode 100644 index 746b79d..0000000 --- a/docs/reference/main/generative-ai.generativemodel.presencepenalty.md +++ /dev/null @@ -1,11 +0,0 @@ - - -[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [GenerativeModel](./generative-ai.generativemodel.md) > [presencePenalty](./generative-ai.generativemodel.presencepenalty.md) - -## GenerativeModel.presencePenalty property - -**Signature:** - -```typescript -presencePenalty?: number; -``` diff --git a/docs/reference/main/generative-ai.generativemodel.responselogprobs.md b/docs/reference/main/generative-ai.generativemodel.responselogprobs.md deleted file mode 100644 index 3aebc99..0000000 --- a/docs/reference/main/generative-ai.generativemodel.responselogprobs.md +++ /dev/null @@ -1,11 +0,0 @@ - - -[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [GenerativeModel](./generative-ai.generativemodel.md) > [responseLogProbs](./generative-ai.generativemodel.responselogprobs.md) - -## GenerativeModel.responseLogProbs property - -**Signature:** - -```typescript -responseLogProbs?: boolean; -``` diff --git a/docs/reference/main/generative-ai.candidate.logprobability.md b/docs/reference/main/generative-ai.logprobscandidate.logprobability.md similarity index 54% rename from docs/reference/main/generative-ai.candidate.logprobability.md rename to docs/reference/main/generative-ai.logprobscandidate.logprobability.md index 93a65a5..78917ee 100644 --- a/docs/reference/main/generative-ai.candidate.logprobability.md +++ b/docs/reference/main/generative-ai.logprobscandidate.logprobability.md @@ -1,8 +1,8 @@ -[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [Candidate](./generative-ai.candidate.md) > [logProbability](./generative-ai.candidate.logprobability.md) +[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [LogprobsCandidate](./generative-ai.logprobscandidate.md) > [logProbability](./generative-ai.logprobscandidate.logprobability.md) -## Candidate.logProbability property +## LogprobsCandidate.logProbability property The candidate's log probability. diff --git a/docs/reference/main/generative-ai.logprobscandidate.md b/docs/reference/main/generative-ai.logprobscandidate.md new file mode 100644 index 0000000..782110d --- /dev/null +++ b/docs/reference/main/generative-ai.logprobscandidate.md @@ -0,0 +1,22 @@ + + +[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [LogprobsCandidate](./generative-ai.logprobscandidate.md) + +## LogprobsCandidate interface + +Candidate for the logprobs token and score. + +**Signature:** + +```typescript +export interface LogprobsCandidate +``` + +## Properties + +| Property | Modifiers | Type | Description | +| --- | --- | --- | --- | +| [logProbability](./generative-ai.logprobscandidate.logprobability.md) | | number | The candidate's log probability. | +| [token](./generative-ai.logprobscandidate.token.md) | | string | The candidate's token string value. | +| [tokenID](./generative-ai.logprobscandidate.tokenid.md) | | number | The candidate's token id value. | + diff --git a/docs/reference/main/generative-ai.candidate.token.md b/docs/reference/main/generative-ai.logprobscandidate.token.md similarity index 57% rename from docs/reference/main/generative-ai.candidate.token.md rename to docs/reference/main/generative-ai.logprobscandidate.token.md index c8925d9..6fd3ddb 100644 --- a/docs/reference/main/generative-ai.candidate.token.md +++ b/docs/reference/main/generative-ai.logprobscandidate.token.md @@ -1,8 +1,8 @@ -[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [Candidate](./generative-ai.candidate.md) > [token](./generative-ai.candidate.token.md) +[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [LogprobsCandidate](./generative-ai.logprobscandidate.md) > [token](./generative-ai.logprobscandidate.token.md) -## Candidate.token property +## LogprobsCandidate.token property The candidate's token string value. diff --git a/docs/reference/main/generative-ai.candidate.tokenid.md b/docs/reference/main/generative-ai.logprobscandidate.tokenid.md similarity index 56% rename from docs/reference/main/generative-ai.candidate.tokenid.md rename to docs/reference/main/generative-ai.logprobscandidate.tokenid.md index c1088ed..5f926a4 100644 --- a/docs/reference/main/generative-ai.candidate.tokenid.md +++ b/docs/reference/main/generative-ai.logprobscandidate.tokenid.md @@ -1,8 +1,8 @@ -[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [Candidate](./generative-ai.candidate.md) > [tokenID](./generative-ai.candidate.tokenid.md) +[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [LogprobsCandidate](./generative-ai.logprobscandidate.md) > [tokenID](./generative-ai.logprobscandidate.tokenid.md) -## Candidate.tokenID property +## LogprobsCandidate.tokenID property The candidate's token id value. diff --git a/docs/reference/main/generative-ai.logprobsresult.chosencandidates.md b/docs/reference/main/generative-ai.logprobsresult.chosencandidates.md index 8bb0c7d..0f8063d 100644 --- a/docs/reference/main/generative-ai.logprobsresult.chosencandidates.md +++ b/docs/reference/main/generative-ai.logprobsresult.chosencandidates.md @@ -9,5 +9,5 @@ Length = total number of decoding steps. The chosen candidates may or may not be **Signature:** ```typescript -chosenCandidates: Candidate[]; +chosenCandidates: LogprobsCandidate[]; ``` diff --git a/docs/reference/main/generative-ai.logprobsresult.md b/docs/reference/main/generative-ai.logprobsresult.md index d29531e..1083a5d 100644 --- a/docs/reference/main/generative-ai.logprobsresult.md +++ b/docs/reference/main/generative-ai.logprobsresult.md @@ -16,6 +16,6 @@ export interface LogprobsResult | Property | Modifiers | Type | Description | | --- | --- | --- | --- | -| [chosenCandidates](./generative-ai.logprobsresult.chosencandidates.md) | | [Candidate](./generative-ai.candidate.md)\[\] | Length = total number of decoding steps. The chosen candidates may or may not be in topCandidates. | +| [chosenCandidates](./generative-ai.logprobsresult.chosencandidates.md) | | [LogprobsCandidate](./generative-ai.logprobscandidate.md)\[\] | Length = total number of decoding steps. The chosen candidates may or may not be in topCandidates. | | [topCandidates](./generative-ai.logprobsresult.topcandidates.md) | | [TopCandidates](./generative-ai.topcandidates.md)\[\] | Length = total number of decoding steps. | diff --git a/docs/reference/main/generative-ai.md b/docs/reference/main/generative-ai.md index d50a8e9..33dc5e4 100644 --- a/docs/reference/main/generative-ai.md +++ b/docs/reference/main/generative-ai.md @@ -40,7 +40,6 @@ | [BatchEmbedContentsResponse](./generative-ai.batchembedcontentsresponse.md) | Response from calling [GenerativeModel.batchEmbedContents()](./generative-ai.generativemodel.batchembedcontents.md). | | [CachedContent](./generative-ai.cachedcontent.md) | Describes CachedContent interface for sending to the server (if creating) or received from the server (using getters or list methods). | | [CachedContentBase](./generative-ai.cachedcontentbase.md) | | -| [Candidate](./generative-ai.candidate.md) | Candidate for the logprobs token and score. | | [CitationMetadata](./generative-ai.citationmetadata.md) | Citation metadata that may be found on a [GenerateContentCandidate](./generative-ai.generatecontentcandidate.md). | | [CitationSource](./generative-ai.citationsource.md) | A single citation source. | | [CodeExecutionResult](./generative-ai.codeexecutionresult.md) | Result of executing the ExecutableCode. Only generated when using code execution, and always follows a Part containing the ExecutableCode. | @@ -75,6 +74,7 @@ | [GenerationConfig](./generative-ai.generationconfig.md) | Config options for content-related requests | | [GenerativeContentBlob](./generative-ai.generativecontentblob.md) | Interface for sending an image. | | [InlineDataPart](./generative-ai.inlinedatapart.md) | Content part interface if the part represents an image. | +| [LogprobsCandidate](./generative-ai.logprobscandidate.md) | Candidate for the logprobs token and score. | | [LogprobsResult](./generative-ai.logprobsresult.md) | Logprobs Result | | [ModelParams](./generative-ai.modelparams.md) | Params passed to [GoogleGenerativeAI.getGenerativeModel()](./generative-ai.googlegenerativeai.getgenerativemodel.md). | | [PromptFeedback](./generative-ai.promptfeedback.md) | If the prompt was blocked, this will be populated with blockReason and the relevant safetyRatings. | diff --git a/docs/reference/main/generative-ai.topcandidates.candidates.md b/docs/reference/main/generative-ai.topcandidates.candidates.md index e64ec37..9281683 100644 --- a/docs/reference/main/generative-ai.topcandidates.candidates.md +++ b/docs/reference/main/generative-ai.topcandidates.candidates.md @@ -9,5 +9,5 @@ Sorted by log probability in descending order. **Signature:** ```typescript -candidates: Candidate[]; +candidates: LogprobsCandidate[]; ``` diff --git a/docs/reference/main/generative-ai.topcandidates.md b/docs/reference/main/generative-ai.topcandidates.md index 6525b90..a5cbc6e 100644 --- a/docs/reference/main/generative-ai.topcandidates.md +++ b/docs/reference/main/generative-ai.topcandidates.md @@ -16,5 +16,5 @@ export interface TopCandidates | Property | Modifiers | Type | Description | | --- | --- | --- | --- | -| [candidates](./generative-ai.topcandidates.candidates.md) | | [Candidate](./generative-ai.candidate.md)\[\] | Sorted by log probability in descending order. | +| [candidates](./generative-ai.topcandidates.candidates.md) | | [LogprobsCandidate](./generative-ai.logprobscandidate.md)\[\] | Sorted by log probability in descending order. | diff --git a/src/methods/generate-content.test.ts b/src/methods/generate-content.test.ts index 5f49dc1..3728e6a 100644 --- a/src/methods/generate-content.test.ts +++ b/src/methods/generate-content.test.ts @@ -101,6 +101,33 @@ describe("generateContent()", () => { match.any, ); }); + it("logprobs", async () => { + const mockResponse = getMockResponse("unary-success-logprobs.json"); + const makeRequestStub = stub(request, "makeModelRequest").resolves( + mockResponse as Response, + ); + const result = await generateContent("key", "model", fakeRequestParams); + expect(result.response.text()).to.include("Quantum mechanics is"); + expect(result.response.candidates[0].avgLogprobs).to.equal(7.5); + expect( + result.response.candidates[0].logprobsResult.topCandidates[0].candidates + .length, + ).to.equal(1); + expect( + result.response.candidates[0].logprobsResult.chosenCandidates[0] + .logProbability, + ).to.equal(0.75); + expect( + result.response.candidates[0].citationMetadata.citationSources.length, + ).to.equal(1); + expect(makeRequestStub).to.be.calledWith( + "model", + request.Task.GENERATE_CONTENT, + "key", + false, + match.any, + ); + }); it("blocked prompt", async () => { const mockResponse = getMockResponse( "unary-failure-prompt-blocked-safety.json", diff --git a/src/models/generative-model.test.ts b/src/models/generative-model.test.ts index 46d989e..6485646 100644 --- a/src/models/generative-model.test.ts +++ b/src/models/generative-model.test.ts @@ -95,18 +95,10 @@ describe("GenerativeModel", () => { expect( genModel.generationConfig?.responseSchema.properties.testField.type, ).to.equal(SchemaType.STRING); - expect(genModel.generationConfig?.presencePenalty).to.equal( - 0.6, - ); - expect(genModel.generationConfig?.frequencyPenalty).to.equal( - 0.5, - ); - expect(genModel.generationConfig?.responseLogProbs).to.equal( - true - ); - expect(genModel.generationConfig?.logProbs).to.equal( - 2 - ); + expect(genModel.generationConfig?.presencePenalty).to.equal(0.6); + expect(genModel.generationConfig?.frequencyPenalty).to.equal(0.5); + expect(genModel.generationConfig?.responseLogProbs).to.equal(true); + expect(genModel.generationConfig?.logProbs).to.equal(2); expect(genModel.safetySettings?.length).to.equal(1); expect(genModel.toolConfig?.functionCallingConfig.mode).to.equal( FunctionCallingMode.NONE, @@ -243,7 +235,7 @@ describe("GenerativeModel", () => { value.includes("topK") && value.includes("newTestField") && !value.includes("testField") && - value.includes(HarmCategory.HARM_CATEGORY_HARASSMENT) && + value.includes(HarmCategory.HARM_CATEGORY_HARASSMENT) ); }), {}, diff --git a/test-utils/mock-responses/unary-success-logprobs.json b/test-utils/mock-responses/unary-success-logprobs.json new file mode 100644 index 0000000..e00da7f --- /dev/null +++ b/test-utils/mock-responses/unary-success-logprobs.json @@ -0,0 +1,86 @@ +{ + "candidates": [ + { + "content": { + "parts": [ + { + "text": "1. **Definition:**\nQuantum mechanics is a fundamental theory in physics that provides the foundation for understanding the physical properties of nature at the scale of atoms and subatomic particles. It is based on the idea that energy, momentum, angular momentum, and other quantities are quantized, meaning they can only exist in discrete values. \n\n2. **Key Concepts:**\n - **Wave-particle Duality:** Particles such as electrons and photons can exhibit both wave-like and particle-like behaviors. \n - **Uncertainty Principle:** Proposed by Werner Heisenberg, it states that the more precisely the position of a particle is known, the less precisely its momentum can be known, and vice versa. \n - **Quantum Superposition:** A quantum system can exist in multiple states simultaneously until it is measured. \n - **Quantum Entanglement:** Two or more particles can become linked in such a way that the state of one affects the state of the others, regardless of the distance between them. \n\n3. **Implications and Applications:**\n - **Atomic and Molecular Structure:** Quantum mechanics explains the structure of atoms, molecules, and chemical bonds. \n - **Quantum Computing:** It enables the development of quantum computers that can solve certain computational problems much faster than classical computers. \n - **Quantum Cryptography:** Quantum principles are used to develop secure communication methods. \n - **Quantum Field Theory (QFT):** A relativistic theory that describes interactions between particles in quantum mechanics. It underpins the Standard Model of Physics. \n - **Quantum Gravity:** Attempts to reconcile quantum mechanics with general relativity to explain the behavior of matter and gravity at very small scales. \n\n4. **Learning Resources:**\n - **Books:**\n - \"Quantum Mechanics for Mathematicians\" by James Glimm and Arthur Jaffe \n - \"Principles of Quantum Mechanics\" by R. Shankar\n - \"Quantum Mechanics: Concepts and Applications\" by Nouredine Zettili \n - **Online Courses and Tutorials:**\n - [Quantum Mechanics I](https://www.example.com) on Coursera\n - [MIT OpenCourseWare](https://www.example.com) Quantum Physics I course materials \n - [Khan Academy](https://www.example.com) Quantum Physics video tutorials \n - **Videos and Documentaries:**\n - [Quantum Mechanics - Crash Course Physics](https://www.example.com)\n - [Quantum Mechanics: The Strangest Theory](https://www.example.com)\n - [BBC: The Quantum World with Jim Al-Khalili](https://www.example.com)\n - [NOVA: The Fabric of the Cosmos](https://www.example.com)" + } + ], + "role": "model" + }, + "finishReason": "STOP", + "index": 0, + "safetyRatings": [ + { + "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", + "probability": "NEGLIGIBLE" + }, + { + "category": "HARM_CATEGORY_HATE_SPEECH", + "probability": "NEGLIGIBLE" + }, + { + "category": "HARM_CATEGORY_HARASSMENT", + "probability": "NEGLIGIBLE" + }, + { + "category": "HARM_CATEGORY_DANGEROUS_CONTENT", + "probability": "NEGLIGIBLE" + } + ], + "citationMetadata": { + "citationSources": [ + { + "startIndex": 574, + "endIndex": 705, + "uri": "https://www.example.com", + "license": "" + } + ] + }, + "avgLogprobs": 7.50, + "logprobsResult": { + "topCandidates": [ + { + "candidates": [ + { + "token": "Some string.", + "tokenID": 1, + "logProbability": 0.65 + } + ] + } + ], + "chosenCandidates": [ + { + "token": "Some other string.", + "tokenID": 2, + "logProbability": 0.75 + } + ] + } + } + ], + "promptFeedback": { + "safetyRatings": [ + { + "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", + "probability": "NEGLIGIBLE" + }, + { + "category": "HARM_CATEGORY_HATE_SPEECH", + "probability": "NEGLIGIBLE" + }, + { + "category": "HARM_CATEGORY_HARASSMENT", + "probability": "NEGLIGIBLE" + }, + { + "category": "HARM_CATEGORY_DANGEROUS_CONTENT", + "probability": "NEGLIGIBLE" + } + ] + } + } + \ No newline at end of file diff --git a/types/responses.ts b/types/responses.ts index 54db37f..9966c85 100644 --- a/types/responses.ts +++ b/types/responses.ts @@ -98,14 +98,14 @@ export interface LogprobsResult { * Length = total number of decoding steps. * The chosen candidates may or may not be in topCandidates. */ - chosenCandidates: LogProbsCandidate[]; + chosenCandidates: LogprobsCandidate[]; } /** * Candidate for the logprobs token and score. * @public */ -export interface LogProbsCandidate { +export interface LogprobsCandidate { /** The candidate's token string value. */ token: string; /** The candidate's token id value. */ @@ -119,7 +119,7 @@ export interface LogProbsCandidate { */ export interface TopCandidates { /** Sorted by log probability in descending order. */ - candidates: LogProbsCandidate[]; + candidates: LogprobsCandidate[]; } /** From 21be624adef880989dff3339e72a86cf89d8ba56 Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Thu, 26 Sep 2024 12:45:42 -0700 Subject: [PATCH 8/9] Update case of logprobs --- common/api-review/generative-ai.api.md | 4 ++-- .../main/generative-ai.generationconfig.logprobs.md | 8 ++++---- docs/reference/main/generative-ai.generationconfig.md | 4 ++-- .../generative-ai.generationconfig.responselogprobs.md | 6 +++--- src/models/generative-model.test.ts | 8 ++++---- types/requests.ts | 6 +++--- 6 files changed, 18 insertions(+), 18 deletions(-) diff --git a/common/api-review/generative-ai.api.md b/common/api-review/generative-ai.api.md index 19779e4..9a6f054 100644 --- a/common/api-review/generative-ai.api.md +++ b/common/api-review/generative-ai.api.md @@ -430,11 +430,11 @@ export interface GenerationConfig { // (undocumented) candidateCount?: number; frequencyPenalty?: number; - logProbs?: number; + logprobs?: number; // (undocumented) maxOutputTokens?: number; presencePenalty?: number; - responseLogProbs?: boolean; + responseLogprobs?: boolean; responseMimeType?: string; responseSchema?: ResponseSchema; // (undocumented) diff --git a/docs/reference/main/generative-ai.generationconfig.logprobs.md b/docs/reference/main/generative-ai.generationconfig.logprobs.md index bdf938f..cdb6f1e 100644 --- a/docs/reference/main/generative-ai.generationconfig.logprobs.md +++ b/docs/reference/main/generative-ai.generationconfig.logprobs.md @@ -1,13 +1,13 @@ -[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [GenerationConfig](./generative-ai.generationconfig.md) > [logProbs](./generative-ai.generationconfig.logprobs.md) +[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [GenerationConfig](./generative-ai.generationconfig.md) > [logprobs](./generative-ai.generationconfig.logprobs.md) -## GenerationConfig.logProbs property +## GenerationConfig.logprobs property -Valid if responseLogProbs is set to True. This will set the number of top logprobs to return at each decoding step in the logProbsResult. +Valid if responseLogProbs is set to True. This will set the number of top logprobs to return at each decoding step in the logprobsResult. **Signature:** ```typescript -logProbs?: number; +logprobs?: number; ``` diff --git a/docs/reference/main/generative-ai.generationconfig.md b/docs/reference/main/generative-ai.generationconfig.md index 509604d..06f7695 100644 --- a/docs/reference/main/generative-ai.generationconfig.md +++ b/docs/reference/main/generative-ai.generationconfig.md @@ -18,10 +18,10 @@ export interface GenerationConfig | --- | --- | --- | --- | | [candidateCount?](./generative-ai.generationconfig.candidatecount.md) | | number | _(Optional)_ | | [frequencyPenalty?](./generative-ai.generationconfig.frequencypenalty.md) | | number | _(Optional)_ Frequency penalty applied to the next token's logprobs, multiplied by the number of times each token has been seen in the respponse so far. | -| [logProbs?](./generative-ai.generationconfig.logprobs.md) | | number | _(Optional)_ Valid if responseLogProbs is set to True. This will set the number of top logprobs to return at each decoding step in the logProbsResult. | +| [logprobs?](./generative-ai.generationconfig.logprobs.md) | | number | _(Optional)_ Valid if responseLogProbs is set to True. This will set the number of top logprobs to return at each decoding step in the logprobsResult. | | [maxOutputTokens?](./generative-ai.generationconfig.maxoutputtokens.md) | | number | _(Optional)_ | | [presencePenalty?](./generative-ai.generationconfig.presencepenalty.md) | | number | _(Optional)_ Presence penalty applied to the next token's logprobs if the token has already been seen in the response. | -| [responseLogProbs?](./generative-ai.generationconfig.responselogprobs.md) | | boolean | _(Optional)_ If True, export the logprobs results in response. | +| [responseLogprobs?](./generative-ai.generationconfig.responselogprobs.md) | | boolean | _(Optional)_ If True, export the logprobs results in response. | | [responseMimeType?](./generative-ai.generationconfig.responsemimetype.md) | | string | _(Optional)_ Output response mimetype of the generated candidate text. Supported mimetype: text/plain: (default) Text output. application/json: JSON response in the candidates. | | [responseSchema?](./generative-ai.generationconfig.responseschema.md) | | [ResponseSchema](./generative-ai.responseschema.md) | _(Optional)_ Output response schema of the generated candidate text. Note: This only applies when the specified responseMIMEType supports a schema; currently this is limited to application/json. | | [stopSequences?](./generative-ai.generationconfig.stopsequences.md) | | string\[\] | _(Optional)_ | diff --git a/docs/reference/main/generative-ai.generationconfig.responselogprobs.md b/docs/reference/main/generative-ai.generationconfig.responselogprobs.md index 9ca1c74..8f98afa 100644 --- a/docs/reference/main/generative-ai.generationconfig.responselogprobs.md +++ b/docs/reference/main/generative-ai.generationconfig.responselogprobs.md @@ -1,13 +1,13 @@ -[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [GenerationConfig](./generative-ai.generationconfig.md) > [responseLogProbs](./generative-ai.generationconfig.responselogprobs.md) +[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [GenerationConfig](./generative-ai.generationconfig.md) > [responseLogprobs](./generative-ai.generationconfig.responselogprobs.md) -## GenerationConfig.responseLogProbs property +## GenerationConfig.responseLogprobs property If True, export the logprobs results in response. **Signature:** ```typescript -responseLogProbs?: boolean; +responseLogprobs?: boolean; ``` diff --git a/src/models/generative-model.test.ts b/src/models/generative-model.test.ts index 6485646..427d7aa 100644 --- a/src/models/generative-model.test.ts +++ b/src/models/generative-model.test.ts @@ -66,8 +66,8 @@ describe("GenerativeModel", () => { }, presencePenalty: 0.6, frequencyPenalty: 0.5, - responseLogProbs: true, - logProbs: 2, + responseLogprobs: true, + logprobs: 2, }, safetySettings: [ { @@ -97,8 +97,8 @@ describe("GenerativeModel", () => { ).to.equal(SchemaType.STRING); expect(genModel.generationConfig?.presencePenalty).to.equal(0.6); expect(genModel.generationConfig?.frequencyPenalty).to.equal(0.5); - expect(genModel.generationConfig?.responseLogProbs).to.equal(true); - expect(genModel.generationConfig?.logProbs).to.equal(2); + expect(genModel.generationConfig?.responseLogprobs).to.equal(true); + expect(genModel.generationConfig?.logprobs).to.equal(2); expect(genModel.safetySettings?.length).to.equal(1); expect(genModel.toolConfig?.functionCallingConfig.mode).to.equal( FunctionCallingMode.NONE, diff --git a/types/requests.ts b/types/requests.ts index 197571e..cd0664f 100644 --- a/types/requests.ts +++ b/types/requests.ts @@ -115,12 +115,12 @@ export interface GenerationConfig { /** * If True, export the logprobs results in response. */ - responseLogProbs?: boolean; + responseLogprobs?: boolean; /** * Valid if responseLogProbs is set to True. This will set the number of top - * logprobs to return at each decoding step in the logProbsResult. + * logprobs to return at each decoding step in the logprobsResult. */ - logProbs?: number; + logprobs?: number; } /** From c0a2cffe585e633107b67e12e7c9a6c75be51ede Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Thu, 26 Sep 2024 13:12:59 -0700 Subject: [PATCH 9/9] put back parameters in test case --- src/models/generative-model.test.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/models/generative-model.test.ts b/src/models/generative-model.test.ts index 427d7aa..bfaabcd 100644 --- a/src/models/generative-model.test.ts +++ b/src/models/generative-model.test.ts @@ -123,7 +123,11 @@ describe("GenerativeModel", () => { value.includes("be friendly") && value.includes("temperature") && value.includes("testField") && - value.includes(HarmBlockThreshold.BLOCK_LOW_AND_ABOVE) + value.includes(HarmBlockThreshold.BLOCK_LOW_AND_ABOVE) && + value.includes("presencePenalty") && + value.includes("frequencyPenalty") && + value.includes("responseLogprobs") && + value.includes("logprobs") ); }), match((value) => { @@ -206,6 +210,10 @@ describe("GenerativeModel", () => { }, }, }, + presencePenalty: 0.6, + frequencyPenalty: 0.5, + responseLogprobs: true, + logprobs: 2, }, safetySettings: [ { @@ -235,7 +243,11 @@ describe("GenerativeModel", () => { value.includes("topK") && value.includes("newTestField") && !value.includes("testField") && - value.includes(HarmCategory.HARM_CATEGORY_HARASSMENT) + value.includes(HarmCategory.HARM_CATEGORY_HARASSMENT) && + value.includes("presencePenalty") && + value.includes("frequencyPenalty") && + value.includes("responseLogprobs") && + value.includes("logprobs") ); }), {},