自定义输出格式
使用此示例可以定义代理应该返回给你的输出格式。Copy
from pydantic import BaseModel
# 将输出格式定义为 Pydantic 模型
class Post(BaseModel):
post_title: str
post_url: str
num_comments: int
hours_since_post: int
class Posts(BaseModel):
posts: List[Post]
controller = Controller(output_model=Posts)
async def main():
task = '访问 hackernews show hn 并给我前 5 个帖子'
model = ChatOpenAI(model='gpt-4o')
agent = Agent(task=task, llm=model, controller=controller)
history = await agent.run()
result = history.final_result()
if result:
parsed: Posts = Posts.model_validate_json(result)
for post in parsed.posts:
print('\n--------------------------------')
print(f'标题: {post.post_title}')
print(f'URL: {post.post_url}')
print(f'评论数: {post.num_comments}')
print(f'发布时间(小时): {post.hours_since_post}')
else:
print('无结果')
if __name__ == '__main__':
asyncio.run(main())