Skip to content

Commit

Permalink
Add responseSchema to generationConfig (#158)
Browse files Browse the repository at this point in the history
* Add JSON Schema Support

* Add unit tests

* Add changeset

* update changeset

* Add some unit test cases (#159)

---------

Co-authored-by: Christina Holland <[email protected]>
  • Loading branch information
lahirumaramba and hsubox76 authored May 29, 2024
1 parent bb7e6c9 commit 85ff2c4
Show file tree
Hide file tree
Showing 23 changed files with 261 additions and 101 deletions.
5 changes: 5 additions & 0 deletions .changeset/five-spoons-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@google/generative-ai": minor
---

Added `responseSchema` to `GenerationConfig` to allow user to provide a JSON schema when `responseMimeType` is set to JSON.
1 change: 1 addition & 0 deletions docs/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Steps to build locally:
1. Run `yarn test` to run unit tests.
1. Run `yarn docs` to generate any changes to reference docs (destination dir is docs/reference).
1. Run `yarn format` to fix formatting and add license headers as needed.
1. Run `yarn changeset` to generate the changeset summary.

This repo has a monorepo structure to allow for easily adding additional packages. The `@google/generative-ai` package code is in `packages/main`.

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,12 @@

## FunctionDeclarationSchemaProperty interface

Schema is used to define the format of input/output data. Represents a select subset of an OpenAPI 3.0 schema object. More fields may be added in the future as needed.
Schema for top-level function declaration

**Signature:**

```typescript
export interface FunctionDeclarationSchemaProperty
export interface FunctionDeclarationSchemaProperty extends Schema
```

## Properties

| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| [description?](./generative-ai.functiondeclarationschemaproperty.description.md) | | string | _(Optional)_ Optional. The description of the property. |
| [enum?](./generative-ai.functiondeclarationschemaproperty.enum.md) | | string\[\] | _(Optional)_ Optional. The enum of the property. |
| [example?](./generative-ai.functiondeclarationschemaproperty.example.md) | | unknown | _(Optional)_ Optional. The example of the property. |
| [format?](./generative-ai.functiondeclarationschemaproperty.format.md) | | string | _(Optional)_ Optional. The format of the property. |
| [items?](./generative-ai.functiondeclarationschemaproperty.items.md) | | [FunctionDeclarationSchema](./generative-ai.functiondeclarationschema.md) | _(Optional)_ Optional. The items of the property. [FunctionDeclarationSchema](./generative-ai.functiondeclarationschema.md) |
| [nullable?](./generative-ai.functiondeclarationschemaproperty.nullable.md) | | boolean | _(Optional)_ Optional. Whether the property is nullable. |
| [properties?](./generative-ai.functiondeclarationschemaproperty.properties.md) | | { \[k: string\]: [FunctionDeclarationSchema](./generative-ai.functiondeclarationschema.md)<!-- -->; } | _(Optional)_ Optional. Map of [FunctionDeclarationSchema](./generative-ai.functiondeclarationschema.md)<!-- -->. |
| [required?](./generative-ai.functiondeclarationschemaproperty.required.md) | | string\[\] | _(Optional)_ Optional. Array of required property. |
| [type?](./generative-ai.functiondeclarationschemaproperty.type.md) | | [FunctionDeclarationSchemaType](./generative-ai.functiondeclarationschematype.md) | _(Optional)_ Optional. The type of the property. [FunctionDeclarationSchemaType](./generative-ai.functiondeclarationschematype.md)<!-- -->. |
**Extends:** [Schema](./generative-ai.schema.md)

This file was deleted.

1 change: 1 addition & 0 deletions docs/reference/main/generative-ai.generationconfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface GenerationConfig
| [candidateCount?](./generative-ai.generationconfig.candidatecount.md) | | number | _(Optional)_ |
| [maxOutputTokens?](./generative-ai.generationconfig.maxoutputtokens.md) | | number | _(Optional)_ |
| [responseMimeType?](./generative-ai.generationconfig.responsemimetype.md) | | string | _(Optional)_ Output response mimetype of the generated candidate text. Supported mimetype: <code>text/plain</code>: (default) Text output. <code>application/json</code>: 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 <code>responseMIMEType</code> supports a schema; currently this is limited to <code>application/json</code>. |
| [stopSequences?](./generative-ai.generationconfig.stopsequences.md) | | string\[\] | _(Optional)_ |
| [temperature?](./generative-ai.generationconfig.temperature.md) | | number | _(Optional)_ |
| [topK?](./generative-ai.generationconfig.topk.md) | | number | _(Optional)_ |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [GenerationConfig](./generative-ai.generationconfig.md) &gt; [responseSchema](./generative-ai.generationconfig.responseschema.md)

## GenerationConfig.responseSchema property

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`<!-- -->.

**Signature:**

```typescript
responseSchema?: ResponseSchema;
```
4 changes: 3 additions & 1 deletion docs/reference/main/generative-ai.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
| [FunctionCallPart](./generative-ai.functioncallpart.md) | Content part interface if the part represents FunctionResponse. |
| [FunctionDeclaration](./generative-ai.functiondeclaration.md) | Structured representation of a function declaration as defined by the \[OpenAPI 3.0 specification\](https://spec.openapis.org/oas/v3.0.3). Included in this declaration are the function name and parameters. This FunctionDeclaration is a representation of a block of code that can be used as a Tool by the model and executed by the client. |
| [FunctionDeclarationSchema](./generative-ai.functiondeclarationschema.md) | Schema for parameters passed to [FunctionDeclaration.parameters](./generative-ai.functiondeclaration.parameters.md)<!-- -->. |
| [FunctionDeclarationSchemaProperty](./generative-ai.functiondeclarationschemaproperty.md) | Schema is used to define the format of input/output data. Represents a select subset of an OpenAPI 3.0 schema object. More fields may be added in the future as needed. |
| [FunctionDeclarationSchemaProperty](./generative-ai.functiondeclarationschemaproperty.md) | Schema for top-level function declaration |
| [FunctionDeclarationsTool](./generative-ai.functiondeclarationstool.md) | A FunctionDeclarationsTool is a piece of code that enables the system to interact with external systems to perform an action, or set of actions, outside of knowledge and scope of the model. |
| [FunctionResponse](./generative-ai.functionresponse.md) | The result output from a \[FunctionCall\] that contains a string representing the \[FunctionDeclaration.name\] and a structured JSON object containing any output from the function is used as context to the model. This should contain the result of a \[FunctionCall\] made based on model prediction. |
| [FunctionResponsePart](./generative-ai.functionresponsepart.md) | Content part interface if the part represents FunctionResponse. |
Expand All @@ -68,8 +68,10 @@
| [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 <code>blockReason</code> and the relevant <code>safetyRatings</code>. |
| [RequestOptions](./generative-ai.requestoptions.md) | Params passed to getGenerativeModel() or GoogleAIFileManager(). |
| [ResponseSchema](./generative-ai.responseschema.md) | Schema passed to [GenerationConfig.responseSchema](./generative-ai.generationconfig.responseschema.md) |
| [SafetyRating](./generative-ai.safetyrating.md) | A safety rating associated with a [GenerateContentCandidate](./generative-ai.generatecontentcandidate.md) |
| [SafetySetting](./generative-ai.safetysetting.md) | Safety setting that can be sent as part of request parameters. |
| [Schema](./generative-ai.schema.md) | Schema is used to define the format of input/output data. Represents a select subset of an OpenAPI 3.0 schema object. More fields may be added in the future as needed. |
| [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. |
Expand Down
15 changes: 15 additions & 0 deletions docs/reference/main/generative-ai.responseschema.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [ResponseSchema](./generative-ai.responseschema.md)

## ResponseSchema interface

Schema passed to [GenerationConfig.responseSchema](./generative-ai.generationconfig.responseschema.md)

**Signature:**

```typescript
export interface ResponseSchema extends Schema
```
**Extends:** [Schema](./generative-ai.schema.md)
13 changes: 13 additions & 0 deletions docs/reference/main/generative-ai.schema.description.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [Schema](./generative-ai.schema.md) &gt; [description](./generative-ai.schema.description.md)

## Schema.description property

Optional. The description of the property.

**Signature:**

```typescript
description?: string;
```
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [FunctionDeclarationSchemaProperty](./generative-ai.functiondeclarationschemaproperty.md) &gt; [enum](./generative-ai.functiondeclarationschemaproperty.enum.md)
[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [Schema](./generative-ai.schema.md) &gt; [enum](./generative-ai.schema.enum.md)

## FunctionDeclarationSchemaProperty.enum property
## Schema.enum property

Optional. The enum of the property.

Expand Down
13 changes: 13 additions & 0 deletions docs/reference/main/generative-ai.schema.example.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [Schema](./generative-ai.schema.md) &gt; [example](./generative-ai.schema.example.md)

## Schema.example property

Optional. The example of the property.

**Signature:**

```typescript
example?: unknown;
```
13 changes: 13 additions & 0 deletions docs/reference/main/generative-ai.schema.format.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [Schema](./generative-ai.schema.md) &gt; [format](./generative-ai.schema.format.md)

## Schema.format property

Optional. The format of the property.

**Signature:**

```typescript
format?: string;
```
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [FunctionDeclarationSchemaProperty](./generative-ai.functiondeclarationschemaproperty.md) &gt; [items](./generative-ai.functiondeclarationschemaproperty.items.md)
[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [Schema](./generative-ai.schema.md) &gt; [items](./generative-ai.schema.items.md)

## FunctionDeclarationSchemaProperty.items property
## Schema.items property

Optional. The items of the property. [FunctionDeclarationSchema](./generative-ai.functiondeclarationschema.md)

Expand Down
28 changes: 28 additions & 0 deletions docs/reference/main/generative-ai.schema.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [Schema](./generative-ai.schema.md)

## Schema interface

Schema is used to define the format of input/output data. Represents a select subset of an OpenAPI 3.0 schema object. More fields may be added in the future as needed.

**Signature:**

```typescript
export interface Schema
```

## Properties

| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| [description?](./generative-ai.schema.description.md) | | string | _(Optional)_ Optional. The description of the property. |
| [enum?](./generative-ai.schema.enum.md) | | string\[\] | _(Optional)_ Optional. The enum of the property. |
| [example?](./generative-ai.schema.example.md) | | unknown | _(Optional)_ Optional. The example of the property. |
| [format?](./generative-ai.schema.format.md) | | string | _(Optional)_ Optional. The format of the property. |
| [items?](./generative-ai.schema.items.md) | | [FunctionDeclarationSchema](./generative-ai.functiondeclarationschema.md) | _(Optional)_ Optional. The items of the property. [FunctionDeclarationSchema](./generative-ai.functiondeclarationschema.md) |
| [nullable?](./generative-ai.schema.nullable.md) | | boolean | _(Optional)_ Optional. Whether the property is nullable. |
| [properties?](./generative-ai.schema.properties.md) | | { \[k: string\]: [FunctionDeclarationSchema](./generative-ai.functiondeclarationschema.md)<!-- -->; } | _(Optional)_ Optional. Map of [FunctionDeclarationSchema](./generative-ai.functiondeclarationschema.md)<!-- -->. |
| [required?](./generative-ai.schema.required.md) | | string\[\] | _(Optional)_ Optional. Array of required property. |
| [type?](./generative-ai.schema.type.md) | | [FunctionDeclarationSchemaType](./generative-ai.functiondeclarationschematype.md) | _(Optional)_ Optional. The type of the property. [FunctionDeclarationSchemaType](./generative-ai.functiondeclarationschematype.md)<!-- -->. |

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [FunctionDeclarationSchemaProperty](./generative-ai.functiondeclarationschemaproperty.md) &gt; [nullable](./generative-ai.functiondeclarationschemaproperty.nullable.md)
[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [Schema](./generative-ai.schema.md) &gt; [nullable](./generative-ai.schema.nullable.md)

## FunctionDeclarationSchemaProperty.nullable property
## Schema.nullable property

Optional. Whether the property is nullable.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [FunctionDeclarationSchemaProperty](./generative-ai.functiondeclarationschemaproperty.md) &gt; [properties](./generative-ai.functiondeclarationschemaproperty.properties.md)
[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [Schema](./generative-ai.schema.md) &gt; [properties](./generative-ai.schema.properties.md)

## FunctionDeclarationSchemaProperty.properties property
## Schema.properties property

Optional. Map of [FunctionDeclarationSchema](./generative-ai.functiondeclarationschema.md)<!-- -->.

Expand Down
13 changes: 13 additions & 0 deletions docs/reference/main/generative-ai.schema.required.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [Schema](./generative-ai.schema.md) &gt; [required](./generative-ai.schema.required.md)

## Schema.required property

Optional. Array of required property.

**Signature:**

```typescript
required?: string[];
```
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [FunctionDeclarationSchemaProperty](./generative-ai.functiondeclarationschemaproperty.md) &gt; [type](./generative-ai.functiondeclarationschemaproperty.type.md)
[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [Schema](./generative-ai.schema.md) &gt; [type](./generative-ai.schema.type.md)

## FunctionDeclarationSchemaProperty.type property
## Schema.type property

Optional. The type of the property. [FunctionDeclarationSchemaType](./generative-ai.functiondeclarationschematype.md)<!-- -->.

Expand Down
Loading

0 comments on commit 85ff2c4

Please sign in to comment.