-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
188 additions
and
46 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 |
---|---|---|
|
@@ -29,6 +29,6 @@ | |
"test:watch": "vitest" | ||
}, | ||
"dependencies": { | ||
"@xsai/shared": "workspace:^" | ||
"@xsai/shared": "workspace:" | ||
} | ||
} |
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,40 @@ | ||
{ | ||
"name": "@xsai/generate-object", | ||
"version": "0.0.10", | ||
"type": "module", | ||
"author": "藍+85CD", | ||
"license": "MIT", | ||
"homepage": "https://github.com/moeru-ai/xsai", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/moeru-ai/xsai.git", | ||
"directory": "packages/generate-object" | ||
}, | ||
"bugs": "https://github.com/moeru-ai/xsai/issues", | ||
"main": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"exports": { | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"default": "./dist/index.js" | ||
} | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "pkgroll", | ||
"build:watch": "pkgroll --watch", | ||
"test": "vitest run", | ||
"test:watch": "vitest" | ||
}, | ||
"dependencies": { | ||
"@typeschema/main": "catalog:", | ||
"@xsai/generate-text": "workspace:", | ||
"@xsai/shared": "workspace:" | ||
}, | ||
"devDependencies": { | ||
"@gcornut/valibot-json-schema": "catalog:", | ||
"valibot": "catalog:" | ||
} | ||
} |
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 @@ | ||
export { generateObject, generateObject as default, type GenerateObjectOptions, type GenerateObjectResult } from './utils/generate-object' |
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,43 @@ | ||
import { type Infer, type Schema, toJSONSchema } from '@typeschema/main' | ||
import { generateText, type GenerateTextOptions, type GenerateTextResult } from '@xsai/generate-text' | ||
import { clean } from '@xsai/shared' | ||
|
||
export interface GenerateObjectOptions<T extends Schema> extends GenerateTextOptions { | ||
schema: T | ||
schemaDescription?: string | ||
schemaName?: string | ||
} | ||
|
||
export interface GenerateObjectResult<T extends Schema> extends Omit<GenerateTextResult, 'text'> { | ||
object: Infer<T> | ||
} | ||
|
||
/** | ||
* @experimental | ||
* WIP, test failed | ||
*/ | ||
export const generateObject = async <T extends Schema>(options: GenerateObjectOptions<T>): Promise<GenerateObjectResult<T>> => | ||
await generateText(clean({ | ||
...options, | ||
responseFormat: { | ||
jsonSchema: { | ||
description: options.schemaDescription, | ||
name: options.schemaName ?? 'json_schema', | ||
schema: await toJSONSchema(options.schema) | ||
.then(json => clean({ | ||
...json, | ||
$schema: undefined, | ||
})), | ||
strict: true, | ||
}, | ||
type: 'json_schema', | ||
}, | ||
schema: undefined, | ||
schemaDescription: undefined, | ||
schemaName: undefined, | ||
})) | ||
.then(({ finishReason, text, usage }) => ({ | ||
finishReason, | ||
object: JSON.parse(text), | ||
usage, | ||
})) |
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,28 @@ | ||
import { object as schemaObject, string } from 'valibot' | ||
import { describe, expect, it } from 'vitest' | ||
|
||
import { generateObject } from '../src' | ||
|
||
describe('@xsai/generate-object', () => { | ||
it('basic', async () => { | ||
const { object } = await generateObject({ | ||
messages: [ | ||
{ | ||
content: 'You are a helpful assistant.', | ||
role: 'system', | ||
}, | ||
{ | ||
content: 'This is a test, so please answer \'YES\' and nothing else.', | ||
role: 'user', | ||
}, | ||
], | ||
model: 'llama3.2', | ||
schema: schemaObject({ | ||
anwser: string(), | ||
}), | ||
}) | ||
|
||
console.log('object:', object) | ||
expect(object.anwser).toStrictEqual('YES') | ||
}) | ||
}) |
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,4 @@ | ||
{ | ||
"extends": "@importantimport/tsconfig/app.json", | ||
"include": ["src"] | ||
} |
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 |
---|---|---|
|
@@ -29,6 +29,6 @@ | |
"test:watch": "vitest" | ||
}, | ||
"dependencies": { | ||
"@xsai/shared": "workspace:^" | ||
"@xsai/shared": "workspace:" | ||
} | ||
} |
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 |
---|---|---|
@@ -1 +1 @@ | ||
export const clean = (record: Record<string, undefined | unknown>) => Object.fromEntries(Object.entries(record).filter(([, v]) => !!v)) | ||
export const clean = <T extends Record<string, undefined | unknown> = Record<string, undefined | unknown>>(record: T) => Object.fromEntries(Object.entries(record).filter(([, v]) => !!v)) as Record<keyof T, Exclude<T[keyof T], unknown>> |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,3 +1,9 @@ | ||
packages: | ||
- docs | ||
- packages/* | ||
|
||
catalog: | ||
'@typeschema/main': ^0.14.1 | ||
|
||
'@gcornut/valibot-json-schema': ^0.42.0 | ||
valibot: ^0.42.1 |
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