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 0e202509d3
commit 8550e5be98
7 changed files with 118 additions and 16 deletions
+10
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) &&
@@ -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
+21
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 =
@@ -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> = {
+3
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',