Skip to content

Commit

Permalink
Addes support for larger files, changed model
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzysztof Filipów committed Apr 11, 2024
1 parent 5ac7124 commit 3aecdf7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
12 changes: 1 addition & 11 deletions src/chat/get-init.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion src/chat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
29 changes: 23 additions & 6 deletions src/chat/upload-file.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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,
Expand Down

0 comments on commit 3aecdf7

Please sign in to comment.