AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend): Use raw content from the LLM as conversation history (#9551)
Zamil Majdy83d879ea65bc
diff --git a/autogpt_platform/backend/backend/blocks/llm.py b/autogpt_platform/backend/backend/blocks/llm.pyindex e6927b7e6a..a06bd92e88 100644--- a/autogpt_platform/backend/backend/blocks/llm.py+++ b/autogpt_platform/backend/backend/blocks/llm.py@@ -229,17 +229,6 @@ for model in LlmModel:raise ValueError(f"Missing MODEL_METADATA metadata for model: {model}")-class MessageRole(str, Enum):- SYSTEM = "system"- USER = "user"- ASSISTANT = "assistant"---class Message(BlockSchema):- role: MessageRole- content: str--class ToolCall(BaseModel):name: strarguments: str@@ -252,7 +241,8 @@ class ToolContentBlock(BaseModel):class LLMResponse(BaseModel):- prompt: str+ raw_response: Any+ prompt: List[Any]response: strtool_calls: Optional[List[ToolContentBlock]] | Noneprompt_tokens: int@@ -362,7 +352,8 @@ def llm_call(tool_calls = Nonereturn LLMResponse(- prompt=json.dumps(prompt),+ raw_response=response.choices[0].message,+ prompt=prompt,response=response.choices[0].message.content or "",tool_calls=tool_calls,prompt_tokens=response.usage.prompt_tokens if response.usage else 0,@@ -423,7 +414,8 @@ def llm_call()return LLMResponse(- prompt=json.dumps(prompt),+ raw_response=resp.content[0],+ prompt=prompt,response=(resp.content[0].nameif isinstance(resp.content[0], anthropic.types.ToolUseBlock)@@ -450,7 +442,8 @@ def llm_call(max_tokens=max_tokens,)return LLMResponse(- prompt=json.dumps(prompt),+ raw_response=response.choices[0].message,+ prompt=prompt,response=response.choices[0].message.content or "",tool_calls=None,prompt_tokens=response.usage.prompt_tokens if response.usage else 0,@@ -469,7 +462,8 @@ def llm_call(stream=False,)return LLMResponse(- prompt=json.dumps(prompt),+ raw_response=response.get("response") or "",+ prompt=prompt,response=response.get("response") or "",tool_calls=None,prompt_tokens=response.get("prompt_eval_count") or 0,@@ -515,7 +509,8 @@ def llm_call(tool_calls = Nonereturn LLMResponse(- prompt=json.dumps(prompt),+ raw_response=response.choices[0].message,+ prompt=prompt,response=response.choices[0].message.content or "",tool_calls=tool_calls,prompt_tokens=response.usage.prompt_tokens if response.usage else 0,@@ -557,7 +552,7 @@ class AIStructuredResponseGeneratorBlock(AIBlockBase):default="",description="The system prompt to provide additional context to the model.",)- conversation_history: list[Message] = SchemaField(+ conversation_history: list[Any] = SchemaField(default=[],description="The conversation history to provide context for the prompt.",)@@ -613,7 +608,8 @@ class AIStructuredResponseGeneratorBlock(AIBlockBase):],test_mock={"llm_call": lambda *args, **kwargs: LLMResponse(- prompt="",+ raw_response="",+ prompt=[""],response=json.dumps({"key1": "key1Value",@@ -1032,7 +1028,7 @@ class AITextSummarizerBlock(AIBlockBase):class AIConversationBlock(AIBlockBase):class Input(BlockSchema):- messages: List[Message] = SchemaField(+ messages: List[Any] = SchemaField(description="List of messages in the conversation.", min_length=1)model: LlmModel = SchemaField(diff --git a/autogpt_platform/backend/backend/blocks/smart_decision_maker.py b/autogpt_platform/backend/backend/blocks/smart_decision_maker.pyindex 4d91f8e3ca..a23b4e2a97 100644--- a/autogpt_platform/backend/backend/blocks/smart_decision_maker.py+++ b/autogpt_platform/backend/backend/blocks/smart_decision_maker.py@@ -53,7 +53,7 @@ class SmartDecisionMakerBlock(Block):default="Thinking carefully step by step decide which function to call. Always choose a function call from the list of function signatures.",description="The system prompt to provide additional context to the model.",)- conversation_history: list[llm.Message] = SchemaField(+ conversation_history: list[Any] = SchemaField(default=[],description="The conversation history to provide context for the prompt.",)@@ -84,7 +84,7 @@ class SmartDecisionMakerBlock(Block):finished: str = SchemaField(description="The finished message to display to the user.")- conversations: list[llm.Message] = SchemaField(+ conversations: list[Any] = SchemaField(description="The conversation history to provide context for the prompt.")@@ -312,7 +312,6 @@ class SmartDecisionMakerBlock(Block):if not response.tool_calls:yield "finished", f"No Decision Made finishing task: {response.response}"- assistant_response = response.responseelse:for tool_call in response.tool_calls:tool_name = tool_call.function.name@@ -321,15 +320,6 @@ class SmartDecisionMakerBlock(Block):for arg_name, arg_value in tool_args.items():yield f"tools_^_{tool_name}_{arg_name}".lower(), arg_value- assistant_response = "\n".join(- f"[{c.function.name}] called with arguments: {c.function.arguments}"- for c in response.tool_calls- )-- input_data.conversation_history.append(- llm.Message(role=llm.MessageRole.USER, content=response.prompt)- )- input_data.conversation_history.append(- llm.Message(role=llm.MessageRole.ASSISTANT, content=assistant_response)- )+ input_data.conversation_history.extend(response.prompt)+ input_data.conversation_history.append(response.raw_response)yield "conversations", input_data.conversation_history