Overview

It is crucial for agents’ outputs to be usable. A generated text may not always be useful. Sometimes, we may want to perform various analyses and request the analysis of different points.

In such cases, using ObjectResponse greatly simplifies your work and allows you to directly obtain objects instead of getting a whole text.

Agent with Object Response

This agent will do the same thing as the agent above, fetching OpenAI developments, but this time it will bring back the results in a format we desire: a programmable and usable output.

agent_with_object_response.py
from upsonic import Agent, Task
from upsonic.client.tools import Search

from upsonic import ObjectResponse # Importing Object Response

class ANew(ObjectResponse):
  title: str
  content: str
class News(ObjectResponse):
  news: lists[ANew] # We want to get a list of ANew objects

task = Task(
  "Find the latest OpenAI developments on the internet.", 
  tools=[Search],
  response_format=News # Specifying the Response that we want
)

agent = Agent("Reporter")

agent.print_do(task)

When you want to use the news:

agent_with_object_response.py
result = task.response

for each_new in result.news:
  print()
  print("Title:", each_new.title)
  print("Content:", each_new.content)

To run the agent, install dependencies and export your OPENAI_API_KEY.

1

Setup your virtual environment

2

Install dependencies

3

Put your OpenAI key

4

Run the agent to do the task

python agent_with_object_response.py