From 707c2042688d344da03ff180d662e23d810eadf9 Mon Sep 17 00:00:00 2001
From: Marcus Schiesser <mail@marcusschiesser.de>
Date: Wed, 7 Feb 2024 10:59:31 +0700
Subject: [PATCH] fix(cl): just retrieve top-k 3 for context to prevent token
 exceed

---
 templates/components/engines/python/agent/__init__.py    | 2 +-
 templates/components/engines/python/chat/__init__.py     | 2 +-
 templates/components/vectordbs/typescript/mongo/index.ts | 2 +-
 templates/components/vectordbs/typescript/none/index.ts  | 2 +-
 templates/components/vectordbs/typescript/pg/index.ts    | 2 +-
 5 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/templates/components/engines/python/agent/__init__.py b/templates/components/engines/python/agent/__init__.py
index f1b62b87..86e777ed 100644
--- a/templates/components/engines/python/agent/__init__.py
+++ b/templates/components/engines/python/agent/__init__.py
@@ -36,7 +36,7 @@ def get_chat_engine():
     # Add query tool
     index = get_index()
     llm = index.service_context.llm
-    query_engine = index.as_query_engine(similarity_top_k=5)
+    query_engine = index.as_query_engine(similarity_top_k=3)
     query_engine_tool = QueryEngineTool.from_defaults(query_engine=query_engine)
     tools.append(query_engine_tool)
 
diff --git a/templates/components/engines/python/chat/__init__.py b/templates/components/engines/python/chat/__init__.py
index 18a6039b..95de61cd 100644
--- a/templates/components/engines/python/chat/__init__.py
+++ b/templates/components/engines/python/chat/__init__.py
@@ -3,5 +3,5 @@ from app.engine.index import get_index
 
 def get_chat_engine():
     return get_index().as_chat_engine(
-        similarity_top_k=5, chat_mode="condense_plus_context"
+        similarity_top_k=3, chat_mode="condense_plus_context"
     )
diff --git a/templates/components/vectordbs/typescript/mongo/index.ts b/templates/components/vectordbs/typescript/mongo/index.ts
index 7aceaff0..844789c6 100644
--- a/templates/components/vectordbs/typescript/mongo/index.ts
+++ b/templates/components/vectordbs/typescript/mongo/index.ts
@@ -29,7 +29,7 @@ async function getDataSource(llm: LLM) {
 
 export async function createChatEngine(llm: LLM) {
   const index = await getDataSource(llm);
-  const retriever = index.asRetriever({ similarityTopK: 5 });
+  const retriever = index.asRetriever({ similarityTopK: 3 });
   return new ContextChatEngine({
     chatModel: llm,
     retriever,
diff --git a/templates/components/vectordbs/typescript/none/index.ts b/templates/components/vectordbs/typescript/none/index.ts
index cdd93809..c3b65ce2 100644
--- a/templates/components/vectordbs/typescript/none/index.ts
+++ b/templates/components/vectordbs/typescript/none/index.ts
@@ -35,7 +35,7 @@ async function getDataSource(llm: LLM) {
 export async function createChatEngine(llm: LLM) {
   const index = await getDataSource(llm);
   const retriever = index.asRetriever();
-  retriever.similarityTopK = 5;
+  retriever.similarityTopK = 3;
 
   return new ContextChatEngine({
     chatModel: llm,
diff --git a/templates/components/vectordbs/typescript/pg/index.ts b/templates/components/vectordbs/typescript/pg/index.ts
index 96a98085..7de66a2e 100644
--- a/templates/components/vectordbs/typescript/pg/index.ts
+++ b/templates/components/vectordbs/typescript/pg/index.ts
@@ -31,7 +31,7 @@ async function getDataSource(llm: LLM) {
 
 export async function createChatEngine(llm: LLM) {
   const index = await getDataSource(llm);
-  const retriever = index.asRetriever({ similarityTopK: 5 });
+  const retriever = index.asRetriever({ similarityTopK: 3 });
   return new ContextChatEngine({
     chatModel: llm,
     retriever,
-- 
GitLab