Convert script to a package function
You can convert a script to a package function to:
- Save the script in the version control system (e.g., Git).
- Call your script from other scripts or fucntions by a stable name.
Follow these steps to achieve it:
- Create a TypeScript package.
grok create package_name --ts
-
Add your script to the package.
a. For Python, R, Julia, and other server-side languages, create the
scriptsfolder in your package directory and place your script file in it.b. If you are writing in Javascript/Typescript, you can convert your script to a package function, annotate it, and add directly to
srcpackage.ts file://name: DemoScript
//tags: demo
//input: int input1
//output: int result
export async function DemoScript(input1: number) {
return 1;
} -
Compile and publish the package:
npm run build && grok publish your_environmentTo make your package publicly available, use the
--releasepublishing mode.npm run build && grok publish your_environment --release
Now you can call your function using:
const result = await grok.functions.call('package_name:DemoScript', { input1: 10 }); // result = 1
For a JS function with a single output, the output will be returned directly. If there are multiple outputs, a function will return an Object with key value results.
To return multiple outputs from a function inside package.ts, just return an Object with key value results.