-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrewriter.py
More file actions
76 lines (64 loc) · 2.45 KB
/
Copy pathrewriter.py
File metadata and controls
76 lines (64 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
REWRITE_PROMPT = ChatPromptTemplate.from_messages([
("system", """You are an expert Python engineer specializing in reliability and performance optimization.
You will receive:
1. The CURRENT source code of a tool that is underperforming.
2. Performance metrics showing what is wrong.
3. Recent error messages (if any).
Your job is to produce an IMPROVED version of the same function.
Rules:
- The function name and signature MUST remain identical.
- The function must remain self-contained (all imports inside or at the top of the snippet).
- Focus specifically on the issues described in the metrics.
- Add retry logic with exponential backoff if there are HTTP errors.
- Add timeouts to all network calls.
- Add fallback mechanisms where possible.
- Improve error handling — catch specific exceptions, not bare except.
- Use only: standard library, requests, httpx, pandas, pydantic.
- Do NOT import from langchain, openai, or any LLM library.
- Do NOT use file system access outside of /tmp.
- Return ONLY the raw Python code. No markdown, no explanation.
"""),
("human", """Rewrite this underperforming tool:
## Current Source Code:
```python
{source_code}
```
## Performance Metrics:
- Success rate: {success_rate}
- Average latency: {avg_latency_ms}ms
- Max latency: {max_latency_ms}ms
- Current score: {score} (threshold: 0.6)
## Issues Identified:
{reason}
## Recent Error Messages:
{recent_errors}
Produce an improved version that addresses these specific issues.
""")
])
class ToolRewriter:
def __init__(self, model: str = "gpt-4o"):
self.llm = ChatOpenAI(model=model, temperature=0.1)
self.chain = REWRITE_PROMPT | self.llm
def rewrite(
self,
source_code: str,
success_rate: float,
avg_latency_ms: float,
max_latency_ms: float,
score: float,
reason: str,
recent_errors: list[str],
) -> str:
print(f"[Rewriter] Generating improved version...")
response = self.chain.invoke({
"source_code": source_code,
"success_rate": f"{success_rate:.1%}",
"avg_latency_ms": f"{avg_latency_ms:.0f}",
"max_latency_ms": f"{max_latency_ms:.0f}",
"score": f"{score:.3f}",
"reason": reason,
"recent_errors": "\n".join(recent_errors) if recent_errors else "None",
})
return response.content.strip()