Skip to content

Commit

Permalink
Merge pull request #188 from nasa-petal/Issue185_OY
Browse files Browse the repository at this point in the history
Issue185 oy
  • Loading branch information
bruffridge authored Dec 17, 2024
2 parents 8a4d03a + aff0819 commit f6f5cc0
Show file tree
Hide file tree
Showing 7 changed files with 335 additions and 99 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@
!.vscode/settings.json
!.vscode/extensions.json
!.vscode/tasks.json
.conda
.conda
.env
.pdf
337 changes: 244 additions & 93 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"dependencies": {
"deep-chat-dev": "^9.0.179",
"jspdf": "^2.5.1",
"pino": "^9.5.0",
"sirv-cli": "^2.0.0",
"svelte-agnostic-draggable": "^0.2.0",
"svelte-touch-to-mouse": "^1.0.0"
Expand Down
47 changes: 43 additions & 4 deletions src/components/AssistantDeepChat.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import { DeepChat } from 'deep-chat-dev';
import { setOpenAIKey, cancelThreadRun } from '../utils/openaiUtils';
import * as threadUtils from '../utils/threadUtils';
import logger from '../utils/logger.js';
export let key = null;
export let thread = null;
Expand All @@ -18,6 +20,7 @@
const threadId = thread?.id;
const asstId = thread?.asst?.id;
const asstConfig = asst?.config;
// vars for callbacks
let lastMessageId;
Expand Down Expand Up @@ -63,7 +66,7 @@
if (!deepChatRef || message.isInitial) {
return
}
if (thread.length === 0 || thread.length === asst.history + 1) {
const maxCharLen = 50;
const words = message.message.text.split(/\s+/);
Expand Down Expand Up @@ -195,15 +198,51 @@
}
async function requestInterceptor(request) {
if (newFileUploads.length > 0) {
newFileIds = request.body.attachments.map(attachment => attachment.file_id);
const body = request.body;
// logger.debug("Original Request Body:", JSON.stringify(body, null, 2));
// console.log("Original Request Body:", JSON.stringify(body, null, 2));
// Ensure placeholder text is added for file-only requests, or requests with empty message text.
if (
'attachments' in body &&
Array.isArray(body.attachments) &&
body.attachments.length > 0 &&
(
!('content' in body) ||
!Array.isArray(body.content) ||
body.content.length === 0 ||
!('text' in body.content[0]) ||
!(typeof body.content[0].text === 'string') ||
body.content[0].text.trim().length === 0
)
) {
// logger.info("Adding placeholder text for file-only request.");
body.content = [
{
type: "text",
text:
"I just uploaded a file. Let me know if you got it and what tools you can use to analyze it. Wait for me to tell you what to do next with it though."
},
];
} else {
// logger.warn("Request body does not contain a valid content structure.");
}
handleFileUploads(newFileIds, newFileUploads);
// Transform attachments to include file_id
if (newFileUploads.length > 0) {
if('attachments' in body && Array.isArray(body.attachments) && body.attachments.length > 0) {
newFileIds = request.body.attachments.map(attachment => attachment.file_id);
handleFileUploads(newFileIds, newFileUploads);
}
}
// Debug modified request
// logger.debug("Final Request Body Sent to API:", JSON.stringify(body, null, 2));
return request;
}
function setDeepChatKeyboardSupport() {
const shadowRoot = deepChatRef.shadowRoot;
const input = shadowRoot.getElementById("input");
Expand Down
Binary file added src/utils/FitchGroupDisabilityForm.pdf
Binary file not shown.
37 changes: 37 additions & 0 deletions src/utils/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import pino from 'pino';
// import fs from 'fs';
// import path from 'path';


// // Ensure the logs directory exists
// const logsDir = path.resolve('./logs');
// if (!fs.existsSync(logsDir)) {
// fs.mkdirSync(logsDir);
// }



// // Create a write stream for the log file
// const logFileStream = fs.createWriteStream(path.join(logsDir, 'application.log'), { flags: 'a' });

// const logger = pino(
// {
// level: 'debug', // Set the desired log level
// transport: {
// target: 'pino-pretty', // Human-readable logs in terminal
// options: { colorize: true }, // Colorize logs for console
// },
// },
// logFileStream // Write logs to the file
// );

// export default logger;

const logger = {
debug: (msg, obj) => console.debug(`[DEBUG] ${msg}`, obj),
info: (msg, obj) => console.info(`[INFO] ${msg}`, obj),
warn: (msg, obj) => console.warn(`[WARN] ${msg}`, obj),
error: (msg, obj) => console.error(`[ERROR] ${msg}`, obj),
};

export default logger;
8 changes: 7 additions & 1 deletion src/utils/openaiUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,13 @@ export async function getChatCompletion(model, messages, tokenLimit) {

return r;
}

/**
*
* @param {*} b64Data
* @param {*} fileName
* @param {*} type
* @returns
*/
export async function uploadFile(b64Data, fileName, type) {
if (!openaiKey) {
throw new Error('openai key not set. cannot validate thread.');
Expand Down

0 comments on commit f6f5cc0

Please sign in to comment.