This commit is contained in:
2024-11-09 10:56:07 -05:00
commit a98c467a39
4 changed files with 420 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/.idea/

254
README.md Normal file
View File

@@ -0,0 +1,254 @@
# workflow-function-manifold
> A TypeScript/JavaScript library for building dynamic, LLM-driven workflows using a region-based execution model
![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)
![Node Version](https://img.shields.io/badge/node-%3E%3D%2014.0.0-brightgreen)
## Overview
`workflow-function-manifold` enables you to create dynamic workflows that:
- Navigate between different execution regions based on LLM-interpreted intents
- Execute operations within regions based on state and context
- Maintain workflow state across operations
- Support flexible region-to-region connections
```mermaid
graph TD
MF[Workflow Function Manifold] -->|Initialize| CR[Current Region]
MF -->|Navigate| AR[Adjacent Regions]
MF -->|Execute| OP[Operators]
OP -->|Use| LLM[LLM Service]
LLM -->|Match| INT[Intent]
INT --> OP
OP -->|Update| ST[Workflow State]
ST -->|Inform| AR
AR -->|Check| NR[Next Region]
NR -->|If Valid| CR
CR -->|Continue| OP
NR -->|If Invalid| END[End]
style MF fill:#000000,stroke:#FFFFFF,stroke-width:4px,color:#ffffff
style CR fill:#222222,stroke:#FFFFFF,stroke-width:2px,color:#ffffff
style AR fill:#222222,stroke:#FFFFFF,stroke-width:2px,color:#ffffff
style OP fill:#333333,stroke:#FFFFFF,stroke-width:2px,color:#ffffff
style LLM fill:#333333,stroke:#FFFFFF,stroke-width:2px,color:#ffffff
style INT fill:#333333,stroke:#FFFFFF,stroke-width:2px,color:#ffffff
style NR fill:#333333,stroke:#FFFFFF,stroke-width:2px,color:#ffffff
style END fill:#222222,stroke:#FFFFFF,stroke-width:2px,color:#ffffff
style ST fill:#444444,stroke:#FFFFFF,stroke-width:2px,color:#ffffff
```
## Installation
```bash
npm install workflow-function-manifold
```
## Quick Start
```javascript
import {
WorkflowFunctionManifold,
ManifoldRegion,
WorkflowOperator,
DummyLlmService
} from 'workflow-function-manifold';
// Initialize the manifold with an LLM service
const llm = new DummyLlmService();
const manifold = new WorkflowFunctionManifold(llm);
// Create an operator
const analysisOperator = new WorkflowOperator('analysis', async (state) => {
console.log('Analyzing data...');
return { ...state, analyzed: true };
});
// Create and connect regions
const analysisRegion = new ManifoldRegion('analysis', [analysisOperator]);
manifold.addRegion(analysisRegion);
// Execute workflow
await manifold.navigate('analyze the data');
await manifold.executeWorkflow('analyze the data');
```
## Core Components
### WorkflowFunctionManifold
The main class that orchestrates workflow execution.
```javascript
const manifold = new WorkflowFunctionManifold(llmService);
// Add regions
manifold.addRegion(region);
// Navigate between regions
await manifold.navigate(prompt);
// Execute operations
await manifold.executeWorkflow(prompt);
```
### ManifoldRegion
Represents a workflow region containing operators and connections to other regions.
```javascript
const region = new ManifoldRegion('regionName', [operator1, operator2]);
// Connect regions
region.connectTo(otherRegion);
// Add operators
region.addOperator(newOperator);
```
### WorkflowOperator
Defines operations that can be executed within regions.
```javascript
const operator = new WorkflowOperator('operatorName', async (state) => {
// Modify state
return newState;
});
```
### DummyLlmService
A simple LLM service simulation for intent matching.
```javascript
const llm = new DummyLlmService();
const intent = await llm.query('analyze the data');
// Returns: { confidence: 0.9, action: 'analysis' }
```
## Complete Example
Here's a full example demonstrating a three-stage workflow:
```javascript
async function createWorkflow() {
const llm = new DummyLlmService();
const manifold = new WorkflowFunctionManifold(llm);
// Create operators
const analysisOp = new WorkflowOperator('analysis', async (state) => {
return { ...state, analyzed: true };
});
const processingOp = new WorkflowOperator('processing', async (state) => {
return { ...state, processed: true };
});
const transformOp = new WorkflowOperator('transformation', async (state) => {
return { ...state, transformed: true };
});
// Create and connect regions
const analysisRegion = new ManifoldRegion('analysis', [analysisOp]);
const processingRegion = new ManifoldRegion('processing', [processingOp]);
const transformRegion = new ManifoldRegion('transformation', [transformOp]);
analysisRegion.connectTo(processingRegion);
processingRegion.connectTo(transformRegion);
// Add regions to manifold
manifold.addRegion(analysisRegion);
manifold.addRegion(processingRegion);
manifold.addRegion(transformRegion);
return manifold;
}
// Execute workflow
const manifold = await createWorkflow();
const prompts = [
'analyze the data',
'process the results',
'transform the output'
];
for (const prompt of prompts) {
await manifold.navigate(prompt);
await manifold.executeWorkflow(prompt);
}
```
## API Reference
### WorkflowFunctionManifold
#### Constructor
- `constructor(llmService: LLMService)`
#### Methods
- `addRegion(region: ManifoldRegion): void`
- `async navigate(prompt: string): Promise<boolean>`
- `async executeWorkflow(prompt: string): Promise<boolean>`
### ManifoldRegion
#### Constructor
- `constructor(name: string, operators: WorkflowOperator[] = [])`
#### Methods
- `addOperator(operator: WorkflowOperator): void`
- `connectTo(region: ManifoldRegion): void`
- `async getValidOperators(state: any): Promise<WorkflowOperator[]>`
### WorkflowOperator
#### Constructor
- `constructor(name: string, operation: (state: any) => Promise<any>)`
#### Methods
- `async execute(state: any): Promise<any>`
## State Management
The workflow maintains state across operations. Each operator can access and modify the state:
```javascript
const operator = new WorkflowOperator('example', async (state) => {
// Access existing state
const { previousValue } = state;
// Return modified state
return {
...state,
newValue: 'updated'
};
});
```
## LLM Integration
The system uses LLM services for intent recognition. The default `DummyLlmService` provides basic intent matching, but you can implement your own LLM service:
```javascript
class CustomLLMService {
async query(prompt) {
// Implement custom LLM logic
return {
confidence: number,
action: string
};
}
}
```
## Contributing
1. Fork the repository
2. Create your feature branch: `git checkout -b feature/my-feature`
3. Commit your changes: `git commit -am 'Add new feature'`
4. Push to the branch: `git push origin feature/my-feature`
5. Submit a pull request
## License
MIT © 2024 Geoff Seemueller

153
index.js Normal file
View File

@@ -0,0 +1,153 @@
// Simulated LLM Service
class DummyLlmService {
async query(prompt) {
// Simulate LLM response with basic intent matching
const intents = {
'analyze': { confidence: 0.9, action: 'analysis' },
'process': { confidence: 0.8, action: 'processing' },
'transform': { confidence: 0.7, action: 'transformation' }
};
const matchedIntent = Object.entries(intents)
.find(([key]) => prompt.toLowerCase().includes(key));
return matchedIntent ? matchedIntent[1] : { confidence: 0.1, action: 'unknown' };
}
}
// Interface for workflow operators
class WorkflowOperator {
constructor(name, operation) {
this.name = name;
this.operation = operation;
}
async execute(state) {
return await this.operation(state);
}
}
// Interface for manifold regions
class ManifoldRegion {
constructor(name, operators = []) {
this.name = name;
this.operators = operators;
this.adjacentRegions = new Set();
}
addOperator(operator) {
this.operators.push(operator);
}
connectTo(region) {
this.adjacentRegions.add(region);
region.adjacentRegions.add(this);
}
async getValidOperators(state) {
return this.operators;
}
}
// Main manifold implementation
class WorkflowFunctionManifold {
constructor(llmService) {
this.llmService = llmService;
this.regions = new Map();
this.currentRegion = null;
this.state = {};
}
addRegion(region) {
this.regions.set(region.name, region);
if (!this.currentRegion) {
this.currentRegion = region;
}
}
async navigate(prompt) {
const intent = await this.llmService.query(prompt);
// Find best region based on intent
const nextRegion = Array.from(this.currentRegion.adjacentRegions)
.find(region => region.name.toLowerCase().includes(intent.action));
if (nextRegion && intent.confidence > 0.5) {
this.currentRegion = nextRegion;
return true;
}
return false;
}
async executeWorkflow(prompt) {
const operators = await this.currentRegion.getValidOperators(this.state);
const intent = await this.llmService.query(prompt);
const matchedOperator = operators
.find(op => op.name.toLowerCase().includes(intent.action));
if (matchedOperator) {
this.state = await matchedOperator.execute(this.state);
return true;
}
return false;
}
}
// Usage Example
async function demonstrateManifold() {
// Initialize services and manifold
const llm = new DummyLlmService();
const manifold = new WorkflowFunctionManifold(llm);
// Create operators
const dataAnalysisOp = new WorkflowOperator('analysis', async (state) => {
console.log('Performing data analysis...');
return { ...state, analyzed: true };
});
const dataProcessingOp = new WorkflowOperator('processing', async (state) => {
console.log('Processing data...');
return { ...state, processed: true };
});
const dataTransformOp = new WorkflowOperator('transformation', async (state) => {
console.log('Transforming data...');
return { ...state, transformed: true };
});
// Create regions
const analysisRegion = new ManifoldRegion('analysis', [dataAnalysisOp]);
const processingRegion = new ManifoldRegion('processing', [dataProcessingOp]);
const transformationRegion = new ManifoldRegion('transformation', [dataTransformOp]);
// Connect regions
analysisRegion.connectTo(processingRegion);
processingRegion.connectTo(transformationRegion);
// Add regions to manifold
manifold.addRegion(analysisRegion);
manifold.addRegion(processingRegion);
manifold.addRegion(transformationRegion);
// Demonstrate workflow execution
console.log('Starting workflow demonstration...');
const prompts = [
'analyze the data',
'process the results',
'transform the output'
];
for (const prompt of prompts) {
console.log(`\nExecuting prompt: "${prompt}"`);
await manifold.navigate(prompt);
const executed = await manifold.executeWorkflow(prompt);
console.log(`Current state:`, manifold.state);
console.log(`Current region: ${manifold.currentRegion.name}`);
console.log(`Operation executed: ${executed}`);
}
}
// Run the demonstration
demonstrateManifold().catch(console.error);

12
package.json Normal file
View File

@@ -0,0 +1,12 @@
{
"name": "manifold",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}