Merge pull request #1 from samaypusarla/samay

feat: Added temperature and TopP
This commit is contained in:
yisding
2023-08-04 08:33:40 -07:00
committed by GitHub
2 changed files with 49 additions and 4 deletions
+16 -4
View File
@@ -3,9 +3,11 @@ import type { NextApiRequest, NextApiResponse } from "next";
import {
IndexDict,
OpenAI,
RetrieverQueryEngine,
TextNode,
VectorStoreIndex,
serviceContextFromDefaults,
} from "llamaindex";
type Input = {
@@ -15,6 +17,8 @@ type Input = {
text: string;
embedding: number[];
}[];
temperature: number;
topP: number;
};
type Output = {
@@ -26,14 +30,15 @@ type Output = {
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<Output>
res: NextApiResponse<Output>,
) {
if (req.method !== "POST") {
res.status(405).json({ error: "Method not allowed" });
return;
}
const { query, topK, nodesWithEmbedding }: Input = req.body;
const { query, topK, nodesWithEmbedding, temperature, topP }: Input =
req.body;
const embeddingResults = nodesWithEmbedding.map((config) => {
return {
@@ -46,13 +51,18 @@ export default async function handler(
indexDict.addNode(node);
}
const index = await VectorStoreIndex.init({ indexStruct: indexDict });
const index = await VectorStoreIndex.init({
indexStruct: indexDict,
serviceContext: serviceContextFromDefaults({
llm: new OpenAI({ temperature: temperature, topP: topP }),
}),
});
index.vectorStore.add(embeddingResults);
if (!index.vectorStore.storesText) {
await index.docStore.addDocuments(
embeddingResults.map((result) => result.node),
true
true,
);
}
await index.indexStore?.addIndexStruct(indexDict);
@@ -60,7 +70,9 @@ export default async function handler(
const retriever = index.asRetriever();
retriever.similarityTopK = topK ?? 2;
const queryEngine = new RetrieverQueryEngine(retriever);
const result = await queryEngine.query(query);
res.status(200).json({ payload: { response: result.response } });
+33
View File
@@ -11,6 +11,8 @@ import essay from "@/lib/essay";
const DEFAULT_CHUNK_SIZE = 1024;
const DEFAULT_CHUNK_OVERLAP = 20;
const DEFAULT_TOP_K = 2;
const DEFAULT_TEMPERATURE = 0;
const DEFAULT_TOP_P = 1;
export default function Home() {
const answerId = useId();
@@ -28,6 +30,8 @@ export default function Home() {
const [chunkSize, setChunkSize] = useState(DEFAULT_CHUNK_SIZE);
const [chunkOverlap, setChunkOverlap] = useState(DEFAULT_CHUNK_OVERLAP);
const [topK, setTopK] = useState(DEFAULT_TOP_K);
const [temperature, setTemperature] = useState(DEFAULT_TEMPERATURE);
const [topP, setTopP] = useState(DEFAULT_TOP_P);
const [answer, setAnswer] = useState("");
return (
@@ -136,6 +140,33 @@ export default function Home() {
setTopK(value);
}}
/>
<LinkedSlider
className="my-2"
label="Temperature:"
description={"Fill in"}
min={0}
max={1}
step={0.1}
value={temperature}
onChange={(value: number) => {
setTemperature(value);
}}
/>
<LinkedSlider
className="my-2"
label="Top P:"
description={"Fill in"}
min={0}
max={1}
step={0.1}
value={topP}
onChange={(value: number) => {
setTopP(value);
}}
/>
<div className="my-2 space-y-2">
<Label htmlFor={queryId}>Query:</Label>
<div className="flex w-full space-x-2">
@@ -162,6 +193,8 @@ export default function Home() {
query,
topK,
nodesWithEmbedding,
temperature,
topP,
}),
});