You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: The OpenAPI Server exposes scripts as OpenAPI endpoints.
4
+
---
5
+
6
+
You can launch the [cli](/genaiscript/reference/cli) as a [OpenAPI server](https://swagger.io/specification/)
7
+
to serve scripts as OpenAPI endpoints.
8
+
9
+
```bash
10
+
genaiscript openapi
11
+
```
12
+
13
+
## Scripts as OpenAPI endpoints
14
+
15
+
The OpenAPI endpoint description is the script description.
16
+
**Make sure to carefully craft the description** as it is how the LLM decides
17
+
which tool to use when running a script. If your tool does not get picked up by the LLM, it's probably a description issue.
18
+
19
+
The OpenAPI endpoint parameters is inferred from the [script parameters](/genaiscript/reference/scripts/parameters) and files automatically.
20
+
The OpenAPI parameters will then populate the `env.vars` object in the script
21
+
as usual.
22
+
23
+
The OpenAPI endpoint output is the script output. That is, typically, the last assistant message for a script that uses the top-level context.
24
+
The OpenAPI endpoint output corresponds to the script's output, typically the last assistant message or any content passed to [env.output](/genaiscript/reference/scripts/output-builder).
25
+
26
+
Let's see an example. Here is a script `task.genai.mjs` that takes a `task` parameter input, builds a prompt
27
+
and the LLM output is sent back.
28
+
29
+
```js title="task.genai.mjs"
30
+
script({
31
+
description:"You MUST provide a description!",
32
+
parameters: {
33
+
task: {
34
+
type:"string",
35
+
description:"The task to perform",
36
+
required:true
37
+
}
38
+
}
39
+
})
40
+
41
+
const { task } =env.vars// extract the task parameter
42
+
43
+
...// genaiscript logic
44
+
$`... prompt ... ${task}`// output the result
45
+
```
46
+
47
+
A more advanced script might not use the top-level context and instead use the `env.output` to pass the result.
48
+
49
+
```js title="task.genai.mjs"
50
+
script({
51
+
description:"You should provide a description!",
52
+
accept:"none", // this script does not use 'env.files'
53
+
parameters: {
54
+
task: {
55
+
type:"string",
56
+
description:"The task to perform",
57
+
required:true
58
+
}
59
+
}
60
+
})
61
+
62
+
const { output } = env // store the output builder
63
+
const { task } =env.vars// extract the task parameter
64
+
65
+
...// genaiscript logic with inline prompts
66
+
constres=runPrompt(_=>`... prompt ... ${task}`) // run some inner the prompt
67
+
...
68
+
69
+
// build the output
70
+
output.fence(`The result is ${res.text}`)
71
+
```
72
+
73
+
## Startup script
74
+
75
+
You can specify a startup script id in the command line using the `--startup` option.
76
+
It will run after the server is started.
77
+
78
+
```sh
79
+
genaiscript openapi --startup load-resources
80
+
```
81
+
82
+
You can use this script to load resources or do any other setup you need.
83
+
84
+
### Filtering scripts
85
+
86
+
If you need to filter out which scripts are exposed as OpenAPI endpoints, you can use the `--groups` flag and
87
+
set the `openapi` group in your scripts.
88
+
89
+
```js 'group: "openapi"' title="task.genai.mjs"
90
+
script({
91
+
group:"openapi",
92
+
})
93
+
```
94
+
95
+
```bash
96
+
genaiscript openapi --groups openapi
97
+
```
98
+
99
+
## Running scripts from a remote repository
100
+
101
+
You can use the `--remote` option to load scripts from a remote repository.
102
+
GenAIScript will do a shallow clone of the repository and run the script from the clone folder.
0 commit comments