TLDR

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;
};

Workflow

The first thing we need to do is create a workflow. Go to the workflows page and click on the “New Workflow” button.

After choosing a name, a prompt and a model, now it’s time to integrate it.

Integration

Integrating a workflow is simple. First, you need to install the Itzam SDK.

npm install itzam
# or
yarn add itzam
# or
pnpm add itzam

Then, you can initialize the Itzam client with your API key.

import Itzam from "itzam";

const itzam = new Itzam(process.env.ITZAM_API_KEY);

Sending messages

For this example, we’ll create a talkToTurtle method that sends a message to the AI model and stream the response.

const workflowSlug = "turtle-chatbot";

export const talkToTurtle = async (message: string, threadId: string) => {
  const response = await itzam.streamText({
    input: message,
    threadId,
  });

  return response;
};

This is everything you need to start talking to your AI model.

Creating threads

Threads are the conversations between the user and the AI model. To create a thread, we created a createThread method.

export const createThread = async (userId: string) => {
  const thread = await itzam.threads.create({
    workflowSlug,
    lookupKeys: [userId],
  });

  return thread;
};

Here we’re adding the User ID as a lookup key to create a thread. This will allow us to fetch threads later by the user ID.

Fetching threads

Since we’re using the User ID as a lookup key, we can fetch threads in this workflow by the user ID.

export const getUserThreads = async (userId: string) => {
  const threads = await itzam.threads.list(workflowSlug, {
    lookupKeys: [userId],
  });

  return threads;
};

Fetching older messages

To fetch older messages, we can use the getRuns method. This will return all the runs for a given thread.

export const getThreadRuns = async (threadId: string) => {
  const { runs } = await itzam.threads.getRuns(threadId);

  return runs;
};

The run contains the input sent by the user and the output generated by the AI model.

Conclusion

That’s it! You now have a chatbot where your users can create threads and send messages to the AI model.

You can find the complete code here.