This example will guide you through how to send your first question to the API. Create a new file medisearch_client.js.
Paste the following code into medisearch_client.js. This is just to generate an id for your conversation with MediSearch.
function generateID() {
var id = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (var i = 0; i < 32; i++) {
id += characters.charAt(Math.floor(Math.random() * characters.length));
}
return id;
}
conversation_id = generateID();
Use the above code to generate a new id for each new conversation, but not for each new question. Now create a userConversation object and paste it into medisearch_client.js as follows.
const userConversation = {
event: "user_message",
conversation: ["Is cancer transmissible?"],
key: "your-api-key",
id: conversation_id,
settings: {
language: "English"
}
}
In the above, don’t forget to
'your-api-key' with your api key (which you obtain by emailing [email protected]).'Is cancer transmissible?' with your questionThe last step is to paste the following code into medisearch_client.js. This piece of code sends the request to our server and receives a response.
const WebSocket = require('ws');
const ws = new WebSocket('wss://public.backend.medisearch.io:443/ws/medichat/api');
// Prepare for receiving the request
ws.on('message', function incoming(data) {
const strData = data.toString('utf8');
const jsonData = JSON.parse(strData);
if (jsonData.event === "articles") {
console.log("Got articles");
} else if (jsonData.event === "llm_response") {
console.log("Got llm response");
} else if (jsonData.event === "error") {
console.log("Got error");
}
console.log(jsonData);
});
// WebSocket is open, time to send a request.
ws.on('open', function open() {
ws.send(JSON.stringify(userConversation));
});
Finally, to run this code, open your terminal and run node medisearch_client.js.
Don’t hesitate to reach out if anything does not work.