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

fix: code review and bugs

parent 232b633a
No related branches found
No related tags found
No related merge requests found
...@@ -189,95 +189,84 @@ export const askQuestions = async ( ...@@ -189,95 +189,84 @@ export const askQuestions = async (
} }
} }
if ( if (!program.model) {
program.framework === "express" || if (ciInfo.isCI) {
program.framework === "nextjs" || program.model = getPrefOrDefault("model");
program.framework === "fastapi" } else {
) { const { model } = await prompts(
if (!program.model) { {
if (ciInfo.isCI) { type: "select",
program.model = getPrefOrDefault("model"); name: "model",
} else { message: "Which model would you like to use?",
const { model } = await prompts( choices: [
{ { title: "gpt-3.5-turbo", value: "gpt-3.5-turbo" },
type: "select", { title: "gpt-4", value: "gpt-4" },
name: "model", { title: "gpt-4-1106-preview", value: "gpt-4-1106-preview" },
message: "Which model would you like to use?", {
choices: [ title: "gpt-4-vision-preview",
{ title: "gpt-3.5-turbo", value: "gpt-3.5-turbo" }, value: "gpt-4-vision-preview",
{ title: "gpt-4", value: "gpt-4" }, },
{ title: "gpt-4-1106-preview", value: "gpt-4-1106-preview" }, ],
{ initial: 0,
title: "gpt-4-vision-preview", },
value: "gpt-4-vision-preview", handlers,
}, );
], program.model = model;
initial: 0, preferences.model = model;
}, }
handlers, }
);
program.model = model; if (!program.engine) {
preferences.model = model; if (ciInfo.isCI) {
} program.engine = getPrefOrDefault("engine");
} else {
const { engine } = await prompts(
{
type: "select",
name: "engine",
message: "Which data source would you like to use?",
choices: [
{
title: "No data, just a simple chat",
value: "simple",
},
{ title: "Use an example PDF", value: "context" },
],
initial: 1,
},
handlers,
);
program.engine = engine;
preferences.engine = engine;
} }
} }
if ( if (
program.framework === "express" || program.engine !== "simple" &&
program.framework === "nextjs" || !program.vectorDb &&
program.framework === "fastapi" program.framework !== "fastapi"
) { ) {
if (!program.engine) { if (ciInfo.isCI) {
if (ciInfo.isCI) { program.vectorDb = getPrefOrDefault("vectorDb");
program.engine = getPrefOrDefault("engine"); } else {
} else { const { vectorDb } = await prompts(
const { engine } = await prompts( {
{ type: "select",
type: "select", name: "vectorDb",
name: "engine", message: "Would you like to use a vector database?",
message: "Which data source would you like to use?", choices: [
choices: [ {
{ title: "No, just store the data in the file system",
title: "No data, just a simple chat", value: "none",
value: "simple", },
}, { title: "MongoDB", value: "mongo" },
{ title: "Use an example PDF", value: "context" }, ],
], initial: 0,
initial: 1, },
}, handlers,
handlers, );
); program.vectorDb = vectorDb;
program.engine = engine; preferences.vectorDb = vectorDb;
preferences.engine = engine;
}
}
if (
program.engine !== "simple" &&
!program.vectorDb &&
program.framework !== "fastapi"
) {
if (ciInfo.isCI) {
program.vectorDb = getPrefOrDefault("vectorDb");
} else {
const { vectorDb } = await prompts(
{
type: "select",
name: "vectorDb",
message: "Would you like to use a vector database?",
choices: [
{
title: "No, just store the data in the file system",
value: "none",
},
{ title: "MongoDB", value: "mongo" },
],
initial: 0,
},
handlers,
);
program.vectorDb = vectorDb;
preferences.vectorDb = vectorDb;
}
} }
} }
......
from llama_index import ServiceContext import os
from app.context import create_base_context from llama_index import ServiceContext
from llama_index.llms import OpenAI
from app.engine.constants import CHUNK_SIZE, CHUNK_OVERLAP from app.engine.constants import CHUNK_SIZE, CHUNK_OVERLAP
def create_base_context():
model = os.getenv("MODEL", "gpt-3.5-turbo")
return ServiceContext.from_defaults(
llm=OpenAI(model=model),
)
def create_service_context(): def create_service_context():
base = create_base_context() base = create_base_context()
return ServiceContext.from_defaults( return ServiceContext.from_defaults(
......
...@@ -14,7 +14,7 @@ def get_chat_engine(): ...@@ -14,7 +14,7 @@ def get_chat_engine():
# check if storage already exists # check if storage already exists
if not os.path.exists(STORAGE_DIR): if not os.path.exists(STORAGE_DIR):
raise Exception( raise Exception(
"StorageContext is empty - call 'npm run generate' to generate the storage first" "StorageContext is empty - call 'python app/engine/generate.py' to generate the storage first"
) )
logger = logging.getLogger("uvicorn") logger = logging.getLogger("uvicorn")
# load the existing index # load the existing index
......
...@@ -33,7 +33,7 @@ export const chat = async (req: Request, res: Response) => { ...@@ -33,7 +33,7 @@ export const chat = async (req: Request, res: Response) => {
} }
const llm = new OpenAI({ const llm = new OpenAI({
model: process.env.MODEL, model: process.env.MODEL || "gpt-3.5-turbo",
}); });
const lastMessageContent = getLastMessageContent( const lastMessageContent = getLastMessageContent(
......
...@@ -2,7 +2,8 @@ from typing import List ...@@ -2,7 +2,8 @@ from typing import List
from fastapi import APIRouter, Depends, HTTPException, status from fastapi import APIRouter, Depends, HTTPException, status
from llama_index import VectorStoreIndex from llama_index import VectorStoreIndex
from llama_index.llms.base import MessageRole, ChatMessage from llama_index.llms.base import ChatMessage
from llama_index.llms.types import MessageRole
from pydantic import BaseModel from pydantic import BaseModel
from app.engine.index import get_chat_engine from app.engine.index import get_chat_engine
......
import os import os
import logging
from llama_index import ( from llama_index import ServiceContext
SimpleDirectoryReader, from llama_index.chat_engine import SimpleChatEngine
StorageContext,
VectorStoreIndex,
load_index_from_storage,
ServiceContext,
)
from llama_index.llms import OpenAI from llama_index.llms import OpenAI
STORAGE_DIR = "./storage" # directory to cache the generated index
DATA_DIR = "./data" # directory containing the documents to index
def create_base_context(): def create_base_context():
model = os.getenv("MODEL", "gpt-3.5-turbo") model = os.getenv("MODEL", "gpt-3.5-turbo")
...@@ -19,26 +11,6 @@ def create_base_context(): ...@@ -19,26 +11,6 @@ def create_base_context():
llm=OpenAI(model=model), llm=OpenAI(model=model),
) )
def get_index():
service_context = create_base_context()
logger = logging.getLogger("uvicorn")
# check if storage already exists
if not os.path.exists(STORAGE_DIR):
logger.info("Creating new index")
# load the documents and create the index
documents = SimpleDirectoryReader(DATA_DIR).load_data()
index = VectorStoreIndex.from_documents(documents,service_context=service_context)
# store it for later
index.storage_context.persist(STORAGE_DIR)
logger.info(f"Finished creating new index. Stored in {STORAGE_DIR}")
else:
# load the existing index
logger.info(f"Loading index from {STORAGE_DIR}...")
storage_context = StorageContext.from_defaults(persist_dir=STORAGE_DIR)
index = load_index_from_storage(storage_context,service_context=service_context)
logger.info(f"Finished loading index from {STORAGE_DIR}")
return index
def get_chat_engine(): def get_chat_engine():
index = get_index() return SimpleChatEngine.from_defaults(service_context=create_base_context())
return index.as_chat_engine()
...@@ -35,7 +35,7 @@ export const chat = async (req: Request, res: Response) => { ...@@ -35,7 +35,7 @@ export const chat = async (req: Request, res: Response) => {
} }
const llm = new OpenAI({ const llm = new OpenAI({
model: process.env.MODEL, model: process.env.MODEL || "gpt-3.5-turbo",
}); });
const chatEngine = await createChatEngine(llm); const chatEngine = await createChatEngine(llm);
......
...@@ -17,10 +17,10 @@ Example `.env` file: ...@@ -17,10 +17,10 @@ Example `.env` file:
OPENAI_API_KEY=<openai_api_key> OPENAI_API_KEY=<openai_api_key>
``` ```
Second, generate the embeddings of the documents in the `./data` directory: Second, generate the embeddings of the documents in the `./data` directory (if this folder exists - otherwise, skip this step):
``` ```
python app/engine/generate.py python app/engine/generate.py
``` ```
Third, run the development server: Third, run the development server:
......
import os import os
import logging
from llama_index import ( from llama_index import ServiceContext
SimpleDirectoryReader, from llama_index.chat_engine import SimpleChatEngine
StorageContext,
VectorStoreIndex,
load_index_from_storage,
ServiceContext,
)
from llama_index.llms import OpenAI from llama_index.llms import OpenAI
STORAGE_DIR = "./storage" # directory to cache the generated index
DATA_DIR = "./data" # directory containing the documents to index
def create_base_context(): def create_base_context():
model = os.getenv("MODEL", "gpt-3.5-turbo") model = os.getenv("MODEL", "gpt-3.5-turbo")
...@@ -19,26 +11,6 @@ def create_base_context(): ...@@ -19,26 +11,6 @@ def create_base_context():
llm=OpenAI(model=model), llm=OpenAI(model=model),
) )
def get_index():
service_context = create_base_context()
logger = logging.getLogger("uvicorn")
# check if storage already exists
if not os.path.exists(STORAGE_DIR):
logger.info("Creating new index")
# load the documents and create the index
documents = SimpleDirectoryReader(DATA_DIR).load_data()
index = VectorStoreIndex.from_documents(documents,service_context=service_context)
# store it for later
index.storage_context.persist(STORAGE_DIR)
logger.info(f"Finished creating new index. Stored in {STORAGE_DIR}")
else:
# load the existing index
logger.info(f"Loading index from {STORAGE_DIR}...")
storage_context = StorageContext.from_defaults(persist_dir=STORAGE_DIR)
index = load_index_from_storage(storage_context,service_context=service_context)
logger.info(f"Finished loading index from {STORAGE_DIR}")
return index
def get_chat_engine(): def get_chat_engine():
index = get_index() return SimpleChatEngine.from_defaults(service_context=create_base_context())
return index.as_chat_engine()
...@@ -42,7 +42,7 @@ export async function POST(request: NextRequest) { ...@@ -42,7 +42,7 @@ export async function POST(request: NextRequest) {
} }
const llm = new OpenAI({ const llm = new OpenAI({
model: process.env.MODEL, model: process.env.MODEL || "gpt-3.5-turbo",
maxTokens: 512, maxTokens: 512,
}); });
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment