From 6bc0bfc5f1f77de9228c0ffda6cbb602bc8828e5 Mon Sep 17 00:00:00 2001 From: ferenci84 Date: Thu, 6 Mar 2025 14:05:14 +0100 Subject: [PATCH] Add citations to the log --- core/llm/index.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/core/llm/index.ts b/core/llm/index.ts index ccbad6699..8b63b5660 100644 --- a/core/llm/index.ts +++ b/core/llm/index.ts @@ -763,6 +763,7 @@ export abstract class BaseLLM implements ILLM { } let completion = ""; + let citations: null | string[] = null try { if (this.templateMessages) { @@ -790,6 +791,8 @@ export abstract class BaseLLM implements ILLM { completion = renderChatMessage(msg); } else { // Stream true + console.log("Streaming"); + const stream = this.openaiAdapter.chatCompletionStream( { ...body, @@ -802,6 +805,9 @@ export abstract class BaseLLM implements ILLM { if (result) { yield result; } + if (!citations && (chunk as any).citations && Array.isArray((chunk as any).citations)) { + citations = (chunk as any).citations; + } } } } else { @@ -824,6 +830,10 @@ export abstract class BaseLLM implements ILLM { if (logEnabled && this.writeLog) { await this.writeLog(`Completion:\n${completion}\n\n`); + + if (citations) { + await this.writeLog(`Citations:\n${citations.map((c, i) => `${i + 1}: ${c}`).join("\n")}\n\n`); + } } return {