11import os
22
33import anthropic
4- from anthropic import AI_PROMPT , HUMAN_PROMPT
5- from anthropic .types import Completion
4+ from anthropic .types import Message
65from moonshot .src .connectors .connector import Connector , perform_retry
76from moonshot .src .connectors .connector_response import ConnectorResponse
87from 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