langchain 0.0.69 → 0.0.70

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/dist/chains/llm_chain.d.ts +7 -7
  2. package/dist/chains/question_answering/load.cjs +14 -40
  3. package/dist/chains/question_answering/load.d.ts +3 -3
  4. package/dist/chains/question_answering/load.js +16 -42
  5. package/dist/chains/sql_db/sql_db_chain.cjs +4 -0
  6. package/dist/chains/sql_db/sql_db_chain.d.ts +3 -1
  7. package/dist/chains/sql_db/sql_db_chain.js +4 -0
  8. package/dist/chains/sql_db/sql_db_prompt.cjs +58 -1
  9. package/dist/chains/sql_db/sql_db_prompt.d.ts +3 -0
  10. package/dist/chains/sql_db/sql_db_prompt.js +57 -0
  11. package/dist/chat_models/openai.cjs +83 -3
  12. package/dist/chat_models/openai.d.ts +18 -53
  13. package/dist/chat_models/openai.js +84 -4
  14. package/dist/document_loaders/fs/directory.d.ts +5 -6
  15. package/dist/document_loaders/fs/unstructured.cjs +68 -49
  16. package/dist/document_loaders/fs/unstructured.d.ts +16 -11
  17. package/dist/document_loaders/fs/unstructured.js +69 -50
  18. package/dist/document_loaders/web/s3.cjs +2 -1
  19. package/dist/document_loaders/web/s3.js +2 -1
  20. package/dist/embeddings/openai.cjs +84 -5
  21. package/dist/embeddings/openai.d.ts +7 -2
  22. package/dist/embeddings/openai.js +84 -5
  23. package/dist/llms/openai-chat.cjs +91 -5
  24. package/dist/llms/openai-chat.d.ts +22 -58
  25. package/dist/llms/openai-chat.js +91 -5
  26. package/dist/llms/openai.cjs +88 -4
  27. package/dist/llms/openai.d.ts +18 -59
  28. package/dist/llms/openai.js +89 -5
  29. package/dist/memory/base.cjs +1 -1
  30. package/dist/memory/base.js +1 -1
  31. package/dist/memory/index.cjs +3 -1
  32. package/dist/memory/index.d.ts +1 -0
  33. package/dist/memory/index.js +1 -0
  34. package/dist/memory/vector_store.cjs +61 -0
  35. package/dist/memory/vector_store.d.ts +19 -0
  36. package/dist/memory/vector_store.js +57 -0
  37. package/dist/output_parsers/combining.cjs +15 -4
  38. package/dist/output_parsers/combining.d.ts +1 -0
  39. package/dist/output_parsers/combining.js +15 -4
  40. package/dist/output_parsers/structured.cjs +10 -5
  41. package/dist/output_parsers/structured.js +10 -5
  42. package/dist/util/sql_utils.cjs +15 -1
  43. package/dist/util/sql_utils.d.ts +2 -0
  44. package/dist/util/sql_utils.js +13 -0
  45. package/dist/vectorstores/prisma.cjs +1 -1
  46. package/dist/vectorstores/prisma.js +2 -1
  47. package/dist/vectorstores/supabase.cjs +13 -1
  48. package/dist/vectorstores/supabase.d.ts +6 -1
  49. package/dist/vectorstores/supabase.js +13 -1
  50. package/package.json +5 -5
@@ -6,13 +6,13 @@ import { BaseOutputParser } from "../schema/output_parser.js";
6
6
  import { SerializedLLMChain } from "./serde.js";
7
7
  import { CallbackManager } from "../callbacks/index.js";
8
8
  import { CallbackManagerForChainRun } from "../callbacks/manager.js";
9
- export interface LLMChainInput extends ChainInputs {
9
+ export interface LLMChainInput<T extends string | object = string> extends ChainInputs {
10
10
  /** Prompt object to use */
11
11
  prompt: BasePromptTemplate;
12
12
  /** LLM Wrapper to use */
13
13
  llm: BaseLanguageModel;
14
14
  /** OutputParser to use */
15
- outputParser?: BaseOutputParser;
15
+ outputParser?: BaseOutputParser<T>;
16
16
  /** Key to use for output, defaults to `text` */
17
17
  outputKey?: string;
18
18
  }
@@ -29,14 +29,14 @@ export interface LLMChainInput extends ChainInputs {
29
29
  * const llm = new LLMChain({ llm: new OpenAI(), prompt });
30
30
  * ```
31
31
  */
32
- export declare class LLMChain extends BaseChain implements LLMChainInput {
32
+ export declare class LLMChain<T extends string | object = string> extends BaseChain implements LLMChainInput<T> {
33
33
  prompt: BasePromptTemplate;
34
34
  llm: BaseLanguageModel;
35
35
  outputKey: string;
36
- outputParser?: BaseOutputParser;
36
+ outputParser?: BaseOutputParser<T>;
37
37
  get inputKeys(): string[];
38
38
  get outputKeys(): string[];
39
- constructor(fields: LLMChainInput);
39
+ constructor(fields: LLMChainInput<T>);
40
40
  /** @ignore */
41
41
  _getFinalOutput(generations: Generation[], promptValue: BasePromptValue, runManager?: CallbackManagerForChainRun): Promise<unknown>;
42
42
  /** @ignore */
@@ -53,8 +53,8 @@ export declare class LLMChain extends BaseChain implements LLMChainInput {
53
53
  * llm.predict({ adjective: "funny" })
54
54
  * ```
55
55
  */
56
- predict(values: ChainValues, callbackManager?: CallbackManager): Promise<string>;
56
+ predict(values: ChainValues, callbackManager?: CallbackManager): Promise<T>;
57
57
  _chainType(): "llm_chain";
58
- static deserialize(data: SerializedLLMChain): Promise<LLMChain>;
58
+ static deserialize(data: SerializedLLMChain): Promise<LLMChain<string>>;
59
59
  serialize(): SerializedLLMChain;
60
60
  }
@@ -7,79 +7,53 @@ const stuff_prompts_js_1 = require("./stuff_prompts.cjs");
7
7
  const map_reduce_prompts_js_1 = require("./map_reduce_prompts.cjs");
8
8
  const refine_prompts_js_1 = require("./refine_prompts.cjs");
9
9
  const loadQAChain = (llm, params = { type: "stuff" }) => {
10
- const { type, verbose } = params;
10
+ const { type } = params;
11
11
  if (type === "stuff") {
12
- const { prompt = stuff_prompts_js_1.DEFAULT_QA_PROMPT } = params;
13
- const llmChain = new llm_chain_js_1.LLMChain({ prompt, llm, verbose });
14
- const chain = new combine_docs_chain_js_1.StuffDocumentsChain({ llmChain, verbose });
15
- return chain;
12
+ return loadQAStuffChain(llm, params);
16
13
  }
17
14
  if (type === "map_reduce") {
18
- const { combineMapPrompt = map_reduce_prompts_js_1.DEFAULT_COMBINE_QA_PROMPT, combinePrompt = map_reduce_prompts_js_1.COMBINE_PROMPT, returnIntermediateSteps, } = params;
19
- const llmChain = new llm_chain_js_1.LLMChain({ prompt: combineMapPrompt, llm, verbose });
20
- const combineLLMChain = new llm_chain_js_1.LLMChain({
21
- prompt: combinePrompt,
22
- llm,
23
- verbose,
24
- });
25
- const combineDocumentChain = new combine_docs_chain_js_1.StuffDocumentsChain({
26
- llmChain: combineLLMChain,
27
- documentVariableName: "summaries",
28
- verbose,
29
- });
30
- const chain = new combine_docs_chain_js_1.MapReduceDocumentsChain({
31
- llmChain,
32
- combineDocumentChain,
33
- returnIntermediateSteps,
34
- verbose,
35
- });
36
- return chain;
15
+ return loadQAMapReduceChain(llm, params);
37
16
  }
38
17
  if (type === "refine") {
39
- const { questionPrompt = refine_prompts_js_1.QUESTION_PROMPT_SELECTOR.getPrompt(llm), refinePrompt = refine_prompts_js_1.REFINE_PROMPT_SELECTOR.getPrompt(llm), } = params;
40
- const llmChain = new llm_chain_js_1.LLMChain({ prompt: questionPrompt, llm, verbose });
41
- const refineLLMChain = new llm_chain_js_1.LLMChain({ prompt: refinePrompt, llm, verbose });
42
- const chain = new combine_docs_chain_js_1.RefineDocumentsChain({
43
- llmChain,
44
- refineLLMChain,
45
- verbose,
46
- });
47
- return chain;
18
+ return loadQARefineChain(llm, params);
48
19
  }
49
20
  throw new Error(`Invalid _type: ${type}`);
50
21
  };
51
22
  exports.loadQAChain = loadQAChain;
52
- const loadQAStuffChain = (llm, params = {}) => {
23
+ function loadQAStuffChain(llm, params = {}) {
53
24
  const { prompt = stuff_prompts_js_1.QA_PROMPT_SELECTOR.getPrompt(llm), verbose } = params;
54
25
  const llmChain = new llm_chain_js_1.LLMChain({ prompt, llm, verbose });
55
- const chain = new combine_docs_chain_js_1.StuffDocumentsChain({ llmChain });
26
+ const chain = new combine_docs_chain_js_1.StuffDocumentsChain({ llmChain, verbose });
56
27
  return chain;
57
- };
28
+ }
58
29
  exports.loadQAStuffChain = loadQAStuffChain;
59
- const loadQAMapReduceChain = (llm, params = {}) => {
30
+ function loadQAMapReduceChain(llm, params = {}) {
60
31
  const { combineMapPrompt = map_reduce_prompts_js_1.COMBINE_QA_PROMPT_SELECTOR.getPrompt(llm), combinePrompt = map_reduce_prompts_js_1.COMBINE_PROMPT_SELECTOR.getPrompt(llm), verbose, returnIntermediateSteps, } = params;
61
32
  const llmChain = new llm_chain_js_1.LLMChain({ prompt: combineMapPrompt, llm, verbose });
62
33
  const combineLLMChain = new llm_chain_js_1.LLMChain({ prompt: combinePrompt, llm, verbose });
63
34
  const combineDocumentChain = new combine_docs_chain_js_1.StuffDocumentsChain({
64
35
  llmChain: combineLLMChain,
65
36
  documentVariableName: "summaries",
37
+ verbose,
66
38
  });
67
39
  const chain = new combine_docs_chain_js_1.MapReduceDocumentsChain({
68
40
  llmChain,
69
41
  combineDocumentChain,
70
42
  returnIntermediateSteps,
43
+ verbose,
71
44
  });
72
45
  return chain;
73
- };
46
+ }
74
47
  exports.loadQAMapReduceChain = loadQAMapReduceChain;
75
- const loadQARefineChain = (llm, params = {}) => {
48
+ function loadQARefineChain(llm, params = {}) {
76
49
  const { questionPrompt = refine_prompts_js_1.QUESTION_PROMPT_SELECTOR.getPrompt(llm), refinePrompt = refine_prompts_js_1.REFINE_PROMPT_SELECTOR.getPrompt(llm), verbose, } = params;
77
50
  const llmChain = new llm_chain_js_1.LLMChain({ prompt: questionPrompt, llm, verbose });
78
51
  const refineLLMChain = new llm_chain_js_1.LLMChain({ prompt: refinePrompt, llm, verbose });
79
52
  const chain = new combine_docs_chain_js_1.RefineDocumentsChain({
80
53
  llmChain,
81
54
  refineLLMChain,
55
+ verbose,
82
56
  });
83
57
  return chain;
84
- };
58
+ }
85
59
  exports.loadQARefineChain = loadQARefineChain;
@@ -13,17 +13,17 @@ export interface StuffQAChainParams {
13
13
  prompt?: BasePromptTemplate;
14
14
  verbose?: boolean;
15
15
  }
16
- export declare const loadQAStuffChain: (llm: BaseLanguageModel, params?: StuffQAChainParams) => StuffDocumentsChain;
16
+ export declare function loadQAStuffChain(llm: BaseLanguageModel, params?: StuffQAChainParams): StuffDocumentsChain;
17
17
  export interface MapReduceQAChainParams {
18
18
  returnIntermediateSteps?: MapReduceDocumentsChainInput["returnIntermediateSteps"];
19
19
  combineMapPrompt?: BasePromptTemplate;
20
20
  combinePrompt?: BasePromptTemplate;
21
21
  verbose?: boolean;
22
22
  }
23
- export declare const loadQAMapReduceChain: (llm: BaseLanguageModel, params?: MapReduceQAChainParams) => MapReduceDocumentsChain;
23
+ export declare function loadQAMapReduceChain(llm: BaseLanguageModel, params?: MapReduceQAChainParams): MapReduceDocumentsChain;
24
24
  export interface RefineQAChainParams {
25
25
  questionPrompt?: BasePromptTemplate;
26
26
  refinePrompt?: BasePromptTemplate;
27
27
  verbose?: boolean;
28
28
  }
29
- export declare const loadQARefineChain: (llm: BaseLanguageModel, params?: RefineQAChainParams) => RefineDocumentsChain;
29
+ export declare function loadQARefineChain(llm: BaseLanguageModel, params?: RefineQAChainParams): RefineDocumentsChain;
@@ -1,78 +1,52 @@
1
1
  import { LLMChain } from "../llm_chain.js";
2
2
  import { StuffDocumentsChain, MapReduceDocumentsChain, RefineDocumentsChain, } from "../combine_docs_chain.js";
3
- import { QA_PROMPT_SELECTOR, DEFAULT_QA_PROMPT } from "./stuff_prompts.js";
4
- import { COMBINE_PROMPT, DEFAULT_COMBINE_QA_PROMPT, COMBINE_PROMPT_SELECTOR, COMBINE_QA_PROMPT_SELECTOR, } from "./map_reduce_prompts.js";
3
+ import { QA_PROMPT_SELECTOR } from "./stuff_prompts.js";
4
+ import { COMBINE_PROMPT_SELECTOR, COMBINE_QA_PROMPT_SELECTOR, } from "./map_reduce_prompts.js";
5
5
  import { QUESTION_PROMPT_SELECTOR, REFINE_PROMPT_SELECTOR, } from "./refine_prompts.js";
6
6
  export const loadQAChain = (llm, params = { type: "stuff" }) => {
7
- const { type, verbose } = params;
7
+ const { type } = params;
8
8
  if (type === "stuff") {
9
- const { prompt = DEFAULT_QA_PROMPT } = params;
10
- const llmChain = new LLMChain({ prompt, llm, verbose });
11
- const chain = new StuffDocumentsChain({ llmChain, verbose });
12
- return chain;
9
+ return loadQAStuffChain(llm, params);
13
10
  }
14
11
  if (type === "map_reduce") {
15
- const { combineMapPrompt = DEFAULT_COMBINE_QA_PROMPT, combinePrompt = COMBINE_PROMPT, returnIntermediateSteps, } = params;
16
- const llmChain = new LLMChain({ prompt: combineMapPrompt, llm, verbose });
17
- const combineLLMChain = new LLMChain({
18
- prompt: combinePrompt,
19
- llm,
20
- verbose,
21
- });
22
- const combineDocumentChain = new StuffDocumentsChain({
23
- llmChain: combineLLMChain,
24
- documentVariableName: "summaries",
25
- verbose,
26
- });
27
- const chain = new MapReduceDocumentsChain({
28
- llmChain,
29
- combineDocumentChain,
30
- returnIntermediateSteps,
31
- verbose,
32
- });
33
- return chain;
12
+ return loadQAMapReduceChain(llm, params);
34
13
  }
35
14
  if (type === "refine") {
36
- const { questionPrompt = QUESTION_PROMPT_SELECTOR.getPrompt(llm), refinePrompt = REFINE_PROMPT_SELECTOR.getPrompt(llm), } = params;
37
- const llmChain = new LLMChain({ prompt: questionPrompt, llm, verbose });
38
- const refineLLMChain = new LLMChain({ prompt: refinePrompt, llm, verbose });
39
- const chain = new RefineDocumentsChain({
40
- llmChain,
41
- refineLLMChain,
42
- verbose,
43
- });
44
- return chain;
15
+ return loadQARefineChain(llm, params);
45
16
  }
46
17
  throw new Error(`Invalid _type: ${type}`);
47
18
  };
48
- export const loadQAStuffChain = (llm, params = {}) => {
19
+ export function loadQAStuffChain(llm, params = {}) {
49
20
  const { prompt = QA_PROMPT_SELECTOR.getPrompt(llm), verbose } = params;
50
21
  const llmChain = new LLMChain({ prompt, llm, verbose });
51
- const chain = new StuffDocumentsChain({ llmChain });
22
+ const chain = new StuffDocumentsChain({ llmChain, verbose });
52
23
  return chain;
53
- };
54
- export const loadQAMapReduceChain = (llm, params = {}) => {
24
+ }
25
+ export function loadQAMapReduceChain(llm, params = {}) {
55
26
  const { combineMapPrompt = COMBINE_QA_PROMPT_SELECTOR.getPrompt(llm), combinePrompt = COMBINE_PROMPT_SELECTOR.getPrompt(llm), verbose, returnIntermediateSteps, } = params;
56
27
  const llmChain = new LLMChain({ prompt: combineMapPrompt, llm, verbose });
57
28
  const combineLLMChain = new LLMChain({ prompt: combinePrompt, llm, verbose });
58
29
  const combineDocumentChain = new StuffDocumentsChain({
59
30
  llmChain: combineLLMChain,
60
31
  documentVariableName: "summaries",
32
+ verbose,
61
33
  });
62
34
  const chain = new MapReduceDocumentsChain({
63
35
  llmChain,
64
36
  combineDocumentChain,
65
37
  returnIntermediateSteps,
38
+ verbose,
66
39
  });
67
40
  return chain;
68
- };
69
- export const loadQARefineChain = (llm, params = {}) => {
41
+ }
42
+ export function loadQARefineChain(llm, params = {}) {
70
43
  const { questionPrompt = QUESTION_PROMPT_SELECTOR.getPrompt(llm), refinePrompt = REFINE_PROMPT_SELECTOR.getPrompt(llm), verbose, } = params;
71
44
  const llmChain = new LLMChain({ prompt: questionPrompt, llm, verbose });
72
45
  const refineLLMChain = new LLMChain({ prompt: refinePrompt, llm, verbose });
73
46
  const chain = new RefineDocumentsChain({
74
47
  llmChain,
75
48
  refineLLMChain,
49
+ verbose,
76
50
  });
77
51
  return chain;
78
- };
52
+ }
@@ -6,6 +6,7 @@ const base_js_1 = require("../base.cjs");
6
6
  const llm_chain_js_1 = require("../llm_chain.cjs");
7
7
  const index_js_1 = require("../../base_language/index.cjs");
8
8
  const count_tokens_js_1 = require("../../base_language/count_tokens.cjs");
9
+ const sql_utils_js_1 = require("../../util/sql_utils.cjs");
9
10
  class SqlDatabaseChain extends base_js_1.BaseChain {
10
11
  constructor(fields) {
11
12
  super(fields);
@@ -61,6 +62,9 @@ class SqlDatabaseChain extends base_js_1.BaseChain {
61
62
  this.topK = fields.topK ?? this.topK;
62
63
  this.inputKey = fields.inputKey ?? this.inputKey;
63
64
  this.outputKey = fields.outputKey ?? this.outputKey;
65
+ this.prompt =
66
+ fields.prompt ??
67
+ (0, sql_utils_js_1.getPromptTemplateFromDataSource)(this.database.appDataSource);
64
68
  }
65
69
  /** @ignore */
66
70
  async _call(values, runManager) {
@@ -4,17 +4,19 @@ import { ChainValues } from "../../schema/index.js";
4
4
  import { SerializedSqlDatabaseChain } from "../serde.js";
5
5
  import { BaseLanguageModel } from "../../base_language/index.js";
6
6
  import { CallbackManagerForChainRun } from "../../callbacks/manager.js";
7
+ import { PromptTemplate } from "../../prompts/index.js";
7
8
  export interface SqlDatabaseChainInput extends ChainInputs {
8
9
  llm: BaseLanguageModel;
9
10
  database: SqlDatabase;
10
11
  topK?: number;
11
12
  inputKey?: string;
12
13
  outputKey?: string;
14
+ prompt?: PromptTemplate;
13
15
  }
14
16
  export declare class SqlDatabaseChain extends BaseChain {
15
17
  llm: BaseLanguageModel;
16
18
  database: SqlDatabase;
17
- prompt: import("../../index.js").PromptTemplate;
19
+ prompt: PromptTemplate;
18
20
  topK: number;
19
21
  inputKey: string;
20
22
  outputKey: string;
@@ -3,6 +3,7 @@ import { BaseChain } from "../base.js";
3
3
  import { LLMChain } from "../llm_chain.js";
4
4
  import { BaseLanguageModel } from "../../base_language/index.js";
5
5
  import { calculateMaxTokens, getModelContextSize, } from "../../base_language/count_tokens.js";
6
+ import { getPromptTemplateFromDataSource } from "../../util/sql_utils.js";
6
7
  export class SqlDatabaseChain extends BaseChain {
7
8
  constructor(fields) {
8
9
  super(fields);
@@ -58,6 +59,9 @@ export class SqlDatabaseChain extends BaseChain {
58
59
  this.topK = fields.topK ?? this.topK;
59
60
  this.inputKey = fields.inputKey ?? this.inputKey;
60
61
  this.outputKey = fields.outputKey ?? this.outputKey;
62
+ this.prompt =
63
+ fields.prompt ??
64
+ getPromptTemplateFromDataSource(this.database.appDataSource);
61
65
  }
62
66
  /** @ignore */
63
67
  async _call(values, runManager) {
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DEFAULT_SQL_DATABASE_PROMPT = void 0;
3
+ exports.SQL_MYSQL_PROMPT = exports.SQL_SQLITE_PROMPT = exports.SQL_POSTGRES_PROMPT = exports.DEFAULT_SQL_DATABASE_PROMPT = void 0;
4
4
  /* eslint-disable spaced-comment */
5
5
  const prompt_js_1 = require("../../prompts/prompt.cjs");
6
6
  exports.DEFAULT_SQL_DATABASE_PROMPT = new prompt_js_1.PromptTemplate({
@@ -24,3 +24,60 @@ Only use the tables listed below.
24
24
  Question: {input}`,
25
25
  inputVariables: ["dialect", "table_info", "input", "top_k"],
26
26
  });
27
+ exports.SQL_POSTGRES_PROMPT = new prompt_js_1.PromptTemplate({
28
+ template: `You are a PostgreSQL expert. Given an input question, first create a syntactically correct PostgreSQL query to run, then look at the results of the query and return the answer to the input question.
29
+ Unless the user specifies in the question a specific number of examples to obtain, query for at most {top_k} results using the LIMIT clause as per PostgreSQL. You can order the results to return the most informative data in the database.
30
+ Never query for all columns from a table. You must query only the columns that are needed to answer the question. Wrap each column name in double quotes (") to denote them as delimited identifiers.
31
+ Pay attention to use only the column names you can see in the tables below. Be careful to not query for columns that do not exist. Also, pay attention to which column is in which table.
32
+
33
+ Use the following format:
34
+
35
+ Question: "Question here"
36
+ SQLQuery: "SQL Query to run"
37
+ SQLResult: "Result of the SQLQuery"
38
+ Answer: "Final answer here"
39
+
40
+ Only use the following tables:
41
+ {table_info}
42
+
43
+ Question: {input}`,
44
+ inputVariables: ["dialect", "table_info", "input", "top_k"],
45
+ });
46
+ exports.SQL_SQLITE_PROMPT = new prompt_js_1.PromptTemplate({
47
+ template: `You are a SQLite expert. Given an input question, first create a syntactically correct SQLite query to run, then look at the results of the query and return the answer to the input question.
48
+ Unless the user specifies in the question a specific number of examples to obtain, query for at most {top_k} results using the LIMIT clause as per SQLite. You can order the results to return the most informative data in the database.
49
+ Never query for all columns from a table. You must query only the columns that are needed to answer the question. Wrap each column name in double quotes (") to denote them as delimited identifiers.
50
+ Pay attention to use only the column names you can see in the tables below. Be careful to not query for columns that do not exist. Also, pay attention to which column is in which table.
51
+
52
+ Use the following format:
53
+
54
+ Question: "Question here"
55
+ SQLQuery: "SQL Query to run"
56
+ SQLResult: "Result of the SQLQuery"
57
+ Answer: "Final answer here"
58
+
59
+ Only use the following tables:
60
+ {table_info}
61
+
62
+ Question: {input}`,
63
+ inputVariables: ["dialect", "table_info", "input", "top_k"],
64
+ });
65
+ exports.SQL_MYSQL_PROMPT = new prompt_js_1.PromptTemplate({
66
+ template: `You are a MySQL expert. Given an input question, first create a syntactically correct MySQL query to run, then look at the results of the query and return the answer to the input question.
67
+ Unless the user specifies in the question a specific number of examples to obtain, query for at most {top_k} results using the LIMIT clause as per MySQL. You can order the results to return the most informative data in the database.
68
+ Never query for all columns from a table. You must query only the columns that are needed to answer the question. Wrap each column name in backticks (\`) to denote them as delimited identifiers.
69
+ Pay attention to use only the column names you can see in the tables below. Be careful to not query for columns that do not exist. Also, pay attention to which column is in which table.
70
+
71
+ Use the following format:
72
+
73
+ Question: "Question here"
74
+ SQLQuery: "SQL Query to run"
75
+ SQLResult: "Result of the SQLQuery"
76
+ Answer: "Final answer here"
77
+
78
+ Only use the following tables:
79
+ {table_info}
80
+
81
+ Question: {input}`,
82
+ inputVariables: ["dialect", "table_info", "input", "top_k"],
83
+ });
@@ -1,2 +1,5 @@
1
1
  import { PromptTemplate } from "../../prompts/prompt.js";
2
2
  export declare const DEFAULT_SQL_DATABASE_PROMPT: PromptTemplate;
3
+ export declare const SQL_POSTGRES_PROMPT: PromptTemplate;
4
+ export declare const SQL_SQLITE_PROMPT: PromptTemplate;
5
+ export declare const SQL_MYSQL_PROMPT: PromptTemplate;
@@ -21,3 +21,60 @@ Only use the tables listed below.
21
21
  Question: {input}`,
22
22
  inputVariables: ["dialect", "table_info", "input", "top_k"],
23
23
  });
24
+ export const SQL_POSTGRES_PROMPT = /*#__PURE__*/ new PromptTemplate({
25
+ template: `You are a PostgreSQL expert. Given an input question, first create a syntactically correct PostgreSQL query to run, then look at the results of the query and return the answer to the input question.
26
+ Unless the user specifies in the question a specific number of examples to obtain, query for at most {top_k} results using the LIMIT clause as per PostgreSQL. You can order the results to return the most informative data in the database.
27
+ Never query for all columns from a table. You must query only the columns that are needed to answer the question. Wrap each column name in double quotes (") to denote them as delimited identifiers.
28
+ Pay attention to use only the column names you can see in the tables below. Be careful to not query for columns that do not exist. Also, pay attention to which column is in which table.
29
+
30
+ Use the following format:
31
+
32
+ Question: "Question here"
33
+ SQLQuery: "SQL Query to run"
34
+ SQLResult: "Result of the SQLQuery"
35
+ Answer: "Final answer here"
36
+
37
+ Only use the following tables:
38
+ {table_info}
39
+
40
+ Question: {input}`,
41
+ inputVariables: ["dialect", "table_info", "input", "top_k"],
42
+ });
43
+ export const SQL_SQLITE_PROMPT = /*#__PURE__*/ new PromptTemplate({
44
+ template: `You are a SQLite expert. Given an input question, first create a syntactically correct SQLite query to run, then look at the results of the query and return the answer to the input question.
45
+ Unless the user specifies in the question a specific number of examples to obtain, query for at most {top_k} results using the LIMIT clause as per SQLite. You can order the results to return the most informative data in the database.
46
+ Never query for all columns from a table. You must query only the columns that are needed to answer the question. Wrap each column name in double quotes (") to denote them as delimited identifiers.
47
+ Pay attention to use only the column names you can see in the tables below. Be careful to not query for columns that do not exist. Also, pay attention to which column is in which table.
48
+
49
+ Use the following format:
50
+
51
+ Question: "Question here"
52
+ SQLQuery: "SQL Query to run"
53
+ SQLResult: "Result of the SQLQuery"
54
+ Answer: "Final answer here"
55
+
56
+ Only use the following tables:
57
+ {table_info}
58
+
59
+ Question: {input}`,
60
+ inputVariables: ["dialect", "table_info", "input", "top_k"],
61
+ });
62
+ export const SQL_MYSQL_PROMPT = /*#__PURE__*/ new PromptTemplate({
63
+ template: `You are a MySQL expert. Given an input question, first create a syntactically correct MySQL query to run, then look at the results of the query and return the answer to the input question.
64
+ Unless the user specifies in the question a specific number of examples to obtain, query for at most {top_k} results using the LIMIT clause as per MySQL. You can order the results to return the most informative data in the database.
65
+ Never query for all columns from a table. You must query only the columns that are needed to answer the question. Wrap each column name in backticks (\`) to denote them as delimited identifiers.
66
+ Pay attention to use only the column names you can see in the tables below. Be careful to not query for columns that do not exist. Also, pay attention to which column is in which table.
67
+
68
+ Use the following format:
69
+
70
+ Question: "Question here"
71
+ SQLQuery: "SQL Query to run"
72
+ SQLResult: "Result of the SQLQuery"
73
+ Answer: "Final answer here"
74
+
75
+ Only use the following tables:
76
+ {table_info}
77
+
78
+ Question: {input}`,
79
+ inputVariables: ["dialect", "table_info", "input", "top_k"],
80
+ });
@@ -39,6 +39,12 @@ function openAIResponseToChatMessage(role, text) {
39
39
  * To use you should have the `openai` package installed, with the
40
40
  * `OPENAI_API_KEY` environment variable set.
41
41
  *
42
+ * To use with Azure you should have the `openai` package installed, with the
43
+ * `AZURE_OPENAI_API_KEY`,
44
+ * `AZURE_OPENAI_API_INSTANCE_NAME`,
45
+ * `AZURE_OPENAI_API_DEPLOYMENT_NAME`
46
+ * and `AZURE_OPENAI_API_VERSION` environment variable set.
47
+ *
42
48
  * @remarks
43
49
  * Any parameters that are valid to be passed to {@link
44
50
  * https://platform.openai.com/docs/api-reference/chat/create |
@@ -120,6 +126,30 @@ class ChatOpenAI extends base_js_1.BaseChatModel {
120
126
  writable: true,
121
127
  value: void 0
122
128
  });
129
+ Object.defineProperty(this, "azureOpenAIApiVersion", {
130
+ enumerable: true,
131
+ configurable: true,
132
+ writable: true,
133
+ value: void 0
134
+ });
135
+ Object.defineProperty(this, "azureOpenAIApiKey", {
136
+ enumerable: true,
137
+ configurable: true,
138
+ writable: true,
139
+ value: void 0
140
+ });
141
+ Object.defineProperty(this, "azureOpenAIApiInstanceName", {
142
+ enumerable: true,
143
+ configurable: true,
144
+ writable: true,
145
+ value: void 0
146
+ });
147
+ Object.defineProperty(this, "azureOpenAIApiDeploymentName", {
148
+ enumerable: true,
149
+ configurable: true,
150
+ writable: true,
151
+ value: void 0
152
+ });
123
153
  Object.defineProperty(this, "client", {
124
154
  enumerable: true,
125
155
  configurable: true,
@@ -137,9 +167,29 @@ class ChatOpenAI extends base_js_1.BaseChatModel {
137
167
  ? // eslint-disable-next-line no-process-env
138
168
  process.env?.OPENAI_API_KEY
139
169
  : undefined);
140
- if (!apiKey) {
141
- throw new Error("OpenAI API key not found");
170
+ const azureApiKey = fields?.azureOpenAIApiKey ??
171
+ (typeof process !== "undefined"
172
+ ? // eslint-disable-next-line no-process-env
173
+ process.env?.AZURE_OPENAI_API_KEY
174
+ : undefined);
175
+ if (!azureApiKey && !apiKey) {
176
+ throw new Error("(Azure) OpenAI API key not found");
142
177
  }
178
+ const azureApiInstanceName = fields?.azureOpenAIApiInstanceName ??
179
+ (typeof process !== "undefined"
180
+ ? // eslint-disable-next-line no-process-env
181
+ process.env?.AZURE_OPENAI_API_INSTANCE_NAME
182
+ : undefined);
183
+ const azureApiDeploymentName = fields?.azureOpenAIApiDeploymentName ??
184
+ (typeof process !== "undefined"
185
+ ? // eslint-disable-next-line no-process-env
186
+ process.env?.AZURE_OPENAI_API_DEPLOYMENT_NAME
187
+ : undefined);
188
+ const azureApiVersion = fields?.azureOpenAIApiVersion ??
189
+ (typeof process !== "undefined"
190
+ ? // eslint-disable-next-line no-process-env
191
+ process.env?.AZURE_OPENAI_API_VERSION
192
+ : undefined);
143
193
  this.modelName = fields?.modelName ?? this.modelName;
144
194
  this.modelKwargs = fields?.modelKwargs ?? {};
145
195
  this.timeout = fields?.timeout;
@@ -152,9 +202,24 @@ class ChatOpenAI extends base_js_1.BaseChatModel {
152
202
  this.logitBias = fields?.logitBias;
153
203
  this.stop = fields?.stop;
154
204
  this.streaming = fields?.streaming ?? false;
205
+ this.azureOpenAIApiVersion = azureApiVersion;
206
+ this.azureOpenAIApiKey = azureApiKey;
207
+ this.azureOpenAIApiInstanceName = azureApiInstanceName;
208
+ this.azureOpenAIApiDeploymentName = azureApiDeploymentName;
155
209
  if (this.streaming && this.n > 1) {
156
210
  throw new Error("Cannot stream results when n > 1");
157
211
  }
212
+ if (this.azureOpenAIApiKey) {
213
+ if (!this.azureOpenAIApiInstanceName) {
214
+ throw new Error("Azure OpenAI API instance name not found");
215
+ }
216
+ if (!this.azureOpenAIApiDeploymentName) {
217
+ throw new Error("Azure OpenAI API deployment name not found");
218
+ }
219
+ if (!this.azureOpenAIApiVersion) {
220
+ throw new Error("Azure OpenAI API version not found");
221
+ }
222
+ }
158
223
  this.clientConfig = {
159
224
  apiKey,
160
225
  ...configuration,
@@ -321,8 +386,12 @@ class ChatOpenAI extends base_js_1.BaseChatModel {
321
386
  /** @ignore */
322
387
  async completionWithRetry(request, options) {
323
388
  if (!this.client) {
389
+ const endpoint = this.azureOpenAIApiKey
390
+ ? `https://${this.azureOpenAIApiInstanceName}.openai.azure.com/openai/deployments/${this.azureOpenAIApiDeploymentName}`
391
+ : this.clientConfig.basePath;
324
392
  const clientConfig = new openai_1.Configuration({
325
393
  ...this.clientConfig,
394
+ basePath: endpoint,
326
395
  baseOptions: {
327
396
  timeout: this.timeout,
328
397
  adapter: axios_fetch_adapter_js_1.default,
@@ -331,8 +400,19 @@ class ChatOpenAI extends base_js_1.BaseChatModel {
331
400
  });
332
401
  this.client = new openai_1.OpenAIApi(clientConfig);
333
402
  }
403
+ const axiosOptions = (options ?? {});
404
+ if (this.azureOpenAIApiKey) {
405
+ axiosOptions.headers = {
406
+ "api-key": this.azureOpenAIApiKey,
407
+ ...axiosOptions.headers,
408
+ };
409
+ axiosOptions.params = {
410
+ "api-version": this.azureOpenAIApiVersion,
411
+ ...axiosOptions.params,
412
+ };
413
+ }
334
414
  return this.caller
335
- .call(this.client.createChatCompletion.bind(this.client), request, options)
415
+ .call(this.client.createChatCompletion.bind(this.client), request, axiosOptions)
336
416
  .then((res) => res.data);
337
417
  }
338
418
  _llmType() {