Conversation
* Add two-sum example * Allow GH action to post review as comment
Here are the improvements I'd suggest:
Here's an updated version of the code incorporating some of these suggestions: deftwo_sum(nums: list[int], target: int) ->tuple[int, int]:
"""Returns a tuple of two numbers in the input list that add up to the target sum."""num_map= {}
fornuminnums:
complement=target-numifcomplementinnum_map:
return (complement, num)
num_map[num] =Truereturn ()
print(two_sum([2, 7, 11, 15], 9)) # (2, 7) |
Code Review The provided code diff appears to be a GitHub Actions workflow configuration file and a Python script for a code review bot. Here are some improvements and potential optimizations: Improvements:
Potential Optimizations:
Security:
Best Practices:
Here is an updated version of the code incorporating some of these suggestions: reviewbot/main.py importosimportrequestsimportloggingfromreviewbot.utilsimportget_diff, post_pr_comment, get_pr_numberllm_prompt="Please review this code diff, list improvements and potential optimizations in bullet points"defreview_with_groq(diff: str) ->str:
api_key=os.environ.get("GROQ_API_KEY")
ifnotapi_key:
logging.error("GROQ_API_KEY environment variable is not set")
return""headers= {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
try:
response=requests.post(
"https://api.groq.com/v1/chat/completions",
headers=headers,
json={
"model": "meta-llama/llama-4-scout-17b-16e-instruct",
"messages": [
{"role": "system", "content": "You are a senior software engineer doing code reviews."},
{"role": "user", "content": f"{llm_prompt} given the following diff:\n{diff}"}
],
"temperature": 0.3,
},
timeout=30# Add a timeout to prevent hanging
)
response.raise_for_status()
returnresponse.json()["choices"][0]["message"]["content"]
exceptrequests.RequestExceptionase:
logging.error(f"Error reviewing code: {e}")
return""defmain():
diff=get_diff()
review=review_with_groq(diff)
post_pr_comment(review)
if__name__=="__main__":
main()reviewbot/utils.py importosimportrequestsdefget_pr_number() ->int:
event=requests.get("https://api.github.com/repository").json()
returnevent["pull_request"]["number"]
defget_diff() ->str:
repo=os.environ.get("GITHUB_REPOSITORY")
pr_number=get_pr_number()
token=os.environ.get("GITHUB_TOKEN")
ifnotrepoornottoken:
logging.error("GITHUB_REPOSITORY or GITHUB_TOKEN environment variable is not set")
return""try:
response=requests.get(
f"https://api.github.com/repos/{repo}/pulls/{pr_number}/diff",
headers={"Authorization": f"Bearer {token}"}
)
response.raise_for_status()
returnresponse.textexceptrequests.RequestExceptionase:
logging.error(f"Error getting diff: {e}")
return""Note that this is not an exhaustive review, and there may be other improvements and optimizations that can be made. |
Code Review The provided code diff appears to be a GitHub Actions workflow configuration file and a Python script for a code review bot. Here are some improvements and potential optimizations: Improvements:
Potential Optimizations:
Code Smells:
Security:
Here is an updated version of the code incorporating some of these suggestions: reviewbot/main.py importosimportrequestsfromreviewbot.utilsimportget_diff, post_pr_comment, get_pr_numberLLM_PROMPT="Please review this code diff, list improvements and potential optimizations in bullet points"defreview_with_groq(diff: str) ->str:
api_key=os.environ.get("GROQ_API_KEY")
ifnotapi_key:
raiseValueError("GROQ_API_KEY environment variable is not set")
headers= {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
}
payload= {
"model": "meta-llama/llama-4-scout-17b-16e-instruct",
"messages": [
{"role": "system", "content": "You are a senior software engineer doing code reviews."},
{"role": "user", "content": f"{LLM_PROMPT} given the following diff:\n{diff}"},
],
"temperature": 0.3,
}
response=requests.post("https://api.groq.com/v1/chat", headers=headers, json=payload)
response.raise_for_status()
returnresponse.json()["text"]
defmain():
diff=get_diff()
review=review_with_groq(diff)
post_pr_comment(review)
if__name__=="__main__":
main()reviewbot/utils.py importosimportrequestsdefget_pr_number() ->int:
event=requests.get("https://api.github.com/repos/{}/events".format(os.environ.get("GITHUB_REPOSITORY"))).json()
returnevent["pull_request"]["number"]
defget_diff() ->str:
repo=os.environ.get("GITHUB_REPOSITORY")
pr_number=get_pr_number()
token=os.environ.get("GITHUB_TOKEN")
headers= {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json",
}
response=requests.get(f"https://api.github.com/repos/{repo}/pulls/{pr_number}/diff", headers=headers)
response.raise_for_status()
returnresponse.text |
No description provided.