@scoopika/scoopika
Advanced tools
Comparing version 2.1.2 to 2.1.4
@@ -731,3 +731,3 @@ import { Store, SavedProvider, ProvidersName, VoiceResponse, Audio, StoreSession, RunHistory, RunInputs, RunOptions, Hooks, ToolSchema, ModelTextResponse, ModelObjectResponse, CoreTool, HooksHub } from '@scoopika/types'; | ||
}) => Promise<ModelTextResponse>; | ||
structuredOutput: <SCHEMA extends ZodTypeAny = any, DATA = TypeOf<SCHEMA>>(args: { | ||
structuredOutput: <SCHEMA extends ZodTypeAny = any, DATA = TypeOf<SCHEMA>>({ inputs, schema, prompt, options, max_tries }: { | ||
run_id?: string; | ||
@@ -739,3 +739,9 @@ prompt?: string; | ||
max_tries?: number; | ||
}) => Promise<ModelObjectResponse<DATA>>; | ||
}) => Promise<{ | ||
data: null; | ||
error: string; | ||
} | { | ||
error: null; | ||
data: NonNullable<Awaited<DATA>>; | ||
}>; | ||
generateObjectWithSchema<DATA>({ inputs, prompt, schema, options, max_tries, }: { | ||
@@ -749,3 +755,3 @@ run_id?: string; | ||
}): Promise<ModelObjectResponse<DATA>>; | ||
generateObject<SCHEMA extends ZodTypeAny = any, DATA = TypeOf<SCHEMA>>(args: { | ||
generateObject<SCHEMA extends ZodTypeAny = any, DATA = TypeOf<SCHEMA>>({ inputs, schema, prompt, options, max_tries }: { | ||
run_id?: string; | ||
@@ -757,3 +763,9 @@ prompt?: string; | ||
max_tries?: number; | ||
}): Promise<ModelObjectResponse<DATA>>; | ||
}): Promise<{ | ||
data: null; | ||
error: string; | ||
} | { | ||
error: null; | ||
data: NonNullable<Awaited<DATA>>; | ||
}>; | ||
addTool<PARAMETERS extends ZodTypeAny, RESULT = any>(tool_schema: CoreTool<PARAMETERS, RESULT>): void; | ||
@@ -760,0 +772,0 @@ removeToll(name: string): void; |
62
index.js
@@ -1469,8 +1469,58 @@ "use strict"; | ||
} | ||
async generateObject(args) { | ||
const json_schema = createSchema(args.schema); | ||
const res = await this.generateObjectWithSchema(__spreadProps(__spreadValues({}, args), { | ||
schema: json_schema | ||
})); | ||
return res; | ||
async generateObject({ inputs, schema, prompt, options, max_tries }) { | ||
const json_schema = createSchema(schema); | ||
const client = await this.getLLMClient(); | ||
const hooksHub = new Hookshub(); | ||
const { messages, new_message } = await this.getInputs({ inputs, options }); | ||
let system_prompt = prompt || generate_object_prompt; | ||
if (!this.model.includes("fireworks")) { | ||
system_prompt += ` | ||
The data HAS to be valid against this JSON schema: | ||
${JSON.stringify(json_schema)}`; | ||
} | ||
if (!system_prompt.toLowerCase().includes("json")) { | ||
system_prompt += "\nYour main mission is to generate JSON data"; | ||
} | ||
max_tries = max_tries || 1; | ||
let tries = 0; | ||
let failed = false; | ||
let errors = []; | ||
const generate = async () => { | ||
const response = await client.generateObject( | ||
{ | ||
system_prompt, | ||
schema: json_schema, | ||
messages, | ||
prompt: { role: "user", content: new_message }, | ||
options: options == null ? void 0 : options.llm | ||
}, | ||
hooksHub | ||
); | ||
const data = JSON.parse(response); | ||
const is_valid = validate(json_schema, data); | ||
if (!is_valid.success) { | ||
tries += 1; | ||
errors = is_valid.errors; | ||
if (tries < max_tries) return await generate(); | ||
return null; | ||
} | ||
return data; | ||
}; | ||
const object = await generate().catch((err) => { | ||
errors.push(readError(err)); | ||
failed = true; | ||
}); | ||
if (!object || failed) { | ||
const error = errors.join("\n"); | ||
return { | ||
data: null, | ||
error: `Couldn't generate a valid JSON object in ${tries} tries due to the following errors: | ||
${error}` | ||
}; | ||
} | ||
return { | ||
error: null, | ||
data: object | ||
}; | ||
} | ||
@@ -1477,0 +1527,0 @@ addTool(tool_schema) { |
{ | ||
"name": "@scoopika/scoopika", | ||
"version": "2.1.2", | ||
"version": "2.1.4", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "./index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
157120
3877