-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes langchain4j/langchain4j#2112 Publish a `AiServiceRegisteredEvent` Spring Event after registering the `AiService` bean in `AiServicesAutoConfig`. This event contains the `AiService` class and its corresponding tools description information. Once a user implements the even listener to listen for this event, they can receive the event during the Spring Boot startup phase and handle their business logic as needed.
- Loading branch information
1 parent
3bdf8f2
commit a4280ba
Showing
6 changed files
with
139 additions
and
9 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
28 changes: 28 additions & 0 deletions
28
...-starter/src/main/java/dev/langchain4j/service/spring/event/AiServiceRegisteredEvent.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.event; | ||
|
||
import dev.langchain4j.agent.tool.ToolSpecification; | ||
import org.springframework.context.ApplicationEvent; | ||
|
||
import java.util.List; | ||
|
||
import static dev.langchain4j.internal.Utils.copyIfNotNull; | ||
|
||
public class AiServiceRegisteredEvent extends ApplicationEvent { | ||
|
||
private final Class<?> aiServiceClass; | ||
private final List<ToolSpecification> toolSpecifications; | ||
|
||
public AiServiceRegisteredEvent(Object source, Class<?> aiServiceClass, List<ToolSpecification> toolSpecifications) { | ||
super(source); | ||
this.aiServiceClass = aiServiceClass; | ||
this.toolSpecifications = copyIfNotNull(toolSpecifications); | ||
} | ||
|
||
public Class<?> aiServiceClass() { | ||
return aiServiceClass; | ||
} | ||
|
||
public List<ToolSpecification> toolSpecifications() { | ||
return toolSpecifications; | ||
} | ||
} |
16 changes: 15 additions & 1 deletion
16
...ev/langchain4j/service/spring/mode/automatic/withTools/AiServiceWithToolsApplication.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 |
---|---|---|
@@ -1,12 +1,26 @@ | ||
package dev.langchain4j.service.spring.mode.automatic.withTools; | ||
|
||
import dev.langchain4j.agent.tool.ToolSpecification; | ||
import dev.langchain4j.service.spring.event.AiServiceRegisteredEvent; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.context.ApplicationListener; | ||
|
||
import java.util.List; | ||
|
||
@SpringBootApplication | ||
class AiServiceWithToolsApplication { | ||
class AiServiceWithToolsApplication implements ApplicationListener<AiServiceRegisteredEvent> { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(AiServiceWithToolsApplication.class, args); | ||
} | ||
|
||
@Override | ||
public void onApplicationEvent(AiServiceRegisteredEvent event) { | ||
Class<?> aiServiceClass = event.aiServiceClass(); | ||
List<ToolSpecification> toolSpecifications = event.toolSpecifications(); | ||
for (int i = 0; i < toolSpecifications.size(); i++) { | ||
System.out.printf("[%s]: [Tool-%s]: %s%n", aiServiceClass.getSimpleName(), i + 1, toolSpecifications.get(i)); | ||
} | ||
} | ||
} |
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
24 changes: 24 additions & 0 deletions
24
...chain4j/service/spring/mode/automatic/withTools/listener/AbstractApplicationListener.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,24 @@ | ||
package dev.langchain4j.service.spring.mode.automatic.withTools.listener; | ||
|
||
import org.springframework.context.ApplicationEvent; | ||
import org.springframework.context.ApplicationListener; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class AbstractApplicationListener<E extends ApplicationEvent> implements ApplicationListener<E> { | ||
private final List<E> receivedEvents = new ArrayList<>(); | ||
|
||
@Override | ||
public void onApplicationEvent(E event) { | ||
receivedEvents.add(event); | ||
} | ||
|
||
public List<E> getReceivedEvents() { | ||
return receivedEvents; | ||
} | ||
|
||
public boolean isEventReceived() { | ||
return !receivedEvents.isEmpty(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...4j/service/spring/mode/automatic/withTools/listener/AiServiceRegisteredEventListener.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,8 @@ | ||
package dev.langchain4j.service.spring.mode.automatic.withTools.listener; | ||
|
||
import dev.langchain4j.service.spring.event.AiServiceRegisteredEvent; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class AiServiceRegisteredEventListener extends AbstractApplicationListener<AiServiceRegisteredEvent> { | ||
} |