-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(js/plugins/express): added new express plugin for various expres…
…s integrations (#1434)
- Loading branch information
Showing
14 changed files
with
948 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# Genkit Express Plugin | ||
|
||
This plugin provides utilities for conveninetly exposing Genkit flows and actions via Express HTTP server as REST APIs. | ||
|
||
```ts | ||
import { handler } from '@genkit-ai/express'; | ||
import express from 'express'; | ||
|
||
const simpleFlow = ai.defineFlow( | ||
'simpleFlow', | ||
async (input, streamingCallback) => { | ||
const { text } = await ai.generate({ | ||
model: gemini15Flash, | ||
prompt: input, | ||
streamingCallback, | ||
}); | ||
return text; | ||
} | ||
); | ||
|
||
const app = express(); | ||
app.use(express.json()); | ||
|
||
app.post('/simpleFlow', handler(simpleFlow)); | ||
|
||
app.listen(8080); | ||
``` | ||
|
||
You can also set auth policies: | ||
|
||
```ts | ||
// middleware for handling auth headers. | ||
const authMiddleware = async (req, resp, next) => { | ||
// parse auth headers and convert to auth object. | ||
(req as RequestWithAuth).auth = { | ||
user: | ||
req.header('authorization') === 'open sesame' ? 'Ali Baba' : '40 thieves', | ||
}; | ||
next(); | ||
}; | ||
|
||
app.post( | ||
'/simpleFlow', | ||
authMiddleware, | ||
handler(simpleFlow, { | ||
authPolicy: ({ auth }) => { | ||
if (auth.user !== 'Ali Baba') { | ||
throw new Error('not authorized'); | ||
} | ||
}, | ||
}) | ||
); | ||
``` | ||
|
||
Flows and actions exposed using the `handler` function can be accessed using `genkit/client` library: | ||
|
||
```ts | ||
import { runFlow, streamFlow } from 'genkit/client'; | ||
|
||
const result = await runFlow({ | ||
url: `http://localhost:${port}/simpleFlow`, | ||
input: 'say hello', | ||
}); | ||
|
||
console.log(result); // hello | ||
|
||
// set auth headers (when using auth policies) | ||
const result = await runFlow({ | ||
url: `http://localhost:${port}/simpleFlow`, | ||
headers: { | ||
Authorization: 'open sesame', | ||
}, | ||
input: 'say hello', | ||
}); | ||
|
||
console.log(result); // hello | ||
|
||
// and streamed | ||
const result = streamFlow({ | ||
url: `http://localhost:${port}/simpleFlow`, | ||
input: 'say hello', | ||
}); | ||
for await (const chunk of result.stream()) { | ||
console.log(chunk); | ||
} | ||
console.log(await result.output()); | ||
``` | ||
|
||
The sources for this package are in the main [Genkit](https://github.com/firebase/genkit) repo. Please file issues and pull requests against that repo. | ||
|
||
Usage information and reference details can be found in [Genkit documentation](https://firebase.google.com/docs/genkit). | ||
|
||
License: Apache 2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
{ | ||
"name": "@genkit-ai/express", | ||
"description": "Genkit AI framework plugin for Express server", | ||
"keywords": [ | ||
"genkit", | ||
"genkit-plugin", | ||
"langchain", | ||
"ai", | ||
"genai", | ||
"generative-ai" | ||
], | ||
"version": "0.9.6", | ||
"type": "commonjs", | ||
"scripts": { | ||
"check": "tsc", | ||
"compile": "tsup-node", | ||
"build:clean": "rimraf ./lib", | ||
"build": "npm-run-all build:clean check compile", | ||
"build:watch": "tsup-node --watch", | ||
"test": "node --import tsx --test tests/*_test.ts", | ||
"test:watch": "node --import tsx --watch --test tests/*_test.ts" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/firebase/genkit.git", | ||
"directory": "js/plugins/express" | ||
}, | ||
"author": "genkit", | ||
"license": "Apache-2.0", | ||
"dependencies": {}, | ||
"peerDependencies": { | ||
"genkit": "workspace:*", | ||
"express": "^4.21.1" | ||
}, | ||
"devDependencies": { | ||
"get-port": "^5.1.0", | ||
"@types/express": "^4.17.21", | ||
"@types/node": "^20.11.16", | ||
"npm-run-all": "^4.1.5", | ||
"rimraf": "^6.0.1", | ||
"tsup": "^8.3.5", | ||
"tsx": "^4.19.2", | ||
"typescript": "^4.9.0" | ||
}, | ||
"types": "./lib/index.d.ts", | ||
"exports": { | ||
".": { | ||
"require": "./lib/index.js", | ||
"default": "./lib/index.js", | ||
"import": "./lib/index.mjs", | ||
"types": "./lib/index.d.ts" | ||
} | ||
} | ||
} |
Oops, something went wrong.