TypeScript
This example provides basic steps on how to use the murai API using TypeScript as programming language.
import axios from 'axios';
/** Definition of interfaces used in the example */
interface DetailsResponse {
status: number;
data: Details;
}
interface Details {
chatBotName: string;
chatBotDescription: string;
workspaceName: string;
}
interface ChatInfo {
id: number;
name: string;
userRef: string;
_createTime: string;
}
interface ChatMessageResponse {
status: number;
meta: any;
data: ChatMessage[];
}
interface ChatMessage {
id: number;
_createTime: string;
chat_id: number;
question: string | null;
answer: string;
source: string;
moderation: string;
flagged: boolean;
}
interface ChatAnswer {
id: number;
_createTime: string;
chat_id: number;
question: string | null;
answer: string;
source: string;
moderation: string;
flagged: boolean;
}
interface ChatAnswerResponse {
status: number;
meta: any;
data: ChatAnswer;
}
interface ChatMessageResponse {
status: number;
meta: any;
data: ChatMessage[];
}
interface ChatInfoResponse {
status: number;
meta: any;
data: ChatInfo[];
}
interface ChatCreateResponse {
status: number;
meta: any;
data: ChatInfo;
}
/** Provide the API key and base url to the api endpoint */
const murai_API_KEY = 'XXXXXX-XXX-XXXXX-XXXXXXXXXX';
const baseURL = 'https://api.murai.ai';
const askUrl = 'https://ask-api.murai.ai';
const headers = {
'Content-Type': 'application/json; charset=utf-8',
'x-api-key': `${murai_API_KEY}`
};
/**
* This part of the example shows how to use the API to create a chat, ask a question and get the answer.
* It also provide a way to get the chat history.
* The code of the example should be self-explanatory for even a beginner.
*
* The code is wrapped in a function to be able to use async/await.
* The function is called at the end of the file.
*/
const chatBotWorkflow = async () => {
try {
const details = await checkChatDetails();
console.log('--- Details data', details, '---');
const chatData = {
name: 'My First chat',
userRef: 'FC-ref'
};
const checkChat = await checkIfChatExists(chatData.name);
const chatId = checkChat[0]?.id || (await createChat(chatData));
console.log('--- Chat id:', chatId, '---');
const answer = await askChatBot(chatId, 'What is the weather like today?');
console.log('--- Chat answer: ', answer.answer, '---');
const history = await getChatHistory(chatId);
console.log('---- History: ', history.data, history.meta, '------');
} catch (error) {
console.error('Error while working with chat API' + error);
}
};
const askChatBot = async (chatId: number, question: string): Promise<ChatAnswer> => {
const askData = { chat_id: chatId, query: question };
const res = await axios.post<ChatAnswerResponse>(askUrl, askData, { headers });
if (res.data.status !== 201) {
throw new Error('Error getting the answer');
}
return res.data.data;
};
const checkIfChatExists = async (search: string): Promise<ChatInfo[]> => {
const res = await axios.get<ChatInfoResponse>(`${baseURL}/chats?search=${search}`, { headers });
if (res.data.status !== 200) {
throw new Error('Error getting the chat info');
}
return res.data.data;
};
const createChat = async (chatData): Promise<number> => {
const res = await axios.post<ChatCreateResponse>(`${baseURL}/chats`, chatData, { headers });
if (res.data.status !== 201) {
throw new Error('Error creating the chat');
}
return res.data.data.id;
};
const getChatHistory = async (chatId: number): Promise<{ data: ChatMessage[]; meta: any }> => {
const res = await axios.get<ChatMessageResponse>(`${baseURL}/chats/${chatId}/history?limit=10`, { headers });
if (res.data.status !== 200) {
throw new Error('Error getting the chat history');
}
return { data: res.data.data, meta: res.data.meta };
};
const checkChatDetails = async (): Promise<Details> => {
const res = await axios.get<DetailsResponse>(`${baseURL}/details`, { headers });
if (res.data.status !== 200) {
throw new Error('Error getting the chat details');
}
return res.data.data;
};
// Call the function to start the example
chatBotWorkflow()
.then(() => {
console.log('Example done');
})
.catch((error) => {
console.error(error);
});