16
16
17
17
package io .serverlessworkflow .impl .executors .ai ;
18
18
19
- import java .util .ArrayList ;
20
- import java .util .HashSet ;
21
- import java .util .List ;
22
- import java .util .Map ;
23
- import java .util .ServiceLoader ;
24
- import java .util .Set ;
25
- import java .util .concurrent .CompletableFuture ;
26
- import java .util .regex .Matcher ;
27
- import java .util .regex .Pattern ;
28
-
29
19
import dev .langchain4j .data .message .AiMessage ;
30
20
import dev .langchain4j .data .message .ChatMessage ;
31
21
import dev .langchain4j .data .message .SystemMessage ;
34
24
import dev .langchain4j .model .chat .response .ChatResponse ;
35
25
import dev .langchain4j .model .output .FinishReason ;
36
26
import dev .langchain4j .model .output .TokenUsage ;
27
+ import io .serverlessworkflow .ai .api .types .CallAILangChainChatModel ;
37
28
import io .serverlessworkflow .api .types .TaskBase ;
38
29
import io .serverlessworkflow .api .types .ai .CallAIChatModel ;
39
30
import io .serverlessworkflow .impl .TaskContext ;
44
35
import io .serverlessworkflow .impl .executors .CallableTask ;
45
36
import io .serverlessworkflow .impl .resources .ResourceLoader ;
46
37
import io .serverlessworkflow .impl .services .ChatModelService ;
47
-
38
+ import java .util .ArrayList ;
39
+ import java .util .HashSet ;
40
+ import java .util .List ;
41
+ import java .util .Map ;
42
+ import java .util .ServiceLoader ;
43
+ import java .util .Set ;
44
+ import java .util .concurrent .CompletableFuture ;
45
+ import java .util .regex .Matcher ;
46
+ import java .util .regex .Pattern ;
48
47
49
48
public class AIChatModelCallExecutor implements CallableTask <CallAIChatModel > {
50
49
51
50
private static final Pattern VARIABLE_PATTERN = Pattern .compile ("\\ {\\ {\\ s*(.+?)\\ s*\\ }\\ }" );
52
51
53
52
@ Override
54
- public void init (CallAIChatModel task , WorkflowApplication application , ResourceLoader loader ) {
55
-
56
- }
53
+ public void init (CallAIChatModel task , WorkflowApplication application , ResourceLoader loader ) {}
57
54
58
55
@ Override
59
- public CompletableFuture <WorkflowModel > apply (WorkflowContext workflowContext , TaskContext taskContext , WorkflowModel input ) {
56
+ public CompletableFuture <WorkflowModel > apply (
57
+ WorkflowContext workflowContext , TaskContext taskContext , WorkflowModel input ) {
60
58
WorkflowModelFactory modelFactory = workflowContext .definition ().application ().modelFactory ();
59
+ if (taskContext .task () instanceof CallAILangChainChatModel callAILangChainChatModel ) {
60
+ return CompletableFuture .completedFuture (
61
+ modelFactory .fromAny (doCall (callAILangChainChatModel , input .asJavaObject ())));
62
+ }
63
+
61
64
if (taskContext .task () instanceof CallAIChatModel callAIChatModel ) {
62
- return CompletableFuture .completedFuture (modelFactory .fromAny (doCall (callAIChatModel , input .asJavaObject ())));
65
+ return CompletableFuture .completedFuture (
66
+ modelFactory .fromAny (doCall (callAIChatModel , input .asJavaObject ())));
63
67
}
64
- throw new IllegalArgumentException ("AIChatModelCallExecutor can only process CallAIChatModel tasks, but received: " + taskContext .task ().getClass ().getName ());
68
+ throw new IllegalArgumentException (
69
+ "AIChatModelCallExecutor can only process CallAIChatModel tasks, but received: "
70
+ + taskContext .task ().getClass ().getName ());
65
71
}
66
72
67
73
@ Override
68
74
public boolean accept (Class <? extends TaskBase > clazz ) {
69
75
return CallAIChatModel .class .isAssignableFrom (clazz );
70
76
}
71
77
78
+ private Object doCall (CallAILangChainChatModel callAIChatModel , Object javaObject ) {
79
+ ChatModel chatModel = callAIChatModel .getChatModel ();
80
+ Class <?> chatModelRequest = callAIChatModel .getChatModelRequest ();
81
+ }
82
+
72
83
private Object doCall (CallAIChatModel callAIChatModel , Object javaObject ) {
73
84
validate (callAIChatModel , javaObject );
74
85
ChatModel chatModel = createChatModel (callAIChatModel );
@@ -114,7 +125,8 @@ private ChatModel createChatModel(CallAIChatModel callAIChatModel) {
114
125
if (chatModelService != null ) {
115
126
return chatModelService .getChatModel (callAIChatModel .getPreferences ());
116
127
}
117
- throw new IllegalStateException ("No LLM models found. Please ensure that you have the required dependencies in your classpath." );
128
+ throw new IllegalStateException (
129
+ "No LLM models found. Please ensure that you have the required dependencies in your classpath." );
118
130
}
119
131
120
132
private ChatModelService getAvailableModel () {
@@ -124,7 +136,8 @@ private ChatModelService getAvailableModel() {
124
136
return service ;
125
137
}
126
138
127
- throw new IllegalStateException ("No LLM models found. Please ensure that you have the required dependencies in your classpath." );
139
+ throw new IllegalStateException (
140
+ "No LLM models found. Please ensure that you have the required dependencies in your classpath." );
128
141
}
129
142
130
143
private Map <String , Object > prepareResponse (ChatResponse response , Object javaObject ) {
0 commit comments