From fbbff391ae7b619be0b3b784a0ead0578dbd0ef4 Mon Sep 17 00:00:00 2001
From: Marcus Schiesser <mail@marcusschiesser.de>
Date: Fri, 3 Nov 2023 10:39:17 +0700
Subject: [PATCH] inline simple chat engine as a default

---
 templates/index.ts                                  | 13 ++-----------
 .../express/src/controllers/chat.controller.ts      |  2 +-
 .../simple/express/src/controllers/engine}/index.ts |  0
 templates/types/simple/express/tsconfig.json        |  5 +++--
 .../simple/nextjs/app/api/chat/engine/index.ts      |  7 +++++++
 templates/types/simple/nextjs/app/api/chat/route.ts |  2 +-
 .../express/src/controllers/chat.controller.ts      |  2 +-
 .../express/src/controllers/engine/index.ts         |  7 +++++++
 templates/types/streaming/express/tsconfig.json     |  3 ++-
 .../streaming/nextjs/app/api/chat/engine/index.ts   |  7 +++++++
 .../types/streaming/nextjs/app/api/chat/route.ts    |  2 +-
 11 files changed, 32 insertions(+), 18 deletions(-)
 rename templates/{components/engines/simple => types/simple/express/src/controllers/engine}/index.ts (100%)
 create mode 100644 templates/types/simple/nextjs/app/api/chat/engine/index.ts
 create mode 100644 templates/types/streaming/express/src/controllers/engine/index.ts
 create mode 100644 templates/types/streaming/nextjs/app/api/chat/engine/index.ts

diff --git a/templates/index.ts b/templates/index.ts
index 7e23f793..f1b5b2ef 100644
--- a/templates/index.ts
+++ b/templates/index.ts
@@ -71,17 +71,6 @@ export const installTemplate = async ({
       parents: true,
       cwd: enginePath,
     });
-    const routeFile = path.join(
-      root,
-      relativeEngineDestPath,
-      framework === "nextjs" ? "route.ts" : "chat.controller.ts",
-    );
-    const routeFileContent = await fs.readFile(routeFile, "utf8");
-    const newContent = routeFileContent.replace(
-      /^import { createChatEngine }.*$/m,
-      'import { createChatEngine } from "./engine"\n',
-    );
-    await fs.writeFile(routeFile, newContent);
   }
 
   /**
@@ -125,8 +114,10 @@ export const installTemplate = async ({
       customApiPath,
       "\n",
     );
+    // remove the default api folder
     const apiPath = path.join(root, "app", "api");
     await fs.rmdir(apiPath, { recursive: true });
+    // modify the dev script to use the custom api path
     packageJson.scripts = {
       ...packageJson.scripts,
       dev: `NEXT_PUBLIC_CHAT_API=${customApiPath} next dev`,
diff --git a/templates/types/simple/express/src/controllers/chat.controller.ts b/templates/types/simple/express/src/controllers/chat.controller.ts
index b11e1050..476c0c35 100644
--- a/templates/types/simple/express/src/controllers/chat.controller.ts
+++ b/templates/types/simple/express/src/controllers/chat.controller.ts
@@ -1,6 +1,6 @@
 import { NextFunction, Request, Response } from "express";
 import { ChatMessage, OpenAI } from "llamaindex";
-import { createChatEngine } from "../../../../engines/context";
+import { createChatEngine } from "./engine";
 
 export const chat = async (req: Request, res: Response, next: NextFunction) => {
   try {
diff --git a/templates/components/engines/simple/index.ts b/templates/types/simple/express/src/controllers/engine/index.ts
similarity index 100%
rename from templates/components/engines/simple/index.ts
rename to templates/types/simple/express/src/controllers/engine/index.ts
diff --git a/templates/types/simple/express/tsconfig.json b/templates/types/simple/express/tsconfig.json
index 57c4a628..e886da1e 100644
--- a/templates/types/simple/express/tsconfig.json
+++ b/templates/types/simple/express/tsconfig.json
@@ -4,6 +4,7 @@
     "esModuleInterop": true,
     "forceConsistentCasingInFileNames": true,
     "strict": true,
-    "skipLibCheck": true
+    "skipLibCheck": true,
+    "moduleResolution": "node"
   }
-}
+}
\ No newline at end of file
diff --git a/templates/types/simple/nextjs/app/api/chat/engine/index.ts b/templates/types/simple/nextjs/app/api/chat/engine/index.ts
new file mode 100644
index 00000000..abb02e90
--- /dev/null
+++ b/templates/types/simple/nextjs/app/api/chat/engine/index.ts
@@ -0,0 +1,7 @@
+import { LLM, SimpleChatEngine } from "llamaindex";
+
+export async function createChatEngine(llm: LLM) {
+  return new SimpleChatEngine({
+    llm,
+  });
+}
diff --git a/templates/types/simple/nextjs/app/api/chat/route.ts b/templates/types/simple/nextjs/app/api/chat/route.ts
index 651a020f..097341ab 100644
--- a/templates/types/simple/nextjs/app/api/chat/route.ts
+++ b/templates/types/simple/nextjs/app/api/chat/route.ts
@@ -1,6 +1,6 @@
 import { ChatMessage, OpenAI } from "llamaindex";
 import { NextRequest, NextResponse } from "next/server";
-import { createChatEngine } from "../../../../../../engines/context";
+import { createChatEngine } from "./engine";
 
 export const runtime = "nodejs";
 export const dynamic = "force-dynamic";
diff --git a/templates/types/streaming/express/src/controllers/chat.controller.ts b/templates/types/streaming/express/src/controllers/chat.controller.ts
index 58f96b03..162b5db7 100644
--- a/templates/types/streaming/express/src/controllers/chat.controller.ts
+++ b/templates/types/streaming/express/src/controllers/chat.controller.ts
@@ -1,7 +1,7 @@
 import { streamToResponse } from "ai";
 import { NextFunction, Request, Response } from "express";
 import { ChatMessage, OpenAI } from "llamaindex";
-import { createChatEngine } from "../../../../engines/context";
+import { createChatEngine } from "./engine";
 import { LlamaIndexStream } from "./llamaindex-stream";
 
 export const chat = async (req: Request, res: Response, next: NextFunction) => {
diff --git a/templates/types/streaming/express/src/controllers/engine/index.ts b/templates/types/streaming/express/src/controllers/engine/index.ts
new file mode 100644
index 00000000..abb02e90
--- /dev/null
+++ b/templates/types/streaming/express/src/controllers/engine/index.ts
@@ -0,0 +1,7 @@
+import { LLM, SimpleChatEngine } from "llamaindex";
+
+export async function createChatEngine(llm: LLM) {
+  return new SimpleChatEngine({
+    llm,
+  });
+}
diff --git a/templates/types/streaming/express/tsconfig.json b/templates/types/streaming/express/tsconfig.json
index bf2e3ec6..e886da1e 100644
--- a/templates/types/streaming/express/tsconfig.json
+++ b/templates/types/streaming/express/tsconfig.json
@@ -4,6 +4,7 @@
     "esModuleInterop": true,
     "forceConsistentCasingInFileNames": true,
     "strict": true,
-    "skipLibCheck": true
+    "skipLibCheck": true,
+    "moduleResolution": "node"
   }
 }
\ No newline at end of file
diff --git a/templates/types/streaming/nextjs/app/api/chat/engine/index.ts b/templates/types/streaming/nextjs/app/api/chat/engine/index.ts
new file mode 100644
index 00000000..abb02e90
--- /dev/null
+++ b/templates/types/streaming/nextjs/app/api/chat/engine/index.ts
@@ -0,0 +1,7 @@
+import { LLM, SimpleChatEngine } from "llamaindex";
+
+export async function createChatEngine(llm: LLM) {
+  return new SimpleChatEngine({
+    llm,
+  });
+}
diff --git a/templates/types/streaming/nextjs/app/api/chat/route.ts b/templates/types/streaming/nextjs/app/api/chat/route.ts
index e87ef01d..989a5fec 100644
--- a/templates/types/streaming/nextjs/app/api/chat/route.ts
+++ b/templates/types/streaming/nextjs/app/api/chat/route.ts
@@ -1,7 +1,7 @@
 import { Message, StreamingTextResponse } from "ai";
 import { OpenAI } from "llamaindex";
 import { NextRequest, NextResponse } from "next/server";
-import { createChatEngine } from "../../../../../../engines/context";
+import { createChatEngine } from "./engine";
 import { LlamaIndexStream } from "./llamaindex-stream";
 
 export const runtime = "nodejs";
-- 
GitLab