Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release: Logging #49

Merged
merged 2 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions graphql/queries/me.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ fragment UserDetails on User {
firstName
lastName
email
gender
phone
accountUpn
agent {
Expand Down Expand Up @@ -92,4 +91,4 @@ fragment UserAgent on User {
__typename
}
__typename
}
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@alkemio/space-ingest",
"version": "0.8.1",
"version": "0.8.2",
"description": "",
"author": "Alkemio Foundation",
"private": true,
Expand Down
11 changes: 10 additions & 1 deletion src/callout.handlers/base.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { Logger } from 'winston';
import { Callout, CalloutContribution } from '../generated/graphql';
import { Document } from 'langchain/document';
import generateDocument from '../generate.document';

export const baseHandler = async (
callout: Partial<Callout>
callout: Partial<Callout>,
logger: Logger
): Promise<Document[]> => {
const { id: documentId, type } = callout;
logger.info(
`Generating document for Callout (${documentId}) of type ${type}`
);

const generated = generateDocument(callout.framing);
const { title, source } = generated;
Expand Down Expand Up @@ -43,6 +48,7 @@ export const baseHandler = async (
}),
];

logger.info(`Generating documents for Callout (${documentId}) contributions`);
// extra loop but will do for now
callout.contributions
?.map((contribution: Partial<CalloutContribution>) => {
Expand All @@ -68,5 +74,8 @@ export const baseHandler = async (
})
.join('\n');

logger.info(
`Documents for Callout (${documentId}) generated. # of documents ${result.length}`
);
return result;
};
5 changes: 4 additions & 1 deletion src/callout.handlers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Logger } from 'winston';
import { Callout, CalloutType } from '../generated/graphql';
import { Document } from 'langchain/document';
import { baseHandler } from './base';
Expand All @@ -8,6 +9,7 @@ const handlersMap: Record<
CalloutType,
(
callout: Partial<Callout>,
logger: Logger,
alkemioClient: AlkemioCliClient | null
) => Promise<Document[]>
> = {
Expand All @@ -20,11 +22,12 @@ const handlersMap: Record<

export const handleCallout = async (
callout: Partial<Callout>,
logger: Logger,
alkemioClient: AlkemioCliClient | null = null
): Promise<Document[]> => {
if (!callout.type) {
throw new Error('Callout type is not part of query.');
}
const handler = handlersMap[callout.type];
return handler(callout, alkemioClient);
return handler(callout, logger, alkemioClient);
};
26 changes: 15 additions & 11 deletions src/callout.handlers/link.collection.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import fs from 'fs';
import https from 'https';
import http from 'http';
import { Logger } from 'winston';
import { MimeType, Callout } from '../generated/graphql';
import { Document } from 'langchain/document';
import { BaseDocumentLoader } from '@langchain/core/document_loaders/base';
import { PDFLoader } from '@langchain/community/document_loaders/fs/pdf';
import { DocxLoader } from '@langchain/community/document_loaders/fs/docx';
import { MimeTypeDocumentMap } from '../document.type';
import logger from '..//logger';
import { SpreadSheetLoader, DocLoader } from '../loaders';
import { AlkemioCliClient } from 'src/graphql-client/AlkemioCliClient';

Expand Down Expand Up @@ -73,6 +73,7 @@ const fileLoaderFactories: {

export const linkCollectionHandler = async (
callout: Partial<Callout>,
logger: Logger,
alkemioClient: AlkemioCliClient | null
): Promise<Document[]> => {
if (!callout.contributions?.length || !alkemioClient) {
Expand Down Expand Up @@ -123,10 +124,11 @@ export const linkCollectionHandler = async (

try {
download = await downloadDocument(link.uri, path, alkemioClient.apiToken);
} catch (error: any) {
logger.error('Error downloading file:');
logger.error(error.message);
logger.error(error.stack);
} catch (error) {
logger.error({
...(error as Error),
error: 'Error downloading file',
});
download = false;
}

Expand All @@ -147,12 +149,14 @@ export const linkCollectionHandler = async (
};
documents.push(doc);
}
} catch (error: any) {
logger.error(
`${docInfo.mimeType} file ${documentId} - ${link.uri} failed to load.`
);
logger.error(error.message);
logger.error(error.stack);
} catch (error) {
logger.error({
...(error as Error),
error: 'File failed to load',
mimeType: docInfo.mimeType,
file: documentId,
uri: link.uri,
});
}
fs.unlinkSync(path);
}
Expand Down
Loading
Loading