Skip to content

Commit

Permalink
added a few helper decorators for common types
Browse files Browse the repository at this point in the history
  • Loading branch information
atinylittleshell committed Aug 21, 2023
1 parent a8c982f commit 66a372d
Show file tree
Hide file tree
Showing 8 changed files with 450 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .changeset/short-beds-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'function-gpt': minor
---

added a few helper decorators for common types
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,16 @@
## Example

```typescript
import { gptFunction, gptObjectField, ChatGPTSession } from 'function-gpt';
import { gptFunction, gptString, ChatGPTSession } from 'function-gpt';

// First create your own class that extends ChatGPTSession.
// Define the type of the input parameter for functions above.
class BrowseParams {
// Decorate each field with @gptObjectField to provide necessary metadata.
@gptString('url of the web page to browse')
public url!: string;
}

// Create your own class that extends ChatGPTSession.
class BrowseSession extends ChatGPTSession {
// Define functions that you want to provide to ChatGPT for function calling.
// Decorate each function with @gptFunction to provide necessary metadata.
Expand All @@ -30,13 +37,6 @@ class BrowseSession extends ChatGPTSession {
}
}

// Define the type of the input parameter for functions above.
class BrowseParams {
// Decorate each field with @gptObjectField to provide necessary metadata.
@gptObjectField('string', 'url of the web page to browse')
public url!: string;
}

const session = new BrowseSession();
const response = await session.send('count characters in the html content of https://www.google.com.');

Expand Down
239 changes: 232 additions & 7 deletions doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ function-gpt

### Functions

- [gptArray](README.md#gptarray)
- [gptBoolean](README.md#gptboolean)
- [gptEnum](README.md#gptenum)
- [gptFunction](README.md#gptfunction)
- [gptNumber](README.md#gptnumber)
- [gptObject](README.md#gptobject)
- [gptObjectField](README.md#gptobjectfield)
- [gptString](README.md#gptstring)

## Type Aliases

Expand All @@ -37,7 +43,7 @@ Represents a function call requested by ChatGPT.

#### Defined in

[src/session.ts:71](https://github.com/atinylittleshell/function-gpt/blob/04eb21b/src/session.ts#L71)
[src/session.ts:78](https://github.com/atinylittleshell/function-gpt/blob/a8c982f/src/session.ts#L78)

___

Expand Down Expand Up @@ -69,7 +75,7 @@ Options for the ChatGPTSession.send method.

#### Defined in

[src/session.ts:91](https://github.com/atinylittleshell/function-gpt/blob/04eb21b/src/session.ts#L91)
[src/session.ts:98](https://github.com/atinylittleshell/function-gpt/blob/a8c982f/src/session.ts#L98)

___

Expand All @@ -90,7 +96,7 @@ Represents a message in a ChatGPT session.

#### Defined in

[src/session.ts:79](https://github.com/atinylittleshell/function-gpt/blob/04eb21b/src/session.ts#L79)
[src/session.ts:86](https://github.com/atinylittleshell/function-gpt/blob/a8c982f/src/session.ts#L86)

___

Expand All @@ -106,10 +112,120 @@ Options for the ChatGPTSession constructor. Compatible with the OpenAI node clie

#### Defined in

[src/session.ts:64](https://github.com/atinylittleshell/function-gpt/blob/04eb21b/src/session.ts#L64)
[src/session.ts:71](https://github.com/atinylittleshell/function-gpt/blob/a8c982f/src/session.ts#L71)

## Functions

### gptArray

**gptArray**(`type`, `description`, `optional?`): (`target`: `object`, `propertyKey`: `string`) => `void`

Use this decorator on an array of strings property within a custom class to include it as a parameter for function-calling.

#### Parameters

| Name | Type | Default value | Description |
| :------ | :------ | :------ | :------ |
| `type` | ``"string"`` \| ``"number"`` \| ``"boolean"`` \| { `enum`: `string`[] } \| () => `unknown` | `undefined` | - |
| `description` | `string` | `undefined` | Description of the field. |
| `optional` | `boolean` | `false` | Whether the field is optional. Default to `false`. |

#### Returns

`fn`

▸ (`target`, `propertyKey`): `void`

##### Parameters

| Name | Type |
| :------ | :------ |
| `target` | `object` |
| `propertyKey` | `string` |

##### Returns

`void`

#### Defined in

[src/decorators.ts:194](https://github.com/atinylittleshell/function-gpt/blob/a8c982f/src/decorators.ts#L194)

___

### gptBoolean

**gptBoolean**(`description`, `optional?`): (`target`: `object`, `propertyKey`: `string`) => `void`

Use this decorator on a boolean property within a custom class to include it as a parameter for function-calling.

#### Parameters

| Name | Type | Default value | Description |
| :------ | :------ | :------ | :------ |
| `description` | `string` | `undefined` | Description of the field. |
| `optional` | `boolean` | `false` | Whether the field is optional. Default to `false`. |

#### Returns

`fn`

▸ (`target`, `propertyKey`): `void`

##### Parameters

| Name | Type |
| :------ | :------ |
| `target` | `object` |
| `propertyKey` | `string` |

##### Returns

`void`

#### Defined in

[src/decorators.ts:162](https://github.com/atinylittleshell/function-gpt/blob/a8c982f/src/decorators.ts#L162)

___

### gptEnum

**gptEnum**(`values`, `description`, `optional?`): (`target`: `object`, `propertyKey`: `string`) => `void`

Use this decorator on a custom class property within a custom class to include it as a parameter for function-calling.

#### Parameters

| Name | Type | Default value | Description |
| :------ | :------ | :------ | :------ |
| `values` | `string`[] | `undefined` | Possible values of the enum. |
| `description` | `string` | `undefined` | Description of the field. |
| `optional` | `boolean` | `false` | Whether the field is optional. Default to `false`. |

#### Returns

`fn`

▸ (`target`, `propertyKey`): `void`

##### Parameters

| Name | Type |
| :------ | :------ |
| `target` | `object` |
| `propertyKey` | `string` |

##### Returns

`void`

#### Defined in

[src/decorators.ts:184](https://github.com/atinylittleshell/function-gpt/blob/a8c982f/src/decorators.ts#L184)

___

### gptFunction

**gptFunction**(`description`, `inputType`): (`target`: `object`, `propertyKey`: `string`, `descriptor`: `PropertyDescriptor`) => `void`
Expand Down Expand Up @@ -147,7 +263,80 @@ Use this decorator on a method within a ChatGPTSession subclass to enable it for

#### Defined in

[src/decorators.ts:19](https://github.com/atinylittleshell/function-gpt/blob/04eb21b/src/decorators.ts#L19)
[src/decorators.ts:19](https://github.com/atinylittleshell/function-gpt/blob/a8c982f/src/decorators.ts#L19)

___

### gptNumber

**gptNumber**(`description`, `optional?`): (`target`: `object`, `propertyKey`: `string`) => `void`

Use this decorator on a number property within a custom class to include it as a parameter for function-calling.

#### Parameters

| Name | Type | Default value | Description |
| :------ | :------ | :------ | :------ |
| `description` | `string` | `undefined` | Description of the field. |
| `optional` | `boolean` | `false` | Whether the field is optional. Default to `false`. |

#### Returns

`fn`

▸ (`target`, `propertyKey`): `void`

##### Parameters

| Name | Type |
| :------ | :------ |
| `target` | `object` |
| `propertyKey` | `string` |

##### Returns

`void`

#### Defined in

[src/decorators.ts:152](https://github.com/atinylittleshell/function-gpt/blob/a8c982f/src/decorators.ts#L152)

___

### gptObject

**gptObject**(`type`, `description`, `optional?`): (`target`: `object`, `propertyKey`: `string`) => `void`

Use this decorator on a custom class property within a custom class to include it as a parameter for function-calling.

#### Parameters

| Name | Type | Default value | Description |
| :------ | :------ | :------ | :------ |
| `type` | () => `unknown` | `undefined` | Type of the field. |
| `description` | `string` | `undefined` | Description of the field. |
| `optional` | `boolean` | `false` | Whether the field is optional. Default to `false`. |

#### Returns

`fn`

▸ (`target`, `propertyKey`): `void`

##### Parameters

| Name | Type |
| :------ | :------ |
| `target` | `object` |
| `propertyKey` | `string` |

##### Returns

`void`

#### Defined in

[src/decorators.ts:173](https://github.com/atinylittleshell/function-gpt/blob/a8c982f/src/decorators.ts#L173)

___

Expand All @@ -161,7 +350,43 @@ Use this decorator on a property within a custom class to include it as a parame

| Name | Type | Default value | Description |
| :------ | :------ | :------ | :------ |
| `type` | ``"string"`` \| ``"number"`` \| ``"boolean"`` \| [``"string"`` \| ``"number"`` \| ``"boolean"``] \| [() => `unknown`] \| () => `unknown` | `undefined` | Type of the field. Use `'string'`, `'number'`, `'boolean'` for primitive types. Use `['string']`, `['number']`, `['boolean']` for arrays of primitive types. Use a ClassName for custom types. Use `[ClassName]` for arrays of custom types. |
| `type` | ``"string"`` \| ``"number"`` \| ``"boolean"`` \| { `enum`: `string`[] } \| [``"string"`` \| ``"number"`` \| ``"boolean"`` \| { `enum`: `string`[] } \| () => `unknown`] \| () => `unknown` | `undefined` | Type of the field. Use `'string'`, `'number'`, `'boolean'` for primitive types. Use `['string']`, `['number']`, `['boolean']` for arrays of primitive types. Use a ClassName for custom types. Use `[ClassName]` for arrays of custom types. |
| `description` | `string` | `undefined` | Description of the field. |
| `optional` | `boolean` | `false` | Whether the field is optional. Default to `false`. |

#### Returns

`fn`

▸ (`target`, `propertyKey`): `void`

##### Parameters

| Name | Type |
| :------ | :------ |
| `target` | `object` |
| `propertyKey` | `string` |

##### Returns

`void`

#### Defined in

[src/decorators.ts:53](https://github.com/atinylittleshell/function-gpt/blob/a8c982f/src/decorators.ts#L53)

___

### gptString

**gptString**(`description`, `optional?`): (`target`: `object`, `propertyKey`: `string`) => `void`

Use this decorator on a string property within a custom class to include it as a parameter for function-calling.

#### Parameters

| Name | Type | Default value | Description |
| :------ | :------ | :------ | :------ |
| `description` | `string` | `undefined` | Description of the field. |
| `optional` | `boolean` | `false` | Whether the field is optional. Default to `false`. |

Expand All @@ -184,4 +409,4 @@ Use this decorator on a property within a custom class to include it as a parame

#### Defined in

[src/decorators.ts:53](https://github.com/atinylittleshell/function-gpt/blob/04eb21b/src/decorators.ts#L53)
[src/decorators.ts:142](https://github.com/atinylittleshell/function-gpt/blob/a8c982f/src/decorators.ts#L142)
Loading

0 comments on commit 66a372d

Please sign in to comment.