forked from langchain4j/langchain4j-spring
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Publish a Spring Event after registering the AiService Bean in AiServ…
…icesAutoConfig. This event contains the AiService class and its corresponding tools description information. Once a user implements the AiServiceRegisteredEventListener to listen for this event, they can receive the event during the Spring Boot startup phase and handle their business logic as needed. original PR link: langchain4j#77 Issues link: langchain4j/langchain4j#2112
- Loading branch information
1 parent
a803860
commit 4f5c678
Showing
6 changed files
with
144 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
26 changes: 26 additions & 0 deletions
26
...-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,26 @@ | ||
package dev.langchain4j.service.spring.event; | ||
|
||
import dev.langchain4j.agent.tool.ToolSpecification; | ||
import org.springframework.context.ApplicationEvent; | ||
|
||
import java.util.List; | ||
|
||
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 = toolSpecifications; | ||
} | ||
|
||
public Class<?> getAiServiceClass() { | ||
return aiServiceClass; | ||
} | ||
|
||
public List<ToolSpecification> getToolSpecifications() { | ||
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.getAiServiceClass(); | ||
List<ToolSpecification> toolSpecifications = event.getToolSpecifications(); | ||
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(); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...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,7 @@ | ||
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> { | ||
} |