Feat/claude websearch support (#5771)

* feat: enhance web search capabilities in AnthropicProvider

- Added support for Claude models in web search functionality with a new regex.
- Implemented logic to retrieve web search parameters and handle web search results.
- Updated message handling to include web search progress and completion states.
- Enhanced citation formatting for web search results from Anthropic source.

* feat: import WebSearchResultBlock for enhanced message handling

- Added import for WebSearchResultBlock from the Anthropic SDK to improve message processing capabilities in messageBlock.ts.
- Removed duplicate import to streamline the code.

* chore: update @anthropic-ai/sdk to version 0.41.0 in package.json and yarn.lock

* Update src/renderer/src/store/messageBlock.ts

Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>

---------

Co-authored-by: Chen Tao <70054568+eeee0717@users.noreply.github.com>
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
This commit is contained in:
SuYao 2025-05-08 21:30:43 +08:00 committed by GitHub
parent e2ffea93af
commit c17fdb81aa
7 changed files with 118 additions and 16 deletions

View File

@ -110,7 +110,7 @@
"@agentic/searxng": "^7.3.3",
"@agentic/tavily": "^7.3.3",
"@ant-design/v5-patch-for-react-19": "^1.0.3",
"@anthropic-ai/sdk": "^0.38.0",
"@anthropic-ai/sdk": "^0.41.0",
"@electron-toolkit/eslint-config-prettier": "^3.0.0",
"@electron-toolkit/eslint-config-ts": "^3.0.0",
"@electron-toolkit/preload": "^3.0.0",

View File

@ -230,6 +230,12 @@ export const FUNCTION_CALLING_REGEX = new RegExp(
`\\b(?!(?:${FUNCTION_CALLING_EXCLUDED_MODELS.join('|')})\\b)(?:${FUNCTION_CALLING_MODELS.join('|')})\\b`,
'i'
)
export const CLAUDE_SUPPORTED_WEBSEARCH_REGEX = new RegExp(
`\\b(?:claude-3(-|\\.)(7|5)-sonnet(?:-[\\w-]+)|claude-3(-|\\.)5-haiku(?:-[\\w-]+))\\b`,
'i'
)
export function isFunctionCallingModel(model: Model): boolean {
if (model.type?.includes('function_calling')) {
return true
@ -2399,6 +2405,10 @@ export function isWebSearchModel(model: Model): boolean {
return false
}
if (model.id.includes('claude')) {
return CLAUDE_SUPPORTED_WEBSEARCH_REGEX.test(model.id)
}
if (provider.type === 'openai') {
if (
isOpenAILLMModel(model) &&

View File

@ -1,7 +1,15 @@
import Anthropic from '@anthropic-ai/sdk'
import { MessageCreateParamsNonStreaming, MessageParam, TextBlockParam } from '@anthropic-ai/sdk/resources'
import {
MessageCreateParamsNonStreaming,
MessageParam,
TextBlockParam,
ToolUnion,
WebSearchResultBlock,
WebSearchTool20250305,
WebSearchToolResultError
} from '@anthropic-ai/sdk/resources'
import { DEFAULT_MAX_TOKENS } from '@renderer/config/constant'
import { isReasoningModel, isVisionModel } from '@renderer/config/models'
import { isReasoningModel, isVisionModel, isWebSearchModel } from '@renderer/config/models'
import { getStoreSetting } from '@renderer/hooks/useSettings'
import i18n from '@renderer/i18n'
import { getAssistantSettings, getDefaultModel, getTopNamingModel } from '@renderer/services/AssistantService'
@ -11,7 +19,16 @@ import {
filterEmptyMessages,
filterUserRoleStartMessages
} from '@renderer/services/MessagesService'
import { Assistant, EFFORT_RATIO, FileTypes, MCPToolResponse, Model, Provider, Suggestion } from '@renderer/types'
import {
Assistant,
EFFORT_RATIO,
FileTypes,
MCPToolResponse,
Model,
Provider,
Suggestion,
WebSearchSource
} from '@renderer/types'
import { ChunkType } from '@renderer/types/chunk'
import type { Message } from '@renderer/types/newMessage'
import { removeSpecialCharactersForTopicName } from '@renderer/utils'
@ -109,6 +126,18 @@ export default class AnthropicProvider extends BaseProvider {
}
}
private async getWebSearchParams(model: Model): Promise<WebSearchTool20250305 | undefined> {
if (!isWebSearchModel(model)) {
return undefined
}
return {
type: 'web_search_20250305',
name: 'web_search',
max_uses: 5
} as WebSearchTool20250305
}
/**
* Get the temperature
* @param assistant - The assistant
@ -201,6 +230,17 @@ export default class AnthropicProvider extends BaseProvider {
}
}
const isEnabledBuiltinWebSearch = assistant.enableWebSearch
const tools: ToolUnion[] = []
if (isEnabledBuiltinWebSearch) {
const webSearchTool = await this.getWebSearchParams(model)
if (webSearchTool) {
tools.push(webSearchTool)
}
}
const body: MessageCreateParamsNonStreaming = {
model: model.id,
messages: userMessages,
@ -211,6 +251,7 @@ export default class AnthropicProvider extends BaseProvider {
system: systemMessage ? [systemMessage] : undefined,
// @ts-ignore thinking
thinking: this.getBudgetToken(assistant, model),
tools: tools,
...this.getCustomParameters(assistant)
}
@ -289,6 +330,34 @@ export default class AnthropicProvider extends BaseProvider {
onChunk({ type: ChunkType.TEXT_DELTA, text })
})
.on('contentBlock', (block) => {
if (block.type === 'server_tool_use' && block.name === 'web_search') {
onChunk({
type: ChunkType.LLM_WEB_SEARCH_IN_PROGRESS
})
} else if (block.type === 'web_search_tool_result') {
if (
block.content &&
(block.content as WebSearchToolResultError).type === 'web_search_tool_result_error'
) {
onChunk({
type: ChunkType.ERROR,
error: {
code: (block.content as WebSearchToolResultError).error_code,
message: (block.content as WebSearchToolResultError).error_code
}
})
} else {
onChunk({
type: ChunkType.LLM_WEB_SEARCH_COMPLETE,
llm_web_search: {
results: block.content as Array<WebSearchResultBlock>,
source: WebSearchSource.ANTHROPIC
}
})
}
}
})
.on('thinking', (thinking) => {
hasThinkingContent = true
const currentTime = new Date().getTime() // Get current time for each chunk

View File

@ -1,3 +1,4 @@
import { WebSearchResultBlock } from '@anthropic-ai/sdk/resources'
import type { GroundingMetadata } from '@google/genai'
import { createEntityAdapter, createSelector, createSlice, type PayloadAction } from '@reduxjs/toolkit'
import type { Citation } from '@renderer/pages/home/Messages/CitationsList'
@ -136,6 +137,26 @@ const formatCitationsFromBlock = (block: CitationMessageBlock | undefined): Cita
}
}) || []
break
case WebSearchSource.ANTHROPIC:
formattedCitations =
(block.response.results as Array<WebSearchResultBlock>)?.map((result, index) => {
const {url} = result
let hostname: string | undefined
try {
hostname = new URL(url).hostname
} catch {
hostname = url
}
return {
number: index + 1,
url: url,
title: result.title,
hostname: hostname,
showFavicon: true,
type: 'websearch'
}
}) || []
break
case WebSearchSource.OPENROUTER:
case WebSearchSource.PERPLEXITY:
formattedCitations =

View File

@ -483,6 +483,12 @@ const fetchAndProcessAssistantResponseImpl = async (
console.error('[onExternalToolComplete] citationBlockId is null. Cannot update.')
}
},
onLLMWebSearchInProgress: () => {
const citationBlock = createCitationBlock(assistantMsgId, {}, { status: MessageBlockStatus.PROCESSING })
citationBlockId = citationBlock.id
handleBlockTransition(citationBlock, MessageBlockType.CITATION)
saveUpdatedBlockToDB(citationBlock.id, assistantMsgId, topicId, getState)
},
onLLMWebSearchComplete(llmWebSearchResult) {
if (citationBlockId) {
const changes: Partial<CitationMessageBlock> = {

View File

@ -1,3 +1,4 @@
import type { WebSearchResultBlock } from '@anthropic-ai/sdk/resources'
import type { GroundingMetadata } from '@google/genai'
import type OpenAI from 'openai'
import React from 'react'
@ -450,6 +451,7 @@ export type WebSearchResults =
| GroundingMetadata
| OpenAI.Chat.Completions.ChatCompletionMessage.Annotation.URLCitation[]
| OpenAI.Responses.ResponseOutputText.URLCitation[]
| WebSearchResultBlock[]
| any[]
export enum WebSearchSource {
@ -457,6 +459,7 @@ export enum WebSearchSource {
OPENAI = 'openai',
OPENAI_COMPATIBLE = 'openai-compatible',
OPENROUTER = 'openrouter',
ANTHROPIC = 'anthropic',
GEMINI = 'gemini',
PERPLEXITY = 'perplexity',
QWEN = 'qwen',

View File

@ -176,9 +176,9 @@ __metadata:
languageName: node
linkType: hard
"@anthropic-ai/sdk@npm:^0.38.0":
version: 0.38.0
resolution: "@anthropic-ai/sdk@npm:0.38.0"
"@anthropic-ai/sdk@npm:^0.41.0":
version: 0.41.0
resolution: "@anthropic-ai/sdk@npm:0.41.0"
dependencies:
"@types/node": "npm:^18.11.18"
"@types/node-fetch": "npm:^2.6.4"
@ -187,7 +187,7 @@ __metadata:
form-data-encoder: "npm:1.7.2"
formdata-node: "npm:^4.3.2"
node-fetch: "npm:^2.6.7"
checksum: 10c0/accd003cbe314d32d4d36f5fd7fd743c32e2a896c9ea57190966eda20b8c46e00f542bf03ec3603d1274a7ac18e902bed4158ff5980e4e248a6d5c75e3fd891a
checksum: 10c0/b57df42df33f29f3baa682da663d7411624f511384f84c790a936b505c5074d9676749ce05d45b1caa9af7077a20013889efb65f3e25f979e7ab2fa245e2a444
languageName: node
linkType: hard
@ -1488,13 +1488,6 @@ __metadata:
languageName: node
linkType: hard
"@google/generative-ai@npm:^0.24.1":
version: 0.24.1
resolution: "@google/generative-ai@npm:0.24.1"
checksum: 10c0/8da77fc648b04fc2ecef53e75230e2ee67a8fd29a34b6b8874e77a7332b2a1e4b51d44dd9eb604fb063ed8ea46d293aac5b1e2a955ae2435e2582a265f2cb80d
languageName: node
linkType: hard
"@hello-pangea/dnd@npm:^16.6.0":
version: 16.6.0
resolution: "@hello-pangea/dnd@npm:16.6.0"
@ -4378,7 +4371,7 @@ __metadata:
"@agentic/searxng": "npm:^7.3.3"
"@agentic/tavily": "npm:^7.3.3"
"@ant-design/v5-patch-for-react-19": "npm:^1.0.3"
"@anthropic-ai/sdk": "npm:^0.38.0"
"@anthropic-ai/sdk": "npm:^0.41.0"
"@cherrystudio/embedjs": "npm:^0.1.28"
"@cherrystudio/embedjs-libsql": "npm:^0.1.28"
"@cherrystudio/embedjs-loader-csv": "npm:^0.1.28"