Skip to content
Snippets Groups Projects
Commit 81cf59de authored by thucpn's avatar thucpn Committed by Marcus Schiesser
Browse files

feat: remove constants.ts in ts templates

parent 2e4be99f
Branches
Tags
No related merge requests found
...@@ -19,17 +19,24 @@ import { ...@@ -19,17 +19,24 @@ import {
const createEnvLocalFile = async ( const createEnvLocalFile = async (
root: string, root: string,
openAiKey?: string, opts?: {
vectorDb?: TemplateVectorDB, openAiKey?: string;
vectorDb?: TemplateVectorDB;
model?: string;
},
) => { ) => {
const envFileName = ".env"; const envFileName = ".env";
let content = ""; let content = "";
if (openAiKey) { const model = opts?.model || "gpt-3.5-turbo";
content += `OPENAI_API_KEY=${openAiKey}\n`; content += `NEXT_PUBLIC_MODEL=${model}\n`;
content += `MODEL=${model}\n`;
if (opts?.openAiKey) {
content += `OPENAI_API_KEY=${opts?.openAiKey}\n`;
} }
switch (vectorDb) { switch (opts?.vectorDb) {
case "mongo": { case "mongo": {
content += `MONGODB_URI=\n`; content += `MONGODB_URI=\n`;
content += `MONGODB_DATABASE=\n`; content += `MONGODB_DATABASE=\n`;
...@@ -205,10 +212,6 @@ const installTSTemplate = async ({ ...@@ -205,10 +212,6 @@ const installTSTemplate = async ({
} }
if (framework === "nextjs" || framework === "express") { if (framework === "nextjs" || framework === "express") {
await fs.writeFile(
path.join(root, "constants.ts"),
`export const MODEL = "${model || "gpt-3.5-turbo"}";\n`,
);
console.log("\nUsing OpenAI model: ", model || "gpt-3.5-turbo", "\n"); console.log("\nUsing OpenAI model: ", model || "gpt-3.5-turbo", "\n");
} }
...@@ -369,7 +372,11 @@ export const installTemplate = async ( ...@@ -369,7 +372,11 @@ export const installTemplate = async (
// This is a backend, so we need to copy the test data and create the env file. // This is a backend, so we need to copy the test data and create the env file.
// Copy the environment file to the target directory. // Copy the environment file to the target directory.
await createEnvLocalFile(props.root, props.openAiKey, props.vectorDb); await createEnvLocalFile(props.root, {
openAiKey: props.openAiKey,
vectorDb: props.vectorDb,
model: props.model,
});
// Copy test pdf file // Copy test pdf file
await copyTestData( await copyTestData(
......
export const MODEL = "gpt-3.5-turbo";
import { Request, Response } from "express"; import { Request, Response } from "express";
import { ChatMessage, MessageContent, OpenAI } from "llamaindex"; import { ChatMessage, MessageContent, OpenAI } from "llamaindex";
import { MODEL } from "../../constants";
import { createChatEngine } from "./engine"; import { createChatEngine } from "./engine";
const getLastMessageContent = ( const getLastMessageContent = (
...@@ -34,7 +33,7 @@ export const chat = async (req: Request, res: Response) => { ...@@ -34,7 +33,7 @@ export const chat = async (req: Request, res: Response) => {
} }
const llm = new OpenAI({ const llm = new OpenAI({
model: MODEL, model: process.env.MODEL,
}); });
const lastMessageContent = getLastMessageContent( const lastMessageContent = getLastMessageContent(
......
export const MODEL = "gpt-3.5-turbo";
import { streamToResponse } from "ai"; import { streamToResponse } from "ai";
import { Request, Response } from "express"; import { Request, Response } from "express";
import { ChatMessage, MessageContent, OpenAI } from "llamaindex"; import { ChatMessage, MessageContent, OpenAI } from "llamaindex";
import { MODEL } from "../../constants";
import { createChatEngine } from "./engine"; import { createChatEngine } from "./engine";
import { LlamaIndexStream } from "./llamaindex-stream"; import { LlamaIndexStream } from "./llamaindex-stream";
...@@ -36,7 +35,7 @@ export const chat = async (req: Request, res: Response) => { ...@@ -36,7 +35,7 @@ export const chat = async (req: Request, res: Response) => {
} }
const llm = new OpenAI({ const llm = new OpenAI({
model: MODEL, model: process.env.MODEL,
}); });
const chatEngine = await createChatEngine(llm); const chatEngine = await createChatEngine(llm);
......
import { MODEL } from "@/constants";
import { Message, StreamingTextResponse } from "ai"; import { Message, StreamingTextResponse } from "ai";
import { MessageContent, OpenAI } from "llamaindex"; import { MessageContent, OpenAI } from "llamaindex";
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
...@@ -43,7 +42,7 @@ export async function POST(request: NextRequest) { ...@@ -43,7 +42,7 @@ export async function POST(request: NextRequest) {
} }
const llm = new OpenAI({ const llm = new OpenAI({
model: MODEL, model: process.env.MODEL,
maxTokens: 512, maxTokens: 512,
}); });
......
"use client"; "use client";
import { MODEL } from "@/constants";
import { useChat } from "ai/react"; import { useChat } from "ai/react";
import { ChatInput, ChatMessages } from "./ui/chat"; import { ChatInput, ChatMessages } from "./ui/chat";
...@@ -33,7 +32,7 @@ export default function ChatSection() { ...@@ -33,7 +32,7 @@ export default function ChatSection() {
handleSubmit={handleSubmit} handleSubmit={handleSubmit}
handleInputChange={handleInputChange} handleInputChange={handleInputChange}
isLoading={isLoading} isLoading={isLoading}
multiModal={MODEL === "gpt-4-vision-preview"} multiModal={process.env.NEXT_PUBLIC_MODEL === "gpt-4-vision-preview"}
/> />
</div> </div>
); );
......
export const MODEL = "gpt-4-vision-preview";
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment