An open-source Java library to build, package, integrate, orchestrate and monitor agentic AI systems for java developers 💡
🌟 Starring me: If you find this repository beneficial, don't forget to give it a star! 🌟 It's a simple way to show your appreciation and help this project grow!
JavAI Workflow (named initially Langchain4j-workflow) is a dynamic, stateful workflow engine crafted as a Java library. It empowers java developers with granular control over the orchestrated workflows as a graph, iteratively, with cycles, flexibility, control, and conditional decisions. This engine is a game-changer for building sophisticated AI applications, such as multiples RAG-based approaches using modern paradigms and agent architectures. It enables the crafting of custom behavior, leading to a significant reduction in hallucinations and an increase in response reliability.
jAI Workflow is influenced by LangFlow, LangGraph, Graphviz.
Nodes
, Conditional Nodes
, Edges
, and workflows as a graph. This feature allows you to define custom workflows with multiple Transitions
between nodes such as one-way, round trip and recursive.workflow.run()
and streaming worflow.runStream()
runs the outputs as they are produced by each node. This last feature allows for real-time processing and response in your application.Graphhviz
lib to generate the image, but you implement your own image generator on GraphImageGenerator.java
interface.jAI Workflow is designed with a modular architecture, enabling you to define custom workflows, modules, or agents to build RAG systems as LEGO-like. A module can be decoupled and integrated into any other workflow.
📖 Full documentation will be available soon
In jAI Workflow, the notion of state plays a pivotal role. Every execution of the graph initiates a state, which is then transferred among the nodes during their execution. Each node, after its execution, updates this internal state with its own return value. The method by which the graph updates its internal state is determined by user-defined functions.
The simplest way to use jAI Workflow in your project is with the LangChain4j integration because enables you to define custom workflows using all the features that LangChain4j offers. This integration could provide a comprehensive toolset for building advanced AI applications:
<dependency>
<groupId>com.github.czelabueno</groupId>
<artifactId>langchain4j-workflow</artifactId>
<version>0.2.0</version> <!--Change to the latest version-->
</dependency>
If you would want to use jAI workflow without LangChain4j or with other framework, add the following dependency to your pom.xml
file:
<dependency>
<groupId>com.github.czelabueno</groupId>
<artifactId>jai-workflow-core</artifactId>
<version>0.2.0</version> <!--Change to the latest version-->
</dependency>
Define a stateful bean with fields that will be used to store the state of the workflow:
// Define a stateful bean
public class MyStatefulBean {
int value = 0;
}
Create a simple workflow with 4 nodes and conditional edges:
public class Example {
public static void main(String[] args) {
MyStatefulBean myStatefulBean = new MyStatefulBean();
// Define functions that determines statefulBean state
Function<MyStatefulBean, String> node1Func = obj -> {
obj.value +=1;
System.out.println("Node 1: [" + obj.value + "]");
return "Node1: function proceed";
};
Function<MyStatefulBean, String> node2Func = obj -> {
obj.value +=2;
System.out.println("Node 2: [" + obj.value + "]");
return "Node2: function proceed";
};
Function<MyStatefulBean, String> node3Func = obj -> {
obj.value +=3;
System.out.println("Node 3: [" + obj.value + "]");
return "Node3: function proceed";
};
Function<MyStatefulBean, String> node4Func = obj -> {
obj.value +=4;
System.out.println("Node 4: [" + obj.value + "]");
return "Node4: function proceed";
};
// Create the nodes and associate them with the functions to be used during execution.
Node<MyStatefulBean, String> node1 = Node.from("node1", node1Func);
Node<MyStatefulBean, String> node2 = Node.from("node2", node2Func);
Node<MyStatefulBean, String> node3 = Node.from("node3", node3Func);
Node<MyStatefulBean, String> node4 = Node.from("node4", node4Func);
// Create workflow
StateWorkflow<MyStatefulBean> workflow = DefaultStateWorkflow.<MyStatefulBean>builder()
.statefulBean(myStatefulBean)
.addNodes(Arrays.asList(node1, node2, node3))
.build();
// You can add more nodes after workflow build. E.g. node4
workflow.addNode(node4);
// Define edges
workflow.putEdge(node1, node2);
workflow.putEdge(node2, node3);
// Conditional edge
workflow.putEdge(node3, Conditional.eval(obj -> {
System.out.println("Stateful Value [" + obj.value + "]");
if (obj.value > 6) {
return node4;
} else {
return node2;
}
}));
workflow.putEdge(node4, WorkflowStateName.END);
// Define which node to start
workflow.startNode(node1);
// Run workflow normally
workflow.run();
// OR
// Run workflow in streaming mode
workflow.runStream(node -> {
System.out.println("Processing node: " + node.getName());
});
// Print all computed transitions
String transitions = workflow.prettyTransitions();
System.out.println("Transitions: \n");
System.out.println(transitions);
// Generate workflow image
workflow.generateWorkflowImage("image/my-workflow.svg");
// workflow.generateWorkflowImage(); // if you use this method, it'll use by default the root path and default image name.
}
}
Now you can check the output of the workflow execution.
STARTING workflow in stream mode..
Processing node: node1
Node 1: [1]
Processing node: node2
Node 2: [3]
Processing node: node3
Node 3: [6]
Stateful Value [6]
Processing node: node2
Node 2: [8]
Processing node: node3
Node 3: [11]
Stateful Value [11]
Processing node: node4
Node 4: [15]
Reached END state
You can print all computed transitions:
START -> node1 -> node2 -> node3 -> node2 -> node3 -> node4 -> END
You can generate a workflow image with all computed transitions:
> image/
> ├── my-workflow.svg
You can check all examples in the langchain4j-worflow-examples repository where show you how-to implement multiple RAG patterns, agent architectures and AI papers using LangChain4j and jAI Workflow.
Please note that examples can be modified and more examples will be added over time.
langchain4j-moa
langchain4j-corrective-rag
If you have any feedback, suggestions, or want to contribute, please feel free to open an issue or a pull request. We are open to new ideas and suggestions. Help us to maturity this project and make it more useful for the java community.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。