{
 "cells": [
  {
   "cell_type": "raw",
   "id": "9cebfedd",
   "metadata": {},
   "source": [
    "---\n",
    "summary: Building a basic chatbot.\n",
    "title: Chat\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cfa8d479",
   "metadata": {},
   "source": [
    "By the end of this tutorial, you will have built a functional AI chatbot that can handle user input and exit gracefully.\n",
    "\n",
    "We'll start by building the logic from scratch so you can see how the interaction loop works, and then we'll use Yera's built-in shortcuts to simplify the code.\n",
    "\n",
    "## A Basic Interaction Loop\n",
    "\n",
    "Fundamentally, a chatbot is just a loop where the user sends a prompt, the prompt\n",
    "is submitted to the LLM, and receives the response.\n",
    "You've already seen the two pieces we'll use in that loop\n",
    "\n",
    "- `yr.text_input` lets you request text input from the user.\n",
    "- `yr.response` lets you send a prompt to the active LLM.\n",
    "\n",
    "Let's start by importing Yera and creating a chatbot function.\n",
    "You'll want a loop with the input and response bits inside"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8bfe59d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import yera as yr\n",
    "\n",
    "@yr.app\n",
    "def chatbot():\n",
    "    while True:\n",
    "      prompt = yr.text_input()\n",
    "      yr.response(prompt)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69946c60",
   "metadata": {},
   "source": [
    "Try it out now.\n",
    "\n",
    "Notice that there's no easy way to quit the program, it'll just keep looping\n",
    "unless you force it to quit. This clearly isn't a great user experience.\n",
    "\n",
    "We don't want them stuck in there forever, so we should also add the ability to\n",
    "quit by typing `/quit`. Inside the loop, between the input and response we detect this,\n",
    "and trigger `yr.quit` and `break` out of the loop.\n",
    "\n",
    "`yr.quit` is a new function for you and represents a normal exit of a Yera program such\n",
    "as a user quitting. It clears up the running process and\n",
    "other bits that are supporting your app behind the scenes.\n",
    "\n",
    "Give it a try by running your program again, asking a few questions and then\n",
    "typing \"/quit\". You should see your chatbot exit gracefully.\n",
    "\n",
    "Here's what you should have now."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f718edbd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import yera as yr\n",
    "\n",
    "@yr.app\n",
    "def chatbot():\n",
    "    while True:\n",
    "        prompt = yr.text_input()\n",
    "\n",
    "        if prompt.startswith(\"/quit\"):\n",
    "            yr.quit()\n",
    "            break\n",
    "\n",
    "        yr.response(prompt)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c16f0e34",
   "metadata": {},
   "source": [
    "## The `yr.chat` Function\n",
    "\n",
    "The code you just wrote is a pattern used repeatedly when building\n",
    "interactive agents: get input, check for user quit, do something with it.\n",
    "Yera has a handy function that implements it for you so you don't have to keep\n",
    "writing it yourself.\n",
    "\n",
    "Using `yr.chat` you can build a chatbot in a few lines:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "819bdff5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import yera as yr\n",
    "\n",
    "@yr.app\n",
    "def chatbot():\n",
    "    for prompt in yr.chat():\n",
    "        yr.response(prompt)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fcc31bfa",
   "metadata": {},
   "source": [
    "Try running this and doing the same stuff as before. You'll see it behaves the same\n",
    "way and exits gracefully when asked as well.\n",
    "\n",
    "## Summary\n",
    "\n",
    "You now have your first working chatbot and know how to build interactive agent\n",
    "loops in Yera.\n",
    "In the next tutorial we'll look at `yr.Struct`, Yera's feature for extracting\n",
    "structured data from text.\n",
    "This is the first step towards building your first data pipeline."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
