Skip to content

Commit

Permalink
Merge branch 'release/1.0-20240621'
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Jun 21, 2024
2 parents 5391138 + ef7953b commit a4301d7
Show file tree
Hide file tree
Showing 92 changed files with 922 additions and 179 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

| Date | Release | info
|--------------| --- | ---
| Jun 21, 2024 | `1.0-SNAPSHOT` | Add support of [Mermaid] diagram generation - [issue #5](https://github.com/bsorrentino/langgraph4j/issues/5)
| Jun 19, 2024 | `1.0-SNAPSHOT` | Add [adaptive rag](adaptice-rag/README.md) sample
| Jun 10, 2024 | `1.0-SNAPSHOT` | Refactoring how generate graph representation (plantuml)
| May 20, 2024 | `1.0-SNAPSHOT` | Add "[Image To PlantUML Diagram](agents-jdk8/README.md#generate-plantuml-diagram-from-image)" sample
Expand Down Expand Up @@ -148,9 +149,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 All @@ -167,6 +168,7 @@ return app.stream( inputs );
[AgentExecutor]: https://github.com/langchain-ai/langchain/blob/master/libs/langchain/langchain/agents/agent.py
[PlantUML]: https://plantuml.com
[java-async-generator]: https://github.com/bsorrentino/java-async-generator
[Mermaid]: https://mermaid.js.org

[javadocs]: https://bsorrentino.github.io/langgraph4j/apidocs/index.html
[snapshots]: https://oss.sonatype.org/content/repositories/snapshots/org/bsc/langgraph4j/langgraph4j-jdk8/1.0-SNAPSHOT
33 changes: 33 additions & 0 deletions adaptive-rag/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,39 @@ Java implementation of [Adaptive Rag]

[Adaptive Rag]:https://github.com/langchain-ai/langgraph/blob/main/examples/rag/langgraph_adaptive_rag.ipynb

## Mermaid diagram

```mermaid
---
title: Adaptive RAG
---
flowchart TD
start((start))
stop((stop))
web_search("web_search")
retrieve("retrieve")
grade_documents("grade_documents")
generate("generate")
transform_query("transform_query")
condition1{"check state"}
condition2{"check state"}
startcondition{"check state"}
start --> startcondition
startcondition -->|web_search| web_search
startcondition -->|vectorstore| retrieve
web_search --> generate
retrieve --> grade_documents
grade_documents --> condition1
condition1 -->|transform_query| transform_query
condition1 -->|generate| generate
transform_query --> retrieve
generate --> condition2
condition2 -->|not supported| generate
condition2 -->|not useful| transform_query
condition2 -->|useful| stop
```

## PlantUML diagram
![diagram](AdaptiveRag.png)

## Getting Started
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.store.embedding.EmbeddingSearchResult;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.bsc.langgraph4j.CompiledGraph;
Expand Down Expand Up @@ -57,17 +58,22 @@ public List<String> documents() {

private final String openApiKey;
private final String tavilyApiKey;
private final ChromaStore chroma;
@Getter(lazy = true)
private final ChromaStore chroma = openChroma();

public AdaptiveRag( String openApiKey, String tavilyApiKey ) {
Objects.requireNonNull(openApiKey, "no OPENAI APIKEY provided!");
Objects.requireNonNull(tavilyApiKey, "no TAVILY APIKEY provided!");
this.openApiKey = openApiKey;
this.tavilyApiKey = tavilyApiKey;
this.chroma = ChromaStore.of(openApiKey);
//this.chroma = ChromaStore.of(openApiKey);

}

private ChromaStore openChroma() {
return ChromaStore.of(openApiKey);
}

/**
* Node: Retrieve documents
* @param state The current graph state
Expand All @@ -78,7 +84,7 @@ private Map<String,Object> retrieve( State state ) {

String question = state.question();

EmbeddingSearchResult<TextSegment> relevant = this.chroma.search( question );
EmbeddingSearchResult<TextSegment> relevant = this.getChroma().search( question );

List<String> documents = relevant.matches().stream()
.map( m -> m.embedded().text() )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,12 @@ public void getGraphTest() throws Exception {

var graph = adaptiveRag.buildGraph();

var plantUml = graph.getGraph( GraphRepresentation.Type.PLANTUML );
var plantUml = graph.getGraph( GraphRepresentation.Type.PLANTUML, "Adaptive RAG" );

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

var mermaid = graph.getGraph( GraphRepresentation.Type.MERMAID, "Adaptive RAG" );

System.out.println( mermaid.getContent() );
}
}
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() );
}
}
Loading

0 comments on commit a4301d7

Please sign in to comment.