Skip to content

Commit

Permalink
Handle misconfigured tools (#1054)
Browse files Browse the repository at this point in the history
## Issue
#983

## Change
When we configure a tool by using a `Class` instance, we get an
exception because only object instances are allowed.


## General checklist
<!-- Please double-check the following points and mark them like this:
[X] -->
- [ ] There are no breaking changes
- [ ] I have added unit and integration tests for my change
- [ ] I have manually run all the unit and integration tests in the
module I have added/changed, and they are all green
- [ ] I have manually run all the unit and integration tests in the
[core](https://github.com/langchain4j/langchain4j/tree/main/langchain4j-core)
and
[main](https://github.com/langchain4j/langchain4j/tree/main/langchain4j)
modules, and they are all green
<!-- Before adding documentation and example(s) (below), please wait
until the PR is reviewed and approved. -->
- [ ] I have added/updated the
[documentation](https://github.com/langchain4j/langchain4j/tree/main/docs/docs)
- [ ] I have added an example in the [examples
repo](https://github.com/langchain4j/langchain4j-examples) (only for
"big" features)


## Checklist for adding new model integration
<!-- Please double-check the following points and mark them like this:
[X] -->
- [ ] I have added my new module in the
[BOM](https://github.com/langchain4j/langchain4j/blob/main/langchain4j-bom/pom.xml)


## Checklist for adding new embedding store integration
<!-- Please double-check the following points and mark them like this:
[X] -->
- [ ] I have added a `{NameOfIntegration}EmbeddingStoreIT` that extends
from either `EmbeddingStoreIT` or `EmbeddingStoreWithFilteringIT`
- [ ] I have added my new module in the
[BOM](https://github.com/langchain4j/langchain4j/blob/main/langchain4j-bom/pom.xml)


## Checklist for changing existing embedding store integration
<!-- Please double-check the following points and mark them like this:
[X] -->
- [ ] I have manually verified that the
`{NameOfIntegration}EmbeddingStore` works correctly with the data
persisted using the latest released version of LangChain4j

---------

Co-authored-by: LangChain4j <langchain4j@gmail.com>
  • Loading branch information
anunnakian and langchain4j committed May 17, 2024
1 parent d365ca4 commit e60c49d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,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());
}
}

0 comments on commit e60c49d

Please sign in to comment.