Here’s how to build a chatbot with Itzam.
import Itzam from "itzam"; const itzam = new Itzam(process.env.ITZAM_API_KEY); const workflowSlug = "turtle-chat"; // 1. Create a thread export const createThread = async (userId: string) => { const thread = await itzam.threads.create({ workflowSlug, // You can add more lookup keys to find the thread later lookupKeys: [userId], }); return thread; }; // 2. Send a message using the thread ID export const sendMessage = async (message: string, threadId: string) => { const response = await itzam.streamText({ input: message, threadId, // This will automatically add the older messages }); return response; }; // 3. Fetch user threads by the user ID export const getUserThreads = async (userId: string) => { const threads = await itzam.threads.list(workflowSlug, { lookupKeys: [userId], }); return threads; }; // 4. Fetch older messages by the thread ID export const getThreadRuns = async (threadId: string) => { const { runs } = await itzam.threads.getRuns(threadId); return runs; };
npm install itzam # or yarn add itzam # or pnpm add itzam
import Itzam from "itzam"; const itzam = new Itzam(process.env.ITZAM_API_KEY);
talkToTurtle
const workflowSlug = "turtle-chatbot"; export const talkToTurtle = async (message: string, threadId: string) => { const response = await itzam.streamText({ input: message, threadId, }); return response; };
createThread
export const createThread = async (userId: string) => { const thread = await itzam.threads.create({ workflowSlug, lookupKeys: [userId], }); return thread; };
export const getUserThreads = async (userId: string) => { const threads = await itzam.threads.list(workflowSlug, { lookupKeys: [userId], }); return threads; };
getRuns
export const getThreadRuns = async (threadId: string) => { const { runs } = await itzam.threads.getRuns(threadId); return runs; };