Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 13 additions & 44 deletions examples/notebooks/1_getting_started_with_nat.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -528,53 +528,22 @@
"import asyncio\n",
"import sys\n",
"\n",
"from nat.runtime.loader import load_workflow\n",
"from nat.utils.type_utils import StrPath\n",
"\n",
"\n",
"async def get_callable_for_workflow(config_file: StrPath):\n",
" \"\"\"\n",
" Creates an end-to-end async callable which can run a NAT workflow.\n",
"\n",
" Note that this \"yields\" the callable so you have to access via an\n",
" asynchronous generator:\n",
"\n",
" async for callable in get_callable_for_workflow(..)):\n",
" # use callable here\n",
"\n",
" Args:\n",
" config_file (StrPath): a valid path to a NAT configuration file\n",
"\n",
" Yields:\n",
" The callable\n",
" \"\"\"\n",
" # load a given workflow from a configuration file\n",
" async with load_workflow(config_file) as workflow:\n",
"\n",
" # create an async callable that runs the workflow\n",
" async def single_call(input_str: str) -> str:\n",
"\n",
" # run the input through the workflow\n",
" async with workflow.run(input_str) as runner:\n",
" # wait for the result and cast it to a string\n",
" return await runner.result(to_type=str)\n",
"\n",
" yield single_call\n",
"from nat.runtime.loader import load_config\n",
"from nat.utils import run_workflow\n",
"\n",
"\n",
"async def amain():\n",
" async for callable in get_callable_for_workflow(sys.argv[1]):\n",
" # read queries from stdin and process them serially\n",
" query_num = 1\n",
" try:\n",
" while True:\n",
" query = input()\n",
" result = await callable(query)\n",
" print(f\"Query {query_num}: {query}\")\n",
" print(f\"Result {query_num}: {result}\")\n",
" query_num += 1\n",
" except EOFError:\n",
" pass\n",
" config = load_config(sys.argv[1])\n",
" query_num = 1\n",
" try:\n",
" while True:\n",
" query = input()\n",
" result = await run_workflow(config=config, prompt=query)\n",
" print(f\"Query {query_num}: {query}\")\n",
" print(f\"Result {query_num}: {result}\")\n",
" query_num += 1\n",
" except EOFError:\n",
" pass\n",
"\n",
"\n",
"asyncio.run(amain())"
Expand Down