Skip to content

Commit 2229506

Browse files
authored
Merge pull request #31 from ChiragAgg5k/chore-improve-system-prompt
chore: shifted system prompt to prefixMessages
2 parents 7963089 + 81379c8 commit 2229506

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

src/documents.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import { readFile } from "fs/promises";
33
import { Document } from "langchain/document";
44
import { MarkdownTextSplitter } from "langchain/text_splitter";
55

6+
/**
7+
* @returns {Promise<Document[]>}
8+
*/
69
const getDocumentation = async () => {
710
const filenames = await glob([
811
"./sources/website/src/routes/docs/**/*.markdoc",
@@ -36,6 +39,9 @@ const getDocumentation = async () => {
3639
);
3740
};
3841

42+
/**
43+
* @returns {Promise<Document[]>} Array of Document objects containing processed references
44+
*/
3945
const getReferences = async () => {
4046
const filenames = await glob(["./sources/references/**/*.md"]);
4147

@@ -64,7 +70,7 @@ export const getDocuments = async () => {
6470
return await splitDocuments([...documentation, ...references]);
6571
};
6672

67-
/**x
73+
/**
6874
* @param {Document[]} documents
6975
* @returns {Promise<Document<Record<string, any>>[]>}
7076
*/
@@ -80,6 +86,10 @@ async function splitDocuments(documents) {
8086
return await splitter.createDocuments(texts, metadatas);
8187
}
8288

89+
/**
90+
* @param {string} contents
91+
* @returns {Object.<string, string>}
92+
*/
8393
function parseMarkdownFrontmatter(contents) {
8494
const raw = contents.match(/^---\n([\s\S]*?)\n---/);
8595
if (!raw) {
@@ -94,6 +104,10 @@ function parseMarkdownFrontmatter(contents) {
94104
return frontmatter;
95105
}
96106

107+
/**
108+
* @param {string} filename
109+
* @returns {{sdk: string, service: string}}
110+
*/
97111
function parseReferenceData(filename) {
98112
const [sdk, service] = filename
99113
.replace("sources/references/", "")

src/embeddings.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { OpenAIChat } from "langchain/llms/openai";
44
import { loadQAStuffChain } from "langchain/chains";
55
import { getDocuments } from "./documents.js";
66

7+
/**
8+
* @returns {Promise<VectorStoreRetriever<HNSWLib>>}
9+
*/
710
export const intializeDocumentRetriever = async () => {
811
const embeddings = new OpenAIEmbeddings({
912
openAIApiKey: process.env._APP_ASSISTANT_OPENAI_API_KEY,
@@ -15,7 +18,11 @@ export const intializeDocumentRetriever = async () => {
1518
return vectorStore.asRetriever(5);
1619
};
1720

18-
export const getOpenAIChat = async (onToken) =>
21+
/**
22+
* @param {function} onToken
23+
* @param {string} systemPrompt
24+
*/
25+
export const getOpenAIChat = async (onToken, systemPrompt) =>
1926
new OpenAIChat({
2027
modelName: "gpt-4o",
2128
openAIApiKey: process.env._APP_ASSISTANT_OPENAI_API_KEY,
@@ -27,8 +34,18 @@ export const getOpenAIChat = async (onToken) =>
2734
handleLLMNewToken: onToken,
2835
},
2936
],
37+
prefixMessages: [
38+
{
39+
role: "system",
40+
content: systemPrompt,
41+
},
42+
],
3043
});
3144

32-
export const getRagChain = async (onToken) => {
33-
return loadQAStuffChain(await getOpenAIChat(onToken));
45+
/**
46+
* @param {function} onToken
47+
* @param {string} systemPrompt
48+
*/
49+
export const getRagChain = async (onToken, systemPrompt) => {
50+
return loadQAStuffChain(await getOpenAIChat(onToken, systemPrompt));
3451
};

src/main.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ app.post("/v1/models/assistant/prompt", async (req, res) => {
3030
return;
3131
}
3232

33-
// raw to text
3433
const decoder = new TextDecoder();
3534
const text = decoder.decode(req.body);
3635

@@ -41,11 +40,11 @@ app.post("/v1/models/assistant/prompt", async (req, res) => {
4140

4241
const chain = await getRagChain((token) => {
4342
res.write(token);
44-
});
43+
}, systemPrompt);
4544

4645
await chain.call({
4746
input_documents: relevantDocuments,
48-
question: `${systemPrompt}\n\n${prompt}`,
47+
question: prompt,
4948
});
5049

5150
const sources = new Set(
@@ -71,14 +70,11 @@ app.post("/v1/models/generic/prompt", async (req, res) => {
7170
let { prompt, systemPrompt } = JSON.parse(text);
7271
systemPrompt ??= SYSTEM_PROMPT;
7372

74-
const chain = await getOpenAIChat((token) => {
73+
const chat = await getOpenAIChat((token) => {
7574
res.write(token);
76-
});
75+
}, systemPrompt);
7776

78-
await chain.invoke([
79-
["system", systemPrompt],
80-
["human", prompt],
81-
])
77+
await chat.call(prompt);
8278

8379
res.end();
8480
});

0 commit comments

Comments
 (0)