-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] support tools enhanced by AOP (#80)
Issues link: langchain4j/langchain4j#2113
- Loading branch information
1 parent
10c6cdc
commit f934f16
Showing
7 changed files
with
188 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...c/test/java/dev/langchain4j/service/spring/mode/automatic/withTools/AopEnhancedTools.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package dev.langchain4j.service.spring.mode.automatic.withTools; | ||
|
||
import dev.langchain4j.agent.tool.Tool; | ||
import dev.langchain4j.service.spring.mode.automatic.withTools.aop.ToolObserver; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class AopEnhancedTools { | ||
|
||
public static final String TOOL_OBSERVER_PACKAGE_NAME_DESCRIPTION = | ||
"Find the package directory where @ToolObserver is located."; | ||
public static final String TOOL_OBSERVER_PACKAGE_NAME = ToolObserver.class.getPackageName(); | ||
|
||
public static final String TOOL_OBSERVER_KEY_NAME_DESCRIPTION = | ||
"Find the key name of @ToolObserver"; | ||
public static final String TOOL_OBSERVER_KEY = "AOP_ENHANCED_TOOLS_SUPPORT_@_1122"; | ||
|
||
@Tool(TOOL_OBSERVER_PACKAGE_NAME_DESCRIPTION) | ||
public String getToolObserverPackageName() { | ||
return TOOL_OBSERVER_PACKAGE_NAME; | ||
} | ||
|
||
@ToolObserver(key = TOOL_OBSERVER_KEY) | ||
@Tool(TOOL_OBSERVER_KEY_NAME_DESCRIPTION) | ||
public String getToolObserverKey() { | ||
return TOOL_OBSERVER_KEY; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...c/test/java/dev/langchain4j/service/spring/mode/automatic/withTools/aop/ToolObserver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package dev.langchain4j.service.spring.mode.automatic.withTools.aop; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target({ElementType.METHOD}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface ToolObserver { | ||
|
||
/** | ||
* key just for example | ||
* | ||
* @return the key | ||
*/ | ||
String key(); | ||
} |
47 changes: 47 additions & 0 deletions
47
.../java/dev/langchain4j/service/spring/mode/automatic/withTools/aop/ToolObserverAspect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package dev.langchain4j.service.spring.mode.automatic.withTools.aop; | ||
|
||
import dev.langchain4j.agent.tool.Tool; | ||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.Around; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.reflect.MethodSignature; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
@Aspect | ||
@Component | ||
public class ToolObserverAspect { | ||
|
||
private final List<String> observedTools = new ArrayList<>(); | ||
|
||
@Around("@annotation(toolObserver)") | ||
public Object around(ProceedingJoinPoint joinPoint, ToolObserver toolObserver) throws Throwable { | ||
var signature = (MethodSignature) joinPoint.getSignature(); | ||
var method = signature.getMethod(); | ||
String methodName = method.getName(); | ||
if (method.isAnnotationPresent(Tool.class)) { | ||
Tool toolAnnotation = method.getAnnotation(Tool.class); | ||
observedTools.addAll(Arrays.asList(toolAnnotation.value())); | ||
System.out.printf("Found @Tool %s for method: %s%n%n", Arrays.toString(toolAnnotation.value()), methodName); | ||
} | ||
Object result = joinPoint.proceed(); | ||
System.out.printf(" | key: %s%n | Method name: %s%n | Method arguments: %s%n | Return type: %s%n | Method return value: %s%n%n", | ||
toolObserver.key(), | ||
methodName, | ||
Arrays.toString(joinPoint.getArgs()), | ||
method.getReturnType().getName(), | ||
result); | ||
return result; | ||
} | ||
|
||
public boolean aspectHasBeenCalled() { | ||
return !observedTools.isEmpty(); | ||
} | ||
|
||
public List<String> getObservedTools() { | ||
return observedTools; | ||
} | ||
} |