pass a returned file from a function to another function with agent #5238
Replies: 4 comments 10 replies
-
I am not sure what's the blocker here is? You can call one agent to list the files, and then call other agent to read the files. Or, having the same agent with both tools, and call the agent twice. Could you describe a bit more what is the context and scenario? By just reading the description it feels like this should be accomplished by just calling two agents or one agent two times. |
Beta Was this translation helpful? Give feedback.
-
Hi Eric . I tried to use sequential chat but still I can only receive the
dictionary of python files which is the first step. for the second step I
should somehow give this dictionary and pass it through another agent to
call read_file_content but it can not get. here is my code could you tell
me what is the problem with my prompt:
…On Tue, Jan 28, 2025 at 10:50 PM Eric Zhu ***@***.***> wrote:
@Zohreh6384NKH <https://github.com/Zohreh6384NKH> I believe group chat is
a wrong choice for your scenario. You have deterministic sequence of steps,
but group chat cannot provide that deterministic sequence as it uses an LLM
to choose the next step.
f"Please follow these steps sequentially:\n"
If your steps are sequential, why using group chat?
What will work well for you, is to call each agent in sequence manually,
and pass relevant chat history as context from one agent to the next one.
Group chat is for handling ad-hoc scenarios when for each task, the steps
are decided on the fly. This is similar to a single agent with all the
tools -- the order of tool invocation is different for different tasks. If
you have such scenario, choose GroupChat. You can add some determinism by
using a selector function.
I can see you are using an outdated v0.2 API. I highly recommend upgrading
to v0.4. Make sure you install autogen-agentchat~=0.4.0 to upgrade your
AutoGen.
—
Reply to this email directly, view it on GitHub
<#5238 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BFLSJZJHIQPVIPSUD5F526L2NACSJAVCNFSM6AAAAABWBFV372VHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTCOJYHE3TGOA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Thanks Eric . I just wanted to do it in an agentic workflow. Now that I
have the content of the files, do you think for extracting helper functions
in a code I can create a conversable agent that does the task for me?
…On Wed, Jan 29, 2025 at 5:19 PM Eric Zhu ***@***.***> wrote:
@Zohreh6384NKH <https://github.com/Zohreh6384NKH> I believe you shouldn't
be using AI agents. Use VSCode + GitHub Copilot I can quickly generate the
following code that does what you want, 100 % of the time.
import osimport tempfilefrom typing import Dict, List
def list_python_files_by_directory(dir: str) -> Dict[str, List[str]]:
""" Get all the python files in the given directory, grouped by the directory they are in. """
python_files = dict()
for root, _, files in os.walk(dir):
python_files[root] = [file for file in files if file.endswith(".py")]
return python_files
def read_files_content(files: Dict[str, List[str]]) -> Dict[str, str]:
""" Reads the content of the given files and returns a dictionary with the full file name as the key and the content as the value. """
files_content = dict()
for directory, files in files.items():
for file in files:
with open(os.path.join(directory, file), "r") as f:
files_content[os.path.join(directory, file)] = f.read()
return files_content
if __name__ == "__main__":
with tempfile.TemporaryDirectory() as tmpdirname:
print(f"Created temporary directory: {tmpdirname}")
# Put some files in the temporary directory
with open(os.path.join(tmpdirname, "file1.py"), "w") as f:
f.write("print('Hello, World!')")
with open(os.path.join(tmpdirname, "file2.py"), "w") as f:
f.write("print('Hello, Python!')")
python_files = list_python_files_by_directory(tmpdirname)
files_content = read_files_content(python_files)
for file, content in files_content.items():
print(f"File: {file}\nContent:\n{content}\n")
Maybe you can add a prompting step at the end just to get some natural
language response.
—
Reply to this email directly, view it on GitHub
<#5238 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BFLSJZJJF3264SHG7T64VL32NEERPAVCNFSM6AAAAABWBFV372VHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTCOJZHAZDANI>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Hi Eric. I have an issue with this code. I wanted to ask an agent to
generate a code that can extract all imported modules from a python file.
but I am getting this error continuously. Could you help me with this
please??
I don't know why I get this error: no such file or directory : 'python'.
here is the code
# Configuration for the LLM (assistant)
config_list = [{
"model": "llama3.1",
"api_type": "ollama",
"client_host": "http://localhost:11434",
"timeout": 120,
}]
llm_config = {"config_list": config_list}
# Create the AssistantAgent
assistant = autogen.AssistantAgent(
name="assistant",
llm_config={
"cache_seed": None,
"config_list": config_list,
"temperature": 0,
}
)
# Create the UserProxyAgent
user_proxy = autogen.UserProxyAgent(
name="user_proxy",
human_input_mode="NEVER",
max_consecutive_auto_reply=10,
is_termination_msg=lambda x: x.get("content", "").rstrip().endswith(
"TERMINATE"),
code_execution_config={
"executor": LocalCommandLineCodeExecutor(work_dir="coding"),
},
)
file_path =
"/home/zohreh/workspace/my_project/clone_repo/dinov2/dinov2/train/train.py"
chat_res = user_proxy.initiate_chat(
assistant,
message="""read the file at {file_path} and return the all imported module
""".format(file_path=file_path),
summary_method="reflection_with_llm",
)
here is the error:
user_proxy (to assistant):
read the file at
/home/zohreh/workspace/my_project/clone_repo/dinov2/dinov2/train/train.py
and return the all imported module
--------------------------------------------------------------------------------
assistant (to user_proxy):
```python
# filename: get_imported_modules.py
import os
def get_imported_modules(file_path):
try:
with open(file_path, 'r') as file:
content = file.read()
import ast
tree = ast.parse(content)
imported_modules = [node.name for node in tree.body if
isinstance(node, ast.Import)]
return imported_modules
except Exception as e:
print(f"Error reading file: {e}")
return []
file_path =
'/home/zohreh/workspace/my_project/clone_repo/dinov2/dinov2/train/train.py'
print(get_imported_modules(file_path))
```
Please execute this code. It will read the specified file and return a list
of imported modules. If there's an error, it will print the error message
and return an empty list.
After executing the code, I'll analyze the output to provide the final
answer.
--------------------------------------------------------------------------------
>>>>>>> EXECUTING CODE BLOCK (inferred language is python)...
Traceback (most recent call last):
File
"/home/zohreh/workspace/my_project/clone_repo/code_summarization/all_import_from_file.py",
line 122, in <module>
chat_res = user_proxy.initiate_chat(
^^^^^^^^^^^^^^^^^^^^^^^^^
File
"/usr/local/lib/python3.12/dist-packages/autogen/agentchat/conversable_agent.py",
line 1106, in initiate_chat
self.send(msg2send, recipient, silent=silent)
File
"/usr/local/lib/python3.12/dist-packages/autogen/agentchat/conversable_agent.py",
line 741, in send
recipient.receive(message, self, request_reply, silent)
File
"/usr/local/lib/python3.12/dist-packages/autogen/agentchat/conversable_agent.py",
line 908, in receive
self.send(reply, sender, silent=silent)
File
"/usr/local/lib/python3.12/dist-packages/autogen/agentchat/conversable_agent.py",
line 741, in send
recipient.receive(message, self, request_reply, silent)
File
"/usr/local/lib/python3.12/dist-packages/autogen/agentchat/conversable_agent.py",
line 906, in receive
reply = self.generate_reply(messages=self.chat_messages[sender],
sender=sender)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File
"/usr/local/lib/python3.12/dist-packages/autogen/agentchat/conversable_agent.py",
line 2060, in generate_reply
final, reply = reply_func(self, messages=messages, sender=sender,
config=reply_func_tuple["config"])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File
"/usr/local/lib/python3.12/dist-packages/autogen/agentchat/conversable_agent.py",
line 1558, in _generate_code_execution_reply_using_executor
code_result = self._code_executor.execute_code_blocks(code_blocks)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File
"/usr/local/lib/python3.12/dist-packages/autogen/coding/local_commandline_code_executor.py",
line 257, in execute_code_blocks
return self._execute_code_dont_check_setup(code_blocks)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File
"/usr/local/lib/python3.12/dist-packages/autogen/coding/local_commandline_code_executor.py",
line 316, in _execute_code_dont_check_setup
result = subprocess.run(
^^^^^^^^^^^^^^^
File "/usr/lib/python3.12/subprocess.py", line 548, in run
with Popen(*popenargs, **kwargs) as process:
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.12/subprocess.py", line 1026, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/usr/lib/python3.12/subprocess.py", line 1955, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'python'
…On Wed, Jan 29, 2025 at 6:00 PM Eric Zhu ***@***.***> wrote:
Yes. Just pass it to the prompt.
Though I think you should think about ways to do this without using LLM.
E.g., AST parser
—
Reply to this email directly, view it on GitHub
<#5238 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BFLSJZLONRUKJ7NUYNMNGHD2NEJMFAVCNFSM6AAAAABWBFV372VHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTCOJZHA2TONQ>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
hi could you please help me wih this. i have a function which return a dictionary of files .now i want to get an agent to pass this dictionary to another function as input and want an agent to call the fucntion. how agent can get the output and pass it as input of another function
Beta Was this translation helpful? Give feedback.
All reactions