Skip to content

Commit f4b0c82

Browse files
committed
agent
1 parent b0b169b commit f4b0c82

File tree

6 files changed

+213
-243
lines changed

6 files changed

+213
-243
lines changed

main.py

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,3 @@
1-
# from langchain.agents import initialize_agent
2-
# from langchain_experimental.utilities import PythonREPL
3-
# from langchain_community.tools.ddg_search.tool import DuckDuckGoSearchRun
4-
# from langchain_core.tools import Tool
5-
# from langchain_groq import ChatGroq
6-
7-
# # Create the Python REPL tool
8-
# python_repl = PythonREPL()
9-
# python_repl_tool = Tool(
10-
# name="python_repl",
11-
# description="A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.",
12-
# func=python_repl.run,
13-
# )
14-
15-
# # Create the DuckDuckGo search tool
16-
# duckduckgo_search = DuckDuckGoSearchRun()
17-
# duckduckgo_search_tool = Tool(
18-
# name="duckduckgo_search",
19-
# description="A wrapper around DuckDuckGo Search. Useful for when you need to answer questions about current events. Input should be a search query.",
20-
# func=duckduckgo_search.run,
21-
# )
22-
23-
# # Create the list of tools
24-
# tools = [python_repl_tool, duckduckgo_search_tool]
25-
26-
# # Initialize the LLM
27-
# llm = ChatGroq(temperature=0, groq_api_key="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", model_name="llama3-70b-8192")
28-
29-
# # Initialize the agent
30-
# agent = initialize_agent(tools, llm, agent_type="zero-shot-react-description")
31-
32-
# # Run the agent
33-
# while True:
34-
# user_input = input("Enter a command or search query (or 'quit' to stop): ")
35-
# if user_input.lower() == 'quit':
36-
# break
37-
# result = agent.run(user_input)
38-
# print(result)
39-
40-
411

422
from langchain.agents import initialize_agent
433
from langchain_experimental.utilities import PythonREPL

openai-agent.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import logging
2+
from crewai import Agent, Task, Crew, Process
3+
from langchain.agents import Tool
4+
from langchain_experimental.utilities import PythonREPL
5+
from langchain_community.tools import DuckDuckGoSearchRun
6+
7+
import os
8+
os.environ["OPENAI_API_KEY"] = "your_key"
9+
10+
# Create the Python REPL tool
11+
python_repl = PythonREPL()
12+
python_repl_tool = Tool(
13+
name="python_repl",
14+
description="This tool can execute python code and shell commands Use with caution",
15+
func=python_repl.run,
16+
)
17+
18+
# Create the DuckDuckGo search tool
19+
duckduckgo_search_tool = Tool(
20+
name="duckduckgo_search",
21+
description="A wrapper around DuckDuckGo Search.",
22+
func=DuckDuckGoSearchRun().run,
23+
)
24+
25+
class CustomAgent(Agent):
26+
def __init__(self, *args, **kwargs):
27+
super().__init__(*args, **kwargs)
28+
self.max_attempts = 3
29+
self.attempts = 0
30+
31+
def run(self, task):
32+
if self.attempts < self.max_attempts:
33+
self.attempts += 1
34+
return super().run(task)
35+
else:
36+
return "Task failed after {} attempts.".format(self.max_attempts)
37+
38+
coderAgent = CustomAgent(
39+
role='Senior Software engineer and developer',
40+
goal='Write production grade bug free code on this user prompt :- {topic}',
41+
verbose=True,
42+
memory=True,
43+
backstory="You are an experienced developer in big tech companies",
44+
max_iter=5,
45+
max_rpm=2,
46+
tools=[duckduckgo_search_tool],
47+
allow_delegation=True
48+
)
49+
50+
DebuggerAgent = CustomAgent(
51+
role='Code Debugger and bug solving agent',
52+
goal='You debug the code line by line and solve bugs and errors in the code by using Python_repl tool',
53+
verbose=True,
54+
memory=True,
55+
backstory="You are a debugger agent with access to a python interpreter",
56+
tools=[duckduckgo_search_tool, python_repl_tool],
57+
max_iter=5,
58+
max_rpm=2,
59+
allow_delegation=True
60+
)
61+
62+
coding_task = Task(
63+
description="Write code in this {topic}.",
64+
expected_output='A Bug-free and production-grade code on {topic}',
65+
tools=[duckduckgo_search_tool],
66+
agent=coderAgent,
67+
)
68+
69+
debug_task = Task(
70+
description="You should run the python code given by the CoderAgent and Check for bugs and errors",
71+
expected_output='you should communicate to CoderAgent and give feedback on the code if the code got error while execution',
72+
tools=[duckduckgo_search_tool, python_repl_tool],
73+
agent=DebuggerAgent,
74+
output_file='temp.py'
75+
)
76+
77+
Final_check = Task(
78+
description="You fill finalize the Code which is verified by debugger agent Which is error free no bugs",
79+
expected_output="You should communicate to DebuggerAgent and if the code is bug free and executed Without errors then return the code to user",
80+
agent=coderAgent,
81+
tools=[duckduckgo_search_tool]
82+
)
83+
84+
crew = Crew(
85+
agents=[coderAgent, DebuggerAgent],
86+
tasks=[coding_task, debug_task, Final_check],
87+
process=Process.sequential,
88+
memory=True,
89+
cache=True,
90+
max_rpm=5,
91+
share_crew=True
92+
)
93+
94+
result = crew.kickoff(inputs={'topic': 'Write me code for A* searching algo using python'})
95+
print(result)

python-coding-agent.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import logging
2+
from crewai import Agent, Task, Crew, Process
3+
from langchain.agents import Tool
4+
from langchain_experimental.utilities import PythonREPL
5+
from langchain_community.tools import DuckDuckGoSearchRun
6+
from langchain_groq import ChatGroq
7+
8+
import os
9+
os.environ["OPENAI_API_KEY"] = "Your_api"
10+
11+
llm = ChatGroq(temperature=0, groq_api_key="Your_api", model_name="llama3-70b-8192")
12+
13+
14+
# Create the Python REPL tool
15+
python_repl = PythonREPL()
16+
python_repl_tool = Tool(
17+
name="python_repl",
18+
description="This tool can execute python code and shell commands (pip commands to modules installation) Use with caution",
19+
func=python_repl.run,
20+
)
21+
22+
# Create the DuckDuckGo search tool
23+
duckduckgo_search_tool = Tool(
24+
name="duckduckgo_search",
25+
description="A wrapper around DuckDuckGo Search.",
26+
func=DuckDuckGoSearchRun().run,
27+
)
28+
29+
coderAgent = Agent(
30+
role='Senior Software engineer and developer',
31+
goal='Write production grade bug free code on this user prompt :- {topic}',
32+
verbose=True,
33+
memory=True,
34+
backstory="You are an experienced developer in big tech companies",
35+
max_iter=3,
36+
llm = llm,
37+
max_rpm=10,
38+
tools=[duckduckgo_search_tool],
39+
allow_delegation=False
40+
)
41+
42+
DebuggerAgent = Agent(
43+
role='Code Debugger and bug solving agent',
44+
goal='You debug the code line by line and solve bugs and errors in the code by using Python_repl tool. You also have Access to search tool which can assist you for searching that bug',
45+
verbose=True,
46+
memory=True,
47+
backstory="You are a debugger agent with access to a python interpreter",
48+
tools=[duckduckgo_search_tool, python_repl_tool],
49+
max_iter=3,
50+
llm = llm,
51+
max_rpm=10,
52+
allow_delegation=True
53+
)
54+
55+
coding_task = Task(
56+
description="Write code in this {topic}.",
57+
expected_output='A Bug-free and production-grade code on {topic}',
58+
tools=[duckduckgo_search_tool],
59+
agent=coderAgent,
60+
)
61+
62+
debug_task = Task(
63+
description="You should run the python code given by the CoderAgent and Check for bugs and errors",
64+
expected_output='you should communicate to CoderAgent and give feedback on the code if the code got error while execution',
65+
tools=[duckduckgo_search_tool, python_repl_tool],
66+
agent=DebuggerAgent,
67+
output_file='temp.py'
68+
)
69+
70+
Final_check = Task(
71+
description="You fill finalize the Code which is verified by debugger agent Which is error free no bugs",
72+
expected_output="You should communicate to DebuggerAgent and if the code is bug free and executed Without errors then return the code to user",
73+
agent=coderAgent,
74+
tools=[duckduckgo_search_tool]
75+
)
76+
77+
crew = Crew(
78+
agents=[coderAgent, DebuggerAgent],
79+
tasks=[coding_task, debug_task, Final_check],
80+
process=Process.sequential,
81+
memory=True,
82+
cache=True,
83+
max_rpm=25,
84+
share_crew=True
85+
)
86+
87+
result = crew.kickoff(inputs={'topic': 'Write me code for djkastra searching algo using python'})
88+
print(result)

t.py

Lines changed: 0 additions & 39 deletions
This file was deleted.

temp.py

Lines changed: 30 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,41 @@
1-
from langchain.agents import initialize_agent
2-
from langchain_experimental.utilities import PythonREPL
3-
from langchain_community.tools.ddg_search.tool import DuckDuckGoSearchRun
4-
from langchain_core.tools import Tool
5-
from langchain_groq import ChatGroq
6-
from langchain_community.agent_toolkits import FileManagementToolkit
7-
# from tempfile import TemporaryDirectory
8-
import os
1+
The code has a bug. The error is a NameError("name 'sys' is not defined"). This error occurs because the 'sys' module is not imported.
92

10-
# Create the temporary directory for file operations
11-
working_directory = os.getcwd()
3+
The corrected code is:
124

13-
# Create the file management toolkit
14-
file_management_toolkit = FileManagementToolkit(root_dir=str(working_directory))
15-
file_tools = file_management_toolkit.get_tools()
5+
```
6+
import sys
7+
import heapq
168

17-
# Extract individual file operation tools
18-
read_tool, write_tool, list_tool, copy_tool, delete_tool, move_tool, search_tool = file_tools
9+
def dijkstra(graph, start):
10+
distances = {node: sys.maxsize for node in graph}
11+
distances[start] = 0
12+
pq = [(0, start)]
1913

20-
# Wrap the file operation tools with Tool class
21-
read_file_tool = Tool(
22-
name="read_file",
23-
description="Read a file from the file system.",
24-
func=read_tool.invoke,
25-
)
14+
while pq:
15+
current_distance, current_node = heapq.heappop(pq)
2616

27-
write_file_tool = Tool(
28-
name="write_file",
29-
description="Write a file to the file system.",
30-
func=write_tool.invoke,
31-
)
17+
if current_distance > distances[current_node]:
18+
continue
3219

33-
list_directory_tool = Tool(
34-
name="list_directory",
35-
description="List files in a directory.",
36-
func=list_tool.invoke,
37-
)
20+
for neighbor, weight in graph[current_node].items():
21+
distance = current_distance + weight
3822

39-
# Create the Python REPL tool
40-
python_repl = PythonREPL()
41-
python_repl_tool = Tool(
42-
name="python_repl",
43-
description="A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.",
44-
func=python_repl.run,
45-
)
23+
if distance < distances[neighbor]:
24+
distances[neighbor] = distance
25+
heapq.heappush(pq, (distance, neighbor))
4626

47-
# Create the DuckDuckGo search tool
48-
duckduckgo_search = DuckDuckGoSearchRun()
49-
duckduckgo_search_tool = Tool(
50-
name="duckduckgo_search",
51-
description="A wrapper around DuckDuckGo Search. Useful for when you need to answer questions about current events. Input should be a search query.",
52-
func=duckduckgo_search.run,
53-
)
27+
return distances
5428

55-
# Create the list of tools
56-
tools = [python_repl_tool, duckduckgo_search_tool, read_file_tool, write_file_tool, list_directory_tool]
29+
graph = {
30+
'A': {'B': 1, 'C': 4},
31+
'B': {'A': 1, 'C': 2, 'D': 5},
32+
'C': {'A': 4, 'B': 2, 'D': 1},
33+
'D': {'B': 5, 'C': 1}
34+
}
5735

58-
# Initialize the LLM
59-
llm = ChatGroq(temperature=0, groq_api_key="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", model_name="llama3-8b-8192")
36+
start_node = 'A'
37+
result = dijkstra(graph, start_node)
38+
print(f"Shortest distances from {start_node}: {result}")
39+
```
6040

61-
# Initialize the agent
62-
agent = initialize_agent(tools, llm, agent_type="zero-shot-react-description")
63-
64-
# Run the agent
65-
while True:
66-
user_input = input("Enter a command or search query (or 'quit' to stop): ")
67-
if user_input.lower() == 'quit':
68-
break
69-
result = agent.run(user_input)
70-
print(result)
41+
This code should run without errors and produce the correct output.

0 commit comments

Comments
 (0)