Library
如果您想将TaskWeaver作为库使用,可以参考以下代码示例:
from taskweaver.app.app import TaskWeaverApp
# This is the folder that contains the taskweaver_config.json file and not the repo root. Defaults to "./project/"
app_dir = "./project/"
app = TaskWeaverApp(app_dir=app_dir)
session = app.get_session()
user_query = "hello, what can you do?"
response_round = session.send_message(user_query)
print(response_round.to_dict())
本质上,您需要创建一个TaskWeaverApp对象,然后从中获取一个session对象。
每次您都可以通过调用session.send_message(user_query)向TaskWeaver发送消息。
session.send_message(user_query)的返回值是一个Round对象,其中包含来自TaskWeaver的响应。
一个round代表用户与TaskWeaver之间的一轮对话,包含一系列posts。
下面展示了一个Round对象的示例。如需更好地理解其结构,可以参考概念部分。
{
"id": "round-20231201-043134-218a2681",
"user_query": "hello, what can you do?",
"state": "finished",
"post_list": [
{
"id": "post-20231201-043134-10eedcca",
"message": "hello, what can you do?",
"send_from": "User",
"send_to": "Planner",
"attachment_list": []
},
{
"id": "post-20231201-043141-86a2aaff",
"message": "I can help you with various tasks, such as counting rows in a data file, detecting anomalies in a dataset, searching for products on Klarna, summarizing research papers, and pulling data from a SQL database. Please provide more information about the task you want to accomplish, and I'll guide you through the process.",
"send_from": "Planner",
"send_to": "User",
"attachment_list": [
{
"id": "atta-20231201-043141-6bc4da86",
"type": "init_plan",
"content": "1. list the available functions"
},
{
"id": "atta-20231201-043141-6f29f6c9",
"type": "plan",
"content": "1. list the available functions"
},
{
"id": "atta-20231201-043141-76186c7a",
"type": "current_plan_step",
"content": "1. list the available functions"
}
]
}
]
}
tip
如需查看对话的中间状态,您需要实现一个SessionEventHandler类,并在调用session.send_message(user_query, event_handler=your_event_handler)时传入该处理器。更多关于事件处理器的信息请参阅此章节。