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 all 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ the following libraries.
- [Conversation in Natural Language to Query SQL](./examples/conversation_nl2sql/)
- [Conversation with RAG Agent](./examples/conversation_with_RAG_agents)
- <img src="https://img.alicdn.com/imgextra/i3/O1CN01SFL0Gu26nrQBFKXFR_!!6000000007707-2-tps-500-500.png" alt="new" width="30" height="30"/>[Conversation with gpt-4o](./examples/conversation_with_gpt-4o)
- <img src="https://img.alicdn.com/imgextra/i3/O1CN01SFL0Gu26nrQBFKXFR_!!6000000007707-2-tps-500-500.png" alt="new" width="30" height="30"/>[Conversation with Software Engineering Agent](./examples/swe_agent/)


- Game
- [Gomoku](./examples/game_gomoku)
Expand Down
2 changes: 2 additions & 0 deletions README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ AgentScope支持使用以下库快速部署本地模型服务。
- [通过对话查询SQL信息](./examples/conversation_nl2sql/)
- [与RAG智能体对话](./examples/conversation_with_RAG_agents)
- <img src="https://img.alicdn.com/imgextra/i3/O1CN01SFL0Gu26nrQBFKXFR_!!6000000007707-2-tps-500-500.png" alt="new" width="30" height="30"/>[与gpt-4o模型对话](./examples/conversation_with_gpt-4o)
- <img src="https://img.alicdn.com/imgextra/i3/O1CN01SFL0Gu26nrQBFKXFR_!!6000000007707-2-tps-500-500.png" alt="new" width="30" height="30"/>[与SoftWare Engineering智能体对话](./examples/swe_agent/)


- 游戏
- [五子棋](./examples/game_gomoku)
Expand Down
285 changes: 285 additions & 0 deletions examples/swe_agent/main.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,285 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Conversation with Software Engineering Agent\n",
"\n",
"SWE-agent(SoftWare Engineering Agent) is an agent designed for solving real world software engineering problems, such as fixing github issues.\n",
"More details can be found in the project's [homepage](https://swe-agent.com/) and related [github repo](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",
"You should note that currently how to enable agents with stronger programming capabilities remains an open challenge, and the performance of the paritially implemented SWE-agent is not guaranteed.\n",
"\n",
"## Prerequisites\n",
"\n",
"- Follow [READMD.md](https://github.com/modelscope/agentscope) to install AgentScope. We require the lastest version, so you should build from source by running `pip install -e .` instead of intalling from pypi. \n",
"- 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](https://modelscope.github.io/agentscope/en/tutorial/203-model.html).\n",
"- Understand the ServiceToolkit 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) and you should also refer to the [tutorial](https://modelscope.github.io/agentscope/en/tutorial/204-service.html) for service functions.\n",
"\n",
"\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": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"YOUR_MODEL_CONFIGURATION_NAME = \"{YOUR_MODEL_CONFIGURATION_NAME}\"\n",
"\n",
"YOUR_MODEL_CONFIGURATION = {\n",
" \"model_type\": \"xxx\", \n",
" \"config_name\": YOUR_MODEL_CONFIGURATION_NAME\n",
" \n",
" # ...\n",
"}"
]
},
{
"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",
"\n",
"agentscope.init(model_configs=YOUR_MODEL_CONFIGURATION)\n",
"\n",
"agent = SWEAgent(\n",
" name=\"assistant\",\n",
" model_config_name=YOUR_MODEL_CONFIGURATION_NAME,\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](https://en.wikipedia.org/wiki/Euclidean_algorithm).\n",
"We will ask the SWE-agent to correct it in our next step."
]
},
{
"cell_type": "code",
"execution_count": 3,
"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": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from swe_agent_service_func 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": 4,
"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 thing I'll do is read the content of gcd.py file to understand the existing logic.\",\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 the error in the gcd calculation. The gcd calculation in the file is incorrect. The correct way to find the gcd of two numbers using the Euclidean algorithm is to continually reduce a and b by finding the remainder of a % b until b is 0, at which point a will be the gcd of the original a and b values. In the given code, instead of finding the remainder, it is incorrectly swapping the values of a and b. I'll correct this error by modifying the gcd function using the write_file command, reflecting the correct Euclidean algorithm.\",\n",
" \"action\": {\n",
" \"name\": \"write_file\",\n",
" \"arguments\": {\n",
" \"start_line\": 0,\n",
" \"end_line\": 15,\n",
" \"content\": \"def gcd(a, b):\\n while b != 0:\\n a, b = b, a % b\\n return a\\n\\n\\ndef lcm(a, b):\\n return (a * b) // gcd(a, b)\\n\\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",
" \"file_path\": \"gcd.py\"\n",
" }\n",
" }\n",
"}\n",
"assistant: \n",
"====Observation====\n",
"WRITE OPERATION:\n",
"You have written to \"gcd.py\" on these lines: 0:15.def gcd(a, b):\n",
" while b != 0:\n",
" a, b = b, a % b\n",
" return a\n",
"\n",
"\n",
"def lcm(a, b):\n",
" return (a * b) // gcd(a, b)\n",
"\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 should run the python file to see if the changes I made have fixed the gcd function.\",\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 has been fixed and tested successfully. Therefore, 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 SWE-agent assistant 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 enable agent with general programming ablities 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