Skip to content

Commit

Permalink
docs: improve javadoc
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Dec 29, 2024
1 parent 90dea65 commit a6408c3
Show file tree
Hide file tree
Showing 10 changed files with 192 additions and 20 deletions.
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.11.2</version>
<configuration>
<failOnWarnings>false</failOnWarnings>
<doclint>none</doclint>
</configuration>
<reportSets>
<reportSet>
<id>aggregate</id>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@

import java.util.regex.Pattern;

/**
* This class provides functionality to parse a JSON formatted string and convert it into a `Diagram.Element` object.
*/
public class DiagramOutputParser {
/**
* Parses a JSON string and converts it into a Diagram.Element object.
*
* @param s The JSON string to be parsed.
* @return A Diagram.Element object representing the parsed JSON data.
* @throws IllegalArgumentException If no valid JSON is found in the input string.
*/
public Diagram.Element parse(String s) {
// String pattern = "```json\n(.*?)\n```";
String pattern = "```json\n(.*?)\n```";
Expand All @@ -24,4 +34,4 @@ public Diagram.Element parse(String s) {
Gson gson = new Gson();
return gson.fromJson(matcher.group(1), Diagram.Element.class);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,93 @@
import static java.util.Optional.ofNullable;
import static org.bsc.langgraph4j.utils.CollectionsUtils.*;

/**
* Represents the functionality to convert images to diagrams.
*/
public interface ImageToDiagram {

/**
* Represents the state of an agent, specifically tailored for managing diagram-related data and processes.
*/
class State extends AgentState {
static Map<String, Channel<?>> SCHEMA = mapOf(
"diagramCode", AppenderChannel.<String>of(ArrayList::new)
);

/**
* Constructs a new `State` object with the provided initialization data.
*
* @param initData a map containing initial state data
*/
public State(Map<String, Object> initData) {
super(initData);
}

/**
* Retrieves the image data as an {@link ImageToDiagramProcess.ImageUrlOrData}.
*
* @return An {@link Optional} containing the image data, or empty if not available.
*/
public Optional<ImageToDiagramProcess.ImageUrlOrData> imageData() {
return value("imageData");
}
/**
* Retrieves the diagram element associated with this object.
*
* @return an {@link Optional} containing the diagram element if it exists, otherwise an empty optional.
*/
public Optional<Diagram.Element> diagram() {
return value("diagram");
}
/**
* Returns a list of diagram code.
* If the "diagramCode" value is not present, returns an empty list.
*
* @return List of String representing the diagram code
*/
public List<String> diagramCode() {
return this.<List<String>>value("diagramCode").orElseGet(Collections::emptyList);
}
/**
* Retrieves the evaluation result as an {@link ImageToDiagramProcess.EvaluationResult}.
*
* @return An {@link Optional} containing the evaluation result, or empty if not available.
*/
public Optional<ImageToDiagramProcess.EvaluationResult> evaluationResult() {
return value("evaluationResult" );
}
/**
* Returns an {@link Optional} containing the value associated with "evaluationError".
*
* @return an {@link Optional} containing the value associated with "evaluationError"
*/
public Optional<String> evaluationError() {
return value("evaluationError" );
}
/**
* Retrieves the type of evaluation error.
*
* @return an {@link Optional} containing the error UML type, or an empty optional if not available
*/
public Optional<ErrorUmlType> evaluationErrorType() {
return value("evaluationErrorType" );
}

/**
* Checks if an error of execution type has occurred.
* @return true if the error type is execution error, false otherwise
**/
public boolean isExecutionError() {
return evaluationErrorType()
.map( type -> type == ErrorUmlType.EXECUTION_ERROR )
.orElse(false);
}

/**
* Checks if the last two diagrams in the code are equal.
*
* @return true if the last two diagrams are equal; false otherwise
*/
public boolean lastTwoDiagramsAreEqual() {
if( diagramCode().size() < 2 ) return false;

Expand All @@ -75,6 +126,12 @@ enum EvaluationResult {
UNKNOWN
}

/**
* Retrieves an instance of the {@link OpenAiChatModel} configured for vision tasks.
*
* @return An {@link OpenAiChatModel} object with predefined settings suitable for processing images and videos.
* @throws IllegalArgumentException if no OPENAI_API_KEY is provided in the system properties.
*/
default OpenAiChatModel getVisionModel( ) {

var openApiKey = ofNullable( System.getProperty("OPENAI_API_KEY") )
Expand All @@ -93,6 +150,12 @@ default OpenAiChatModel getVisionModel( ) {

}

/**
* Returns an instance of the default {@link OpenAiChatModel}.
*
* @return a configured {@link OpenAiChatModel} with the API key obtained from the system property "OPENAI_API_KEY".
* @throws IllegalArgumentException if no OPENAI_API_KEY is provided as a system property.
*/
default OpenAiChatModel getModel( ) {
var openApiKey = ofNullable( System.getProperty("OPENAI_API_KEY") )
.orElseThrow( () -> new IllegalArgumentException("no OPENAI_API_KEY provided!") );
Expand All @@ -109,6 +172,13 @@ default OpenAiChatModel getModel( ) {

}

/**
* Loads a {@link PromptTemplate} from the specified resource name.
*
* @param resourceName the name of the resource file, must not be null or empty
* @return the loaded {@code PromptTemplate}
* @throws Exception if the resource is not found or an error occurs during reading
*/
static PromptTemplate loadPromptTemplate(String resourceName ) throws Exception {
final ClassLoader classLoader = ImageToDiagram.class.getClassLoader();
final InputStream inputStream = classLoader.getResourceAsStream(resourceName);
Expand All @@ -125,4 +195,4 @@ static PromptTemplate loadPromptTemplate(String resourceName ) throws Exception
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,41 @@
import static org.bsc.langgraph4j.action.AsyncNodeAction.node_async;
import static org.bsc.langgraph4j.utils.CollectionsUtils.mapOf;

/**
* Class for processing images to generate diagrams.
*/
@Slf4j( topic="ImageToDiagramProcess" )
public class ImageToDiagramProcess implements ImageToDiagram {

/**
* Represents an image that can be identified by a URL or encapsulated as data.
*
* The {@code ImageUrlOrData} record is designed to hold either a URL pointing to an image
* or the image data itself. It provides two static factory methods for creating instances:
* one from a URL and another from the image data.
* @param url the URI representing the image URL
* @param data the image data as a string
*/
public record ImageUrlOrData(
URI url,
String data
) {
/**
* Creates an instance of {@code ImageUrlOrData} from a given URL.
*
* @param url the URI representing the image URL
* @return a new instance of {@code ImageUrlOrData} with the specified URL and no data
*/
public static ImageUrlOrData of( URI url) {
Objects.requireNonNull(url);
return new ImageUrlOrData(url, null);
}
/**
* Creates a new instance of the {@code ImageUrlOrData} class with the provided data.
*
* @param data image data as a string
* @return a new {@code ImageUrlOrData} instance containing the specified image data
*/
public static ImageUrlOrData of( String data) {
Objects.requireNonNull(data);
return new ImageUrlOrData(null, data );
Expand All @@ -46,6 +70,12 @@ public static ImageUrlOrData of( String data) {
final AsyncEdgeAction<State> routeDiagramTranslation = new RouteDiagramTranslation();


/**
* Generates a workflow graph using the StateGraph framework with specific nodes and conditional edges.
*
* @return A configured StateGraph instance representing the workflow.
* @throws Exception if an error occurs during the configuration of the graph.
*/
public StateGraph<State> workflow() throws Exception {
var stateSerializer = new JSONStateSerializer();

Expand All @@ -68,6 +98,14 @@ public StateGraph<State> workflow() throws Exception {
;
}

/**
* This method orchestrates a workflow for processing diagrams. It initializes a state graph and adds nodes and subgraphs
* to represent different stages of diagram correction and translation. The method also sets up conditional edges based on
* the type of diagram being translated.
*
* @return A StateGraph object representing the complete workflow diagram.
* @throws Exception if any errors occur during the setup of the workflow.
*/
public StateGraph<State> workflowWithCorrection() throws Exception {

var stateSerializer = new JSONStateSerializer();
Expand All @@ -90,12 +128,26 @@ public StateGraph<State> workflowWithCorrection() throws Exception {
.addEdge("evaluate_result", END);
}

/**
* Executes the workflow using the provided image data and returns a stream of NodeOutput objects.
*
* @param imageData The image data to be processed, which must not be null.
* @return A stream of NodeOutput objects generated by the workflow.
* @throws Exception if an error occurs during execution.
*/
public AsyncGenerator<NodeOutput<State>> execute( @NonNull ImageUrlOrData imageData ) throws Exception {
return workflow().compile().stream( Map.of( "imageData", imageData ) );
}

/**
* Executes the workflow with correction for the given image data.
*
* @param imageData The image data to process, cannot be null.
* @return An asynchronous generator of node outputs.
* @throws Exception If an error occurs during execution.
*/
public AsyncGenerator<NodeOutput<State>> executeWithCorrection( @NonNull ImageUrlOrData imageData ) throws Exception {
return workflowWithCorrection().compile().stream( Map.of( "imageData", imageData ) );
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ public DescribeDiagramImage( @NonNull ChatLanguageModel visionModel) {
this.visionModel = visionModel;
}

/**
* Applies the provided image to create a diagram.
*
* @param state The current state of the image to be converted into a diagram.
* @return A map containing the resulting diagram and associated image data.
* @throws Exception if no image data is provided in the state.
*/
@Override
public Map<String, Object> apply(ImageToDiagram.State state) throws Exception {
// Load the prompt template for diagram image description
Expand Down Expand Up @@ -63,4 +70,4 @@ public Map<String, Object> apply(ImageToDiagram.State state) throws Exception {
// Return a map containing the result diagram and image data
return Map.of( "diagram",result, "imageData", new Object() );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ public EvaluateResult(OpenAiChatModel model) {
}

/**
* Overrides the apply method from AsyncNodeAction, which takes an ImageToDiagram.State object
* and processes it asynchronously using a DiagramCorrectionProcess. The method returns a {@code CompletableFuture<Map<String, Object>>}
* that represents the result of the asynchronous processing.
* Converts an ImageToDiagram state to a diagram using the {@link DiagramCorrectionProcess}.
* This method processes the input state asynchronously and returns a {@link CompletableFuture} with
* the resulting diagram data. If no results are generated, it throws a {@link RuntimeException}.
*
* @param state The ImageToDiagram.State object containing the data to be processed.
* @return A {@code CompletableFuture<Map<String, Object>>} representing the outcome of the asynchronous operation.
* @param state The input state for the diagram correction process.
* @return A {@link CompletableFuture} that will contain the diagram data upon successful completion,
* or be completed exceptionally if an error occurs during the processing.
*/
@Override
public CompletableFuture<Map<String, Object>> apply(ImageToDiagram.State state) {
Expand All @@ -63,4 +64,4 @@ public CompletableFuture<Map<String, Object>> apply(ImageToDiagram.State state)
}
return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
*/
public class RouteDiagramTranslation implements AsyncEdgeAction<ImageToDiagram.State> {

/**
* Applies the transformation to convert an {@code ImageToDiagram.State}
* to a diagram type. If the state's diagram type is "sequence" (case-insensitive),
* it returns "sequence"; otherwise, it returns "generic".
*
* @param state The current state of the image-to-diagram conversion process.
* @return A {@link CompletableFuture} that completes with the determined diagram type as a string.
*/
@Override
public CompletableFuture<String> apply(ImageToDiagram.State state) {

Expand All @@ -23,4 +31,4 @@ public CompletableFuture<String> apply(ImageToDiagram.State state) {
return completedFuture(diagramType);

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ public TranslateSequenceDiagramToPlantUML(OpenAiChatModel model) {
this.model = model;
}

/**
* Converts a sequence diagram from an image to PlantUML code.
*
* This method processes the given state containing an image, generates a system prompt,
* and uses a language model to produce PlantUML code representing the diagram.
* If no diagram is provided in the state, it throws an IllegalArgumentException.
*
* @param state The input state containing an image for conversion.
* @return A map with a key "diagramCode" containing the generated PlantUML code.
* @throws Exception if there is an error during the conversion process.
*/
@Override
public Map<String, Object> apply(ImageToDiagram.State state) throws Exception {
Diagram.Element diagram = state.diagram()
Expand All @@ -47,4 +58,4 @@ public Map<String, Object> apply(ImageToDiagram.State state) throws Exception {

return Map.of("diagramCode", result );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,16 @@
*/
public class EventuallyPerformCorrection implements AsyncNodeActionWithConfig<ImageToDiagram.State> {

/**
* Converts an image to a diagram asynchronously.
*
* @param t the current state of the image to be converted
* @param config configuration for the conversion process
* @return a CompletableFuture that will contain a Map object with the resulting diagram or an exception if the conversion fails
* @see ImageToDiagram.State
*/
@Override
public CompletableFuture<Map<String, Object>> apply(ImageToDiagram.State t, RunnableConfig config) {
return null;
}
}
}
Loading

0 comments on commit a6408c3

Please sign in to comment.