Skip to content

Commit

Permalink
added test for explicit tool wiring
Browse files Browse the repository at this point in the history
  • Loading branch information
langchain4j committed Nov 14, 2024
1 parent 1cf87b8 commit c6c183b
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package dev.langchain4j.service.spring.mode.explicit.tools;

import dev.langchain4j.service.spring.AiService;

import static dev.langchain4j.service.spring.AiServiceWiringMode.EXPLICIT;

@AiService(wiringMode = EXPLICIT, chatModel = "openAiChatModel", tools = {"tools1"})
public interface AiServiceWithExplicitTools {

String chat(String userMessage);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dev.langchain4j.service.spring.mode.explicit.tools;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
class AiServiceWithExplicitToolsApplication {

public static void main(String[] args) {
SpringApplication.run(AiServiceWithExplicitToolsApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package dev.langchain4j.service.spring.mode.explicit.tools;

import dev.langchain4j.service.spring.AiServicesAutoConfig;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;

import static dev.langchain4j.service.spring.mode.explicit.tools.Tools1.TOOL_1_TEMPERATURE;
import static dev.langchain4j.service.spring.mode.explicit.tools.Tools2.TOOL_2_TEMPERATURE;
import static org.assertj.core.api.Assertions.assertThat;

class AiServiceWithExplicitToolsIT {

ApplicationContextRunner contextRunner = new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(AiServicesAutoConfig.class));

@Test
void should_create_AI_service_with_explicit_tools() {
contextRunner
.withPropertyValues(
"langchain4j.open-ai.chat-model.api-key=" + System.getenv("OPENAI_API_KEY"),
"langchain4j.open-ai.chat-model.max-tokens=100",
"langchain4j.open-ai.chat-model.temperature=0.0")
.withUserConfiguration(AiServiceWithExplicitToolsApplication.class)
.run(context -> {

// given
AiServiceWithExplicitTools aiService = context.getBean(AiServiceWithExplicitTools.class);

// when
String answer = aiService.chat("What is the temperature?");

// then
assertThat(answer).contains(TOOL_1_TEMPERATURE).doesNotContain(TOOL_2_TEMPERATURE);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package dev.langchain4j.service.spring.mode.explicit.tools;

import dev.langchain4j.agent.tool.Tool;
import org.springframework.stereotype.Component;

@Component
class Tools1 {

public static final String TOOL_1_TEMPERATURE = "6";

@Tool
String getCurrentTemperature() {
return TOOL_1_TEMPERATURE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package dev.langchain4j.service.spring.mode.explicit.tools;

import dev.langchain4j.agent.tool.Tool;
import org.springframework.stereotype.Component;

@Component
class Tools2 {

public static final String TOOL_2_TEMPERATURE = "9";

@Tool
String getCurrentTemperature() {
return TOOL_2_TEMPERATURE;
}
}

0 comments on commit c6c183b

Please sign in to comment.