From e9c8e5fd3ddaf26645e6129e7b3047fb091f6225 Mon Sep 17 00:00:00 2001 From: aivf_baisheng Date: Tue, 2 Dec 2025 14:19:28 +0800 Subject: [PATCH 1/5] ws-296 update the anthropic-connector, update the endpoint and removing unnecessary one --- connectors-endpoints/anthropic-claude2.json | 15 -------- connectors-endpoints/anthropic-claude4.5.json | 14 +++++++ connectors/anthropic-connector.py | 38 +++++++++++++++---- 3 files changed, 45 insertions(+), 22 deletions(-) delete mode 100644 connectors-endpoints/anthropic-claude2.json create mode 100644 connectors-endpoints/anthropic-claude4.5.json diff --git a/connectors-endpoints/anthropic-claude2.json b/connectors-endpoints/anthropic-claude2.json deleted file mode 100644 index f98f8d64..00000000 --- a/connectors-endpoints/anthropic-claude2.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "name": "Anthropic-Claude2", - "connector_type": "anthropic-connector", - "uri": "", - "token": "", - "max_calls_per_second": 1, - "max_concurrency": 1, - "model": "claude-2", - "params": { - "timeout": 300, - "max_attempts": 3, - "temperature": 0.5, - "max_tokens_to_sample": 300 - } -} \ No newline at end of file diff --git a/connectors-endpoints/anthropic-claude4.5.json b/connectors-endpoints/anthropic-claude4.5.json new file mode 100644 index 00000000..83503c4e --- /dev/null +++ b/connectors-endpoints/anthropic-claude4.5.json @@ -0,0 +1,14 @@ +{ + "name": "Anthropic-Claude4.5", + "connector_type": "anthropic-connector", + "uri": "", + "token": "", + "max_calls_per_second": 1, + "max_concurrency": 1, + "model": "claude-sonnet-4-5-20250929", + "params": { + "timeout": 300, + "max_attempts": 3, + "temperature": 0.0 + } + } \ No newline at end of file diff --git a/connectors/anthropic-connector.py b/connectors/anthropic-connector.py index 2f7b0a9a..89e9df7e 100644 --- a/connectors/anthropic-connector.py +++ b/connectors/anthropic-connector.py @@ -1,8 +1,7 @@ import os import anthropic -from anthropic import AI_PROMPT, HUMAN_PROMPT -from anthropic.types import Completion +from anthropic.types import Message from moonshot.src.connectors.connector import Connector, perform_retry from moonshot.src.connectors.connector_response import ConnectorResponse from moonshot.src.connectors_endpoints.connector_endpoint_arguments import ( @@ -38,16 +37,29 @@ async def get_response(self, prompt: str) -> ConnectorResponse: ConnectorResponse: An object containing the text response generated by the Anthropic model. """ connector_prompt = f"{self.pre_prompt}{prompt}{self.post_prompt}" + + # Build messages list + messages = [{"role": "user", "content": connector_prompt}] + # Merge self.optional_params with additional parameters new_params = { **self.optional_params, "model": self.model, - "prompt": f"{HUMAN_PROMPT}{connector_prompt}{AI_PROMPT}", + "messages": messages, } - response = await self._client.completions.create(**new_params) + + # Add max_tokens if not already provided (required by Anthropic API) + if "max_tokens" not in new_params: + new_params["max_tokens"] = 4096 # Default value for Anthropic models + + # Add system prompt if available + if self.system_prompt: + new_params["system"] = self.system_prompt + + response: Message = await self._client.messages.create(**new_params) return ConnectorResponse(response=await self._process_response(response)) - async def _process_response(self, response: Completion) -> str: + async def _process_response(self, response: Message) -> str: """ Process an HTTP response and extract relevant information as a string. @@ -56,9 +68,21 @@ async def _process_response(self, response: Completion) -> str: from the response body, headers, or other attributes. Args: - response (Completion): An HTTP response object containing the response data. + response (Message): An Anthropic Message response object containing the response data. Returns: str: A string representing the relevant information extracted from the response. """ - return response.completion[1:] + # Extract text from all text content blocks in the response + # Anthropic can return multiple content blocks (text, tool_use, etc.) + # We filter for text blocks and join them together + if not response.content: + return "" + + text_blocks = [] + for block in response.content: + # Check if this is a text block and extract its text + if hasattr(block, "type") and block.type == "text" and hasattr(block, "text"): + text_blocks.append(block.text) + + return "\n\n".join(text_blocks) if text_blocks else "" From 3ba7b18ecff15704e5c844180e224e8e4791ba73 Mon Sep 17 00:00:00 2001 From: aivf_baisheng Date: Tue, 2 Dec 2025 14:21:47 +0800 Subject: [PATCH 2/5] fix formatting --- connectors-endpoints/anthropic-claude4.5.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/connectors-endpoints/anthropic-claude4.5.json b/connectors-endpoints/anthropic-claude4.5.json index 83503c4e..dc0b4fe4 100644 --- a/connectors-endpoints/anthropic-claude4.5.json +++ b/connectors-endpoints/anthropic-claude4.5.json @@ -11,4 +11,4 @@ "max_attempts": 3, "temperature": 0.0 } - } \ No newline at end of file +} \ No newline at end of file From 3b7f33a7d30b2c8400815571c704d005831d277b Mon Sep 17 00:00:00 2001 From: aivf_baisheng Date: Tue, 2 Dec 2025 14:47:49 +0800 Subject: [PATCH 3/5] ws-296 remove arbitrary max_tokens --- connectors/anthropic-connector.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/connectors/anthropic-connector.py b/connectors/anthropic-connector.py index 89e9df7e..4c2aeece 100644 --- a/connectors/anthropic-connector.py +++ b/connectors/anthropic-connector.py @@ -48,10 +48,6 @@ async def get_response(self, prompt: str) -> ConnectorResponse: "messages": messages, } - # Add max_tokens if not already provided (required by Anthropic API) - if "max_tokens" not in new_params: - new_params["max_tokens"] = 4096 # Default value for Anthropic models - # Add system prompt if available if self.system_prompt: new_params["system"] = self.system_prompt From f28777743fb6ea3f5553a00b521ed01dc2573b31 Mon Sep 17 00:00:00 2001 From: aivf_baisheng Date: Tue, 2 Dec 2025 15:04:18 +0800 Subject: [PATCH 4/5] ws-296 max tokens handling updated --- connectors-endpoints/anthropic-claude4.5.json | 5 +++-- connectors/anthropic-connector.py | 6 ++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/connectors-endpoints/anthropic-claude4.5.json b/connectors-endpoints/anthropic-claude4.5.json index dc0b4fe4..bd7cf9c6 100644 --- a/connectors-endpoints/anthropic-claude4.5.json +++ b/connectors-endpoints/anthropic-claude4.5.json @@ -9,6 +9,7 @@ "params": { "timeout": 300, "max_attempts": 3, - "temperature": 0.0 + "temperature": 0.0, + "max_tokens": 64000 } -} \ No newline at end of file + } \ No newline at end of file diff --git a/connectors/anthropic-connector.py b/connectors/anthropic-connector.py index 4c2aeece..3c4f63c3 100644 --- a/connectors/anthropic-connector.py +++ b/connectors/anthropic-connector.py @@ -52,6 +52,12 @@ async def get_response(self, prompt: str) -> ConnectorResponse: if self.system_prompt: new_params["system"] = self.system_prompt + # Validate that max_tokens is provided and is greater than 0 + # This assertion is to make the requirements from anthropic API clear to the user + # The anthropic API will raise an error if max_tokens is not provided or is less than 0 + if "max_tokens" not in new_params or new_params.get("max_tokens", 0) <= 0: + raise ValueError("max_tokens is required and must be greater than 0") + response: Message = await self._client.messages.create(**new_params) return ConnectorResponse(response=await self._process_response(response)) From c15d6fd96342bd909cdcf2e6cc8805b8fa6ac154 Mon Sep 17 00:00:00 2001 From: aivf_baisheng Date: Tue, 2 Dec 2025 15:27:27 +0800 Subject: [PATCH 5/5] ws-296 added additional comments on the seperators --- connectors/anthropic-connector.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/connectors/anthropic-connector.py b/connectors/anthropic-connector.py index 3c4f63c3..d6b31c5a 100644 --- a/connectors/anthropic-connector.py +++ b/connectors/anthropic-connector.py @@ -87,4 +87,6 @@ async def _process_response(self, response: Message) -> str: if hasattr(block, "type") and block.type == "text" and hasattr(block, "text"): text_blocks.append(block.text) + # We have decided to join text blocks with double newlines to separate paragraphs, + # whichever join we decide would have affected the result anyway return "\n\n".join(text_blocks) if text_blocks else ""