One tool for several generative AIs: we need a common interface to do so.
Tplfa ("templating for api") defines such common interface and provides API-specific templates.
The templates are written in jsonnet and can be used in most popular languages.
Tplfa is a part of Gewova collaborative prompt engineering project. Tplfa is published under permissive MIT license and can be included in commercial applications.
cd in-action/nodejs-cli/
export OPENAI_API_KEY=...
npx ts-node src/index.ts --debug \
--api ../../apis/openai/lib \
--prompt "Tell me a joke about APIs."
The prompt is converted to a fetch-object:
{
"url": "https://api.openai.com/v1/chat/completions",
"method": "POST",
"headers": {
"Authorization": "...",
"Content-type": "application/json"
},
"body": {
"messages": [
{
"content": "Tell me a joke about APIs.",
"role": "user"
}
],
"model": "gpt-3.5-turbo"
}
}
The response from OpenAI is converted to the document representation:
{
"doc": [
{
"content": [
{
"text": "Why did the API go to therapy? \n\nBecause it couldn't handle all the rejection codes!",
"type": "text"
}
],
"type": "markdown"
}
]
}
At the moment, there is only a template for OpenAI chat completion.
- Copy-paste the openai template to a new directory
- Modify the templates and update tests
There are official libraries for Python, Go, C and C++, plus unofficial third-party bindings: https://jsonnet.org/ref/bindings.html.
This repository templating-for-api
provides JavaScript and TypeScript bindings to jsonnet
through WebAssembly compilation. There is no npm
package, you are supposed to copy files into your project: ./jsonnet/dist/, ./jsonnet/typescript/jsonnet.ts
- ./apis/
- ./tplfa/: JSON validation schemas for template output
- ./in-action/nodejs-cli/: Command-like tool to run templates and use APIs
- ./in-action/web-js/: Using jsonnet in a browser
- ./jsonnet/: Typescript library