@fractal-solutions/qflow 1.0.12 → 1.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +263 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -84,6 +84,44 @@ A helper class used in conjunction with `Node.transition()` to define conditiona
|
|
|
84
84
|
|
|
85
85
|
* `to(targetNode)`: Specifies the target node for the transition based on the action returned by the preceding node.
|
|
86
86
|
|
|
87
|
+
|
|
88
|
+
## What Can We Do Now That We Have Agents?
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
The introduction of the `AgentNode` is a game-changer for qflow. It shifts the paradigm from
|
|
92
|
+
pre-defined, static workflows to dynamic, intelligent, and autonomous task execution.
|
|
93
|
+
|
|
94
|
+
Here are some key capabilities and applications unlocked by having agents:
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
1. **Autonomous Goal Achievement:** Agents can now take high-level, open-ended goals (e.g.,
|
|
98
|
+
"Research the best AI frameworks for 2025") and dynamically break them down into
|
|
99
|
+
sub-tasks, selecting and executing the appropriate tools (web search, web scraper, LLM
|
|
100
|
+
for summarization) to achieve the objective.
|
|
101
|
+
2. **Complex Problem Solving:** Agents can tackle multi-step problems that require iterative
|
|
102
|
+
reasoning, external information gathering, and dynamic decision-making based on
|
|
103
|
+
observations. They can adapt their plan as they go.
|
|
104
|
+
3. **Self-Correction and Robustness:** With the **"Reason -> Act -> Observe"** loop, agents can
|
|
105
|
+
identify when a tool fails or produces unexpected results. They can then use their LLM
|
|
106
|
+
reasoning to diagnose the problem and attempt alternative strategies or tools.
|
|
107
|
+
4. **Dynamic Workflow Generation:** Instead of you explicitly defining every step of a workflow,
|
|
108
|
+
the agent generates its own execution path at runtime, choosing tools as needed. This
|
|
109
|
+
makes qflow highly adaptable to unforeseen circumstances.
|
|
110
|
+
5. **Enhanced Automation:** Automate tasks that previously required human intervention, complex
|
|
111
|
+
branching logic, or rigid, brittle scripts. Agents can handle variability and
|
|
112
|
+
uncertainty.
|
|
113
|
+
6. **Interactive Assistants:** Combined with the UserInputNode, agents can become truly
|
|
114
|
+
interactive. They can ask clarifying questions, seek approval for critical actions, or
|
|
115
|
+
provide progress updates, making them more collaborative.
|
|
116
|
+
7. **Data Analysis and Reporting:** Agents can gather data from various sources (web, files,
|
|
117
|
+
APIs), process it (potentially with a future code interpreter), and then synthesize
|
|
118
|
+
findings into structured reports or summaries.
|
|
119
|
+
8. **Research and Information Synthesis:** Agents can research topics, scrape relevant pages,
|
|
120
|
+
and synthesize information into comprehensive answers or documents, acting as automated
|
|
121
|
+
research assistants.
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
|
|
87
125
|
## Basic Usage & Examples
|
|
88
126
|
|
|
89
127
|
### 1. Simple Node
|
|
@@ -549,7 +587,9 @@ import {
|
|
|
549
587
|
HttpRequestNode,
|
|
550
588
|
ScrapeURLNode,
|
|
551
589
|
UserInputNode,
|
|
552
|
-
AgentNode
|
|
590
|
+
AgentNode,
|
|
591
|
+
CodeInterpreterNode,
|
|
592
|
+
MemoryNode
|
|
553
593
|
} from '@fractal-solutions/qflow/nodes';
|
|
554
594
|
|
|
555
595
|
// Ensure your DeepSeek API Key is set as an environment variable
|
|
@@ -579,6 +619,8 @@ const DEEPSEEK_API_KEY = process.env.DEEPSEEK_API_KEY;
|
|
|
579
619
|
const httpRequest = new HttpRequestNode();
|
|
580
620
|
const webScraper = new ScrapeURLNode();
|
|
581
621
|
const userInput = new UserInputNode(); // Agent can also ask for user input
|
|
622
|
+
const codeInterpreter = new CodeInterpreterNode(); // Agent can execute code
|
|
623
|
+
const memoryNode = new MemoryNode(); // Agent can store and retrieve memories
|
|
582
624
|
|
|
583
625
|
// Map tool names to their instances
|
|
584
626
|
const availableTools = {
|
|
@@ -589,6 +631,8 @@ const DEEPSEEK_API_KEY = process.env.DEEPSEEK_API_KEY;
|
|
|
589
631
|
http_request: httpRequest,
|
|
590
632
|
web_scraper: webScraper,
|
|
591
633
|
user_input: userInput,
|
|
634
|
+
code_interpreter: codeInterpreter,
|
|
635
|
+
memory_node: memoryNode,
|
|
592
636
|
// Add other tools as needed
|
|
593
637
|
};
|
|
594
638
|
|
|
@@ -615,14 +659,228 @@ const DEEPSEEK_API_KEY = process.env.DEEPSEEK_API_KEY;
|
|
|
615
659
|
})();
|
|
616
660
|
```
|
|
617
661
|
|
|
662
|
+
### 13. Data Extraction Example
|
|
663
|
+
|
|
664
|
+
Extracting structured data from HTML, JSON, or plain text.
|
|
665
|
+
|
|
666
|
+
```javascript
|
|
667
|
+
import { AsyncFlow } from '@fractal-solutions/qflow';
|
|
668
|
+
import { DataExtractorNode, WebScraperNode } from '@fractal-solutions/qflow/nodes';
|
|
669
|
+
|
|
670
|
+
(async () => {
|
|
671
|
+
console.log('--- Running DataExtractorNode Test Workflow ---');
|
|
672
|
+
|
|
673
|
+
// Example 1: Extracting from HTML (using WebScraperNode to get HTML)
|
|
674
|
+
console.log('\n--- HTML Extraction Example ---');
|
|
675
|
+
const scrapeNode = new WebScraperNode();
|
|
676
|
+
scrapeNode.setParams({ url: 'https://www.example.com' });
|
|
677
|
+
|
|
678
|
+
const extractHtmlNode = new DataExtractorNode();
|
|
679
|
+
extractHtmlNode.setParams({
|
|
680
|
+
type: 'html',
|
|
681
|
+
selector: 'h1' // Extract the text from the first h1 tag
|
|
682
|
+
});
|
|
683
|
+
|
|
684
|
+
scrapeNode.next(extractHtmlNode);
|
|
685
|
+
|
|
686
|
+
extractHtmlNode.postAsync = async (shared, prepRes, execRes) => {
|
|
687
|
+
console.log('Extracted HTML (h1 content):', execRes);
|
|
688
|
+
return 'default';
|
|
689
|
+
};
|
|
690
|
+
|
|
691
|
+
const htmlFlow = new AsyncFlow(scrapeNode);
|
|
692
|
+
try {
|
|
693
|
+
await htmlFlow.runAsync({});
|
|
694
|
+
} catch (error) {
|
|
695
|
+
console.error('HTML Extraction Flow Failed:', error);
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
// Example 2: Extracting from JSON
|
|
699
|
+
console.log('\n--- JSON Extraction Example ---');
|
|
700
|
+
const jsonContent = JSON.stringify({
|
|
701
|
+
user: {
|
|
702
|
+
id: 123,
|
|
703
|
+
name: 'John Doe',
|
|
704
|
+
contact: {
|
|
705
|
+
email: 'john.doe@example.com',
|
|
706
|
+
phone: '123-456-7890'
|
|
707
|
+
},
|
|
708
|
+
roles: ['admin', 'editor']
|
|
709
|
+
},
|
|
710
|
+
products: [
|
|
711
|
+
{ id: 1, name: 'Laptop', price: 1200 },
|
|
712
|
+
{ id: 2, name: 'Mouse', price: 25 }
|
|
713
|
+
]
|
|
714
|
+
}, null, 2);
|
|
715
|
+
|
|
716
|
+
const extractJsonNode = new DataExtractorNode();
|
|
717
|
+
extractJsonNode.setParams({
|
|
718
|
+
input: jsonContent,
|
|
719
|
+
type: 'json',
|
|
720
|
+
jsonPath: 'user.contact.email' // Extract email from nested JSON
|
|
721
|
+
});
|
|
722
|
+
|
|
723
|
+
extractJsonNode.postAsync = async (shared, prepRes, execRes) => {
|
|
724
|
+
console.log('Extracted JSON (user email):', execRes);
|
|
725
|
+
return 'default';
|
|
726
|
+
};
|
|
727
|
+
|
|
728
|
+
const jsonFlow = new AsyncFlow(extractJsonNode);
|
|
729
|
+
try {
|
|
730
|
+
await jsonFlow.runAsync({});
|
|
731
|
+
} catch (error) {
|
|
732
|
+
console.error('JSON Extraction Flow Failed:', error);
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
// Example 3: Extracting from Text (Regex)
|
|
736
|
+
console.log('\n--- Text Extraction Example (Regex) ---');
|
|
737
|
+
const textContent = "User IDs: user_123, user_456, admin_789. Emails: test@example.com, another@domain.org.";
|
|
738
|
+
|
|
739
|
+
const extractTextNode = new DataExtractorNode();
|
|
740
|
+
extractTextNode.setParams({
|
|
741
|
+
input: textContent,
|
|
742
|
+
type: 'text',
|
|
743
|
+
regex: 'user_(\\d+)', // Extract numbers after 'user_'
|
|
744
|
+
group: 1 // Capture group 1
|
|
745
|
+
});
|
|
746
|
+
|
|
747
|
+
extractTextNode.postAsync = async (shared, prepRes, execRes) => {
|
|
748
|
+
console.log('Extracted Text (User IDs):', execRes);
|
|
749
|
+
return 'default';
|
|
750
|
+
};
|
|
751
|
+
|
|
752
|
+
const textFlow = new AsyncFlow(extractTextNode);
|
|
753
|
+
try {
|
|
754
|
+
await textFlow.runAsync({});
|
|
755
|
+
} catch (error) {
|
|
756
|
+
console.error('Text Extraction Flow Failed:', error);
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
console.log('\n--- DataExtractorNode Test Workflow Finished ---');
|
|
760
|
+
})();
|
|
761
|
+
|
|
762
|
+
```
|
|
763
|
+
|
|
764
|
+
### 14. Code Interpreter Example
|
|
765
|
+
|
|
766
|
+
Executing Python code within the workflow.
|
|
767
|
+
|
|
768
|
+
```javascript
|
|
769
|
+
import { AsyncFlow } from '@fractal-solutions/qflow';
|
|
770
|
+
import { CodeInterpreterNode } from '@fractal-solutions/qflow/nodes';
|
|
771
|
+
|
|
772
|
+
(async () => {
|
|
773
|
+
const pythonCode = `
|
|
774
|
+
print("Hello from the Code Interpreter!")
|
|
775
|
+
result = 10 + 20
|
|
776
|
+
print(f"The sum is: {result}")
|
|
777
|
+
`;
|
|
778
|
+
|
|
779
|
+
const codeInterpreter = new CodeInterpreterNode();
|
|
780
|
+
codeInterpreter.setParams({
|
|
781
|
+
code: pythonCode,
|
|
782
|
+
timeout: 5000, // Max 5 seconds for execution
|
|
783
|
+
requireConfirmation: false // Set to false to bypass user confirmation
|
|
784
|
+
});
|
|
785
|
+
|
|
786
|
+
codeInterpreter.postAsync = async (shared, prepRes, execRes) => {
|
|
787
|
+
console.log('--- Code Interpreter Output ---');
|
|
788
|
+
console.log('Stdout:', execRes.stdout);
|
|
789
|
+
console.log('Stderr:', execRes.stderr);
|
|
790
|
+
console.log('Exit Code:', execRes.exitCode);
|
|
791
|
+
return 'default';
|
|
792
|
+
};
|
|
793
|
+
|
|
794
|
+
const flow = new AsyncFlow(codeInterpreter);
|
|
795
|
+
try {
|
|
796
|
+
await flow.runAsync({});
|
|
797
|
+
} catch (error) {
|
|
798
|
+
console.error('Code Interpreter Flow Failed:', error);
|
|
799
|
+
}
|
|
800
|
+
})();
|
|
801
|
+
```
|
|
802
|
+
|
|
803
|
+
### 15. Memory Node (Lightweight RAG)
|
|
804
|
+
|
|
805
|
+
Storing and retrieving text-based memories for lightweight Retrieval-Augmented Generation (RAG).
|
|
806
|
+
|
|
807
|
+
```javascript
|
|
808
|
+
import { AsyncFlow } from '@fractal-solutions/qflow';
|
|
809
|
+
import { MemoryNode, AgentDeepSeekLLMNode } from '@fractal-solutions/qflow/nodes';
|
|
810
|
+
|
|
811
|
+
const DEEPSEEK_API_KEY = process.env.DEEPSEEK_API_KEY;
|
|
812
|
+
|
|
813
|
+
(async () => {
|
|
814
|
+
if (!DEEPSEEK_API_KEY) {
|
|
815
|
+
console.warn("WARNING: DEEPSEEK_API_KEY is not set. Please set it to run the MemoryNode RAG example.");
|
|
816
|
+
return;
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
console.log('--- Running MemoryNode RAG Example ---');
|
|
820
|
+
|
|
821
|
+
// 1. Store a memory
|
|
822
|
+
const storeMemory = new MemoryNode();
|
|
823
|
+
storeMemory.setParams({
|
|
824
|
+
action: 'store',
|
|
825
|
+
content: 'The capital of France is Paris. It is known for the Eiffel Tower.',
|
|
826
|
+
id: 'france_capital'
|
|
827
|
+
});
|
|
828
|
+
await new AsyncFlow(storeMemory).runAsync({});
|
|
829
|
+
console.log('Memory stored.');
|
|
830
|
+
|
|
831
|
+
// 2. Store another memory for RAG
|
|
832
|
+
const storeRagMemory = new MemoryNode();
|
|
833
|
+
storeRagMemory.setParams({
|
|
834
|
+
action: 'store',
|
|
835
|
+
content: 'The primary function of a CPU is to execute instructions that make up a computer program.',
|
|
836
|
+
id: 'cpu_function'
|
|
837
|
+
});
|
|
838
|
+
await new AsyncFlow(storeRagMemory).runAsync({});
|
|
839
|
+
console.log('Another memory stored.');
|
|
840
|
+
|
|
841
|
+
// 3. Retrieve relevant memories based on a query
|
|
842
|
+
const ragRetrieve = new MemoryNode();
|
|
843
|
+
ragRetrieve.setParams({
|
|
844
|
+
action: 'retrieve',
|
|
845
|
+
query: 'computer program'
|
|
846
|
+
});
|
|
847
|
+
|
|
848
|
+
// 4. Use an LLM to answer a question based on retrieved memories
|
|
849
|
+
const ragLLM = new AgentDeepSeekLLMNode();
|
|
850
|
+
ragLLM.setParams({ apiKey: DEEPSEEK_API_KEY });
|
|
851
|
+
|
|
852
|
+
ragRetrieve.next(ragLLM);
|
|
853
|
+
|
|
854
|
+
ragLLM.preparePrompt = (shared) => {
|
|
855
|
+
const retrievedContent = shared.memoryResult.map(mem => mem.content).join('\n\n');
|
|
856
|
+
ragLLM.setParams({
|
|
857
|
+
prompt: `Based on the following context, answer the question:\n\nContext:\n${retrievedContent}\n\nQuestion: What is the main role of a CPU?`
|
|
858
|
+
});
|
|
859
|
+
};
|
|
860
|
+
|
|
861
|
+
const ragFlow = new AsyncFlow(ragRetrieve);
|
|
862
|
+
try {
|
|
863
|
+
const ragResult = await ragFlow.runAsync({});
|
|
864
|
+
console.log('\n--- RAG Example LLM Response ---');
|
|
865
|
+
console.log(ragResult);
|
|
866
|
+
} catch (error) {
|
|
867
|
+
console.error('RAG Flow Failed:', error);
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
console.log('\n--- MemoryNode RAG Example Finished ---');
|
|
871
|
+
})();
|
|
872
|
+
```
|
|
618
873
|
|
|
619
874
|
## Exploring More Examples
|
|
620
875
|
|
|
876
|
+
|
|
621
877
|
The examples above cover the core functionalities of `qflow`. For more advanced and specific use cases involving the built-in integrations, please explore the [`examples/` folder](https://github.com/fractal-solutions/qflow/tree/main/examples) in the project's GitHub repository. There you will find detailed scripts demonstrating how to use nodes for:
|
|
622
878
|
|
|
623
|
-
* **LLMs (DeepSeek, OpenAI, Gemini):** The core of agentic behavior.
|
|
624
|
-
* For agents, use specialized LLM nodes like `AgentDeepSeekLLMNode`
|
|
879
|
+
* **LLMs (DeepSeek, OpenAI, Gemini, Ollama):** The core of agentic behavior.
|
|
880
|
+
* For agents, use specialized LLM nodes like `AgentDeepSeekLLMNode`, `AgentOpenAILLMNode`, `AgentOllamaLLMNode`.
|
|
625
881
|
* **Agent:** Orchestrating tools and LLM reasoning to achieve complex goals.
|
|
882
|
+
* **Memory:** For lightweight, keyword-based long-term memory and RAG.
|
|
883
|
+
* **CodeInterpreter:** For executing dynamic code (e.g., Python) within the workflow.
|
|
626
884
|
* **Interactive Agent:** An agent that takes a goal from user input and uses its tools to achieve it.
|
|
627
885
|
* **Shell:** For system-level interaction and execution.
|
|
628
886
|
* **HTTP:** For universal API access.
|
|
@@ -631,7 +889,8 @@ The examples above cover the core functionalities of `qflow`. For more advanced
|
|
|
631
889
|
* **Web Search:** Discovering information on the web using either:
|
|
632
890
|
* `DuckDuckGoSearchNode`: API-key-free, using DuckDuckGo's HTML interface.
|
|
633
891
|
* `GoogleSearchNode`: Requires a Google API Key and Custom Search Engine ID for more robust results.
|
|
634
|
-
* **WebScraper:** For targeted
|
|
892
|
+
* **WebScraper:** For targeted web scraping.
|
|
893
|
+
* **DataExtractor:** For targeted data extraction.
|
|
635
894
|
* **GitHub:** Creating and managing issues.
|
|
636
895
|
* **HackerNews:** Fetching top stories and item details.
|
|
637
896
|
* **Stripe:** Creating charges and retrieving account balances.
|
package/package.json
CHANGED