Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Mini256 committed Jun 3, 2024
1 parent 241927d commit 2213f7f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 31 deletions.
8 changes: 6 additions & 2 deletions src/app/api/v1/chats/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {type Chat, createChat, getChatByUrlKey, listChats} from '@/core/repositories/chat';
import {getChatEngineByNameOrDefault} from '@/core/repositories/chat_engine';
import {getChatEngineByIdOrName} from '@/core/repositories/chat_engine';
import {getIndexByNameOrThrow} from '@/core/repositories/index_';
import {LlamaindexChatService} from '@/core/services/llamaindex/chating';
import {toPageRequest} from '@/lib/database';
Expand All @@ -21,6 +21,9 @@ const ChatRequest = z.object({
// TODO: using `title` instead of.
name: z.string().optional(),
index: z.string().optional(),
// TODO: remove it
// @Deprecated
engine: z.number().optional(),
chat_engine: z.string().optional(),
regenerate: z.boolean().optional(),
messageId: z.coerce.number().int().optional(),
Expand All @@ -42,7 +45,7 @@ export const POST = defineHandler({
messages,
} = body;

const engine = await getChatEngineByNameOrDefault(body.chat_engine);
const engine = await getChatEngineByIdOrName(body.chat_engine ?? body.engine)

// TODO: need refactor, it is too complex now
// For chat page, create a chat and return the session ID (url_key) first.
Expand Down Expand Up @@ -75,6 +78,7 @@ export const POST = defineHandler({
if (!sessionId) {
chat = await createChat({
engine: engine.engine,
engine_id: engine.id,
engine_name: engine.name,
engine_options: JSON.stringify(engine.engine_options),
created_at: new Date(),
Expand Down
17 changes: 2 additions & 15 deletions src/app/api/v1/indexes/[name]/retrieve/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import {
ChatEngine,
getChatEngineByIdOrDefault,
getChatEngineByNameOrDefault,
getDefaultChatEngine
getChatEngineByIdOrName,
} from "@/core/repositories/chat_engine";
import {getIndexByName} from '@/core/repositories/index_';
import {LlamaindexRetrieveService} from '@/core/services/llamaindex/retrieving';
Expand Down Expand Up @@ -31,17 +28,7 @@ export const POST = defineHandler({
notFound();
}

let engine: ChatEngine;
if (body.chat_engine) {
engine = await getChatEngineByNameOrDefault(body.chat_engine);
} else if (body.engine) {
// TODO: remove it after migration is finished.
engine = await getChatEngineByIdOrDefault(body.engine)
} else {
engine = await getDefaultChatEngine();
}
const { engine_options } = engine;

const { engine_options } = await getChatEngineByIdOrName(body.chat_engine ?? body.engine)
const {
llm: llmConfig = {
provider: LLMProvider.OPENAI,
Expand Down
21 changes: 8 additions & 13 deletions src/core/repositories/chat_engine.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {typeOf} from "react-is";
import {ChatEngineProvider} from "../schema/chat_engines";
import {
CondenseQuestionChatEngineOptions,
Expand Down Expand Up @@ -52,23 +53,17 @@ export async function getDefaultChatEngine () {
.executeTakeFirstOrThrow();
}

export async function getChatEngineByIdOrDefault (engineId?: number): Promise<ChatEngine> {
if (engineId) {
const engine = await getChatEngineById(engineId);
export async function getChatEngineByIdOrName (engineIdOrName?: number | string): Promise<ChatEngine> {
if (typeof engineIdOrName === 'number') {
const engine = await getChatEngineById(engineIdOrName);
if (!engine) {
throw CHAT_ENGINE_NOT_FOUND_ERROR.format(engineId);
throw CHAT_ENGINE_NOT_FOUND_ERROR.format(engineIdOrName);
}
return engine;
} else {
return await getDefaultChatEngine();
}
}

export async function getChatEngineByNameOrDefault (engineName?: string): Promise<ChatEngine> {
if (engineName) {
const engine = await getChatEngineByName(engineName);
} else if (typeof engineIdOrName === 'string') {
const engine = await getChatEngineByName(engineIdOrName);
if (!engine) {
throw CHAT_ENGINE_NOT_FOUND_ERROR.format(engineName);
throw CHAT_ENGINE_NOT_FOUND_ERROR.format(engineIdOrName);
}
return engine;
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/core/services/llamaindex/chating.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export class LlamaindexChatService extends AppChatService {
prompts
} = engineOptions;

const chatEngineName = await getChatEngineNameByID(chat.engine_id);
const chatEngineName = chat.engine_name || await getChatEngineNameByID(chat.engine_id);

// Init tracing.
const trace = this.langfuse?.trace({
Expand Down

0 comments on commit 2213f7f

Please sign in to comment.