Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add code execution feature #185

Merged
merged 6 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cool-rivers-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@google/generative-ai": minor
---

Add code execution feature.
96 changes: 88 additions & 8 deletions common/api-review/generative-ai-server.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,45 @@ export interface CachedContentUpdateParams {
updateMask?: string[];
}

// Warning: (ae-internal-missing-underscore) The name "CachedContentUpdateRequest" should be prefixed with an underscore because the declaration is marked as @internal
//
// @internal
export interface CachedContentUpdateRequest {
export interface _CachedContentUpdateRequest {
// (undocumented)
cachedContent: CachedContentUpdateRequestFields;
cachedContent: _CachedContentUpdateRequestFields;
updateMask?: string[];
}

// Warning: (ae-internal-missing-underscore) The name "CachedContentUpdateRequestFields" should be prefixed with an underscore because the declaration is marked as @internal
//
// @internal
export interface CachedContentUpdateRequestFields {
export interface _CachedContentUpdateRequestFields {
// (undocumented)
expireTime?: string;
// (undocumented)
ttl?: string;
}

// @public
export interface CodeExecutionResult {
outcome: Outcome;
output: string;
}

// @public
export interface CodeExecutionResultPart {
// (undocumented)
codeExecutionResult: CodeExecutionResult;
// (undocumented)
executableCode?: never;
// (undocumented)
fileData?: never;
// (undocumented)
functionCall?: never;
// (undocumented)
functionResponse?: never;
// (undocumented)
inlineData?: never;
// (undocumented)
text?: never;
}

// @public
export interface Content {
// (undocumented)
Expand All @@ -91,6 +111,38 @@ export interface ErrorDetails {
reason?: string;
}

// @public
export interface ExecutableCode {
code: string;
language: ExecutableCodeLanguage;
}

// @public (undocumented)
export enum ExecutableCodeLanguage {
// (undocumented)
LANGUAGE_UNSPECIFIED = "language_unspecified",
// (undocumented)
PYTHON = "python"
}

// @public
export interface ExecutableCodePart {
// (undocumented)
codeExecutionResult?: never;
// (undocumented)
executableCode: ExecutableCode;
// (undocumented)
fileData?: never;
// (undocumented)
functionCall?: never;
// (undocumented)
functionResponse?: never;
// (undocumented)
inlineData?: never;
// (undocumented)
text?: never;
}

// @public
export interface FileData {
// (undocumented)
Expand All @@ -101,6 +153,10 @@ export interface FileData {

// @public
export interface FileDataPart {
// (undocumented)
codeExecutionResult?: never;
// (undocumented)
executableCode?: never;
// (undocumented)
fileData: FileData;
// (undocumented)
Expand Down Expand Up @@ -191,6 +247,10 @@ export enum FunctionCallingMode {

// @public
export interface FunctionCallPart {
// (undocumented)
codeExecutionResult?: never;
// (undocumented)
executableCode?: never;
// (undocumented)
fileData?: never;
// (undocumented)
Expand Down Expand Up @@ -249,6 +309,10 @@ export interface FunctionResponse {

// @public
export interface FunctionResponsePart {
// (undocumented)
codeExecutionResult?: never;
// (undocumented)
executableCode?: never;
// (undocumented)
fileData?: never;
// (undocumented)
Expand Down Expand Up @@ -293,6 +357,10 @@ export class GoogleAIFileManager {

// @public
export interface InlineDataPart {
// (undocumented)
codeExecutionResult?: never;
// (undocumented)
executableCode?: never;
// (undocumented)
fileData?: never;
// (undocumented)
Expand Down Expand Up @@ -330,7 +398,15 @@ export interface ListParams {
}

// @public
export type Part = TextPart | InlineDataPart | FunctionCallPart | FunctionResponsePart | FileDataPart;
export enum Outcome {
OUTCOME_DEADLINE_EXCEEDED = "outcome_deadline_exceeded",
OUTCOME_FAILED = "outcome_failed",
OUTCOME_OK = "outcome_ok",
OUTCOME_UNSPECIFIED = "outcome_unspecified"
}

// @public
export type Part = TextPart | InlineDataPart | FunctionCallPart | FunctionResponsePart | FileDataPart | ExecutableCodePart | CodeExecutionResultPart;

// @public
export interface RequestOptions {
Expand Down Expand Up @@ -369,6 +445,10 @@ export interface Schema {

// @public
export interface TextPart {
// (undocumented)
codeExecutionResult?: never;
// (undocumented)
executableCode?: never;
// (undocumented)
fileData?: never;
// (undocumented)
Expand Down
101 changes: 93 additions & 8 deletions common/api-review/generative-ai.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,35 @@ export interface CitationSource {
uri?: string;
}

// @public
export interface CodeExecutionResult {
outcome: Outcome;
output: string;
}

// @public
export interface CodeExecutionResultPart {
// (undocumented)
codeExecutionResult: CodeExecutionResult;
// (undocumented)
executableCode?: never;
// (undocumented)
fileData?: never;
// (undocumented)
functionCall?: never;
// (undocumented)
functionResponse?: never;
// (undocumented)
inlineData?: never;
// (undocumented)
text?: never;
}

// @public
export interface CodeExecutionTool {
codeExecution: {};
}

// @public
export interface Content {
// (undocumented)
Expand All @@ -114,14 +143,12 @@ export interface CountTokensRequest {
generateContentRequest?: GenerateContentRequest;
}

// Warning: (ae-internal-missing-underscore) The name "CountTokensRequestInternal" should be prefixed with an underscore because the declaration is marked as @internal
//
// @internal
export interface CountTokensRequestInternal {
export interface _CountTokensRequestInternal {
// (undocumented)
contents?: Content[];
// (undocumented)
generateContentRequest?: GenerateContentRequestInternal;
generateContentRequest?: _GenerateContentRequestInternal;
}

// @public
Expand Down Expand Up @@ -168,6 +195,38 @@ export interface ErrorDetails {
reason?: string;
}

// @public
export interface ExecutableCode {
code: string;
language: ExecutableCodeLanguage;
}

// @public (undocumented)
export enum ExecutableCodeLanguage {
// (undocumented)
LANGUAGE_UNSPECIFIED = "language_unspecified",
// (undocumented)
PYTHON = "python"
}

// @public
export interface ExecutableCodePart {
// (undocumented)
codeExecutionResult?: never;
// (undocumented)
executableCode: ExecutableCode;
// (undocumented)
fileData?: never;
// (undocumented)
functionCall?: never;
// (undocumented)
functionResponse?: never;
// (undocumented)
inlineData?: never;
// (undocumented)
text?: never;
}

// @public
export interface FileData {
// (undocumented)
Expand All @@ -178,6 +237,10 @@ export interface FileData {

// @public
export interface FileDataPart {
// (undocumented)
codeExecutionResult?: never;
// (undocumented)
executableCode?: never;
// (undocumented)
fileData: FileData;
// (undocumented)
Expand Down Expand Up @@ -236,6 +299,10 @@ export enum FunctionCallingMode {

// @public
export interface FunctionCallPart {
// (undocumented)
codeExecutionResult?: never;
// (undocumented)
executableCode?: never;
// (undocumented)
fileData?: never;
// (undocumented)
Expand Down Expand Up @@ -294,6 +361,10 @@ export interface FunctionResponse {

// @public
export interface FunctionResponsePart {
// (undocumented)
codeExecutionResult?: never;
// (undocumented)
executableCode?: never;
// (undocumented)
fileData?: never;
// (undocumented)
Expand Down Expand Up @@ -335,10 +406,8 @@ export interface GenerateContentRequest extends BaseParams {
tools?: Tool[];
}

// Warning: (ae-internal-missing-underscore) The name "GenerateContentRequestInternal" should be prefixed with an underscore because the declaration is marked as @internal
//
// @internal
export interface GenerateContentRequestInternal extends GenerateContentRequest {
export interface _GenerateContentRequestInternal extends GenerateContentRequest {
// (undocumented)
model?: string;
}
Expand Down Expand Up @@ -498,6 +567,10 @@ export enum HarmProbability {

// @public
export interface InlineDataPart {
// (undocumented)
codeExecutionResult?: never;
// (undocumented)
executableCode?: never;
// (undocumented)
fileData?: never;
// (undocumented)
Expand Down Expand Up @@ -525,7 +598,15 @@ export interface ModelParams extends BaseParams {
}

// @public
export type Part = TextPart | InlineDataPart | FunctionCallPart | FunctionResponsePart | FileDataPart;
export enum Outcome {
OUTCOME_DEADLINE_EXCEEDED = "outcome_deadline_exceeded",
OUTCOME_FAILED = "outcome_failed",
OUTCOME_OK = "outcome_ok",
OUTCOME_UNSPECIFIED = "outcome_unspecified"
}

// @public
export type Part = TextPart | InlineDataPart | FunctionCallPart | FunctionResponsePart | FileDataPart | ExecutableCodePart | CodeExecutionResultPart;

// @public
export const POSSIBLE_ROLES: readonly ["user", "model", "function", "system"];
Expand Down Expand Up @@ -615,6 +696,10 @@ export enum TaskType {

// @public
export interface TextPart {
// (undocumented)
codeExecutionResult?: never;
// (undocumented)
executableCode?: never;
// (undocumented)
fileData?: never;
// (undocumented)
Expand Down
1 change: 1 addition & 0 deletions config/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ module.exports = {
regex: "^I[A-Z]",
match: false,
},
leadingUnderscore: 'allow'
},
],
"@typescript-eslint/consistent-type-definitions": ["error", "interface"],
Expand Down
6 changes: 5 additions & 1 deletion packages/main/src/methods/chat-session-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ const VALID_PART_FIELDS: Array<keyof Part> = [
"inlineData",
"functionCall",
"functionResponse",
"executableCode",
"codeExecutionResult",
];

const VALID_PARTS_PER_ROLE: { [key in Role]: Array<keyof Part> } = {
user: ["text", "inlineData"],
function: ["functionResponse"],
model: ["text", "functionCall"],
model: ["text", "functionCall", "executableCode", "codeExecutionResult"],
// System instructions shouldn't be in history anyway.
system: ["text"],
};
Expand Down Expand Up @@ -72,6 +74,8 @@ export function validateChatHistory(history: Content[]): void {
functionCall: 0,
functionResponse: 0,
fileData: 0,
executableCode: 0,
codeExecutionResult: 0,
};

for (const part of parts) {
Expand Down
Loading
Loading