-
-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat(autofix): Add first pass at interactive flow #1168
Changes from 14 commits
a748d76
8cb25f0
73bc674
77fb80a
79837a1
1dce4a2
51e162e
bd5c109
6d66401
ef7ed8d
281cc25
c62b346
f7294fe
0717cef
b3d010f
f879db1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -147,6 +147,10 @@ def clean_tool_call_assistant_messages(self, messages: list[Message]) -> list[Me | |
) | ||
elif message.role == "tool": | ||
new_messages.append(Message(role="user", content=message.content, tool_calls=[])) | ||
elif message.role == "tool_use": | ||
new_messages.append( | ||
Message(role="assistant", content=message.content, tool_calls=[]) | ||
) | ||
Comment on lines
+150
to
+153
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe I did need to use this hack for Claude, I can't recall the exact error that was occurring though |
||
else: | ||
new_messages.append(message) | ||
return new_messages | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import re | ||
import textwrap | ||
|
||
from langfuse.decorators import observe | ||
from sentry_sdk.ai.monitoring import ai_track | ||
|
||
from seer.automation.agent.client import GptClient | ||
from seer.automation.agent.models import Message, Usage | ||
from seer.automation.autofix.autofix_context import AutofixContext | ||
from seer.automation.autofix.components.insight_sharing.models import ( | ||
InsightContextOutput, | ||
InsightSharingOutput, | ||
InsightSharingRequest, | ||
) | ||
from seer.automation.component import BaseComponent | ||
from seer.dependency_injection import inject, injected | ||
|
||
|
||
class InsightSharingPrompts: | ||
@staticmethod | ||
def format_step_one( | ||
task_description: str, | ||
latest_thought: str, | ||
past_insights: list[str], | ||
): | ||
past_insights = [f"{i + 1}. {insight}" for i, insight in enumerate(past_insights)] | ||
return textwrap.dedent( | ||
"""\ | ||
Given the chain of thought below for {task_description}: | ||
{insights} | ||
|
||
Write the next under-25-words conclusion in the chain of thought based on the notes below, or if there is no good conclusion to add, return <NO_INSIGHT/>. The criteria for a good conclusion are that it should be a large, novel jump in insights, not similar to any item in the existing chain of thought, it should be a complete conclusion after analysis, it should not be a plan of what to analyze next, and it should be valuable for {task_description}. Every item in the chain of thought should read like a chain that clearly builds off of the previous step. If you can't find a conclusion that meets these criteria, return <NO_INSIGHT/>. | ||
|
||
{latest_thought}""" | ||
).format( | ||
task_description=task_description, | ||
latest_thought=latest_thought, | ||
insights="\n".join(past_insights) if past_insights else "None", | ||
) | ||
|
||
@staticmethod | ||
def format_step_two(insight: str, latest_thought: str): | ||
return textwrap.dedent( | ||
"""\ | ||
Return the pieces of context from the issue details or the files in the codebase that are directly relevant to the text below: | ||
{insight} | ||
|
||
That means choose the most relevant codebase snippets, event logs, stacktraces, or other information, that show specifically what the text mentions. Don't include any repeated information; just include what's needed. | ||
|
||
Also provide a one-line explanation of how the pieces of context directly explain the text. | ||
|
||
To know what's needed, reference these notes: | ||
{latest_thought}""" | ||
).format( | ||
insight=insight, | ||
latest_thought=latest_thought, | ||
) | ||
|
||
|
||
class InsightSharingComponent(BaseComponent[InsightSharingRequest, InsightSharingOutput]): | ||
context: AutofixContext | ||
|
||
@observe(name="Sharing Insights") | ||
@ai_track(description="Sharing Insights") | ||
@inject | ||
def invoke( | ||
self, request: InsightSharingRequest, gpt_client: GptClient = injected | ||
) -> InsightSharingOutput | None: | ||
prompt_one = InsightSharingPrompts.format_step_one( | ||
task_description=request.task_description, | ||
latest_thought=request.latest_thought, | ||
past_insights=request.past_insights, | ||
) | ||
completion = gpt_client.openai_client.chat.completions.create( | ||
model="gpt-4o-mini-2024-07-18", | ||
messages=[Message(role="user", content=prompt_one).to_message()], | ||
temperature=0.0, | ||
) | ||
with self.context.state.update() as cur: | ||
usage = Usage( | ||
completion_tokens=completion.usage.completion_tokens, | ||
prompt_tokens=completion.usage.prompt_tokens, | ||
total_tokens=completion.usage.total_tokens, | ||
) | ||
cur.usage += usage | ||
insight = completion.choices[0].message.content | ||
if insight == "<NO_INSIGHT/>": | ||
return None | ||
|
||
insight = re.sub( | ||
r"^\d+\.\s+", "", insight | ||
) # since the model often starts the insight with a number, e.g. "3. Insight..." | ||
|
||
prompt_two = InsightSharingPrompts.format_step_two( | ||
insight=insight, | ||
latest_thought=request.latest_thought, | ||
) | ||
memory = [] | ||
for i, message in enumerate(gpt_client.clean_tool_call_assistant_messages(request.memory)): | ||
if message.role != "system": | ||
memory.append(message.to_message()) | ||
memory.append(Message(role="user", content=prompt_two).to_message()) | ||
|
||
completion = gpt_client.openai_client.beta.chat.completions.parse( | ||
model="gpt-4o-mini-2024-07-18", | ||
messages=memory, | ||
response_format=InsightContextOutput, | ||
temperature=0.0, | ||
max_tokens=2048, | ||
) | ||
with self.context.state.update() as cur: | ||
usage = Usage( | ||
completion_tokens=completion.usage.completion_tokens, | ||
prompt_tokens=completion.usage.prompt_tokens, | ||
total_tokens=completion.usage.total_tokens, | ||
) | ||
cur.usage += usage | ||
structured_message = completion.choices[0].message | ||
if structured_message.refusal: | ||
raise RuntimeError(structured_message.refusal) | ||
if not structured_message.parsed: | ||
raise RuntimeError("Failed to parse message") | ||
|
||
res = completion.choices[0].message.parsed | ||
roaga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
response = InsightSharingOutput( | ||
insight=insight, | ||
justification=res.explanation, | ||
error_message_context=res.error_message_context, | ||
codebase_context=res.codebase_context, | ||
stacktrace_context=res.stacktrace_context, | ||
breadcrumb_context=res.event_log_context, | ||
) | ||
return response |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from pydantic import BaseModel | ||
|
||
from seer.automation.agent.models import Message | ||
from seer.automation.component import BaseComponentOutput, BaseComponentRequest | ||
|
||
|
||
class CodeSnippetContext(BaseModel): | ||
repo_name: str | ||
file_path: str | ||
snippet: str | ||
|
||
|
||
class BreadcrumbContext(BaseModel): | ||
type: str | ||
category: str | ||
body: str | ||
level: str | ||
data_as_json: str | ||
|
||
|
||
class StacktraceContext(BaseModel): | ||
file_name: str | ||
repo_name: str | ||
function: str | ||
line_no: int | ||
col_no: int | ||
code_snippet: str | ||
vars_as_json: str | ||
|
||
|
||
class InsightContextOutput(BaseModel): | ||
explanation: str | ||
error_message_context: list[str] | ||
codebase_context: list[CodeSnippetContext] | ||
stacktrace_context: list[StacktraceContext] | ||
event_log_context: list[BreadcrumbContext] | ||
|
||
|
||
class InsightSharingRequest(BaseComponentRequest): | ||
latest_thought: str | ||
task_description: str | ||
memory: list[Message] | ||
past_insights: list[str] | ||
|
||
|
||
class InsightSharingOutput(BaseComponentOutput): | ||
insight: str | ||
error_message_context: list[str] | ||
codebase_context: list[CodeSnippetContext] | ||
stacktrace_context: list[StacktraceContext] | ||
breadcrumb_context: list[BreadcrumbContext] | ||
justification: str |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm I think this is worth pulling out of the general agent logic and into a specific
AutofixAgent
that inherits from this genericAgent
as codecov will be using this too and we don't need to generate insights there.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since in many cases we'll want to disable it for Autofix too (e.g. evals, GH Copilot), I'm adding an
interactive
flag to the AgentConfig so that these kinds of features are easy to enable/disable whenever we want. It defaults toFalse
so the unit test gen code will not generate any insights.