in wiki-interface/lib/vertex/vertex.js [103:181]
async function sendDocMessage(transId, message) {
// Config
let projectId = configFile.getAiProject();
let locationId = configFile.getAiLocation();
let modelId = configFile.getAiModel();
let maxOutTokens = Number(configFile.getAiOutputTokens());
let temperature = Number(configFile.getAiTemperature());
let maxChatHistory = Number(configFile.getChatHistory()) * 2 * -1;
// Initialize Vertex with your Cloud project and location
const vertex_ai = new VertexAI({ project: projectId, location: locationId });
// Fetch System Instruction and Chat History from Cloud Storage
const docCtx = ctxHelper.getDocContext();
const systemInstruction = docCtx + await gcsUtils.getFileContents(transId);
const chatHistory = await gcsUtils.getJSONFileAsObject(transId);
// Optionally, you can concatenate the chat history to the system instruction
let completeHistory = [];
let thisHistory = {};
if (chatHistory && chatHistory.length > 0) {
// Get the last 10 entries of chatHistory
const lastTenEntries = chatHistory.slice(maxChatHistory)
for (const thisEntry of lastTenEntries) {
thisHistory = {
role: thisEntry.role,
parts: [{
text: thisEntry.message
}]
}
completeHistory.push(thisHistory);
}
}
// Instantiate the models
let generativeModel = vertex_ai.preview.getGenerativeModel({
model: modelId,
generationConfig: {
'maxOutputTokens': maxOutTokens,
'temperature': temperature,
'topP': 0.95,
},
safetySettings: [
{
'category': 'HARM_CATEGORY_HATE_SPEECH',
'threshold': 'BLOCK_ONLY_HIGH',
},
{
'category': 'HARM_CATEGORY_DANGEROUS_CONTENT',
'threshold': 'BLOCK_ONLY_HIGH',
},
{
'category': 'HARM_CATEGORY_SEXUALLY_EXPLICIT',
'threshold': 'BLOCK_ONLY_HIGH',
},
{
'category': 'HARM_CATEGORY_HARASSMENT',
'threshold': 'BLOCK_ONLY_HIGH',
}
],
systemInstruction: {
parts: [{ "text": systemInstruction }]
},
});
const chat = generativeModel.startChat({
history: completeHistory
});
const streamResult = await chat.sendMessageStream(message);
let response = (await streamResult.response).candidates[0].content.parts[0].text;
// Store the messages in Cloud Storage
await gcsUtils.appendToJson(transId, message, response);
return response + '\n';
}