Skip to content

feat(anthropic): Implemented a web search tool, provided by Anthropic #3353

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

dev-jonghoonpark
Copy link
Contributor

@dev-jonghoonpark dev-jonghoonpark commented May 27, 2025

Implemented a web search tool, provided by Anthropic
This web search tool is categorized as a server tool.

related doc: https://docs.anthropic.com/en/docs/agents-and-tools/tool-use/web-search-tool

Changes

The serverTools property has been added to AnthropicChatOptions.

It can be used as follows:

AnthropicChatOptions.builder()
    .serverTools(List.of(WebSearchTool.builder().maxUses(1).build()))
    .build();

Note 1

I'm not sure if this is a temporary issue, but I was occasionally getting a 529 error (overloaded_error: Anthropic's API is temporarily overloaded.) during development.

related doc: https://docs.anthropic.com/en/api/errors

Note 2

The stream part was not developed in this PR.

@dev-jonghoonpark dev-jonghoonpark force-pushed the anthropic-web-search-tool branch 2 times, most recently from d086f6e to 9efd6a5 Compare May 28, 2025 13:39
@markpollack
Copy link
Member

We will review, then we can discuss the streaming support.

@dev-jonghoonpark dev-jonghoonpark force-pushed the anthropic-web-search-tool branch from 9efd6a5 to 3aafc80 Compare June 3, 2025 09:56
@@ -526,22 +540,31 @@ else if (message.getMessageType() == MessageType.TOOL) {

// Add the tool definitions to the request's tools parameter.
List<ToolDefinition> toolDefinitions = this.toolCallingManager.resolveToolDefinitions(requestOptions);
Stream<Tool> toolStream = Stream.empty();
if (!CollectionUtils.isEmpty(toolDefinitions)) {
request = ModelOptionsUtils.merge(request, this.defaultOptions, ChatCompletionRequest.class);
Copy link
Contributor Author

@dev-jonghoonpark dev-jonghoonpark Jun 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

request = ModelOptionsUtils.merge(request, this.defaultOptions, ChatCompletionRequest.class);

I'm not sure why this code is needed or what it does.
I kept it to preserve the existing logic.

If I can understand why this is necessary, I think I can improve the code further.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dev-jonghoonpark Good point. This line makes sure to update the ChatCompletionRequest with any of the missing chat options which can be set via default chat options.

Though we use the default options to create the ChatCompletion Request:

ChatCompletionRequest request = new ChatCompletionRequest(this.defaultOptions.getModel(), userMessages,
				systemPrompt, this.defaultOptions.getMaxTokens(), this.defaultOptions.getTemperature(), stream);

not all the properties are updated in this and the model options utils helps merging any of the missing ones.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ilayaperumalg Thank you for the explanation.
However, why is it called only when !CollectionUtils.isEmpty(toolDefinitions) is true?

This web search tool is categorized as a `server tool`.
related doc: https://docs.anthropic.com/en/docs/agents-and-tools/tool-use/web-search-tool

Signed-off-by: jonghoonpark <[email protected]>
@dev-jonghoonpark dev-jonghoonpark force-pushed the anthropic-web-search-tool branch from 3aafc80 to 936505c Compare June 3, 2025 10:28
@dev-jonghoonpark
Copy link
Contributor Author

Resolve conflicts with PR: #3365

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants