Quick Start: Chat Table
4
6
Creating Chat Sessions
async function createNewChat(): Promise<string | null> {
const timestamp = Date.now();
const newTableId = `Chat_${timestamp}`;
try {
await jamai.table.duplicateTable({
table_type: "chat",
table_id_src: "example_agent", // Your base agent ID
table_id_dst: newTableId,
include_data: true,
create_as_child: true,
});
return newTableId;
} catch (error) {
console.error(`Error creating new chat: ${error}`);
return null;
}
}7
Basic Chat Interaction
// Send a message and get response
async function chatInteraction(
tableId: string,
userMessage: string
): Promise<string> {
const response = await jamai.table.addRowStream({
table_type: "chat",
table_id: tableId,
data: [{ User: userMessage }],
});
// For streaming response
let fullResponse = "";
for await (const value of response) {
if (
value.object === "gen_table.completion.chunk" &&
value.choices?.[0]?.message?.content
) {
fullResponse += value.choices[0].message.content;
console.log(value.choices[0].message.content);
}
}
return fullResponse;
}
// For non-streaming response
async function chatInteractionNonStream(tableId: string, userMessage: string) {
const response = await jamai.table.addRow({
table_type: "chat",
table_id: tableId,
data: [{ User: userMessage }],
concurrent: false,
});
return response.rows[0]?.columns["AI"]?.choices[0]?.message?.content;
}8
11
Complete Runnable Example with CLI
import JamAI from "jamaibase";
import * as readline from "readline";
import * as dotenv from "dotenv";
// Load environment variables (optional)
dotenv.config();
// Constants
const PROJECT_ID = process.env.JAMAI_PROJECT_ID || "your_project_id";
const PAT = process.env.JAMAI_API_KEY || "your_pat_token";
const AGENT_ID = "example_agent"; // Replace with your agent ID
const OPENER = "Hello! How can I help you today?";
// Initialize JamAI
const jamai = new JamAI({
projectId: PROJECT_ID,
token: PAT,
});
interface Message {
role: "user" | "assistant";
content: string;
}
class ChatSession {
private tableId: string | null = null;
private messages: Message[] = [];
private rl: readline.Interface;
constructor() {
this.rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
}
async initialize(): Promise<boolean> {
this.tableId = await this.createNewChat();
if (this.tableId) {
this.messages.push({ role: "assistant", content: OPENER });
console.log(`\nAssistant: ${OPENER}\n`);
return true;
}
return false;
}
async createNewChat(): Promise<string | null> {
const timestamp = Date.now();
const newTableId = `Chat_${timestamp}`;
try {
await jamai.table.duplicateTable({
table_type: "chat",
table_id_src: AGENT_ID,
table_id_dst: newTableId,
include_data: true,
create_as_child: true,
});
console.log(`Created new chat session: ${newTableId}`);
return newTableId;
} catch (error) {
console.error(`Error creating new chat: ${error}`);
return null;
}
}
async sendMessage(userMessage: string): Promise<void> {
if (!this.tableId) {
console.error("No active chat session");
return;
}
// Add user message
this.messages.push({ role: "user", content: userMessage });
// Get AI response with streaming
console.log("\nAssistant: ");
const response = await jamai.table.addRowStream({
table_type: "chat",
table_id: this.tableId,
data: [{ User: userMessage }],
});
let fullResponse = "";
for await (const value of response) {
if (
value.object === "gen_table.completion.chunk" &&
value.choices?.[0]?.message?.content
) {
const content = value.choices[0].message.content;
fullResponse += content;
console.log(content);
}
}
console.log("\n");
this.messages.push({ role: "assistant", content: fullResponse });
}
async startChat(): Promise<void> {
console.log(
"\nChat started! Type 'exit' to quit, 'new' to start a new chat.\n"
);
const askQuestion = () => {
this.rl.question("You: ", async (input) => {
const userInput = input.trim();
if (userInput.toLowerCase() === "exit") {
console.log("Goodbye!");
this.rl.close();
process.exit(0);
} else if (userInput.toLowerCase() === "new") {
const success = await this.initialize();
if (success) {
askQuestion();
} else {
console.log("Failed to create new chat. Exiting...");
this.rl.close();
process.exit(1);
}
} else if (userInput) {
await this.sendMessage(userInput);
askQuestion();
} else {
askQuestion();
}
});
};
askQuestion();
}
}
async function main() {
console.log("=== JamAI Base Chat Demo ===\n");
const chat = new ChatSession();
const initialized = await chat.initialize();
if (initialized) {
await chat.startChat();
} else {
console.error("Failed to initialize chat session");
process.exit(1);
}
}
main().catch(console.error);JAMAI_PROJECT_ID=your_project_id JAMAI_API_KEY=your_pat_tokennpm install jamaibase dotenv npm install --save-dev @types/node typescript ts-nodets-node chat_demo.ts
tsc chat_demo.ts
node chat_demo.js12
Web-Based Chat Example (Express + HTML)
import JamAI from "jamaibase";
import express from "express";
import * as dotenv from "dotenv";
dotenv.config();
const app = express();
const PORT = 3000;
const PROJECT_ID = process.env.JAMAI_PROJECT_ID || "your_project_id";
const PAT = process.env.JAMAI_API_KEY || "your_pat_token";
const AGENT_ID = "example_agent";
const jamai = new JamAI({
projectId: PROJECT_ID,
token: PAT,
});
app.use(express.json());
app.use(express.static("public"));
// Create new chat session
app.post("/api/chat/new", async (req, res) => {
try {
const timestamp = Date.now();
const newTableId = `Chat_${timestamp}`;
await jamai.table.duplicateTable({
table_type: "chat",
table_id_src: AGENT_ID,
table_id_dst: newTableId,
include_data: true,
create_as_child: true,
});
res.json({ chatId: newTableId });
} catch (error) {
res.status(500).json({ error: String(error) });
}
});
// Send message
app.post("/api/chat/message", async (req, res) => {
try {
const { chatId, message } = req.body;
const response = await jamai.table.addRow({
table_type: "chat",
table_id: chatId,
data: [{ User: message }],
concurrent: false,
});
const aiResponse =
response.rows[0]?.columns["AI"]?.choices[0]?.message?.content;
res.json({ response: aiResponse });
} catch (error) {
res.status(500).json({ error: String(error) });
}
});
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});<!DOCTYPE html>
<html>
<head>
<title>JamAI Chat Demo</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 50px auto;
}
#chat {
border: 1px solid #ccc;
height: 400px;
overflow-y: scroll;
padding: 10px;
margin-bottom: 10px;
}
.message {
margin: 10px 0;
padding: 10px;
border-radius: 5px;
}
.user {
background: #e3f2fd;
text-align: right;
}
.assistant {
background: #f5f5f5;
}
#input {
width: 80%;
padding: 10px;
}
button {
padding: 10px 20px;
}
</style>
</head>
<body>
<h1>JamAI Chat Demo</h1>
<div id="chat"></div>
<input type="text" id="input" placeholder="Type your message..." />
<button onclick="sendMessage()">Send</button>
<button onclick="newChat()">New Chat</button>
<script>
let chatId = null;
async function newChat() {
const response = await fetch("/api/chat/new", { method: "POST" });
const data = await response.json();
chatId = data.chatId;
document.getElementById("chat").innerHTML = "";
addMessage("assistant", "Hello! How can I help you today?");
}
async function sendMessage() {
const input = document.getElementById("input");
const message = input.value.trim();
if (!message || !chatId) return;
addMessage("user", message);
input.value = "";
const response = await fetch("/api/chat/message", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ chatId, message }),
});
const data = await response.json();
addMessage("assistant", data.response);
}
function addMessage(role, content) {
const chat = document.getElementById("chat");
const div = document.createElement("div");
div.className = `message ${role}`;
div.textContent = content;
chat.appendChild(div);
chat.scrollTop = chat.scrollHeight;
}
// Initialize with new chat
newChat();
</script>
</body>
</html>npm install express dotenv
ts-node server.tsLast updated
Was this helpful?