-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerateStructuredData.ts
More file actions
139 lines (128 loc) · 4.48 KB
/
Copy pathgenerateStructuredData.ts
File metadata and controls
139 lines (128 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import {ActionSdk, HerculesFunctionContext} from "@code0-tech/hercules";
import {Model} from "../types/aiModel";
import {Tool} from "../types/aiTool";
import {extractToken} from "../helpers";
import {generateText, isLoopFinished, Output} from "ai";
import {PlainValue} from "@code0-tech/tucana/helpers";
import {buildProvider, buildTools} from "./helpers";
import z from "zod";
function generateZodSchema(value: PlainValue): z.ZodTypeAny {
if (value === null) {
return z.null();
}
if (Array.isArray(value)) {
if (value.length === 0) {
return z.array(z.unknown());
}
const itemSchemas = value.map((item) => generateZodSchema(item));
const itemSchema =
itemSchemas.length === 1
? itemSchemas[0]
: itemSchemas.slice(1).reduce((acc, current) => z.union([acc, current]), itemSchemas[0]);
return z.array(itemSchema);
}
switch (typeof value) {
case "string":
return z.string();
case "number":
return z.number();
case "boolean":
return z.boolean();
case "object": {
const shape: Record<string, z.ZodTypeAny> = {};
for (const [key, nestedValue] of Object.entries(value as Record<string, PlainValue>)) {
shape[key] = generateZodSchema(nestedValue);
}
return z.object(shape);
}
default:
return z.unknown();
}
}
export const handler = async (context: HerculesFunctionContext, model: Model, system: string, prompt: string, tools: Tool[], type: PlainValue): Promise<PlainValue> => {
const apiKey = extractToken(context, model.provider)
const provider = buildProvider(model, apiKey);
const modelTools = await buildTools(tools, context);
const generated = await generateText({
model: provider.languageModel(model.model),
prompt: prompt,
system: system,
tools: modelTools,
stopWhen: isLoopFinished(),
output: Output.object({
schema: generateZodSchema(type)
})
})
return generated.output as any
};
export default (sdk: ActionSdk) => {
return sdk.registerRuntimeFunctionDefinitionsAndFunctionDefinitions(
{
definition: {
runtimeName: "generateStructuredData",
signature: "(model: AI_MODEL, system: string, prompt: string, tools: AI_TOOL[], type: TYPE): TYPE",
linkedDataTypes: ["AI_MODEL", "AI_TOOL", "TYPE"],
name: [
{
code: "en-US",
content: "Generate structured Data"
}
],
displayMessage: [
{
code: "en-US",
content: "generate structured Data"
}
],
parameters: [
{
runtimeName: "model",
name: [
{
code: "en-US",
content: "Model"
}
],
},
{
runtimeName: "system",
name: [
{
code: "en-US",
content: "System prompt"
}
]
},
{
runtimeName: "prompt",
name: [
{
code: "en-US",
content: "Prompt"
}
]
},
{
runtimeName: "tools",
name: [
{
code: "en-US",
content: "Tools"
}
]
},
{
runtimeName: "type",
name: [
{
code: "en-US",
content: "Output Type"
}
]
}
]
},
handler: handler
}
)
}