Skip to content
Snippets Groups Projects
Commit 6a9204b0 authored by Embruch, Gerd's avatar Embruch, Gerd
Browse files

fixed extendChat by adding findRecordByID(); added exceptions for registering users by artillery

parent fcd78394
No related branches found
No related tags found
No related merge requests found
......@@ -102,7 +102,7 @@ Accept: application/json
Content-Type: application/json
{
"input": "Under what path could members of the working group can find the exam git directory?",
"input": "When does mocking stops feeling like torture?",
"model": "llama3"
}
......
......
......@@ -2,7 +2,7 @@ import { Ollama } from 'ollama';
import Chat from "../models/Chat.js";
import { aiDeleteModel, aiGetModels, aiGetModel, aiInstallModel, aiIsRunning, summarizeText } from "../utils/handleAI.js";
import { mapStoredMessagesToChatMessages } from "@langchain/core/messages";
import { createRecord, findOneRecord, findRecords } from '../utils/handleDB.js';
import { createRecord, findOneRecord, findRecordByID, findRecords } from '../utils/handleDB.js';
import { prefillDocumentObject } from '../utils/handleSchemes.js';
......@@ -95,21 +95,15 @@ export const getChat = async (req, res, next) => {
// IF CHATID GIVEN
try {
// fetch chat record
const record = await findOneRecord(Chat, { id: req.body.chatId });
const record = await findRecordByID(Chat, req.body.chatId);
if (!record) {
return res.status(404).json({ message: `No chat history with ID ${req.body.chatId} found.` });
}
console.log("🚀 ~ getChat ~ record SIC:", record);
// remember chat history
// cite: https://js.langchain.com/v0.1/docs/modules/memory/chat_messages/custom/
req.body.chatHistory = mapStoredMessagesToChatMessages(record.chatHistory);
console.log("🚀 ~ getChat ~ req.body.chatHistory:", req.body.chatHistory);
// go on
next();
} catch (error) {
next(error);
......@@ -140,15 +134,8 @@ export const createChat = async (model, input) => {
try {
// create chat title
const title = await summarizeText(model, input);
console.log("🚀 ~ createChat ~ title:", title);
// create record
const record = await createRecord(Chat, prefillDocumentObject(Chat, { title }));
console.log("🚀 ~ createChat ~ record:", record);
// return record id
return record.id;
} catch (error) {
......
......
......@@ -7,10 +7,23 @@ import { prefillDocumentObject, hideConfidentialFields } from '../utils/handleSc
*/
export const createUser = async (req, res, next) => {
try {
// autoverify if user-agent is Artillery
const isArtilleryAgent = req.get('user-agent').includes('Artillery');
if (isArtilleryAgent) {
req.body.verified = true;
}
// console.log("🚀 ~ createUser ~ isArtilleryAgent:", isArtilleryAgent);
// return res.status(200).json({ message: 'tmp abort', isArtilleryAgent });
// create user object
const newRecord = await createRecord(User, prefillDocumentObject(User, req.body));
// remember document but remove confidential info
req.document = hideConfidentialFields(User, newRecord);
// return if user-agent is Artillery
if (isArtilleryAgent) {
return res.status(201).json({ message: 'User created', document: req.document });
}
// continue to send verification
next();
// on error
} catch (error) {
......
......
......@@ -193,9 +193,5 @@ export const chat = async (req, res, next) => {
]);
// return the answer
console.log('Answer: ', result.answer);
console.log('Chat: ', chat);
return res.json({ answer: result.answer, chat });
};
\ No newline at end of file
......@@ -77,6 +77,29 @@ export const findOneRecord = async (model, searchObject = {}, fieldHandler = '')
}
};
/**
* find record by ID
*
* @param {mongoose model} model [required] model to search the record in
* @param {string} ID [required] ID to search the record with
* @param {string} fieldHandler [optional] additional fields to return or drop, i.e. '+password'
*
* @return {object} found document
*/
export const findRecordByID = async (model, id, fieldHandler = '') => {
try {
let foundRecord;
if (!fieldHandler) {
foundRecord = await model.findById(id);
} else {
foundRecord = await model.findById(id).select(fieldHandler);
}
return foundRecord;
} catch (error) {
throw error;
}
};
/**
* find multiple records
*
......@@ -168,7 +191,8 @@ export const extendChat = async (chatId, messages) => {
}
try {
// fetch chat record
const record = await findOneRecord(Chat, { id: chatId });
const record = await findRecordByID(Chat, chatId);
// push new message into chat history
const serializedMessages = mapChatMessagesToStoredMessages(messages);
record.chatHistory ? record.chatHistory.push(...serializedMessages) : record.chatHistory = serializedMessages;
......
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment