MCPcopy Index your code
hub / github.com/sqlchat/sqlchat / generateDbPromptFromContext

Function generateDbPromptFromContext

src/utils/openai.ts:17–55  ·  view source on GitHub ↗
(
  promptGenerator: (engine: Engine | undefined, schema: string | undefined) => string,
  engine: Engine,
  schemaList: Schema[],
  selectedSchemaName: string,
  selectedTableNameList: string[],
  maxToken: number,
  userPrompt?: string
)

Source from the content-addressed store, hash-verified

15};
16
17export function generateDbPromptFromContext(
18 promptGenerator: (engine: Engine | undefined, schema: string | undefined) => string,
19 engine: Engine,
20 schemaList: Schema[],
21 selectedSchemaName: string,
22 selectedTableNameList: string[],
23 maxToken: number,
24 userPrompt?: string
25): string {
26 // userPrompt is the message that user want to send to bot. When to look prompt in drawer, userPrompt is undefined.
27 let tokens = countTextTokens(userPrompt || "");
28
29 // Empty table name(such as []) denote all table. [] and `undefined` both are false in `if`
30 // The above comment is out of date. [] is true in `if` now. And no selected table should not denote all table now.
31 // Because in have Token custom number in connectionSidebar. If [] denote all table. the Token will be inconsistent.
32 const tableList: string[] = [];
33 const selectedSchema = schemaList.find((schema: Schema) => schema.name == (selectedSchemaName || ""));
34 if (selectedTableNameList) {
35 selectedTableNameList.forEach((tableName: string) => {
36 const table = selectedSchema?.tables.find((table: Table) => table.name == tableName);
37 tableList.push(table!.structure);
38 });
39 } else {
40 for (const table of selectedSchema?.tables || []) {
41 tableList.push(table!.structure);
42 }
43 }
44
45 let finalTableList = [];
46 if (tableList) {
47 for (const table of tableList) {
48 if (tokens < maxToken / 2) {
49 tokens += countTextTokens(table + "\n\n");
50 finalTableList.push(table);
51 }
52 }
53 }
54 return promptGenerator(engine, finalTableList.join("\n\n"));
55}
56
57export function allowSelfOpenAIKey() {
58 return process.env.NEXT_PUBLIC_ALLOW_SELF_OPENAI_KEY == "true";

Callers 2

getPromptFunction · 0.90

Calls 1

countTextTokensFunction · 0.85

Tested by

no test coverage detected