Find answers from the community

Updated 3 days ago

I am running the StructuredPlannerAgent how do I get the state of what was set internal t

@kapa.ai I am running the StructuredPlannerAgent how do I get the state of what was set internal the o the agent? it is not outputting what i want
k
c
10 comments
Please stand by @cmosguy as I search the knowledge base. I normally take about 15 seconds or so. :wave:
----------------------
I understand you're having trouble accessing the internal state of the StructuredPlannerAgent. Let me help you with that.

The StructuredPlannerAgent maintains an internal state that includes information about plans, tasks, and their execution status. You can access this state directly through the agent's state attribute.

Here's how you can access and inspect the state:

Plain Text
# Assuming you've already created your agent
agent = StructuredPlannerAgent(worker, tools=[lyft_tool, uber_tool], verbose=True)

# Create a plan
plan_id = agent.create_plan("Your task description here")

# Access the plan from the state
plan = agent.state.plan_dict[plan_id]

# Inspect the plan's sub-tasks
for sub_task in plan.sub_tasks:
    print(f"===== Sub Task {sub_task.name} =====")
    print("Expected output: ", sub_task.expected_output)
    print("Dependencies: ", sub_task.dependencies)


You can also check which tasks are ready to be executed next:

Plain Text
next_tasks = agent.state.get_next_sub_tasks(plan_id)


And after executing tasks, you can mark them as complete:

Plain Text
agent.mark_task_complete(plan_id, sub_task.name)


If you want to see if there are any remaining tasks:

Plain Text
next_tasks = agent.get_next_tasks(plan_id)
if len(next_tasks) == 0:
    print("All tasks completed!")


These examples are based on the low-level API usage shown in the StructuredPlannerAgent documentation.

If you're looking for more detailed information about a specific aspect of the agent's state, please let me know!

kapa.ai
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
@kapa.ai lets say the response finishes form the structurePlannerAgent, what do I need to do output a structured output?
Let me check the knowledge base for you again. One moment! :books:
----------------------
I understand you want to get a structured output from the StructuredPlannerAgent after it completes its execution. Here's how you can do that:

When the StructuredPlannerAgent finishes its execution, you need to finalize the response to get the structured output. Based on the knowledge sources, there are a few approaches:

Using finalize_response


Once all tasks are complete, you can use the finalize_response method to get the final structured output:

Plain Text
# Assuming the agent has completed all tasks
response = agent.finalize_response(task_id)


This method takes the task ID and returns the final response in a structured format. If you're using the step-by-step approach, you can also pass the last step output:

Plain Text
response = agent.finalize_response(task_id, step_output=step_output)


The finalize_response method will:
  1. Validate that the step is the last one
  2. Finalize the task
  3. Attach all sources generated across all steps
  4. Return the structured output

For Asynchronous Execution


If you're using asynchronous execution, you can use the afinalize_response method instead:

Plain Text
response = await agent.afinalize_response(task_id)


Structured Outputs with Pydantic


For more specific structured outputs, you can leverage LlamaIndex's structured output capabilities with Pydantic:

Plain Text
from pydantic import BaseModel

# Define your output structure
class YourStructuredOutput(BaseModel):
    field1: str
    field2: int
    # Add more fields as needed

# Configure your query engine to return this structure
structured_response = agent.query_engine.query_with_structured_output(
    YourStructuredOutput,
    "Your query here"
)


For more details on structured outputs, you can refer to the Structured Outputs guide.
Is there a specific type of structured output you're looking to get from the StructuredPlannerAgent?
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
@kapa.ai how do I make a structured output as a function tool?
Let me check the knowledge base for you again. One moment! :books:
----------------------
I'll explain how to create a structured output as a function tool in LlamaIndex.

To create a function tool that returns structured output, you can use the FunctionTool class along with Pydantic models for structured data. Here's how to do it:

  1. Define a Pydantic model for your structured output


First, define the structure of your output using Pydantic:

Plain Text
from pydantic import BaseModel
from typing import List

class Song(BaseModel):
    title: str
    length_seconds: int

class Album(BaseModel):
    name: str
    artist: str
    songs: List[Song]


  1. Create a function that returns your structured output


Plain Text
def get_album_info() -> Album:
    """Returns information about an album in a structured format"""
    return Album(
        name="Example Album",
        artist="Example Artist",
        songs=[
            Song(title="Song 1", length_seconds=180),
            Song(title="Song 2", length_seconds=210)
        ]
    )


  1. Create a FunctionTool from your function


Plain Text
from llama_index.core.tools import FunctionTool

album_tool = FunctionTool.from_defaults(
    fn=get_album_info,
    name="album_info_tool",
    description="Tool that provides structured information about an album"
)


  1. Use the tool in an agent


Plain Text
from llama_index.core.agent import ReActAgent
from llama_index.llms.openai import OpenAI

llm = OpenAI(model="gpt-3.5-turbo")
agent = ReActAgent.from_tools([album_tool], llm=llm, verbose=True)

response = agent.chat("Tell me about the album")


Converting to LangChain Structured Tool


If you're using LangChain, you can convert your FunctionTool to a LangChain structured tool:

Plain Text
langchain_structured_tool = album_tool.to_langchain_structured_tool(verbose=True)
This method is documented in the FunctionTool API reference.

Using FunctionCallingProgram for Structured Extraction


For more complex scenarios, you can use the FunctionCallingProgram to extract structured data:

Plain Text
from llama_index.program.openai import FunctionCallingProgram

prompt_template_str = "Generate an example album, with an artist and a list of songs."

program = FunctionCallingProgram.from_defaults(
    output_cls=Album,
    prompt_template_str=prompt_template_str,
    verbose=True,
    llm=llm,
)

output = program()  # This will return an Album object


This approach is shown in the NVIDIA output parsing example.

Would you like more details on any specific part of this process?

kapa.ai
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Add a reply
Sign up and join the conversation on Discord