scrimba
AI Engineering
AI Agents
Adding UI to agent - proof of concept
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

Adding UI to agent - proof of concept
AboutCommentsNotes
Adding UI to agent - proof of concept
Expand for more info
index.js
run
preview
console
import OpenAI from "openai"
import { getCurrentWeather, getLocation, functions } from "./tools"
import { renderNewMessage } from "./dom"

export const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
dangerouslyAllowBrowser: true
})

const availableFunctions = {
getCurrentWeather,
getLocation
}

async function agent(query) {
const messages = [
{
role: "system", content: `
You are a helpful AI agent. Transform technical data into engaging,
conversational responses, but only include the normal information a
regular person might want unless they explicitly ask for more. Provide
highly specific answers based on the information you're given. Prefer
to gather information with the tools provided to you rather than
giving basic, generic answers.
`
},
{ role: "user", content: query }
]
renderNewMessage(query, "user")

const runner = openai.beta.chat.completions.runFunctions({
model: "gpt-4-1106-preview",
messages,
functions
}).on("message", (message) => console.log(message))

const finalContent = await runner.finalContent()
renderNewMessage(finalContent, "assistant")
}

document.getElementById("form").addEventListener("submit", async function (event) {
event.preventDefault()
const inputElement = document.getElementById("user-input")
inputElement.focus()
const formData = new FormData(event.target)
const query = formData.get("user-input")
event.target.reset()
await agent(query)
})
Console
/index.html
-4:11