Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Previously, ai.jsx did not pass all function params (e.g. `description`) to the OpenAI API, even when it accepted them. Now it does. This also adds the beginning of a test runner.
- Loading branch information
1 parent
18114b8
commit 4ff41e2
Showing
14 changed files
with
278 additions
and
31 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
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
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
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,18 @@ | ||
import type { Config } from 'jest'; | ||
|
||
const config: Config = { | ||
preset: 'ts-jest', | ||
testMatch: ['<rootDir>/test/**/*.ts', '<rootDir>/test/**/*.tsx'], | ||
testEnvironment: 'node', | ||
testPathIgnorePatterns: ['/node_modules/', '/dist/'], | ||
transform: { | ||
'^.+\\.tsx?$': [ | ||
'ts-jest', | ||
{ | ||
tsconfig: '<rootDir>/test/tsconfig.json', | ||
}, | ||
], | ||
}, | ||
}; | ||
|
||
export default config; |
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,138 @@ | ||
import { Readable } from 'stream'; | ||
|
||
/** | ||
* This is a hack to let jest-fetch-mock handle response streams. | ||
* https://github.com/jefflau/jest-fetch-mock/issues/113#issuecomment-1445010122 | ||
*/ | ||
class TempResponse extends Response { | ||
constructor(...args: any[]) { | ||
if (args[0] instanceof ReadableStream) { | ||
// @ts-expect-error | ||
args[0] = Readable.from(args[0]); | ||
} | ||
super(...args); | ||
} | ||
} | ||
Object.defineProperty(global, 'Response', { | ||
value: TempResponse, | ||
}); | ||
/** | ||
* End hack | ||
*/ | ||
|
||
import jestFetchMock from 'jest-fetch-mock'; | ||
/** | ||
* This _must_ occur before importing ai-jsx code. Otherwise, the mock won't be enabled. | ||
*/ | ||
jestFetchMock.enableFetchMocks(); | ||
|
||
process.env.OPENAI_API_KEY = 'fake-openai-key'; | ||
process.env.ANTHROPIC_API_KEY = 'fake-anthropic-key'; | ||
|
||
import * as AI from 'ai-jsx'; | ||
import { ChatCompletion, UserMessage } from 'ai-jsx/core/completion'; | ||
import { ChatCompletionDelta, SSE_FINAL_EVENT, SSE_PREFIX, SSE_TERMINATOR } from 'ai-jsx/lib/openai'; | ||
import { Tool } from 'ai-jsx/batteries/use-tools'; | ||
|
||
it('passes creates a chat completion', async () => { | ||
mockOpenAIResponse('response from OpenAI'); | ||
|
||
const result = await AI.createRenderContext().render( | ||
<ChatCompletion> | ||
<UserMessage>Hello</UserMessage> | ||
</ChatCompletion> | ||
); | ||
expect(result).toEqual('response from OpenAI'); | ||
}); | ||
|
||
it('passes all function fields', async () => { | ||
const functions: Record<string, Tool> = { | ||
myFunc: { | ||
description: 'My function', | ||
parameters: { | ||
myParam: { | ||
description: 'My parameter', | ||
type: 'string', | ||
enum: ['option1', 'option2'], | ||
required: true, | ||
}, | ||
}, | ||
func: () => undefined, | ||
}, | ||
}; | ||
|
||
const handleRequest = jest.fn(); | ||
mockOpenAIResponse('', handleRequest); | ||
|
||
await AI.createRenderContext().render( | ||
<ChatCompletion functionDefinitions={functions}> | ||
<UserMessage>Hello</UserMessage> | ||
</ChatCompletion> | ||
); | ||
|
||
expect(handleRequest).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
functions: [ | ||
{ | ||
name: 'myFunc', | ||
description: 'My function', | ||
parameters: { | ||
type: 'object', | ||
required: ['myParam'], | ||
properties: { | ||
myParam: { | ||
type: 'string', | ||
enum: ['option1', 'option2'], | ||
description: 'My parameter', | ||
}, | ||
}, | ||
}, | ||
}, | ||
], | ||
}) | ||
); | ||
}); | ||
|
||
function mockOpenAIResponse(message: string, handleRequest?: jest.MockedFn<(req: Request) => Promise<void>>) { | ||
fetchMock.mockIf( | ||
/^https:\/\/api.openai.com\/v1\/chat\/completions/, | ||
// This is a hack to let jest-fetch-mock handle response streams. | ||
// @ts-expect-error | ||
async (req) => { | ||
handleRequest?.(await req.json()); | ||
|
||
const stringStream = new ReadableStream({ | ||
start(controller) { | ||
function sendDelta(messagePart: string) { | ||
const response: ChatCompletionDelta = { | ||
id: 'cmpl-3QJ8ZjX1J5Z5X', | ||
object: 'text_completion', | ||
created: 1624430979, | ||
model: 'gpt-3.5-turbo', | ||
choices: [ | ||
{ | ||
delta: { | ||
role: 'assistant', | ||
content: messagePart, | ||
}, | ||
}, | ||
], | ||
}; | ||
controller.enqueue(`${SSE_PREFIX}${JSON.stringify(response)}${SSE_TERMINATOR}`); | ||
} | ||
|
||
for (const char of message) { | ||
sendDelta(char); | ||
} | ||
|
||
controller.enqueue(SSE_FINAL_EVENT); | ||
controller.close(); | ||
}, | ||
}).pipeThrough(new TextEncoderStream()); | ||
return { | ||
status: 200, | ||
body: stringStream, | ||
}; | ||
} | ||
); | ||
} |
2 changes: 1 addition & 1 deletion
2
packages/ai-jsx/test/core/errors.ts → packages/examples/test/core/errors.ts
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
Oops, something went wrong.
4ff41e2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
ai-jsx-docs – ./packages/docs
docs.ai-jsx.com
ai-jsx-docs-git-main-fixie-ai.vercel.app
ai-jsx-docs-fixie-ai.vercel.app
ai-jsx-docs.vercel.app
4ff41e2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
ai-jsx-nextjs-demo – ./packages/nextjs-demo
ai-jsx-nextjs-demo-git-main-fixie-ai.vercel.app
ai-jsx-nextjs-demo.vercel.app
ai-jsx-nextjs-demo-fixie-ai.vercel.app
4ff41e2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
ai-jsx-tutorial-nextjs – ./packages/tutorial-nextjs
ai-jsx-tutorial-nextjs-git-main-fixie-ai.vercel.app
ai-jsx-tutorial-nextjs.vercel.app
ai-jsx-tutorial-nextjs-fixie-ai.vercel.app