@nlbridge/express
Advanced tools
Comparing version 0.1.3-beta to 0.2.0-beta
@@ -1,1 +0,1 @@ | ||
"use strict";var e=require("@nlbridge/core");exports.middleware=(t,s)=>{const a=e.actionRunner(t,s);return async(t,s,r)=>{if("application/json"!==t.headers["content-type"])return void s.status(400).send("Invalid Content-Type header. Please ensure that the request body is a valid JSON object and that the Content-Type header is set to application/json.");if(!t.body)return o="@nlbridge/express | 500\nInvalid use of @nlbridge/express middleware: The request body is not available in req.body.\nPlease ensure that the request body is available by using a body parser middleware before the @nlbridge/express middleware.",console.error("[31m"+o+"[0m"),void s.status(500).send("Internal server error.");var o;if("string"==typeof t.body)try{t.body=JSON.parse(t.body)}catch(e){return(e=>{console.warn("[33m"+e+"[0m")})("@nlbridge/express middleware was unable to parse the request body as JSON."),void s.status(400).send("Invalid request body. Please ensure that the request body is a valid JSON object.")}if("object"!=typeof t.body||null===t.body||0===Object.keys(t.body).length)return void s.status(400).send({status:"error",message:"@nlbridge/express middleware found invalid request body. Value must be a non-null non-empty JSON object."});const{action:n,payload:d}=t.body,i=e.asValidAction(n);if(!i)return void s.status(400).send({status:"error",message:"Action is not valid. Please ensure that the action provided is a valid action ID. Valid action IDs are: "+e.actions.join(", ")});if(!e.isValidPayloadForAction(i,d))return void s.status(400).send({status:"error",message:"Payload is not valid for the action provided. Please ensure that the payload provided is valid for the action provided."});const l=await a(i,d);s.status(200).send({status:"ok",action:n,result:l})}}; | ||
"use strict";var e=require("@nlbridge/core");exports.middleware=(s,t)=>{if(!t.apiKey)throw new Error("Missing required API key for OpenAI middleware. Please provide an API key.");const r=e.asOpenAiChatModel(t.chatModel),a=e.actionRunner(s,{apiKey:t.apiKey,chatModel:r??e.openAiDefaultChatModel});return async(s,t,r)=>{if("application/json"!==s.headers["content-type"])return void t.status(400).send("Invalid Content-Type header. Please ensure that the request body is a valid JSON object and that the Content-Type header is set to application/json.");if(!s.body)return o="@nlbridge/express | 500\nInvalid use of @nlbridge/express middleware: The request body is not available in req.body.\nPlease ensure that the request body is available by using a body parser middleware before the @nlbridge/express middleware.",console.error("[31m"+o+"[0m"),void t.status(500).send("Internal server error.");var o;if("string"==typeof s.body)try{s.body=JSON.parse(s.body)}catch(e){return(e=>{console.warn("[33m"+e+"[0m")})("@nlbridge/express middleware was unable to parse the request body as JSON."),void t.status(400).send("Invalid request body. Please ensure that the request body is a valid JSON object.")}if("object"!=typeof s.body||null===s.body||0===Object.keys(s.body).length)return void t.status(400).send({status:"error",message:"@nlbridge/express middleware found invalid request body. Value must be a non-null non-empty JSON object."});const{action:n,payload:i}=s.body,d=e.asValidAction(n);if(d)if(e.isValidPayloadForAction(d,i)){if("chat-stream"===d){const e=await a(d,i);return e.success?void await new Promise(((s,r)=>{let a=!1;t.setHeader("Cache-Control","no-cache"),t.setHeader("Content-Type","text/event-stream"),t.setHeader("Access-Control-Allow-Origin","*"),t.setHeader("Connection","keep-alive"),t.flushHeaders(),t.on("close",(()=>{a=!0,s()})),e.result.onChunkReceived((e=>{!a&&"string"==typeof e&&e.length>0&&t.write(e)})),e.result.onComplete((()=>{a||t.end(),s()})),e.result.onError((e=>{r(e)}))})):void t.status(500).send({success:!1,error:e.error})}if("chat"===d){const e=await a(d,i);return e.success?void t.status(200).send({success:!0,result:e.result}):void t.status(500).send({success:!1,error:e.error})}t.status(500).send({error:!1,message:"The action provided is not supported by the middleware."})}else t.status(400).send({status:"error",message:"Payload is not valid for the action provided. Please ensure that the payload provided is valid for the action provided."});else t.status(400).send({status:"error",message:"Action is not valid. Please ensure that the action provided is a valid action ID. Valid action IDs are: "+e.actions.join(", ")})}}; |
@@ -1,1 +0,1 @@ | ||
import{actionRunner as e,asValidAction as t,actions as s,isValidPayloadForAction as a}from"@nlbridge/core";const r=(r,o)=>{const n=e(r,o);return async(e,r,o)=>{if("application/json"!==e.headers["content-type"])return void r.status(400).send("Invalid Content-Type header. Please ensure that the request body is a valid JSON object and that the Content-Type header is set to application/json.");if(!e.body)return d="@nlbridge/express | 500\nInvalid use of @nlbridge/express middleware: The request body is not available in req.body.\nPlease ensure that the request body is available by using a body parser middleware before the @nlbridge/express middleware.",console.error("[31m"+d+"[0m"),void r.status(500).send("Internal server error.");var d;if("string"==typeof e.body)try{e.body=JSON.parse(e.body)}catch(e){return(e=>{console.warn("[33m"+e+"[0m")})("@nlbridge/express middleware was unable to parse the request body as JSON."),void r.status(400).send("Invalid request body. Please ensure that the request body is a valid JSON object.")}if("object"!=typeof e.body||null===e.body||0===Object.keys(e.body).length)return void r.status(400).send({status:"error",message:"@nlbridge/express middleware found invalid request body. Value must be a non-null non-empty JSON object."});const{action:i,payload:l}=e.body,u=t(i);if(!u)return void r.status(400).send({status:"error",message:"Action is not valid. Please ensure that the action provided is a valid action ID. Valid action IDs are: "+s.join(", ")});if(!a(u,l))return void r.status(400).send({status:"error",message:"Payload is not valid for the action provided. Please ensure that the payload provided is valid for the action provided."});const b=await n(u,l);r.status(200).send({status:"ok",action:i,result:b})}};export{r as middleware}; | ||
import{asOpenAiChatModel as e,actionRunner as s,openAiDefaultChatModel as t,asValidAction as r,actions as a,isValidPayloadForAction as o}from"@nlbridge/core";const n=(n,d)=>{if(!d.apiKey)throw new Error("Missing required API key for OpenAI middleware. Please provide an API key.");const i=e(d.chatModel),l=s(n,{apiKey:d.apiKey,chatModel:i??t});return async(e,s,t)=>{if("application/json"!==e.headers["content-type"])return void s.status(400).send("Invalid Content-Type header. Please ensure that the request body is a valid JSON object and that the Content-Type header is set to application/json.");if(!e.body)return n="@nlbridge/express | 500\nInvalid use of @nlbridge/express middleware: The request body is not available in req.body.\nPlease ensure that the request body is available by using a body parser middleware before the @nlbridge/express middleware.",console.error("[31m"+n+"[0m"),void s.status(500).send("Internal server error.");var n;if("string"==typeof e.body)try{e.body=JSON.parse(e.body)}catch(e){return(e=>{console.warn("[33m"+e+"[0m")})("@nlbridge/express middleware was unable to parse the request body as JSON."),void s.status(400).send("Invalid request body. Please ensure that the request body is a valid JSON object.")}if("object"!=typeof e.body||null===e.body||0===Object.keys(e.body).length)return void s.status(400).send({status:"error",message:"@nlbridge/express middleware found invalid request body. Value must be a non-null non-empty JSON object."});const{action:d,payload:i}=e.body,u=r(d);if(u)if(o(u,i)){if("chat-stream"===u){const e=await l(u,i);return e.success?void await new Promise(((t,r)=>{let a=!1;s.setHeader("Cache-Control","no-cache"),s.setHeader("Content-Type","text/event-stream"),s.setHeader("Access-Control-Allow-Origin","*"),s.setHeader("Connection","keep-alive"),s.flushHeaders(),s.on("close",(()=>{a=!0,t()})),e.result.onChunkReceived((e=>{!a&&"string"==typeof e&&e.length>0&&s.write(e)})),e.result.onComplete((()=>{a||s.end(),t()})),e.result.onError((e=>{r(e)}))})):void s.status(500).send({success:!1,error:e.error})}if("chat"===u){const e=await l(u,i);return e.success?void s.status(200).send({success:!0,result:e.result}):void s.status(500).send({success:!1,error:e.error})}s.status(500).send({error:!1,message:"The action provided is not supported by the middleware."})}else s.status(400).send({status:"error",message:"Payload is not valid for the action provided. Please ensure that the payload provided is valid for the action provided."});else s.status(400).send({status:"error",message:"Action is not valid. Please ensure that the action provided is a valid action ID. Valid action IDs are: "+a.join(", ")})}};export{n as middleware}; |
import { Request, Response, NextFunction } from 'express'; | ||
declare const middleware: (api: 'openai', config: { | ||
apiKey: string; | ||
}) => (req: Request, res: Response, next: NextFunction) => Promise<void>; | ||
type MiddlewareConfig = { | ||
apiKey?: string; | ||
chatModel?: string; | ||
chatMode?: 'stream' | 'block'; | ||
}; | ||
declare const middleware: (api: 'openai', config: MiddlewareConfig) => (req: Request, res: Response, next: NextFunction) => Promise<void>; | ||
export { middleware }; |
{ | ||
"name": "@nlbridge/express", | ||
"version": "0.1.3-beta", | ||
"version": "0.2.0-beta", | ||
"description": "An express middleware to connect to several LLMs", | ||
@@ -59,3 +59,3 @@ "keywords": [ | ||
"dependencies": { | ||
"@nlbridge/core": "0.1.3-beta" | ||
"@nlbridge/core": "0.2.0-beta" | ||
}, | ||
@@ -62,0 +62,0 @@ "devDependencies": { |
@@ -1,1 +0,51 @@ | ||
# nlbridge express | ||
# `nlbridge` Express.js 🌲💬⚙️ | ||
![Free And Open Source](https://img.shields.io/badge/Free%20%26%20Open%20Source-1ccb61) | ||
## About `nlbridge` | ||
`@lbridge` is a Node.js library that provides utilities, middleware, and a development server for **building APIs | ||
powered by large language models**. | ||
This package `@nlbridge/express` provides an Express.js middleware for `nlbridge`. It can be used to build custom APIs | ||
that connect to large language models, and expose them as HTTP endpoints. It's designed to easily integrate with | ||
[`nlux`](https://nlux.ai) (the conversational UI React and JS library), but it can also be used independently with any | ||
other client. | ||
## Using `@nlbridge/express` | ||
To use the `nlbridge` middleware in your own Express.js server, install the `@nlbridge/express` package: | ||
```sh | ||
npm install @nlbridge/express | ||
``` | ||
Then, use the middleware in your Express.js server: | ||
```js | ||
import {middleware} from '@nlbridge/express'; | ||
const app = express(); | ||
app.use(middleware( | ||
// The AI backend to use (e.g. 'openai'). Only OpenAI is supported at the moment. | ||
'openai', | ||
// Configuration object. Ref below for details. | ||
config, | ||
)); | ||
``` | ||
Config is an object with the following type: | ||
```ts | ||
type MiddlewareConfig = { | ||
apiKey?: string; | ||
chatModel?: string; | ||
chatMode?: 'stream' | 'block'; | ||
}; | ||
``` | ||
## Building UIs with `nlux` and `nlbridge` | ||
`nlbridge` is designed to work seamlessly with `nlux`, the conversational UI library for any large language model. | ||
To learn more, please refer to the [nlux documentation](https://nlux.dev). |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@nlbridge/core")):"function"==typeof define&&define.amd?define(["exports","@nlbridge/core"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["@nlbridge/express"]={},e.core)}(this,(function(e,t){"use strict";e.middleware=(e,s)=>{const o=t.actionRunner(e,s);return async(e,s,a)=>{if("application/json"!==e.headers["content-type"])return void s.status(400).send("Invalid Content-Type header. Please ensure that the request body is a valid JSON object and that the Content-Type header is set to application/json.");if(!e.body)return n="@nlbridge/express | 500\nInvalid use of @nlbridge/express middleware: The request body is not available in req.body.\nPlease ensure that the request body is available by using a body parser middleware before the @nlbridge/express middleware.",console.error("[31m"+n+"[0m"),void s.status(500).send("Internal server error.");var n;if("string"==typeof e.body)try{e.body=JSON.parse(e.body)}catch(e){return(e=>{console.warn("[33m"+e+"[0m")})("@nlbridge/express middleware was unable to parse the request body as JSON."),void s.status(400).send("Invalid request body. Please ensure that the request body is a valid JSON object.")}if("object"!=typeof e.body||null===e.body||0===Object.keys(e.body).length)return void s.status(400).send({status:"error",message:"@nlbridge/express middleware found invalid request body. Value must be a non-null non-empty JSON object."});const{action:r,payload:i}=e.body,d=t.asValidAction(r);if(!d)return void s.status(400).send({status:"error",message:"Action is not valid. Please ensure that the action provided is a valid action ID. Valid action IDs are: "+t.actions.join(", ")});if(!t.isValidPayloadForAction(d,i))return void s.status(400).send({status:"error",message:"Payload is not valid for the action provided. Please ensure that the payload provided is valid for the action provided."});const l=await o(d,i);s.status(200).send({status:"ok",action:r,result:l})}}})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@nlbridge/core")):"function"==typeof define&&define.amd?define(["exports","@nlbridge/core"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["@nlbridge/express"]={},e.core)}(this,(function(e,t){"use strict";e.middleware=(e,s)=>{if(!s.apiKey)throw new Error("Missing required API key for OpenAI middleware. Please provide an API key.");const r=t.asOpenAiChatModel(s.chatModel),o=t.actionRunner(e,{apiKey:s.apiKey,chatModel:r??t.openAiDefaultChatModel});return async(e,s,r)=>{if("application/json"!==e.headers["content-type"])return void s.status(400).send("Invalid Content-Type header. Please ensure that the request body is a valid JSON object and that the Content-Type header is set to application/json.");if(!e.body)return a="@nlbridge/express | 500\nInvalid use of @nlbridge/express middleware: The request body is not available in req.body.\nPlease ensure that the request body is available by using a body parser middleware before the @nlbridge/express middleware.",console.error("[31m"+a+"[0m"),void s.status(500).send("Internal server error.");var a;if("string"==typeof e.body)try{e.body=JSON.parse(e.body)}catch(e){return(e=>{console.warn("[33m"+e+"[0m")})("@nlbridge/express middleware was unable to parse the request body as JSON."),void s.status(400).send("Invalid request body. Please ensure that the request body is a valid JSON object.")}if("object"!=typeof e.body||null===e.body||0===Object.keys(e.body).length)return void s.status(400).send({status:"error",message:"@nlbridge/express middleware found invalid request body. Value must be a non-null non-empty JSON object."});const{action:n,payload:i}=e.body,d=t.asValidAction(n);if(d)if(t.isValidPayloadForAction(d,i)){if("chat-stream"===d){const e=await o(d,i);return e.success?void await new Promise(((t,r)=>{let o=!1;s.setHeader("Cache-Control","no-cache"),s.setHeader("Content-Type","text/event-stream"),s.setHeader("Access-Control-Allow-Origin","*"),s.setHeader("Connection","keep-alive"),s.flushHeaders(),s.on("close",(()=>{o=!0,t()})),e.result.onChunkReceived((e=>{!o&&"string"==typeof e&&e.length>0&&s.write(e)})),e.result.onComplete((()=>{o||s.end(),t()})),e.result.onError((e=>{r(e)}))})):void s.status(500).send({success:!1,error:e.error})}if("chat"===d){const e=await o(d,i);return e.success?void s.status(200).send({success:!0,result:e.result}):void s.status(500).send({success:!1,error:e.error})}s.status(500).send({error:!1,message:"The action provided is not supported by the middleware."})}else s.status(400).send({status:"error",message:"Payload is not valid for the action provided. Please ensure that the payload provided is valid for the action provided."});else s.status(400).send({status:"error",message:"Action is not valid. Please ensure that the action provided is a valid action ID. Valid action IDs are: "+t.actions.join(", ")})}}})); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11691
41
51
+ Added@nlbridge/core@0.2.0-beta(transitive)
- Removed@nlbridge/core@0.1.3-beta(transitive)
Updated@nlbridge/core@0.2.0-beta