-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support test chat engine with dataset (#152)
- Loading branch information
Showing
6 changed files
with
100 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import {createChat} from "@/core/repositories/chat"; | ||
import {getChatEngineByIdOrName} from "@/core/repositories/chat_engine"; | ||
import {getIndexByNameOrThrow} from "@/core/repositories/index_"; | ||
import {LlamaindexChatService} from "@/core/services/llamaindex/chating"; | ||
import { defineHandler } from '@/lib/next/handler'; | ||
import {baseRegistry} from "@/rag-spec/base"; | ||
import {getFlow} from "@/rag-spec/createFlow"; | ||
import {Langfuse} from "langfuse"; | ||
import { z } from 'zod'; | ||
import { eachOfLimit } from 'async'; | ||
|
||
const SpecificChatEngineSchema = z.object({ | ||
id: z.string(), | ||
}); | ||
|
||
const RunTestParamsSchema = z.object({ | ||
index_name: z.string().default('default'), | ||
dataset_name: z.string(), | ||
run: z.object({ | ||
name: z.string(), | ||
description: z.string().optional(), | ||
metadata: z.record(z.string()).optional(), | ||
}), | ||
concurrency: z.number().default(10) | ||
}); | ||
|
||
export const POST = defineHandler({ | ||
auth: 'admin', | ||
params: SpecificChatEngineSchema, | ||
body: RunTestParamsSchema, | ||
}, async ({ | ||
params: { id }, | ||
body: { | ||
dataset_name, | ||
index_name, | ||
concurrency, | ||
run | ||
}, | ||
}) => { | ||
const engine = await getChatEngineByIdOrName(id) | ||
|
||
const langfuse = new Langfuse(); | ||
const dataset = await langfuse.getDataset(dataset_name); | ||
|
||
const index = await getIndexByNameOrThrow(index_name); | ||
const flow = await getFlow(baseRegistry); | ||
const chatService = new LlamaindexChatService({ flow, index, langfuse }); | ||
|
||
await eachOfLimit(dataset.items, concurrency, async (item: any) => { | ||
const userMessage = item.input; | ||
const userId = 'tester'; | ||
const 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(), | ||
created_by: userId, | ||
title: userMessage, | ||
}); | ||
|
||
console.log(`[Testing] Testing with question (chat engine: ${id}): ${userMessage}`) | ||
const chatResult = await chatService.chat(chat.url_key, userId, userMessage, false, false); | ||
if (chatResult.trace) { | ||
await item.link(chatResult.trace, run.name, { | ||
description: run.description, | ||
metadata: run.metadata | ||
}); | ||
} | ||
}); | ||
}); | ||
|
||
export const dynamic = 'force-dynamic'; | ||
|
||
export const maxDuration = 150; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters