Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(autofix): Handle empty messages and move cleaning calls inside LLMClient #1484

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/seer/automation/agent/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,10 @@ def generate_text(
default_temperature = defaults.temperature if defaults else None
# More defaults to come

messages = LlmClient.clean_message_content(messages if messages else [])
if not tools:
messages = LlmClient.clean_tool_call_assistant_messages(messages)

if model.provider_name == LlmProviderType.OPENAI:
model = cast(OpenAiProvider, model)

Expand Down Expand Up @@ -516,6 +520,9 @@ def generate_structured(
if run_name:
langfuse_context.update_current_observation(name=run_name + " - Generate Structured")

messages = LlmClient.clean_message_content(messages if messages else [])
messages = LlmClient.clean_tool_call_assistant_messages(messages)

if model.provider_name == LlmProviderType.OPENAI:
model = cast(OpenAiProvider, model)
return model.generate_structured(
Expand Down Expand Up @@ -550,6 +557,15 @@ def clean_tool_call_assistant_messages(messages: list[Message]) -> list[Message]
new_messages.append(message)
return new_messages

@staticmethod
def clean_message_content(messages: list[Message]) -> list[Message]:
new_messages = []
for message in messages:
if not message.content:
message.content = "."
new_messages.append(message)
return new_messages


@module.provider
def provide_llm_client() -> LlmClient:
Expand Down
10 changes: 2 additions & 8 deletions src/seer/automation/autofix/components/coding/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,9 @@ def _handle_simple_fix(
):
state = self.context.state.get()

# Clean memory of tool messages since we're running without tools
cleaned_memory = LlmClient.clean_tool_call_assistant_messages(memory)

agent = AutofixAgent(
config=AgentConfig(interactive=True),
memory=cleaned_memory,
memory=memory,
context=self.context,
name="Plan+Code Simple fixer",
)
Expand Down Expand Up @@ -219,11 +216,8 @@ def _is_feedback_obvious(self, memory: list[Message], llm_client: LlmClient = in
class NeedToSearchCodebaseOutput(BaseModel):
need_to_search_codebase: bool

# Clean message roles to ensure compatibility with OpenAI's API
cleaned_memory = LlmClient.clean_tool_call_assistant_messages(memory)

output = llm_client.generate_structured(
messages=cleaned_memory,
messages=memory,
prompt="Given the above instruction, do you need to search the codebase for more context or have an immediate answer?",
model=OpenAiProvider.model("gpt-4o-mini"),
response_format=NeedToSearchCodebaseOutput,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,7 @@ def invoke(
insight=insight,
latest_thought=request.latest_thought,
)
memory = []
for message in llm_client.clean_tool_call_assistant_messages(request.memory):
if message.role != "system":
memory.append(message)

memory = [msg for msg in request.memory if msg.role != "system"]
completion = llm_client.generate_structured(
messages=memory,
prompt=prompt_two,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def invoke(
self.context.event_manager.add_log("Cleaning up the findings...")

formatted_response = llm_client.generate_structured(
messages=LlmClient.clean_tool_call_assistant_messages(agent.memory),
messages=agent.memory,
prompt=RootCauseAnalysisPrompts.root_cause_formatter_msg(),
model=OpenAiProvider.model("gpt-4o-2024-08-06"),
response_format=MultipleRootCauseAnalysisOutputPrompt,
Expand Down
12 changes: 12 additions & 0 deletions tests/automation/agent/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,18 @@ def test_clean_tool_call_assistant_messages():
assert cleaned_messages[4].role == "assistant"


def test_clean_message_content():
messages = [
Message(role="user", content=""),
]

cleaned_messages = LlmClient.clean_message_content(messages)

assert len(cleaned_messages) == 1
assert cleaned_messages[0].role == "user"
assert cleaned_messages[0].content == "."


def test_openai_generate_structured_refusal(mock_openai_client):
llm_client = LlmClient()
model = OpenAiProvider.model("gpt-3.5-turbo")
Expand Down
Loading