-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0486565
Showing
13 changed files
with
463 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module.exports = { | ||
extends: [ | ||
'eslint:recommended', | ||
'plugin:@typescript-eslint/recommended', | ||
'plugin:prettier/recommended', | ||
], | ||
parser: '@typescript-eslint/parser', | ||
plugins: ['@typescript-eslint', 'unused-imports', 'prettier'], | ||
ignorePatterns: [ | ||
'**/node_modules', | ||
'**/dist', | ||
'**/build', | ||
'**/package-lock.json', | ||
], | ||
rules: { | ||
'@typescript-eslint/explicit-module-boundary-types': 'off', | ||
'no-unused-vars': 'off', | ||
'unused-imports/no-unused-imports': 'warn', | ||
'unused-imports/no-unused-vars': [ | ||
'warn', | ||
{ | ||
vars: 'all', | ||
varsIgnorePattern: '^_', | ||
args: 'after-used', | ||
argsIgnorePattern: '^_', | ||
}, | ||
], | ||
'no-undef': 'off', | ||
'no-console': [ | ||
process.env.CI ? 'error' : 'warn', | ||
{ allow: ['warn', 'error', 'info'] }, | ||
], | ||
'prettier/prettier': 'error', | ||
"@typescript-eslint/no-explicit-any": "off", | ||
"@typescript-eslint/ban-ts-comment": "off" | ||
}, | ||
}; |
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,5 @@ | ||
node_modules/ | ||
dist/ | ||
.DS_Store | ||
*.log | ||
package-lock.json |
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,6 @@ | ||
src/ | ||
tests/ | ||
.gitignore | ||
tsconfig.json | ||
jest.config.js | ||
*.log |
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,8 @@ | ||
{ | ||
"semi": true, | ||
"singleQuote": true, | ||
"printWidth": 80, | ||
"tabWidth": 4, | ||
"trailingComma": "es5" | ||
} | ||
|
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) [year] [fullname] | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,63 @@ | ||
# Flowise SDK | ||
|
||
A TypeScript SDK for interacting with the Flowise API. | ||
|
||
## Installation | ||
|
||
```bash | ||
npm install flowise-sdk | ||
``` | ||
|
||
## Usage | ||
|
||
```typescript | ||
import { FlowiseClient } from 'flowise-sdk'; | ||
|
||
const flowise = new FlowiseClient({ baseUrl: 'http://localhost:3000' }); | ||
|
||
async function main() { | ||
const completion = await flowise.createPrediction({ | ||
chatflowId: '<id>', | ||
question: "hello", | ||
streaming: true | ||
}); | ||
|
||
for await (const chunk of completion) { | ||
console.log(chunk); | ||
} | ||
} | ||
|
||
main(); | ||
``` | ||
|
||
## API Reference | ||
|
||
### `FlowiseClient` | ||
|
||
The main class for interacting with the Flowise API. | ||
|
||
#### Constructor | ||
|
||
```typescript | ||
new FlowiseClient(baseUrl?: string) | ||
``` | ||
|
||
- `baseUrl`: Optional. The base URL for the Flowise API. Defaults to 'http://localhost:3000'. | ||
|
||
#### Methods | ||
|
||
##### `predictions.create(params: PredictionParams): Promise<PredictionResponse>` | ||
|
||
Creates a new prediction. | ||
|
||
- `params`: An object containing the following properties: | ||
- `chatflowId`: string - Chatflow ID to execute prediction | ||
- `question`: string - The question to ask. | ||
- `streaming`: boolean (optional) - Whether to stream the response. | ||
- `chatId`: string (optional) - Chat ID of the session | ||
- `overrideConfig`: object (optional) - Override configuration | ||
|
||
|
||
## License | ||
|
||
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
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,24 @@ | ||
// Actual import | ||
// import { FlowiseClient } from 'flowise-sdk'; | ||
import { FlowiseClient } from '../src'; | ||
|
||
async function main() { | ||
const flowise = new FlowiseClient({ baseUrl: 'http://localhost:3000' }); | ||
|
||
try { | ||
const completion = await flowise.createPrediction({ | ||
chatflowId: 'fe1145fa-1b2b-45b7-b2ba-bcc5aaeb5ffd', | ||
question: 'hello there', | ||
streaming: true, | ||
}); | ||
|
||
// Process each chunk of data as it is streamed | ||
for await (const chunk of completion) { | ||
console.log('Received chunk:', chunk); | ||
} | ||
} catch (error) { | ||
console.error('Error:', error); | ||
} | ||
} | ||
|
||
main(); |
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,7 @@ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
roots: ['<rootDir>/tests'], | ||
testMatch: ['**/*.test.ts'], | ||
moduleFileExtensions: ['ts', 'js', 'json', 'node'], | ||
}; |
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,39 @@ | ||
{ | ||
"name": "flowise-sdk", | ||
"version": "1.0.5", | ||
"description": "Flowise SDK for streaming API responses.", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"scripts": { | ||
"build": "tsc", | ||
"start": "node dist/examples/example.js", | ||
"test": "jest", | ||
"lint": "eslint \"**/*.{ts,tsx}\" --fix", | ||
"prepare": "npm run build" | ||
}, | ||
"keywords": [ | ||
"flowise", | ||
"sdk", | ||
"typescript" | ||
], | ||
"author": "Your Name", | ||
"license": "MIT", | ||
"dependencies": { | ||
"node-fetch": "^3.3.0" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^29.5.2", | ||
"@types/node": "^20.3.1", | ||
"@types/node-fetch": "^2.6.4", | ||
"@typescript-eslint/eslint-plugin": "^6.7.0", | ||
"@typescript-eslint/parser": "^6.7.0", | ||
"eslint": "^8.49.0", | ||
"eslint-config-prettier": "^9.1.0", | ||
"eslint-plugin-prettier": "^5.0.1", | ||
"eslint-plugin-unused-imports": "^3.0.0", | ||
"jest": "^29.5.0", | ||
"prettier": "^3.3.3", | ||
"ts-jest": "^29.1.0", | ||
"typescript": "^5.1.3" | ||
} | ||
} |
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,112 @@ | ||
export interface PredictionData { | ||
chatflowId: string; | ||
question: string; | ||
overrideConfig?: Record<string, any>; | ||
chatId?: string; | ||
streaming?: boolean; | ||
} | ||
|
||
interface FlowiseClientOptions { | ||
baseUrl?: string; | ||
} | ||
|
||
type PredictionResponse<T extends PredictionData> = T['streaming'] extends true | ||
? AsyncGenerator<string, void, unknown> // Streaming returns an async generator | ||
: Record<string, any>; | ||
|
||
export default class FlowiseClient { | ||
private baseUrl: string; | ||
|
||
constructor(options: FlowiseClientOptions = {}) { | ||
this.baseUrl = options.baseUrl || 'http://localhost:3000'; | ||
} | ||
|
||
// Method to create a new prediction and handle streaming response | ||
async createPrediction<T extends PredictionData>( | ||
data: T | ||
): Promise<PredictionResponse<T>> { | ||
const { chatflowId, streaming } = data; | ||
|
||
// Check if chatflow is available to stream | ||
const chatFlowStreamingUrl = `${this.baseUrl}/api/v1/chatflows-streaming/${chatflowId}`; | ||
const resp = await fetch(chatFlowStreamingUrl, { | ||
method: 'GET', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
|
||
const chatFlowStreamingData = await resp.json(); | ||
const isChatFlowAvailableToStream = | ||
chatFlowStreamingData.isStreaming || false; | ||
|
||
const predictionUrl = `${this.baseUrl}/api/v1/prediction/${chatflowId}`; | ||
|
||
if (isChatFlowAvailableToStream && streaming) { | ||
return { | ||
async *[Symbol.asyncIterator]() { | ||
const response = await fetch(predictionUrl, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(data), | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error( | ||
`HTTP error! status: ${response.status}` | ||
); | ||
} | ||
|
||
//@ts-ignore | ||
const reader = response.body.getReader(); | ||
const decoder = new TextDecoder(); | ||
let buffer = ''; | ||
|
||
try { | ||
while (true) { | ||
const { done, value } = await reader.read(); | ||
if (done) break; | ||
|
||
buffer += decoder.decode(value, { stream: true }); | ||
const lines = buffer.split('\n'); | ||
buffer = lines.pop() || ''; | ||
|
||
for (const line of lines) { | ||
if (line.trim() === '') continue; | ||
if (line.startsWith('data:')) { | ||
const stringifiedJson = line.replace( | ||
'data:', | ||
'' | ||
); | ||
const event = JSON.parse(stringifiedJson); | ||
yield event; | ||
} | ||
} | ||
} | ||
} finally { | ||
reader.releaseLock(); | ||
} | ||
}, | ||
} as unknown as Promise<PredictionResponse<T>>; | ||
} else { | ||
// Make a POST request and handle streaming response | ||
const options = { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(data), | ||
}; | ||
|
||
try { | ||
const response = await fetch(predictionUrl, options); | ||
const resp = await response.json(); | ||
return resp as Promise<PredictionResponse<T>>; | ||
} catch (error) { | ||
throw new Error('Error creating prediction'); | ||
} | ||
} | ||
} | ||
} |
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 @@ | ||
export { default as FlowiseClient } from './flowise-sdk'; |
Oops, something went wrong.