Skip to content

Commit

Permalink
[FEATURE] Get all metadata of AiService & Tool when the Spring Boot s…
Browse files Browse the repository at this point in the history
  • Loading branch information
catofdestruction committed Nov 18, 2024
1 parent c7a57e7 commit 4e5cc40
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@
import org.springframework.context.annotation.Bean;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

import dev.langchain4j.agent.tool.ToolSpecification;
import dev.langchain4j.service.spring.event.AiServiceRegisteredEvent;
import dev.langchain4j.service.spring.event.AiServiceRegisteredEventListener;
import dev.langchain4j.service.spring.mode.automatic.withTools.listener.AbstractApplicationListener;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Import;

import java.util.List;

@Import(AbstractApplicationListener.class)
@SpringBootApplication
class AiServiceWithToolsApplication implements AiServiceRegisteredEventListener {
class AiServiceWithToolsApplication implements ApplicationListener<AiServiceRegisteredEvent> {

public static void main(String[] args) {
SpringApplication.run(AiServiceWithToolsApplication.class, args);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
package dev.langchain4j.service.spring.mode.automatic.withTools;

import dev.langchain4j.agent.tool.ToolSpecification;
import dev.langchain4j.service.spring.AiServicesAutoConfig;
import dev.langchain4j.service.spring.event.AiServiceRegisteredEvent;
import dev.langchain4j.service.spring.mode.automatic.withTools.listener.AiServiceRegisteredEventListener;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;

import java.util.List;

import static dev.langchain4j.service.spring.mode.ApiKeys.OPENAI_API_KEY;
import static dev.langchain4j.service.spring.mode.automatic.withTools.PackagePrivateTools.CURRENT_TIME;
import static dev.langchain4j.service.spring.mode.automatic.withTools.PublicTools.CURRENT_DATE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

class AiServicesAutoConfigIT {

Expand Down Expand Up @@ -61,6 +69,30 @@ void should_create_AI_service_with_tool_that_is_package_private_method_in_packag
});
}

@Test
void should_receive_ai_service_registered_event() {
contextRunner
.withUserConfiguration(AiServiceWithToolsApplication.class)
.run(context -> {

// given
AiServiceRegisteredEventListener listener = context.getBean(AiServiceRegisteredEventListener.class);

// then should receive AiServiceRegisteredEvent
assertTrue(listener.isEventReceived());
assertEquals(1, listener.getReceivedEvents().size());

AiServiceRegisteredEvent event = listener.getReceivedEvents().stream().findFirst().orElse(null);
assertNotNull(event);
assertEquals(AiServiceWithTools.class, event.getAiServiceClass());
assertEquals(2, event.getToolSpecifications().size());

List<String> tools = event.getToolSpecifications().stream().map(ToolSpecification::name).toList();
assertTrue(tools.contains("getCurrentDate"));
assertTrue(tools.contains("getCurrentTime"));
});
}

// TODO tools which are not @Beans?
// TODO negative cases
// TODO no @AiServices in app, just models
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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 boolean eventReceived = false;
private final List<E> receivedEvents = new ArrayList<>();

@Override
public void onApplicationEvent(E event) {
eventReceived = true;
receivedEvents.add(event);
}

public List<E> getReceivedEvents() {
return receivedEvents;
}

public boolean isEventReceived() {
return eventReceived;
}

public void reset() {
eventReceived = false;
receivedEvents.clear();
}
}
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> {
}

0 comments on commit 4e5cc40

Please sign in to comment.