Skip to content

Commit 9dc4e41

Browse files
committed
add logging
1 parent 046405e commit 9dc4e41

File tree

8 files changed

+184
-8
lines changed

8 files changed

+184
-8
lines changed

.env.template

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ POSTGRES_DB=postgres
66
REDIS_URL=redis://redis:6379
77
OLLAMA_URL=http://host.docker.internal:11434
88
OLLAMA_MODEL=llama3
9-
API_PORT=3000
9+
API_PORT=3000
10+
LOGGER_LEVEL=error

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,8 @@ curl -X POST http://http://localhost:3000/api/repos/Kaltsoon/rag-my-docs \
5050
5151
### CLI
5252

53-
You can run the CLI application by running `docker compose exec -it app npm run start:cli`. The CLI application will provide instructions on the usage.
53+
You can run the CLI application by running `docker compose exec -it app npm run start:cli`. The CLI application will provide instructions on the usage.
54+
55+
### Debugging
56+
57+
Setting `LOGGER_LEVEL=debug` in the `.env` file will enable logging and provide information on, for example, cache performance.

package-lock.json

Lines changed: 123 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"octokit": "^5.0.3",
3030
"ora": "^8.2.0",
3131
"pg": "^8.16.3",
32+
"pino": "^9.7.0",
3233
"redis": "^5.6.0",
3334
"ts-node-dev": "^2.0.0",
3435
"uuid-by-string": "^4.0.0",

src/RepositoryDocumentationLoader.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ import { RecursiveCharacterTextSplitter } from "@langchain/textsplitters";
88
import { Document } from "@langchain/core/documents";
99
import getUuidByString from "uuid-by-string";
1010

11-
import { DocumentationFile, RepositoryIdentifier, RepositoryIdentifierWithAuth } from "./types";
11+
import {
12+
DocumentationFile,
13+
RepositoryIdentifier,
14+
RepositoryIdentifierWithAuth,
15+
} from "./types";
1216
import { getRepositoryContentZip } from "./githubApi";
17+
import { logger } from "./logger";
1318

1419
const textSplitter = RecursiveCharacterTextSplitter.fromLanguage("markdown", {
1520
chunkSize: 2000,
@@ -32,6 +37,7 @@ export class RepositoryDocumentationLoader {
3237
const cacheKey = this.#getCacheKey();
3338

3439
if (cache.has(cacheKey)) {
40+
logger.info(prefixLoggerMessage(`Cache hit for key ${cacheKey}`));
3541
return cache.get(cacheKey)!;
3642
}
3743

@@ -44,6 +50,12 @@ export class RepositoryDocumentationLoader {
4450
documentationFiles
4551
);
4652

53+
logger.info(
54+
prefixLoggerMessage(
55+
`Cache miss for key ${cacheKey}, ${documents.length} documents loaded`
56+
)
57+
);
58+
4759
cache.set(cacheKey, documents);
4860

4961
return documents;
@@ -56,10 +68,17 @@ export class RepositoryDocumentationLoader {
5668
}
5769
}
5870

59-
function getRepositoryFilePath(fullPath: string) {
71+
function prefixLoggerMessage(message: string) {
72+
return `Repository documentation loader: ${message}`;
73+
}
74+
75+
function getRepositoryFilePath(fullPath: string, tempFolderName: string) {
6076
const pathParts = fullPath.split(pathSeparator);
77+
const tempFolderIndex = pathParts.findIndex(
78+
(part) => part === tempFolderName
79+
);
6180

62-
return pathParts.slice(3, pathParts.length).join("/");
81+
return pathParts.slice(tempFolderIndex + 2, pathParts.length).join("/");
6382
}
6483

6584
function getTempRepositoryFolderName(owner: string, repo: string) {
@@ -81,7 +100,7 @@ export async function fetchRepositoryDocumentation(
81100
const documentationFiles: DocumentationFile[] = [];
82101

83102
for (const filePath of await glob(`${path}/**/*.md`)) {
84-
const repositoryFilePath = getRepositoryFilePath(filePath);
103+
const repositoryFilePath = getRepositoryFilePath(filePath, tempFolderName);
85104
const content = await readFile(filePath, "utf-8");
86105

87106
documentationFiles.push({

src/env.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const EnvSchema = z.object({
1010
OLLAMA_URL: z.string(),
1111
OLLAMA_MODEL: z.string(),
1212
API_PORT: z.coerce.number().int(),
13+
LOGGER_LEVEL: z.string().default("silent"),
1314
});
1415

1516
export const env = EnvSchema.parse(process.env);

src/logger.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import pino from "pino";
2+
import { env } from "./env";
3+
4+
export const logger = pino({
5+
level: env.LOGGER_LEVEL,
6+
});

src/vectorStore.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Document } from "@langchain/core/documents";
55
import { env } from "./env";
66
import { RepositoryIdentifier } from "./types";
77
import { setRepositoryDocumentIds, getRepositoryDocumentIds } from "./redis";
8+
import { logger } from "./logger";
89

910
const embeddings = new OllamaEmbeddings({
1011
model: env.OLLAMA_MODEL,
@@ -32,6 +33,8 @@ const config = {
3233

3334
let cachedVectorStore: PGVectorStore;
3435

36+
logger.info("Hello world!")
37+
3538
export async function addDocuments(documents: Document[]) {
3639
const vectorStore = await getVectorStore();
3740
await vectorStore.addDocuments(documents);
@@ -58,20 +61,34 @@ export async function repositorySimilaritySearch(
5861
});
5962
}
6063

61-
export async function updateRepositoryVectorStore(repository: RepositoryIdentifier, documents: Document[]) {
64+
export async function updateRepositoryVectorStore(
65+
repository: RepositoryIdentifier,
66+
documents: Document[]
67+
) {
6268
const nextDocumentIds = documents.map((doc) => doc.id!);
6369
const previousDocumentIds = await getRepositoryDocumentIds(repository);
6470

6571
if (!documentIdsAreEqual(nextDocumentIds, previousDocumentIds)) {
72+
logger.info(prefixLoggerMessage("Cache miss"), {
73+
repository,
74+
nextDocumentIds,
75+
previousDocumentIds,
76+
});
77+
6678
if (previousDocumentIds.length > 0) {
79+
logger.info(prefixLoggerMessage("Deleting previous documents"), {
80+
repository,
81+
});
6782
await deleteDocuments(previousDocumentIds);
6883
}
6984

70-
await Promise.all([
85+
return Promise.all([
7186
addDocuments(documents),
7287
setRepositoryDocumentIds(repository, documents),
7388
]);
7489
}
90+
91+
logger.info(prefixLoggerMessage("Cache hit"), { repository })
7592
}
7693

7794
function documentIdsAreEqual(ids: string[], idsOther: string[]) {
@@ -87,3 +104,7 @@ async function getVectorStore() {
87104

88105
return cachedVectorStore;
89106
}
107+
108+
function prefixLoggerMessage(message: string) {
109+
return `Vector store: ${message}`;
110+
}

0 commit comments

Comments
 (0)