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

Example for SWE agent #173

Merged
merged 46 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
a4bc218
fix minor example bug
Feb 22, 2024
b9f381d
fix single typo
Feb 22, 2024
e88ffad
fix single typo in 105-logging.md
Feb 22, 2024
775913a
Merge remote-tracking branch 'upstream/main'
garyzhang99 Mar 26, 2024
5b082c4
Merge remote-tracking branch 'upstream/main'
garyzhang99 Apr 1, 2024
a396b9b
Merge remote-tracking branch 'upstream/main'
garyzhang99 Apr 7, 2024
ca5d3dc
Merge remote-tracking branch 'upstream/main'
garyzhang99 Apr 12, 2024
4e5c79d
adding more service functions
garyzhang99 Apr 12, 2024
cc82278
dir test
garyzhang99 Apr 12, 2024
48b7a7d
init shell command test
garyzhang99 Apr 12, 2024
84df79f
init shell command test
garyzhang99 Apr 12, 2024
faa99e6
exec shell
garyzhang99 Apr 18, 2024
67a07c9
Merge remote-tracking branch 'upstream/main'
garyzhang99 Apr 18, 2024
895820d
Merge branch 'main' into more_service_functions
garyzhang99 Apr 18, 2024
88d0cd0
precomit
garyzhang99 Apr 18, 2024
0d628b2
change in init and common
garyzhang99 Apr 22, 2024
e835e5c
Merge remote-tracking branch 'upstream/main'
garyzhang99 Apr 22, 2024
2d7fe0b
fix typo in tests
garyzhang99 Apr 22, 2024
cd38a4f
fix typo in tests
garyzhang99 Apr 22, 2024
b5a3123
skip shell command tasks on windows
garyzhang99 Apr 22, 2024
903806e
Merge remote-tracking branch 'upstream/main' into example/swe_agent
garyzhang99 Apr 22, 2024
4813913
resolve comments
garyzhang99 Apr 23, 2024
7878005
resolve comments
garyzhang99 Apr 23, 2024
66f82b9
add files
garyzhang99 Apr 23, 2024
63eb91f
Merge branch 'more_service_functions' into example/swe_agent
garyzhang99 Apr 23, 2024
1cc008f
init SWE-agent example
garyzhang99 Apr 23, 2024
85bf5e4
Merge remote-tracking branch 'upstream/main' into example/swe_agent
garyzhang99 Apr 23, 2024
5fcd43d
Merge remote-tracking branch 'upstream/main'
garyzhang99 Apr 23, 2024
ccaa192
Merge branch 'main' into example/swe_agent
garyzhang99 Apr 23, 2024
95e3909
add missing file
garyzhang99 Apr 23, 2024
21e1d81
add doc string
garyzhang99 Apr 23, 2024
728743b
Merge remote-tracking branch 'upstream/main'
garyzhang99 Apr 24, 2024
ba21e67
Merge remote-tracking branch 'upstream/main'
garyzhang99 Apr 26, 2024
a36de71
add example notebook
garyzhang99 May 8, 2024
47e9a58
fix typos and format
garyzhang99 May 8, 2024
0b936d5
Merge remote-tracking branch 'upstream/main'
garyzhang99 May 8, 2024
adaf390
merge main to resolve confilicts
garyzhang99 May 8, 2024
2f05622
fix typo
garyzhang99 May 9, 2024
903d49d
fix issues and update parser
garyzhang99 May 15, 2024
f88ed5d
resolve threads
garyzhang99 May 15, 2024
815c99f
resolving threads
garyzhang99 May 15, 2024
f7be053
Merge branch 'main' into example/swe_agent
garyzhang99 May 15, 2024
9da20fb
Update main.ipynb
garyzhang99 May 15, 2024
1975a63
Update main.ipynb
garyzhang99 May 15, 2024
e462249
resolve threads
garyzhang99 May 15, 2024
46ae325
README name
garyzhang99 May 15, 2024
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
333 changes: 333 additions & 0 deletions examples/swe_agent/main.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,333 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Example with SWE-agent\n",
garyzhang99 marked this conversation as resolved.
Show resolved Hide resolved
"\n",
"SWE-agent is an agent designed for solving github issues.\n",
garyzhang99 marked this conversation as resolved.
Show resolved Hide resolved
"More details can be found in https://swe-agent.com/.\n",
"\n",
"In the example here, we partially implement the SWE-agent, and provide a simple example of how to use the implemented SWE-agent to fix a bug in a python file.\n",
"\n",
"## Prerequisites\n",
"\n",
"- Follow [READMD.md](https://github.com/modelscope/agentscope) to install AgentScope. \n",
garyzhang99 marked this conversation as resolved.
Show resolved Hide resolved
"- Prepare a model configuration. AgentScope supports both local deployed model services (CPU or GPU) and third-party services. More details and example model configurations please refer to our [tutorial]\n",
garyzhang99 marked this conversation as resolved.
Show resolved Hide resolved
"- Understand the ServiceFactory module and how to use it to pre-process the tool functions for LLMs. You can refer to the [ReAct agent example](../conversation_with_react_agent/main.ipynb).\n",
garyzhang99 marked this conversation as resolved.
Show resolved Hide resolved
"\n",
"## Note\n",
"\n",
"- The example is tested with the following models. For other models, you may need to adjust the prompt.\n",
" - gpt-4\n",
"- How to enable agents with stronger programming capabilities remains an open challenge, and the current implementations are not perfect. Please feel free to explore it yourself."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 1: Initialize the AgentScope environment and SWE-agent\n",
"\n",
"Here we init the agentscope environment and load the SWE-agent.\n",
"\n",
"The code of SWE-agent is in `swe_agent.py`, and the related prompts are in `swe_agent_prompts.py`.\n",
"\n",
"If you are interested in the details, please refer to the code and the origianl SWE-agent repo [here](https://github.com/princeton-nlp/SWE-agent)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from swe_agent import SWEAgent\n",
"\n",
"import agentscope\n",
"from agentscope.models import load_model_by_config_name\n",
"agentscope.init(\n",
" model_configs=\"xxx\", # fill you model config file path here\n",
")\n",
"loaded_model = load_model_by_config_name('gpt-4')\n",
garyzhang99 marked this conversation as resolved.
Show resolved Hide resolved
"\n",
"agent = SWEAgent(\n",
" name=\"assistant\",\n",
" model_config_name='gpt-4',\n",
")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 2: Create the code to be processed by the SWE-agent\n",
"\n",
"Here we use the `write_file` function to write the following code into `gcd.py`.\n",
"The code here is a wrong implementation of the Greatest Common Divisor (GCD) algorithm.\n",
garyzhang99 marked this conversation as resolved.
Show resolved Hide resolved
"We will ask the SWE-agent to correct it in our next step."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'status': <ServiceExecStatus.SUCCESS: 1>,\n",
" 'content': 'WRITE OPERATION:\\nYou have written to \"gcd.py\" on these lines: 0:-1.\\ndef gcd(a, b):\\n if a == 0:\\n return b\\n while a != 0:\\n a, b = b, a\\n return b\\n\\ndef lcm(a, b):\\n return (a * b) // gcd(a, b)\\n\\n# testing on GCD and LCM functions\\nprint(\"GCD of 12 and 18 is:\", gcd(12, 18))\\nprint(\"LCM of 12 and 18 is:\", lcm(12, 18))\\n\\n'}"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from agentscope.service import write_file\n",
"\n",
"# here the gcd code is written in a wrong way\n",
"wrong_gcd_text = \"\"\"\n",
"def gcd(a, b):\n",
" if a == 0:\n",
" return b\n",
" while a != 0:\n",
" a, b = b, a\n",
" return b\n",
"\n",
"def lcm(a, b):\n",
" return (a * b) // gcd(a, b)\n",
"\n",
"# testing on GCD and LCM functions\n",
"print(\"GCD of 12 and 18 is:\", gcd(12, 18))\n",
"print(\"LCM of 12 and 18 is:\", lcm(12, 18))\n",
"\"\"\"\n",
"\n",
"write_file(\"gcd.py\", wrong_gcd_text)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 3: Ask the SWE-agent to correct the code\n",
"\n",
"Here we simple ask the SWE-agent to correct the code in `gcd.py`."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"user: The file in gcd.py is not returning the correct answer, can you fix the file for me?.\n",
"assistant: {\n",
" \"thought\": \"First, I need to view the file gcd.py to understand the bug the user is referring to. I'll read the file gcd.py using the `read_file` command.\",\n",
" \"action\": {\n",
" \"name\": \"read_file\",\n",
" \"arguments\": {\n",
" \"file_path\": \"gcd.py\"\n",
" }\n",
" }\n",
"}\n",
"assistant: \n",
"====Observation====\n",
"\n",
"def gcd(a, b):\n",
" if a == 0:\n",
" return b\n",
" while a != 0:\n",
" a, b = b, a\n",
" return b\n",
"\n",
"def lcm(a, b):\n",
" return (a * b) // gcd(a, b)\n",
"\n",
"# testing on GCD and LCM functions\n",
"print(\"GCD of 12 and 18 is:\", gcd(12, 18))\n",
"print(\"LCM of 12 and 18 is:\", lcm(12, 18))\n",
"\n",
"\n",
"assistant: {\n",
" \"thought\": \"I can see that the gcd algorithm implemented in the gcd function within gcd.py file is incorrect. In the while loop of the gcd function, a and b should repeatedly subtract one from the other until a is zero but it's subtracting a from itself which would result in always zero. I'll correct this by modifying the line 5: 'a, b = b, a' to 'a, b = b % a, a'. \",\n",
" \"action\": {\n",
" \"name\": \"write_file\",\n",
" \"arguments\": {\n",
" \"start_line\": 3,\n",
" \"end_line\": 6,\n",
" \"content\": \" if a == 0:\\n return b\\n while b != 0:\\n a, b = b % a, a\",\n",
" \"file_path\": \"gcd.py\"\n",
" }\n",
" }\n",
"}\n",
"assistant: \n",
"====Observation====\n",
"WRITE OPERATION:\n",
"You have written to \"gcd.py\" on these lines: 3:6.\n",
"def gcd(a, b):\n",
" if a == 0:\n",
" if a == 0:\n",
" return b\n",
" while b != 0:\n",
" a, b = b % a, a\n",
"\n",
"def lcm(a, b):\n",
" return (a * b) // gcd(a, b)\n",
"\n",
"# testing on GCD and LCM functions\n",
"print(\"GCD of 12 and 18 is:\", gcd(12, 18))\n",
"print(\"LCM of 12 and 18 is:\", lcm(12, 18))\n",
"\n",
"\n",
"assistant: {\n",
" \"thought\": \"The gcd function implemented is incorrect and it's causing the lcm function to fail as well. In the gcd function, while loop is incorrect. We should calculate gcd based on the fact that GCD of two numbers a and b is same as the GCD of b and a mod b. 'a mod b' can be calculated using the '%' operator. When b becomes 0, a will hold the largest common divisor (GCD). Let's fix this.\",\n",
" \"action\": {\n",
" \"name\": \"write_file\",\n",
" \"arguments\": {\n",
" \"start_line\": 2,\n",
" \"end_line\": 6,\n",
" \"content\": \" if b == 0:\\n return a\\n while b:\\n a, b = b, a % b\\n return a\",\n",
" \"file_path\": \"gcd.py\"\n",
" }\n",
" }\n",
"}\n",
"assistant: \n",
"====Observation====\n",
"WRITE OPERATION:\n",
"You have written to \"gcd.py\" on these lines: 2:6.\n",
"def gcd(a, b):\n",
" if b == 0:\n",
" return a\n",
" while b:\n",
" a, b = b, a % b\n",
" return a\n",
"\n",
"def lcm(a, b):\n",
" return (a * b) // gcd(a, b)\n",
"\n",
"# testing on GCD and LCM functions\n",
"print(\"GCD of 12 and 18 is:\", gcd(12, 18))\n",
"print(\"LCM of 12 and 18 is:\", lcm(12, 18))\n",
"\n",
"\n",
"assistant: {\n",
" \"thought\": \"It seems the gcd function in gcd.py has been fixed and any changes required have already been made in my previous actions. I should run the python file to determine if it produces expected results. To do that, I can use the 'execute_shell_command' by running the 'python3' command with 'gcd.py'.\",\n",
" \"action\": {\n",
" \"name\": \"execute_shell_command\",\n",
" \"arguments\": {\n",
" \"command\": \"python3 gcd.py\"\n",
" }\n",
" }\n",
"}\n",
"assistant: \n",
"====Observation====\n",
"GCD of 12 and 18 is: 6\n",
"LCM of 12 and 18 is: 36\n",
"assistant: {\n",
" \"thought\": \"The gcd.py file is fixed correctly and no further actions are needed.\",\n",
" \"action\": {\n",
" \"name\": \"exit\",\n",
" \"arguments\": {}\n",
" }\n",
"}\n",
"assistant: \n",
"====Observation====\n",
"Current task finished, exitting.\n"
]
}
],
"source": [
"from loguru import logger\n",
"from agentscope.message import Msg\n",
"\n",
"mss = Msg(\n",
" name=\"user\", \n",
" content=\"The file in gcd.py is not returning the correct answer, can you fix the file for me?\", \n",
" role=\"user\"\n",
")\n",
"logger.chat(mss)\n",
"answer_mss = agent(mss)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Conlusion\n",
"\n",
"After a few iterations, the assistant response:\n",
"```text\n",
"\n",
"assistant: {\n",
" \"thought\": \"It seems the gcd function in gcd.py has been fixed and any changes required have already been made in my previous actions. I should run the python file to determine if it produces expected results. To do that, I can use the 'execute_shell_command' by running the 'python3' command with 'gcd.py'.\",\n",
" \"action\": {\n",
" \"name\": \"execute_shell_command\",\n",
" \"arguments\": {\n",
" \"command\": \"python3 gcd.py\"\n",
" }\n",
" }\n",
"}\n",
"assistant: \n",
"====Observation====\n",
"GCD of 12 and 18 is: 6\n",
"LCM of 12 and 18 is: 36\n",
"\n",
"\n",
"assistant: {\n",
" \"thought\": \"The gcd.py file is fixed correctly and no further actions are needed.\",\n",
" \"action\": {\n",
" \"name\": \"exit\",\n",
" \"arguments\": {}\n",
" }\n",
"}\n",
"assistant: \n",
"====Observation====\n",
"Current task finished, exitting.\n",
"\n",
"```\n",
"\n",
"The SWE-agent finish the job successfully, and the code is now working fine."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Above we shown a example of how to use the SWE-agent to fix code errors.\n",
"Although the design of the SWE-agent is primarily aimed at addressing GitHub issues, with modifications, it can also be utilized for more general programming tasks.\n",
"\n",
"Currently, how to program using an agent remains a challenging open question, with the efficacy of agent programming potentially influenced by factors such as prompt construction, model capabilities, and the complexity of the task at hand. Here we just provide an interesting toy example. \n",
"\n",
"We encourage users to experiment by altering the prompts within this example or by assigning different tasks to the agent, among other methods of exploration. Please feel free to experiment and explore on your own. The AgentScope team will continue to provide updates, enhancing the capabilities of the Programming Agents in the future!"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "datajuicer",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.18"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading