Your First AI Application With Smarter, Part 7: Deploying an AI Chatbot and Embedding It in a Web Page

Deploying a Smarter LLMClient gives it a url of its own, where any chat client, from a curl command to a widget on your website, can talk to it, with the same model, tools and guardrails it has in the workbench. In Part 6, Stanley, Stackademy’s course advisor, learned to answer from Stackademy’s student handbook. He knows the courses, the prerequisites and the rules, but only you have ever talked to him, in the workbench, signed in. In this part, he goes live. You’ll deploy him, ask him a question at his own url, and put him on a page of Stackademy’s website with one script tag.

The series so far, with Part 7 highlighted
Part 7 puts Stanley on a web page

You need Smarter 0.47.3 or later, and the smarter command-line tool 0.6.0 or later. smarter version tells you both.

The workbench is a sandbox

Every conversation you’ve had with Stanley so far went through the prompt engineering workbench, which Smarter calls sandbox mode. The workbench reaches Stanley through the web console, so only a user who is signed in, and allowed to read him, can talk to him there. That’s the right place to build an application, and the wrong place for a student.

A deployed LLMClient has its own host, made of its name, your account number and your platform’s api domain. A student’s browser, a script or another application sends it two kinds of requests. /config/ returns the chat’s configuration and a new chat session, and /prompt/ sends a message and returns Stanley’s answer.

Deploy Stanley

smarter deploy llmclient course_advisor
deployed.

Ask Smarter where he is now.

smarter describe llmclient course_advisor -o yaml
status:
  deployed: true
  url: http://course-advisor.3141-5926-5359.api.localhost:9357/

course-advisor is Stanley’s name, made fit for a hostname, and 3141-5926-5359 is your account’s number. On a platform on your own computer, his url ends in api.localhost:9357. Browsers send any name that ends in .localhost to your own computer, so his url works at once, without any DNS. On a platform in the cloud, the same command creates his DNS record, a Kubernetes ingress and a TLS certificate, and his url is an https:// one on your platform’s domain, a few minutes later.

The workbench’s Console has a Deployment tab, its far right one, which shows Stanley’s url and status, and everything that comes after deploying, such as the plugins that put him on a WordPress, Drupal or Shopify site.

The Deployment tab of course_advisor's workbench, showing his url
Stanley’s url, in the workbench’s Deployment tab

Talk to him at his url

A deployed LLMClient is an api for chat clients, so talk to it the way a chat client does. Start a chat session.

curl -X POST http://course-advisor.3141-5926-5359.api.localhost:9357/config/ \
  -H "Content-Type: application/json"

The response is the chat’s configuration, with Stanley’s welcome message, example prompts and colors, and a session_key. Send it back with a message.

curl -X POST http://course-advisor.3141-5926-5359.api.localhost:9357/prompt/ \
  -H "Content-Type: application/json" \
  -d '{"session_key": "<SESSION-KEY>", "messages": [{"role": "user", "content": "Which AI course should I take first?"}]}'

The first AI course in the catalogue is Artificial Intelligence (CS210). It has prerequisites, so take these first in order: Introduction to Computer Science (CS101), Programming Fundamentals (CS102), Data Structures (CS104), and Algorithms (CS107).

He looked up the catalogue and the prerequisites, as he does in the workbench. His guardrails came with him too. Ask him the capital of France at his url, and stackademy_topics answers for him, as it did in Part 5.

Nobody signed in to send that prompt. Stanley has no api key, so he’s public. Anyone who can reach his url can talk to him, and every prompt is charged to you, his owner. On your own computer, that’s only you. Before you deploy an LLMClient on a platform that the internet can reach, give it an api key, which every request must then carry, or a budget, which caps what it can spend. Part 8 covers budgets.

Allow Stackademy’s website

A web page that embeds Stanley calls his url from the student’s browser. The page and Stanley are on different origins, so the browser asks Stanley first whether the page may call him, and refuses if he doesn’t say yes. This check is called CORS, cross-origin resource sharing, and Stanley’s manifest decides the answer.

The page in this part runs at http://localhost:8000. Add it to course-advisor.yaml, under prompt, and change its version to 1.6.0.

      allowedOrigins:
        - http://localhost:8000

An origin is a scheme, a host and a port, with no path and no wildcard. Your real website’s would be something like https://www.stackademy.edu. Apply it, and commit.

smarter apply -f course-advisor.yaml
git add .
git commit -m "Stanley, deployed, for Stackademy's website"

Applying a deployed LLMClient’s manifest doesn’t undeploy it, so Stanley is still live. His manifest says nothing about whether he’s deployed, which leaves that to smarter deploy and smarter undeploy. A manifest can say deployed: true or deployed: false, and applying it then deploys or undeploys him.

Put him on a page

Save this as index.html, in a new folder.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>Stackademy</title>
  </head>
  <body>
    <h1>Learn to code, at your own pace.</h1>
    <p>Not sure where to start? Ask Stanley, our course advisor, with the chat button in the corner.</p>

    <script
      src="https://cdn.jsdelivr.net/npm/@smarter.sh/ui-chat@1/dist/embed.js"
      data-chatbot="http://course-advisor.3141-5926-5359.api.localhost:9357/"
      data-color="#fca311"
      data-label="Ask Stanley"
      async
    ></script>
  </body>
</html>

The script is Smarter Chat, the same chat that the workbench uses, from the @smarter.sh/ui-chat npm package, bundled with everything it needs into one file. data-chatbot is Stanley’s url, data-color the color of the chat button, and data-label its name for screen readers. The finished page has a little more styling.

Serve the folder from your terminal, with the web server that comes with Python.

python3 -m http.server 8000

Open http://localhost:8000. A button sits in the corner of the page. Click it, and Stanley opens in a panel, in Stackademy’s navy and orange, with his welcome message and example prompts.

Stackademy's page, with Stanley open in the chat panel, answering a question
Stanley, on Stackademy’s website

The chat loads only when a visitor opens it, so it costs the page nothing until then. It draws itself in a shadow DOM, a part of the page whose styles are its own, so the site’s css can’t change the chat, and the chat’s can’t change the site. Its colors and font come from the styles in Stanley’s manifest, which you set in Part 2, so the chat looks the same on every page that embeds him. Change the manifest and apply it, and every page follows.

A student's browser loads the page from localhost:8000, then the chat calls Stanley at his own url, which allows the page's origin
The page and Stanley are on different origins

One script tag works on any page, including a WordPress, Drupal, Squarespace or Shopify site, and a documentation site. A React application can import the chat as a component instead. Embedding Smarter Chat covers both, and the plugins that write the tag for you.

What’s next

Stanley is live. He has his own url, answers from his tools and his handbook inside his guardrails, and sits on a page of Stackademy’s website, all from manifests in a git repository. He’s also public, and every one of his answers costs money. In Part 8, the last part, you’ll give him a budget, read what he did in the Journal, and see what his account has spent.