diff --git a/src/chat/get-init.ts b/src/chat/get-init.ts index 660dfee..45276d7 100644 --- a/src/chat/get-init.ts +++ b/src/chat/get-init.ts @@ -1,6 +1,6 @@ import { z } from "zod"; import { makeGetEndpoint } from "../middleware/validation/makeGetEndpoint.js"; -import { addMessages, getUserVisibleMessages } from "../util/messageStore.js"; +import { getUserVisibleMessages } from "../util/messageStore.js"; import { IDENTITY_HEADER } from "./index.js"; //TODO: Rework type inference of fileReader @@ -9,16 +9,6 @@ export const init = makeGetEndpoint(z.any(), async (_request, response) => { if (!identity) { return response.status(400).send(`Missing ${IDENTITY_HEADER} header.`) } - addMessages(identity, [ - { - role: 'system', - content: "Ask the user to provide the content of the file they want to chat about. Talk only about the content of the provided file." - }, - { - role: "assistant", - content: "Hallo! Bitte laden Sie die Datei hoch, über die Sie chatten möchten." - } - ]); return response .status(200) .send(getUserVisibleMessages(identity)); diff --git a/src/chat/index.ts b/src/chat/index.ts index e40175e..b70ddb3 100644 --- a/src/chat/index.ts +++ b/src/chat/index.ts @@ -10,7 +10,7 @@ import fileUpload from "./upload-file.js"; * Change the prompt file, the model or the response format here */ const PROMPT_FILE_NAME = "Prompt_Baufinanzierung.docx" -const OPENAI_MODEL: ChatCompletionCreateParamsBase["model"] = "gpt-3.5-turbo"; +const OPENAI_MODEL: ChatCompletionCreateParamsBase["model"] = "gpt-4-turbo";//"gpt-3.5-turbo"; const RESPONSE_FORMAT: ChatCompletionCreateParams.ResponseFormat = {type: "text"}; const IDENTITY_HEADER = 'X-Identity'; diff --git a/src/chat/upload-file.ts b/src/chat/upload-file.ts index 5b4139c..4d0eb15 100644 --- a/src/chat/upload-file.ts +++ b/src/chat/upload-file.ts @@ -1,9 +1,16 @@ import formidable from 'formidable'; -import { IncomingMessage } from 'http'; import fs from 'node:fs/promises'; import { fileReader } from '../util/fileReader.js'; +import { IDENTITY_HEADER } from './index.js'; +import { addMessages, getMessages } from '../util/messageStore.js'; +import { ChatCompletionMessageParam } from 'openai/resources/index.js'; -const fileUpload = async (request: IncomingMessage, response: any) => { + +const fileUpload = async (request: any, response: any) => { + const identity = request.header(IDENTITY_HEADER); + if (!identity) { + return response.status(400).send(`Missing ${IDENTITY_HEADER} header.`) + } let file: formidable.File | null = null; let fileContent: string | null = null; try { @@ -19,22 +26,32 @@ const fileUpload = async (request: IncomingMessage, response: any) => { } console.log(`File created ${file.filepath}`); switch(file.mimetype) { - case 'text/plain': + case 'text/plain': //txt fileContent = await fs.readFile(file.filepath, {encoding: 'utf-8'}); break; - case 'application/msword': - case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': + case 'application/msword': //doc + case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': //docx const fileConversionResult = await fileReader(file.filepath, true); fileContent = fileConversionResult.content ?? null; if (fileConversionResult.error) { console.error(fileConversionResult.error); } - console.log(fileConversionResult); break; default: throw (`Unsupported file type: ${file?.mimetype}`); } if (fileContent) { + addMessages(identity, [ + { + role: 'system', + content: `You are a helpful assistant designed to answer + questions only about the following content of the file named "${file.originalFilename}" which is following:\n${fileContent}` + }, + { + role: 'assistant', + content: `Lassen Sie uns über die von Ihnen bereitgestellte Datei „${file.originalFilename}“ sprechen. Was möchten Sie wissen?` + }, + ]); return response.status(200).send({ content: fileContent, name: file.originalFilename,