Skip to content

Commit ae42ac2

Browse files
Merge pull request #209 from aiverify-foundation/ws-296
WS-296 Update Anthropic Connector to work
2 parents 29aae0a + c15d6fd commit ae42ac2

3 files changed

Lines changed: 50 additions & 22 deletions

File tree

connectors-endpoints/anthropic-claude2.json

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "Anthropic-Claude4.5",
3+
"connector_type": "anthropic-connector",
4+
"uri": "",
5+
"token": "",
6+
"max_calls_per_second": 1,
7+
"max_concurrency": 1,
8+
"model": "claude-sonnet-4-5-20250929",
9+
"params": {
10+
"timeout": 300,
11+
"max_attempts": 3,
12+
"temperature": 0.0,
13+
"max_tokens": 64000
14+
}
15+
}

connectors/anthropic-connector.py

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import os
22

33
import anthropic
4-
from anthropic import AI_PROMPT, HUMAN_PROMPT
5-
from anthropic.types import Completion
4+
from anthropic.types import Message
65
from moonshot.src.connectors.connector import Connector, perform_retry
76
from moonshot.src.connectors.connector_response import ConnectorResponse
87
from moonshot.src.connectors_endpoints.connector_endpoint_arguments import (
@@ -38,16 +37,31 @@ async def get_response(self, prompt: str) -> ConnectorResponse:
3837
ConnectorResponse: An object containing the text response generated by the Anthropic model.
3938
"""
4039
connector_prompt = f"{self.pre_prompt}{prompt}{self.post_prompt}"
40+
41+
# Build messages list
42+
messages = [{"role": "user", "content": connector_prompt}]
43+
4144
# Merge self.optional_params with additional parameters
4245
new_params = {
4346
**self.optional_params,
4447
"model": self.model,
45-
"prompt": f"{HUMAN_PROMPT}{connector_prompt}{AI_PROMPT}",
48+
"messages": messages,
4649
}
47-
response = await self._client.completions.create(**new_params)
50+
51+
# Add system prompt if available
52+
if self.system_prompt:
53+
new_params["system"] = self.system_prompt
54+
55+
# Validate that max_tokens is provided and is greater than 0
56+
# This assertion is to make the requirements from anthropic API clear to the user
57+
# The anthropic API will raise an error if max_tokens is not provided or is less than 0
58+
if "max_tokens" not in new_params or new_params.get("max_tokens", 0) <= 0:
59+
raise ValueError("max_tokens is required and must be greater than 0")
60+
61+
response: Message = await self._client.messages.create(**new_params)
4862
return ConnectorResponse(response=await self._process_response(response))
4963

50-
async def _process_response(self, response: Completion) -> str:
64+
async def _process_response(self, response: Message) -> str:
5165
"""
5266
Process an HTTP response and extract relevant information as a string.
5367
@@ -56,9 +70,23 @@ async def _process_response(self, response: Completion) -> str:
5670
from the response body, headers, or other attributes.
5771
5872
Args:
59-
response (Completion): An HTTP response object containing the response data.
73+
response (Message): An Anthropic Message response object containing the response data.
6074
6175
Returns:
6276
str: A string representing the relevant information extracted from the response.
6377
"""
64-
return response.completion[1:]
78+
# Extract text from all text content blocks in the response
79+
# Anthropic can return multiple content blocks (text, tool_use, etc.)
80+
# We filter for text blocks and join them together
81+
if not response.content:
82+
return ""
83+
84+
text_blocks = []
85+
for block in response.content:
86+
# Check if this is a text block and extract its text
87+
if hasattr(block, "type") and block.type == "text" and hasattr(block, "text"):
88+
text_blocks.append(block.text)
89+
90+
# We have decided to join text blocks with double newlines to separate paragraphs,
91+
# whichever join we decide would have affected the result anyway
92+
return "\n\n".join(text_blocks) if text_blocks else ""

0 commit comments

Comments
 (0)