Skip to content

Initial CallChatModel implementation #668

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions experimental/ai/impl/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-experimental-ai-parent</artifactId>
<version>8.0.0-SNAPSHOT</version>
</parent>
<artifactId>serverlessworkflow-experimental-ai-impl</artifactId>
<name>ServelessWorkflow:: Experimental:: AI:: Impl</name>
<dependencies>
<dependency>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-experimental-types</artifactId>
</dependency>
<dependency>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-impl-core</artifactId>
</dependency>
<dependency>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-experimental-ai-types</artifactId>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.serverlessworkflow.impl.executors.ai;

import io.serverlessworkflow.ai.api.types.CallAgentAI;
import io.serverlessworkflow.api.types.TaskBase;
import io.serverlessworkflow.impl.TaskContext;
import io.serverlessworkflow.impl.WorkflowApplication;
import io.serverlessworkflow.impl.WorkflowContext;
import io.serverlessworkflow.impl.WorkflowModel;
import io.serverlessworkflow.impl.WorkflowModelFactory;
import io.serverlessworkflow.impl.executors.CallableTask;
import io.serverlessworkflow.impl.resources.ResourceLoader;
import java.util.concurrent.CompletableFuture;

public class AIChatModelCallExecutor implements CallableTask<CallAgentAI> {

@Override
public void init(CallAgentAI task, WorkflowApplication application, ResourceLoader loader) {}

@Override
public CompletableFuture<WorkflowModel> apply(
WorkflowContext workflowContext, TaskContext taskContext, WorkflowModel input) {
WorkflowModelFactory modelFactory = workflowContext.definition().application().modelFactory();
if (taskContext.task() instanceof CallAgentAI agenticAI) {
return CompletableFuture.completedFuture(
modelFactory.fromAny(new CallAgentAIExecutor().execute(agenticAI, input.asJavaObject())));
}
throw new IllegalArgumentException(
"AIChatModelCallExecutor can only process CallAgentAI tasks, but received: "
+ taskContext.task().getClass().getName());
}

@Override
public boolean accept(Class<? extends TaskBase> clazz) {
return CallAgentAI.class.isAssignableFrom(clazz);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.serverlessworkflow.impl.executors.ai;

import io.serverlessworkflow.impl.executors.DefaultTaskExecutorFactory;

public class AIChatModelTaskExecutorFactory extends DefaultTaskExecutorFactory {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.serverlessworkflow.impl.executors.ai;

import dev.langchain4j.agentic.Cognisphere;
import dev.langchain4j.agentic.internal.AgentExecutor;
import dev.langchain4j.agentic.internal.AgentUtil;
import io.serverlessworkflow.ai.api.types.CallAgentAI;
import java.util.Map;

public class CallAgentAIExecutor {

private static final Cognisphere cognisphere = new Cognisphere();

public Object execute(CallAgentAI callAgentAI, Object input) {
AgentExecutor agentExecutor = AgentUtil.agentToExecutor(callAgentAI.getAgentInstance());

Map<String, Object> output = (Map<String, Object>) input;

cognisphere.writeStates(output);

Object result = agentExecutor.invoke(cognisphere);

output.put(callAgentAI.getAgentInstance().outputName(), result);
return output;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.serverlessworkflow.impl.executors.ai.AIChatModelCallExecutor
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.serverlessworkflow.impl.executors.ai.AIChatModelTaskExecutorFactory
18 changes: 18 additions & 0 deletions experimental/ai/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-experimental</artifactId>
<version>8.0.0-SNAPSHOT</version>
</parent>
<artifactId>serverlessworkflow-experimental-ai-parent</artifactId>
<name>ServelessWorkflow:: Experimental:: AI:: Parent</name>
<packaging>pom</packaging>
<modules>
<module>impl</module>
<module>types</module>
</modules>
</project>
65 changes: 65 additions & 0 deletions experimental/ai/types/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-experimental-ai-parent</artifactId>
<version>8.0.0-SNAPSHOT</version>
</parent>
<artifactId>serverlessworkflow-experimental-ai-types</artifactId>
<name>ServelessWorkflow:: Experimental:: AI:: Types</name>

<dependencies>
<dependency>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-experimental-types</artifactId>
</dependency>
<dependency>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-impl-core</artifactId>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-agentic</artifactId>
<version>1.2.0-beta8-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-open-ai</artifactId>
<version>1.2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-experimental-types</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.serverlessworkflow.ai.api.types;

import dev.langchain4j.agentic.AgentServices;
import dev.langchain4j.agentic.internal.AgentInstance;
import dev.langchain4j.model.chat.ChatModel;
import io.serverlessworkflow.api.types.TaskBase;
import java.util.Objects;

public class CallAgentAI extends TaskBase {

private AgentInstance instance;

public static Builder builder() {
return new Builder();
}

public AgentInstance getAgentInstance() {
return instance;
}

public CallAgentAI setAgentInstance(Object object) {
this.instance = (AgentInstance) object;
return this;
}

public static class Builder {

private Class<?> agentClass;

private ChatModel chatModel;

private String outputName;

private Builder() {}

public CallAgentAI.Builder withAgentClass(Class<?> agentClass) {
this.agentClass = agentClass;
return this;
}

public CallAgentAI.Builder withChatModel(ChatModel chatModel) {
this.chatModel = chatModel;
return this;
}

public CallAgentAI.Builder withOutputName(String outputName) {
this.outputName = outputName;
return this;
}

public AgenticInnerBuilder withAgent(Object agent) {
return new AgenticInnerBuilder().withAgent(agent);
}

public CallAgentAI build() {
Objects.requireNonNull(agentClass, "agentClass must be provided");
Objects.requireNonNull(chatModel, "chatModel must be provided");
Objects.requireNonNull(outputName, "outputName must be provided");

if (outputName.isBlank()) {
throw new IllegalArgumentException("outputName must not be blank");
}

Object instance =
AgentServices.agentBuilder(agentClass)
.chatModel(chatModel)
.outputName(outputName)
.build();

return new AgenticInnerBuilder().withAgent(instance).build();
}

public static class AgenticInnerBuilder {

private Object agent;

public AgenticInnerBuilder withAgent(Object agent) {
this.agent = agent;
return this;
}

public CallAgentAI build() {
return new CallAgentAI().setAgentInstance(agent);
}
}
}
}
Loading