Skip to content

Commit

Permalink
refactor: update project layout
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Jun 21, 2024
1 parent 46d6fc5 commit 4cbd5c0
Show file tree
Hide file tree
Showing 83 changed files with 556 additions and 94 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ return app.stream( inputs );

# Samples

* [Agent Executor](agents-jdk8/README.md#agent-executor)
* [Image To PlantUML Diagram](agents-jdk8/README.md#generate-plantuml-diagram-from-image)
* [adaptive rag](adaptice-rag/README.md)
* [Agent Executor](agent-executor/README.md)
* [Image To PlantUML Diagram](image-to-diagram/README.md)
* [adaptive rag](adaptive-rag/README.md)
# References

* [LangGraph - LangChain Blog][langgraph.blog]
Expand Down
37 changes: 37 additions & 0 deletions agent-executor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Langgraph4j - Agent Executor

The "<u>Agent Executor</u>" flow involves a sequence of steps where the agent receives a query, decides on necessary actions, invokes tools, processes responses, iteratively performs tasks if needed, and finally returns a synthesized response to the user.

This flow ensures that the agent can handle complex tasks efficiently by leveraging the capabilities of various integrated tools and the decision-making power of the language model.

## Mermaid Diagram

```mermaid
---
title: Agent Executor
---
flowchart TD
start((start))
stop((stop))
agent("agent")
action("action")
condition1{"check state"}
start --> agent
agent --> condition1
condition1 -->|continue| action
condition1 -->|end| stop
action --> agent
```

## PlantUML Diagram
![diagram][agentexecutor]

***

> Go to [code](src/main/java/dev/langchain4j/agentexecutor)

[agentexecutor]: agentexecutor.puml.png



File renamed without changes
4 changes: 4 additions & 0 deletions agent-executor/logging.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
handlers=java.util.logging.ConsoleHandler
.level=INFO
java.util.logging.ConsoleHandler.level=ALL
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
98 changes: 98 additions & 0 deletions agent-executor/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.bsc.langgraph4j</groupId>
<artifactId>langgraph4j-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>langgraph4j-agent-executor</artifactId>
<packaging>jar</packaging>

<name>langgraph4j::agent-executor</name>

<properties>
</properties>

<dependencies>

<dependency>
<groupId>org.bsc.langgraph4j</groupId>
<artifactId>langgraph4j-jdk8</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j</artifactId>
<version>${langchai4j.version}</version>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-open-ai</artifactId>
<version>${langchai4j.version}</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>

<plugin>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-maven-plugin</artifactId>
<version>1.18.20.0</version>
<configuration>
<sourceDirectory>src/main/java</sourceDirectory>
</configuration>
<!--
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>delombok</goal>
</goals>
</execution>
</executions>
-->
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ String shouldContinue(State state) {

public AsyncGenerator<NodeOutput<State>> execute(ChatLanguageModel chatLanguageModel, Map<String, Object> inputs, List<Object> objectsWithTools) throws Exception {


var toolInfoList = ToolInfo.fromList( objectsWithTools );

final List<ToolSpecification> toolSpecifications = toolInfoList.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
import dev.langchain4j.DotEnvConfig;
import dev.langchain4j.model.openai.OpenAiChatModel;
import lombok.var;
import org.bsc.langgraph4j.GraphRepresentation;
import org.bsc.langgraph4j.StateGraph;
import org.bsc.langgraph4j.state.AgentState;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static org.bsc.langgraph4j.StateGraph.END;
import static org.bsc.langgraph4j.action.AsyncEdgeAction.edge_async;
import static org.bsc.langgraph4j.action.AsyncNodeAction.node_async;
import static org.bsc.langgraph4j.utils.CollectionsUtils.listOf;
import static org.bsc.langgraph4j.utils.CollectionsUtils.mapOf;
import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -65,6 +71,7 @@ void executeAgentWithSingleToolInvocation() throws Exception {
assertTrue( returnValues.contains( "MY FIRST TEST") );
System.out.println(returnValues);
}

@Test
void executeAgentWithDoubleToolInvocation() throws Exception {

Expand All @@ -82,4 +89,29 @@ void executeAgentWithDoubleToolInvocation() throws Exception {
System.out.println(returnValues);

}

@Test
public void getGraphTest() throws Exception {

var workflow = new StateGraph<>(AgentState::new);

workflow.setEntryPoint("agent");
workflow.addNode( "agent", node_async( state -> mapOf() ));
workflow.addNode( "action", node_async( state -> mapOf() ));
workflow.addConditionalEdges(
"agent",
edge_async(state -> ""),
mapOf("continue", "action", "end", END)
);
workflow.addEdge("action", "agent");
var app = workflow.compile();

var plantUml = app.getGraph( GraphRepresentation.Type.PLANTUML, "Agent Executor" );

System.out.println( plantUml.getContent() );

var mermaid = app.getGraph( GraphRepresentation.Type.MERMAID, "Agent Executor" );

System.out.println( mermaid.getContent() );
}
}
18 changes: 4 additions & 14 deletions agents-jdk8/README.md → image-to-diagram/README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,24 @@
# Langgraph4j Samples


## Agent Executor

The "<u>Agent Executor</u>" flow involves a sequence of steps where the agent receives a query, decides on necessary actions, invokes tools, processes responses, iteratively performs tasks if needed, and finally returns a synthesized response to the user.

This flow ensures that the agent can handle complex tasks efficiently by leveraging the capabilities of various integrated tools and the decision-making power of the language model.


![diagram][agentexecutor]

> Go to [code](src/main/java/dev/langchain4j/agentexecutor)
# Langgraph4j - Image To Diagram

## Generate PlantUML diagram from Image

The "<u>Generate PlantUML diagram from Image</u>" flow involves structured sequence of steps. Initially, an agent receives an image and is responsible for analyzing and describing its content. This description is then passed to a specialized agent equipped with the skills to translate the description into PlantUML code. To ensure precision in diagram generation, the type of diagram identified within the image dictates the selection of the appropriately skilled agent for the translation task. This ensures that each diagram type is handled by an agent with specific expertise relevant to that diagram.

### PlantUML Diagram
![diagram][image_to_diagram]

### Handle translation errors

In the case that there are errors in result of PlantUML code we have established a supplementary flow that provided a correction process consisting of iteration between both verification and rewrite steps as shown below

#### PlantUML Diagram
![diagram][correction_process]

### Merge All

Finally we can put all together having a complete flow that include also a refinement process over result.

#### PlantUML Diagram
![diagram][image_to_diagram_correction]

----
Expand Down
File renamed without changes
File renamed without changes
File renamed without changes.
4 changes: 2 additions & 2 deletions agents-jdk8/pom.xml → image-to-diagram/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>langgraph4j-agents-jdk8</artifactId>
<artifactId>langgraph4j-image-to-diagram</artifactId>
<packaging>jar</packaging>

<name>langgraph4j::agents::jdk8</name>
<name>langgraph4j::image-to-diagram</name>

<properties>
</properties>
Expand Down
54 changes: 54 additions & 0 deletions image-to-diagram/src/test/java/dev/langchain4j/DotEnvConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package dev.langchain4j;


import java.io.FileReader;
import java.io.Reader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;

import static java.util.Optional.ofNullable;

public interface DotEnvConfig {

static void load() {

// Search for .env file
Path path = Paths.get(".").toAbsolutePath();

Path filePath = Paths.get( path.toString(), ".env");

for( int i=0; !filePath.toFile().exists(); ++i ) {
path = path.getParent();

filePath = Paths.get(path.toString(), ".env");

if (i == 3) {
throw new RuntimeException("no .env file found!");
}
}

// load .env contents in System.properties
try {
final java.util.Properties properties = new java.util.Properties();

try( Reader r = new FileReader(filePath.toFile())) {
properties.load(r);
}
System.getProperties().putAll(properties);
} catch (Exception e) {
throw new RuntimeException(e);
}
}


static Optional<String> valueOf(String key ) {
String value = System.getenv(key);
if (value == null) {
value = System.getProperty(key);
}
return ofNullable(value);
}


}
Loading

0 comments on commit 4cbd5c0

Please sign in to comment.