-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from atinylittleshell/features/primitive_arrays
support object fields that are array of primitives
- Loading branch information
Showing
5 changed files
with
104 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'function-gpt': minor | ||
--- | ||
|
||
support object fields that are array of primitives |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { expect, test } from 'vitest'; | ||
|
||
import { ChatGPTSession, gptFunction, gptObjectField } from '../index.js'; | ||
|
||
process.env.OPENAI_API_KEY = 'test'; | ||
|
||
test('basic function schema is generated correctly', async () => { | ||
class TestFuncInput { | ||
@gptObjectField('string', 'this is a test string', true) | ||
public testString!: string; | ||
|
||
@gptObjectField('number', 'this is a test number', false) | ||
public testNumber!: number; | ||
} | ||
|
||
class TestSession extends ChatGPTSession { | ||
@gptFunction('this is a test function', TestFuncInput) | ||
testFunc(params: TestFuncInput) { | ||
return params; | ||
} | ||
} | ||
|
||
const testSession = new TestSession(); | ||
const schema = testSession.getFunctionSchema(); | ||
expect(schema).toEqual([ | ||
{ | ||
name: 'testFunc', | ||
description: 'this is a test function', | ||
parameters: { | ||
type: 'object', | ||
properties: { | ||
testString: { | ||
type: 'string', | ||
description: 'this is a test string', | ||
}, | ||
testNumber: { | ||
type: 'number', | ||
description: 'this is a test number', | ||
}, | ||
}, | ||
required: ['testNumber'], | ||
}, | ||
}, | ||
]); | ||
}); | ||
|
||
test('input parameter can be an array of strings', () => { | ||
class TestParam { | ||
@gptObjectField(['string'], 'test words') | ||
words!: string[]; | ||
} | ||
|
||
class TestSession extends ChatGPTSession { | ||
@gptFunction('this is a test function', TestParam) | ||
testFunc(params: TestParam) { | ||
return params; | ||
} | ||
} | ||
|
||
const testSession = new TestSession(); | ||
const schema = testSession.getFunctionSchema(); | ||
expect(schema).toEqual([ | ||
{ | ||
name: 'testFunc', | ||
description: 'this is a test function', | ||
parameters: { | ||
type: 'object', | ||
properties: { | ||
words: { | ||
type: 'array', | ||
items: { | ||
type: 'string', | ||
}, | ||
description: 'test words', | ||
}, | ||
}, | ||
required: ['words'], | ||
}, | ||
}, | ||
]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters