Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle misconfigured tools #1054

Merged
merged 6 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,10 @@ public AiServices<T> tools(List<Object> objectsWithTools) { // TODO Collection?
context.toolExecutors = new HashMap<>();

for (Object objectWithTool : objectsWithTools) {
if (objectWithTool instanceof Class) {
throw illegalConfiguration("Tool '%s' must be an object, not a class", objectWithTool);
}

for (Method method : objectWithTool.getClass().getDeclaredMethods()) {
if (method.isAnnotationPresent(Tool.class)) {
ToolSpecification toolSpecification = toolSpecificationFrom(method);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package dev.langchain4j.service;

import dev.langchain4j.agent.tool.Tool;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.exception.IllegalConfigurationException;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.mock.ChatModelMock;
import dev.langchain4j.model.output.Response;
import dev.langchain4j.rag.RetrievalAugmentor;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.retriever.Retriever;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.mockito.Spy;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -100,4 +106,26 @@ public void testRetrievalAugmentorAndContentRetriever() {
});
}

static class HelloWorld {

@Tool("Say hello")
void add(String name) {
System.out.printf("Hello %s!", name);
}
}

interface Assistant {

Response<AiMessage> chat(String userMessage);
}

@Test
public void should_raise_an_error_when_tools_are_classes() {
ChatLanguageModel chatLanguageModel = ChatModelMock.thatAlwaysResponds("Hello there!");

assertThrows(IllegalConfigurationException.class, () -> AiServices.builder(Assistant.class)
.chatLanguageModel(chatLanguageModel)
.tools(HelloWorld.class)
.build());
}
}