Chat

Create and respond to chats

Chats are stored as sessions and have memory. The intelligent content engine takes the history of chat messages in a session into account when generating responses.

Streaming chat creation

To stream chat output, set the stream parameter to true:

const cortex = await client.getCortex("support-engineer");
 
// ask questions and stream responses
const streamingChatResponse = await cortex.chat({
  message: "What kind of hammers are best to use on my ACME anvil?",
  stream: true,
});
streamingChatResponse.responseStream.on("data", (data) => {
  process.stdout.write(data.toString());
});
 
const chat = await streamingChatResponse.chat;
 
console.log(JSON.stringify(chat.messages));

Synchronous chat creation

const cortex = await client.getCortex("support-engineer");
 
const chat = await cortex.chat({
  message: "What kind of hammers are best to use on my ACME anvil?",
});
 
console.log(JSON.stringify(chat.messages));

Streaming responses

To respond to a chat with memory of previous messages and stream the result, set the stream parameter to true:

const chatId = "...";
const chat = await client.getChat(chatId);
 
const respondResponse = await chat.respond({
  message: "Give me three more examples.",
  stream: true,
});
 
respondResponse.responseStream.on("data", (data) => {
  process.stdout.write(data.toString());
});
 
await respondResponse.chat;

Synchronous responses

const chatId = "...";
const chat = await client.getChat(chatId);
 
await chat.respond({ message: "Give me three more examples" });
 
console.log(JSON.stringify(chat.messages));

Identifying users from your platform

An external user ID can be provided when creating a chat from the SDK. Subsequently, chats can be filtered and searched by external ID from the app and with the SDK to analyze support trends and usage patterns for customers.

Create a chat with external user IDs

const cortex = await client.getCortex("support-engineer");
 
const message = "...";
const externalUserId = "...";
 
const chat = await cortex.createChat({ message, externalUserId });

List chats by external user IDs

const externalUserId = "...";
 
const chatPage = await client.listChats({ externalUserId });

Get a chat

const chatId = "...";
const chat = await client.getChat(chatId);

List chats

List and paginate through all chats, or filter by externalUserId, userEmail, or cortexName:

const allChatsPage = await client.listChats();
 
for (const chatListResult of allChatsPage.chats) {
  console.log(chatListResult.title);
  const chat = await chatListResult.Chat();
  console.log(JSON.stringify(chat.messages));
}
 
const nextPage = await allChatsPage.nextPage();
 
// ...
 
const chatsByUserAndCortex = await client.listChats({
  userEmail: "example@example.com",
  cortexName: "sales-support",
});
 
const chatsByExternalId = await client.listChats({
  externalUserId: "123",
});

Embedded copilots

Cortex Click makes it easy to embed a copilot within your product so that users can get answers to their questions instantenously, and in context. This keeps users within your product, helps them activate and reach the "a-ha" moment faster, and reduces support burden.

Embedded chat

Cortex provides a chat UX that you can host within your own app or product. It is available as either an iframe or can be be initialized by important a JavaScript script tag.

Embedded chat is currently in alpha, for more details on integration please reach out for more details.

Tips for building a custom copilot

The Cortex SDK provides everything you need to build custom embedded copilots that answer questions about your platform, docs, and how to use your product offering. In just a few lines of code you can:

  • Stream responses to users from the intelligent content engine
  • Respond to chat sessions with memory about previous messages
  • Save extenal ID from your system on a chat for downstream analytics
  • Lookup, list, and retrieve previous sessions for a user

Building your own copilot enables deeper customization of look and feel, and allows building custom features and actions specific to your platform. For instance, if your company provide a data platform your users query with SQL, you might use Cortex Click to generate queries and add customizations in your copilot that allows users to execute the queries on your platform with a single click rather than copying and pasting.

For a live example of a custom copilot, see the Allium.so marketing website.