Thursday, July 24, 2025

Agentic retrieval often requires functions to be invoked. This is how to do that:

def run_function_tools(query_text, account_id):

    project_client = AIProjectClient(endpoint=project_endpoint, credential=DefaultAzureCredential()) 

    agents_client = AgentsClient(

        endpoint=project_endpoint,

        credential=DefaultAzureCredential(),

    )

    from analyzer_functions import analyzer_functions, image_user_functions


    # Initialize function tool with user functions

    functions = FunctionTool(functions=image_user_functions)

    instructions = "You are an assistant that answers the question how many objects were found in an image when both are given by their image URI. You evaluate a function to do this by passing their uri to the function and respond with the count."

    query_text = f"How many objects given by its image URI {object_uri} are found in the image given by its image URI {scene_uri}?"

    with agents_client:

        # Create an agent and run user's request with function calls

        # agent = agents_client.get_agent(agent_id="asst_qyMFcz1BnU0BS0QUmhxAAyFk")

        # """

        agent = agents_client.create_agent(

            model=agent_model,

            name=fn_agent_name,

            instructions=instructions,

            tools=functions.definitions,

            tool_resources=functions.resources,

            top_p=1

        )

        # """

        #print(f"Created agent, ID: {agent.id}")


        thread = agents_client.threads.create()

        #print(f"Created thread, ID: {thread.id}")


        message = agents_client.messages.create(

            thread_id=thread.id,

            role="user",

            content=query_text,

        )

        #print(f"Created message, ID: {message.id}")


        run = agents_client.runs.create(thread_id=thread.id, agent_id=agent.id)

        #print(f"Created run, ID: {run.id}")


        while run.status in ["queued", "in_progress", "requires_action"]:

            time.sleep(1)

            run = agents_client.runs.get(thread_id=thread.id, run_id=run.id)


            if run.status == "requires_action" and isinstance(run.required_action, SubmitToolOutputsAction):

                tool_calls = run.required_action.submit_tool_outputs.tool_calls

                if not tool_calls:

                    print("No tool calls provided - cancelling run")

                    agents_client.runs.cancel(thread_id=thread.id, run_id=run.id)

                    break


                tool_outputs = []

                for tool_call in tool_calls:

                    if isinstance(tool_call, RequiredFunctionToolCall):

                        #print("Is an instance of RequiredFunctionToolCall")

                        try:

                            #print(f"Executing tool call: {tool_call}")

                            output = functions.execute(tool_call)

                            #print(output)

                            tool_outputs.append(

                                ToolOutput(

                                    tool_call_id=tool_call.id,

                                    output=output,

                                )

                            )

                        except Exception as e:

                            print(f"Error executing tool_call {tool_call.id}: {e}")

                    else:

                        print(f"{tool_call} skipped.")


                print(f"Tool outputs: {tool_outputs}")

                if tool_outputs:

                    agents_client.runs.submit_tool_outputs(thread_id=thread.id, run_id=run.id, tool_outputs=tool_outputs)

                else:

                    print(f"No tool output.")

            else:

                print(f"Waiting: {run}")


            print(f"Current run status: {run.status}")


        print(f"Run completed with status: {run.status} and details {run}")


        # Delete the agent when done

        # agents_client.delete_agent(agent.id)

        # print("Deleted agent")


        # Fetch and log all messages

        messages = agents_client.messages.list(thread_id=thread.id, order=ListSortOrder.ASCENDING)

        for msg in messages:

            if msg.text_messages:

                last_text = msg.text_messages[-1]

                print(f"{msg.role}: {last_text.text.value}")

                return last_text.text.value

        return None 

No comments:

Post a Comment