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

Multi Agent Code is giving errors #159

Open
ArpitaisAn0maly opened this issue Dec 6, 2024 · 1 comment
Open

Multi Agent Code is giving errors #159

ArpitaisAn0maly opened this issue Dec 6, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@ArpitaisAn0maly
Copy link

Operating System

Windows

Version Information

azureai-samples/scenarios/Assistants/multi-agent/multi-agent.ipynb at main · Azure-Samples/azureai-samples · GitHub. I am getting an error message on last code block "thread = assistant_client.beta.threads.create()


user_message = input("User Query: ")
message = dispatch_message(user_message, user_proxy, thread)"


The error message is below:

"{
"name": "AttributeError",
"message": "'ImageFileContentBlock' object has no attribute 'text'",
"stack": "---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[48], line 5
3 # Initiate proxy agent and the main thread. This thread will remain active until the task is completed and will serve as the main communication thread between the other agents.
4 user_message = input("User Query: ")
----> 5 message = dispatch_message(user_message, user_proxy, thread)

Cell In[24], line 49, in dispatch_message(message, agent, thread)
47 # Assign the appropriate function to the agent for invocation.
48 function_to_call = available_functions[call.function.name]
---> 49 tool_response = function_to_call(**json.loads(call.function.arguments))
50 tool_responses.append({"tool_call_id": call.id, "output": tool_response})
52 # Present the outcomes produced by the tool.

Cell In[23], line 31, in send_message(query, agent_name)
28 recipient_info["thread"] = thread_object
30 # This function dispatches a message to the proper agent and it's thread
---> 31 return dispatch_message(query, recipient_info["agent"], recipient_info["thread"])

Cell In[24], line 65, in dispatch_message(message, agent, thread)
62 messages = assistant_client.beta.threads.messages.list(thread_id=thread.id)
64 # Transmit the response message back to the facilitating agent.
---> 65 return messages.data[0].content[0].text.value

File /workspaces/azureai-assistant-tool/.venv/lib/python3.12/site-packages/pydantic/main.py:892, in BaseModel.getattr(self, item)
889 return super().getattribute(item) # Raises AttributeError if appropriate
890 else:
891 # this is the current error
--> 892 raise AttributeError(f'{type(self).name!r} object has no attribute {item!r}')

AttributeError: 'ImageFileContentBlock' object has no attribute 'text'"
}

Steps to reproduce

enter user query: " Generate an image of a boat drifting in the water and analyze it and enhance the image"

Expected behavior

Image should be generated

Actual behavior

error message above

Addition information

No response

@ArpitaisAn0maly ArpitaisAn0maly added the bug Something isn't working label Dec 6, 2024
@ArpitaisAn0maly
Copy link
Author

I followed the lead from other posts and was able to update multi_agent code. If anyone run into issue. here is the updated dispatch_message function.

import json
from openai.types.beta.threads.message import Message
from openai.types.beta.threads.text_content_block import TextContentBlock
from openai.types.beta.threads.image_file_content_block import ImageFileContentBlock

def dispatch_message(message: str, agent: Assistant, thread: Thread) -> str:
# Loops through all the agents functions to determine which function to use

available_functions = {}
# Iterate through each tool in the agent.tools list
for tool in agent.tools:
    # Check if the tool has a 'function' attribute
    if hasattr(tool, "function"):
        function_name = tool.function.name
        # Attempt to retrieve the function by its name and add it to the available_functions dictionary
        if function_name in globals():
            available_functions[function_name] = globals()[function_name]
    else:
        # Handle the case where the tool does not have a 'function' attribute
        print("This tool does not have a 'function' attribute.")
# Draft a new message as part of the ongoing conversation.
message = assistant_client.beta.threads.messages.create(thread_id=thread.id, role="user", content=message)
# Carry out the tasks delineated in this discussion thread.
run = assistant_client.beta.threads.runs.create(
    thread_id=thread.id,
    assistant_id=agent.id,
)
while True:
    # Await the completion of the thread execution.
    while run.status in ["queued", "in_progress"]:
        run = assistant_client.beta.threads.runs.retrieve(thread_id=thread.id, run_id=run.id)
        time.sleep(1)

    # If an action is necessary, initiate the appropriate function to perform the task.
    if run.status == "requires_action":
        tool_calls = run.required_action.submit_tool_outputs.tool_calls
        for _tool_call in tool_calls:
            tool_responses = []
            if (
                run.required_action.type == "submit_tool_outputs"
                and run.required_action.submit_tool_outputs.tool_calls is not None
            ):
                tool_calls = run.required_action.submit_tool_outputs.tool_calls
                for call in tool_calls:
                    if call.type == "function":
                        if call.function.name not in available_functions:
                            raise Exception("Function requested by the model does not exist")

                        # Assign the appropriate function to the agent for invocation.
                        function_to_call = available_functions[call.function.name]
                        tool_response = function_to_call(**json.loads(call.function.arguments))
                        tool_responses.append({"tool_call_id": call.id, "output": tool_response})

        # Present the outcomes produced by the tool.
        run = assistant_client.beta.threads.runs.submit_tool_outputs(
            thread_id=thread.id, run_id=run.id, tool_outputs=tool_responses
        )

    # if the run is completed, return the assistant message else provide error
    elif run.status == "failed":
        raise Exception("Run Failed. ", run.last_error)
    # Craft a reply from the assistant.
    else:
        messages = assistant_client.beta.threads.messages.list(thread_id=thread.id)
        if not messages:print("No messages received.")
        return "No response from assistant."

         # Print the user or Assistant messages or images
        for message in messages:
            for item in message.content:
                # Determine the content type
                if isinstance(item, TextContentBlock):
                    print(f"{message.role}:\n{item.text.value}\n")

                 # Optionally, you can add logic to capture Vision Assistant's feedback separately
                if message.role == "assistant" and "vision_assistant" in message.content[0].text.value.lower():
                    print("Vision Assistant Feedback: ", item.text.value)
                elif isinstance(item, ImageFileContentBlock):
                    # Retrieve image from file id
                    response_content = client.files.content(item.image_file.file_id)
                    data_in_bytes = response_content.read()
                    # Convert bytes to image
                    readable_buffer = io.BytesIO(data_in_bytes)
                    image = Image.open(readable_buffer)
                    # Resize image to fit in terminal
                    width, height = image.size
                    image = image.resize((width // 2, height // 2), Image.LANCZOS)
                    # Display image
                    image.show()
        print(f"Returning message content: {messages[0].content[0].text.value}")
        return messages[0].content[0].text.value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant