langchain 0.0.155 → 0.0.157
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.
- package/chains/graph_qa/cypher.cjs +1 -0
- package/chains/graph_qa/cypher.d.ts +1 -0
- package/chains/graph_qa/cypher.js +1 -0
- package/chat_models/bedrock.cjs +1 -0
- package/chat_models/bedrock.d.ts +1 -0
- package/chat_models/bedrock.js +1 -0
- package/dist/agents/index.d.ts +12 -12
- package/dist/agents/toolkits/index.d.ts +2 -2
- package/dist/cache/upstash_redis.cjs +1 -1
- package/dist/cache/upstash_redis.js +1 -1
- package/dist/callbacks/index.d.ts +3 -3
- package/dist/chains/graph_qa/cypher.cjs +151 -0
- package/dist/chains/graph_qa/cypher.d.ts +45 -0
- package/dist/chains/graph_qa/cypher.js +147 -0
- package/dist/chains/graph_qa/prompts.cjs +34 -0
- package/dist/chains/graph_qa/prompts.d.ts +9 -0
- package/dist/chains/graph_qa/prompts.js +31 -0
- package/dist/chains/index.d.ts +19 -19
- package/dist/chains/index.js +2 -2
- package/dist/chains/openai_functions/index.d.ts +3 -3
- package/dist/chains/query_constructor/index.d.ts +1 -1
- package/dist/chat_models/bedrock.cjs +260 -0
- package/dist/chat_models/bedrock.d.ts +58 -0
- package/dist/chat_models/bedrock.js +254 -0
- package/dist/chat_models/googlevertexai/index.cjs +1 -3
- package/dist/chat_models/googlevertexai/index.d.ts +1 -1
- package/dist/chat_models/googlevertexai/index.js +0 -1
- package/dist/chat_models/googlevertexai/web.cjs +1 -3
- package/dist/chat_models/googlevertexai/web.d.ts +1 -1
- package/dist/chat_models/googlevertexai/web.js +0 -1
- package/dist/chat_models/openai.d.ts +1 -1
- package/dist/embeddings/cloudflare_workersai.cjs +69 -0
- package/dist/embeddings/cloudflare_workersai.d.ts +28 -0
- package/dist/embeddings/cloudflare_workersai.js +65 -0
- package/dist/experimental/autogpt/index.d.ts +3 -3
- package/dist/experimental/babyagi/index.d.ts +1 -1
- package/dist/experimental/plan_and_execute/index.d.ts +1 -1
- package/dist/graphs/neo4j_graph.cjs +112 -0
- package/dist/graphs/neo4j_graph.d.ts +18 -0
- package/dist/graphs/neo4j_graph.js +105 -0
- package/dist/llms/bedrock.cjs +57 -67
- package/dist/llms/bedrock.d.ts +8 -35
- package/dist/llms/bedrock.js +57 -67
- package/dist/llms/openai-chat.d.ts +1 -1
- package/dist/llms/openai.d.ts +1 -1
- package/dist/load/import_constants.cjs +5 -0
- package/dist/load/import_constants.js +5 -0
- package/dist/memory/index.d.ts +8 -8
- package/dist/memory/index.js +1 -1
- package/dist/output_parsers/index.d.ts +3 -3
- package/dist/prompts/index.d.ts +8 -8
- package/dist/retrievers/remote/index.d.ts +3 -3
- package/dist/schema/runnable/index.d.ts +3 -3
- package/dist/sql_db.d.ts +1 -1
- package/dist/tools/index.d.ts +12 -12
- package/dist/util/bedrock.cjs +54 -0
- package/dist/util/bedrock.d.ts +59 -0
- package/dist/util/bedrock.js +50 -0
- package/dist/vectorstores/cloudflare_vectorize.cjs +200 -0
- package/dist/vectorstores/cloudflare_vectorize.d.ts +90 -0
- package/dist/vectorstores/cloudflare_vectorize.js +173 -0
- package/dist/vectorstores/supabase.d.ts +1 -1
- package/embeddings/cloudflare_workersai.cjs +1 -0
- package/embeddings/cloudflare_workersai.d.ts +1 -0
- package/embeddings/cloudflare_workersai.js +1 -0
- package/graphs/neo4j_graph.cjs +1 -0
- package/graphs/neo4j_graph.d.ts +1 -0
- package/graphs/neo4j_graph.js +1 -0
- package/package.json +62 -14
- package/vectorstores/cloudflare_vectorize.cjs +1 -0
- package/vectorstores/cloudflare_vectorize.d.ts +1 -0
- package/vectorstores/cloudflare_vectorize.js +1 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('../../dist/chains/graph_qa/cypher.cjs');
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../../dist/chains/graph_qa/cypher.js'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../../dist/chains/graph_qa/cypher.js'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('../dist/chat_models/bedrock.cjs');
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../dist/chat_models/bedrock.js'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../dist/chat_models/bedrock.js'
|
package/dist/agents/index.d.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
export { Agent, AgentArgs, BaseSingleActionAgent, LLMSingleActionAgent, LLMSingleActionAgentInput, OutputParserArgs, } from "./agent.js";
|
|
2
|
-
export { JsonToolkit, OpenApiToolkit, RequestsToolkit, VectorStoreInfo, VectorStoreRouterToolkit, VectorStoreToolkit, ZapierToolKit, createJsonAgent, createOpenApiAgent, createVectorStoreAgent, createVectorStoreRouterAgent, } from "./toolkits/index.js";
|
|
1
|
+
export { Agent, type AgentArgs, BaseSingleActionAgent, LLMSingleActionAgent, type LLMSingleActionAgentInput, type OutputParserArgs, } from "./agent.js";
|
|
2
|
+
export { JsonToolkit, OpenApiToolkit, RequestsToolkit, type VectorStoreInfo, VectorStoreRouterToolkit, VectorStoreToolkit, ZapierToolKit, createJsonAgent, createOpenApiAgent, createVectorStoreAgent, createVectorStoreRouterAgent, } from "./toolkits/index.js";
|
|
3
3
|
export { Toolkit } from "./toolkits/base.js";
|
|
4
|
-
export { ChatAgent, ChatAgentInput, ChatCreatePromptArgs, } from "./chat/index.js";
|
|
4
|
+
export { ChatAgent, type ChatAgentInput, type ChatCreatePromptArgs, } from "./chat/index.js";
|
|
5
5
|
export { ChatAgentOutputParser } from "./chat/outputParser.js";
|
|
6
|
-
export { ChatConversationalAgent, ChatConversationalAgentInput, ChatConversationalCreatePromptArgs, } from "./chat_convo/index.js";
|
|
7
|
-
export { ChatConversationalAgentOutputParser, ChatConversationalAgentOutputParserArgs, ChatConversationalAgentOutputParserWithRetries, ChatConversationalAgentOutputParserFormatInstructionsOptions, } from "./chat_convo/outputParser.js";
|
|
8
|
-
export { AgentExecutor, AgentExecutorInput } from "./executor.js";
|
|
9
|
-
export { initializeAgentExecutor, initializeAgentExecutorWithOptions, InitializeAgentExecutorOptions, } from "./initialize.js";
|
|
10
|
-
export { ZeroShotAgent, ZeroShotAgentInput, ZeroShotCreatePromptArgs, } from "./mrkl/index.js";
|
|
6
|
+
export { ChatConversationalAgent, type ChatConversationalAgentInput, type ChatConversationalCreatePromptArgs, } from "./chat_convo/index.js";
|
|
7
|
+
export { ChatConversationalAgentOutputParser, type ChatConversationalAgentOutputParserArgs, ChatConversationalAgentOutputParserWithRetries, type ChatConversationalAgentOutputParserFormatInstructionsOptions, } from "./chat_convo/outputParser.js";
|
|
8
|
+
export { AgentExecutor, type AgentExecutorInput } from "./executor.js";
|
|
9
|
+
export { initializeAgentExecutor, initializeAgentExecutorWithOptions, type InitializeAgentExecutorOptions, } from "./initialize.js";
|
|
10
|
+
export { ZeroShotAgent, type ZeroShotAgentInput, type ZeroShotCreatePromptArgs, } from "./mrkl/index.js";
|
|
11
11
|
export { ZeroShotAgentOutputParser } from "./mrkl/outputParser.js";
|
|
12
|
-
export { AgentActionOutputParser, AgentInput, SerializedAgent, SerializedAgentT, SerializedZeroShotAgent, StoppingMethod, } from "./types.js";
|
|
13
|
-
export { StructuredChatAgent, StructuredChatAgentInput, StructuredChatCreatePromptArgs, } from "./structured_chat/index.js";
|
|
14
|
-
export { StructuredChatOutputParser, StructuredChatOutputParserArgs, StructuredChatOutputParserWithRetries, } from "./structured_chat/outputParser.js";
|
|
15
|
-
export { OpenAIAgent, OpenAIAgentInput, OpenAIAgentCreatePromptArgs, } from "./openai/index.js";
|
|
12
|
+
export { AgentActionOutputParser, type AgentInput, type SerializedAgent, type SerializedAgentT, type SerializedZeroShotAgent, type StoppingMethod, } from "./types.js";
|
|
13
|
+
export { StructuredChatAgent, type StructuredChatAgentInput, type StructuredChatCreatePromptArgs, } from "./structured_chat/index.js";
|
|
14
|
+
export { StructuredChatOutputParser, type StructuredChatOutputParserArgs, StructuredChatOutputParserWithRetries, } from "./structured_chat/outputParser.js";
|
|
15
|
+
export { OpenAIAgent, type OpenAIAgentInput, type OpenAIAgentCreatePromptArgs, } from "./openai/index.js";
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { JsonToolkit, createJsonAgent } from "./json/json.js";
|
|
2
2
|
export { RequestsToolkit, OpenApiToolkit, createOpenApiAgent, } from "./openapi/openapi.js";
|
|
3
|
-
export { VectorStoreInfo, VectorStoreToolkit, VectorStoreRouterToolkit, createVectorStoreAgent, createVectorStoreRouterAgent, } from "./vectorstore/vectorstore.js";
|
|
3
|
+
export { type VectorStoreInfo, VectorStoreToolkit, VectorStoreRouterToolkit, createVectorStoreAgent, createVectorStoreRouterAgent, } from "./vectorstore/vectorstore.js";
|
|
4
4
|
export { ZapierToolKit } from "./zapier/zapier.js";
|
|
5
5
|
export { createRetrieverTool } from "./conversational_retrieval/tool.js";
|
|
6
|
-
export { createConversationalRetrievalAgent, ConversationalRetrievalAgentOptions, } from "./conversational_retrieval/openai_functions.js";
|
|
6
|
+
export { createConversationalRetrievalAgent, type ConversationalRetrievalAgentOptions, } from "./conversational_retrieval/openai_functions.js";
|
|
7
7
|
export { OpenAIAgentTokenBufferMemory } from "./conversational_retrieval/token_buffer_memory.js";
|
|
@@ -37,7 +37,7 @@ class UpstashRedisCache extends index_js_1.BaseCache {
|
|
|
37
37
|
let value = await this.redisClient.get(key);
|
|
38
38
|
const generations = [];
|
|
39
39
|
while (value) {
|
|
40
|
-
generations.push((0, base_js_1.deserializeStoredGeneration)(
|
|
40
|
+
generations.push((0, base_js_1.deserializeStoredGeneration)(value));
|
|
41
41
|
idx += 1;
|
|
42
42
|
key = (0, base_js_1.getCacheKey)(prompt, llmKey, String(idx));
|
|
43
43
|
value = await this.redisClient.get(key);
|
|
@@ -34,7 +34,7 @@ export class UpstashRedisCache extends BaseCache {
|
|
|
34
34
|
let value = await this.redisClient.get(key);
|
|
35
35
|
const generations = [];
|
|
36
36
|
while (value) {
|
|
37
|
-
generations.push(deserializeStoredGeneration(
|
|
37
|
+
generations.push(deserializeStoredGeneration(value));
|
|
38
38
|
idx += 1;
|
|
39
39
|
key = getCacheKey(prompt, llmKey, String(idx));
|
|
40
40
|
value = await this.redisClient.get(key);
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
export { BaseCallbackHandler, CallbackHandlerMethods, BaseCallbackHandlerInput, NewTokenIndices, } from "./base.js";
|
|
2
|
-
export { Run, RunType, BaseTracer } from "./handlers/tracer.js";
|
|
1
|
+
export { BaseCallbackHandler, type CallbackHandlerMethods, type BaseCallbackHandlerInput, type NewTokenIndices, } from "./base.js";
|
|
2
|
+
export { type Run, type RunType, BaseTracer } from "./handlers/tracer.js";
|
|
3
3
|
export { ConsoleCallbackHandler } from "./handlers/console.js";
|
|
4
4
|
export { RunCollectorCallbackHandler } from "./handlers/run_collector.js";
|
|
5
5
|
export { LangChainTracer } from "./handlers/tracer_langchain.js";
|
|
6
6
|
export { LangChainTracerV1 } from "./handlers/tracer_langchain_v1.js";
|
|
7
7
|
export { getTracingCallbackHandler, getTracingV2CallbackHandler, } from "./handlers/initialize.js";
|
|
8
|
-
export { CallbackManager, CallbackManagerForRetrieverRun, CallbackManagerForChainRun, CallbackManagerForLLMRun, CallbackManagerForToolRun, CallbackManagerOptions, Callbacks, TraceGroup, traceAsGroup, } from "./manager.js";
|
|
8
|
+
export { CallbackManager, CallbackManagerForRetrieverRun, CallbackManagerForChainRun, CallbackManagerForLLMRun, CallbackManagerForToolRun, type CallbackManagerOptions, type Callbacks, TraceGroup, traceAsGroup, } from "./manager.js";
|
|
9
9
|
export { awaitAllCallbacks, consumeCallback } from "./promises.js";
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GraphCypherQAChain = exports.INTERMEDIATE_STEPS_KEY = void 0;
|
|
4
|
+
const llm_chain_js_1 = require("../../chains/llm_chain.cjs");
|
|
5
|
+
const base_js_1 = require("../base.cjs");
|
|
6
|
+
const prompts_js_1 = require("./prompts.cjs");
|
|
7
|
+
exports.INTERMEDIATE_STEPS_KEY = "intermediateSteps";
|
|
8
|
+
class GraphCypherQAChain extends base_js_1.BaseChain {
|
|
9
|
+
constructor(props) {
|
|
10
|
+
super(props);
|
|
11
|
+
Object.defineProperty(this, "graph", {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
configurable: true,
|
|
14
|
+
writable: true,
|
|
15
|
+
value: void 0
|
|
16
|
+
});
|
|
17
|
+
Object.defineProperty(this, "cypherGenerationChain", {
|
|
18
|
+
enumerable: true,
|
|
19
|
+
configurable: true,
|
|
20
|
+
writable: true,
|
|
21
|
+
value: void 0
|
|
22
|
+
});
|
|
23
|
+
Object.defineProperty(this, "qaChain", {
|
|
24
|
+
enumerable: true,
|
|
25
|
+
configurable: true,
|
|
26
|
+
writable: true,
|
|
27
|
+
value: void 0
|
|
28
|
+
});
|
|
29
|
+
Object.defineProperty(this, "inputKey", {
|
|
30
|
+
enumerable: true,
|
|
31
|
+
configurable: true,
|
|
32
|
+
writable: true,
|
|
33
|
+
value: "query"
|
|
34
|
+
});
|
|
35
|
+
Object.defineProperty(this, "outputKey", {
|
|
36
|
+
enumerable: true,
|
|
37
|
+
configurable: true,
|
|
38
|
+
writable: true,
|
|
39
|
+
value: "result"
|
|
40
|
+
});
|
|
41
|
+
Object.defineProperty(this, "topK", {
|
|
42
|
+
enumerable: true,
|
|
43
|
+
configurable: true,
|
|
44
|
+
writable: true,
|
|
45
|
+
value: 10
|
|
46
|
+
});
|
|
47
|
+
Object.defineProperty(this, "returnDirect", {
|
|
48
|
+
enumerable: true,
|
|
49
|
+
configurable: true,
|
|
50
|
+
writable: true,
|
|
51
|
+
value: false
|
|
52
|
+
});
|
|
53
|
+
Object.defineProperty(this, "returnIntermediateSteps", {
|
|
54
|
+
enumerable: true,
|
|
55
|
+
configurable: true,
|
|
56
|
+
writable: true,
|
|
57
|
+
value: false
|
|
58
|
+
});
|
|
59
|
+
const { graph, cypherGenerationChain, qaChain, inputKey, outputKey, topK, returnIntermediateSteps, returnDirect, } = props;
|
|
60
|
+
this.graph = graph;
|
|
61
|
+
this.cypherGenerationChain = cypherGenerationChain;
|
|
62
|
+
this.qaChain = qaChain;
|
|
63
|
+
if (inputKey) {
|
|
64
|
+
this.inputKey = inputKey;
|
|
65
|
+
}
|
|
66
|
+
if (outputKey) {
|
|
67
|
+
this.outputKey = outputKey;
|
|
68
|
+
}
|
|
69
|
+
if (topK) {
|
|
70
|
+
this.topK = topK;
|
|
71
|
+
}
|
|
72
|
+
if (returnIntermediateSteps) {
|
|
73
|
+
this.returnIntermediateSteps = returnIntermediateSteps;
|
|
74
|
+
}
|
|
75
|
+
if (returnDirect) {
|
|
76
|
+
this.returnDirect = returnDirect;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
_chainType() {
|
|
80
|
+
return "graph_cypher_chain";
|
|
81
|
+
}
|
|
82
|
+
get inputKeys() {
|
|
83
|
+
return [this.inputKey];
|
|
84
|
+
}
|
|
85
|
+
get outputKeys() {
|
|
86
|
+
return [this.outputKey];
|
|
87
|
+
}
|
|
88
|
+
static fromLLM(props) {
|
|
89
|
+
const { graph, qaPrompt = prompts_js_1.CYPHER_QA_PROMPT, cypherPrompt = prompts_js_1.CYPHER_GENERATION_PROMPT, llm, cypherLLM, qaLLM, returnIntermediateSteps = false, returnDirect = false, } = props;
|
|
90
|
+
if (!cypherLLM && !llm) {
|
|
91
|
+
throw new Error("Either 'llm' or 'cypherLLM' parameters must be provided");
|
|
92
|
+
}
|
|
93
|
+
if (!qaLLM && !llm) {
|
|
94
|
+
throw new Error("Either 'llm' or 'qaLLM' parameters must be provided");
|
|
95
|
+
}
|
|
96
|
+
if (cypherLLM && qaLLM && llm) {
|
|
97
|
+
throw new Error("You can specify up to two of 'cypherLLM', 'qaLLM', and 'llm', but not all three simultaneously.");
|
|
98
|
+
}
|
|
99
|
+
const qaChain = new llm_chain_js_1.LLMChain({
|
|
100
|
+
llm: (qaLLM || llm),
|
|
101
|
+
prompt: qaPrompt,
|
|
102
|
+
});
|
|
103
|
+
const cypherGenerationChain = new llm_chain_js_1.LLMChain({
|
|
104
|
+
llm: (cypherLLM || llm),
|
|
105
|
+
prompt: cypherPrompt,
|
|
106
|
+
});
|
|
107
|
+
return new GraphCypherQAChain({
|
|
108
|
+
cypherGenerationChain,
|
|
109
|
+
qaChain,
|
|
110
|
+
graph,
|
|
111
|
+
returnIntermediateSteps,
|
|
112
|
+
returnDirect,
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
extractCypher(text) {
|
|
116
|
+
const pattern = /```(.*?)```/s;
|
|
117
|
+
const matches = text.match(pattern);
|
|
118
|
+
return matches ? matches[1] : text;
|
|
119
|
+
}
|
|
120
|
+
async _call(values, runManager) {
|
|
121
|
+
const callbacks = runManager?.getChild();
|
|
122
|
+
const question = values[this.inputKey];
|
|
123
|
+
const intermediateSteps = [];
|
|
124
|
+
const generatedCypher = await this.cypherGenerationChain.call({ question, schema: this.graph.getSchema() }, callbacks);
|
|
125
|
+
const extractedCypher = this.extractCypher(generatedCypher.text);
|
|
126
|
+
await runManager?.handleText(`Generated Cypher:\n`);
|
|
127
|
+
await runManager?.handleText(`${extractedCypher} green\n`);
|
|
128
|
+
intermediateSteps.push({ query: extractedCypher });
|
|
129
|
+
let chainResult;
|
|
130
|
+
const context = await this.graph.query(extractedCypher, {
|
|
131
|
+
topK: this.topK,
|
|
132
|
+
});
|
|
133
|
+
if (this.returnDirect) {
|
|
134
|
+
chainResult = { [this.outputKey]: context };
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
await runManager?.handleText("Full Context:\n");
|
|
138
|
+
await runManager?.handleText(`${context} green\n`);
|
|
139
|
+
intermediateSteps.push({ context });
|
|
140
|
+
const result = await this.qaChain.call({ question, context: JSON.stringify(context) }, callbacks);
|
|
141
|
+
chainResult = {
|
|
142
|
+
[this.outputKey]: result[this.qaChain.outputKey],
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
if (this.returnIntermediateSteps) {
|
|
146
|
+
chainResult[exports.INTERMEDIATE_STEPS_KEY] = intermediateSteps;
|
|
147
|
+
}
|
|
148
|
+
return chainResult;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
exports.GraphCypherQAChain = GraphCypherQAChain;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { LLMChain } from "../../chains/llm_chain.js";
|
|
2
|
+
import { ChainValues } from "../../schema/index.js";
|
|
3
|
+
import { BasePromptTemplate } from "../../prompts/base.js";
|
|
4
|
+
import { BaseChain, ChainInputs } from "../base.js";
|
|
5
|
+
import { BaseLanguageModel } from "../../base_language/index.js";
|
|
6
|
+
import { CallbackManagerForChainRun } from "../../callbacks/manager.js";
|
|
7
|
+
import { Neo4jGraph } from "../../graphs/neo4j_graph.js";
|
|
8
|
+
export declare const INTERMEDIATE_STEPS_KEY = "intermediateSteps";
|
|
9
|
+
export interface GraphCypherQAChainInput extends ChainInputs {
|
|
10
|
+
graph: Neo4jGraph;
|
|
11
|
+
cypherGenerationChain: LLMChain;
|
|
12
|
+
qaChain: LLMChain;
|
|
13
|
+
inputKey?: string;
|
|
14
|
+
outputKey?: string;
|
|
15
|
+
topK?: number;
|
|
16
|
+
returnIntermediateSteps?: boolean;
|
|
17
|
+
returnDirect?: boolean;
|
|
18
|
+
}
|
|
19
|
+
export interface FromLLMInput {
|
|
20
|
+
graph: Neo4jGraph;
|
|
21
|
+
llm?: BaseLanguageModel;
|
|
22
|
+
cypherLLM?: BaseLanguageModel;
|
|
23
|
+
qaLLM?: BaseLanguageModel;
|
|
24
|
+
qaPrompt?: BasePromptTemplate;
|
|
25
|
+
cypherPrompt?: BasePromptTemplate;
|
|
26
|
+
returnIntermediateSteps?: boolean;
|
|
27
|
+
returnDirect?: boolean;
|
|
28
|
+
}
|
|
29
|
+
export declare class GraphCypherQAChain extends BaseChain {
|
|
30
|
+
private graph;
|
|
31
|
+
private cypherGenerationChain;
|
|
32
|
+
private qaChain;
|
|
33
|
+
private inputKey;
|
|
34
|
+
private outputKey;
|
|
35
|
+
private topK;
|
|
36
|
+
private returnDirect;
|
|
37
|
+
private returnIntermediateSteps;
|
|
38
|
+
constructor(props: GraphCypherQAChainInput);
|
|
39
|
+
_chainType(): "graph_cypher_chain";
|
|
40
|
+
get inputKeys(): string[];
|
|
41
|
+
get outputKeys(): string[];
|
|
42
|
+
static fromLLM(props: FromLLMInput): GraphCypherQAChain;
|
|
43
|
+
private extractCypher;
|
|
44
|
+
_call(values: ChainValues, runManager?: CallbackManagerForChainRun): Promise<ChainValues>;
|
|
45
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { LLMChain } from "../../chains/llm_chain.js";
|
|
2
|
+
import { BaseChain } from "../base.js";
|
|
3
|
+
import { CYPHER_GENERATION_PROMPT, CYPHER_QA_PROMPT } from "./prompts.js";
|
|
4
|
+
export const INTERMEDIATE_STEPS_KEY = "intermediateSteps";
|
|
5
|
+
export class GraphCypherQAChain extends BaseChain {
|
|
6
|
+
constructor(props) {
|
|
7
|
+
super(props);
|
|
8
|
+
Object.defineProperty(this, "graph", {
|
|
9
|
+
enumerable: true,
|
|
10
|
+
configurable: true,
|
|
11
|
+
writable: true,
|
|
12
|
+
value: void 0
|
|
13
|
+
});
|
|
14
|
+
Object.defineProperty(this, "cypherGenerationChain", {
|
|
15
|
+
enumerable: true,
|
|
16
|
+
configurable: true,
|
|
17
|
+
writable: true,
|
|
18
|
+
value: void 0
|
|
19
|
+
});
|
|
20
|
+
Object.defineProperty(this, "qaChain", {
|
|
21
|
+
enumerable: true,
|
|
22
|
+
configurable: true,
|
|
23
|
+
writable: true,
|
|
24
|
+
value: void 0
|
|
25
|
+
});
|
|
26
|
+
Object.defineProperty(this, "inputKey", {
|
|
27
|
+
enumerable: true,
|
|
28
|
+
configurable: true,
|
|
29
|
+
writable: true,
|
|
30
|
+
value: "query"
|
|
31
|
+
});
|
|
32
|
+
Object.defineProperty(this, "outputKey", {
|
|
33
|
+
enumerable: true,
|
|
34
|
+
configurable: true,
|
|
35
|
+
writable: true,
|
|
36
|
+
value: "result"
|
|
37
|
+
});
|
|
38
|
+
Object.defineProperty(this, "topK", {
|
|
39
|
+
enumerable: true,
|
|
40
|
+
configurable: true,
|
|
41
|
+
writable: true,
|
|
42
|
+
value: 10
|
|
43
|
+
});
|
|
44
|
+
Object.defineProperty(this, "returnDirect", {
|
|
45
|
+
enumerable: true,
|
|
46
|
+
configurable: true,
|
|
47
|
+
writable: true,
|
|
48
|
+
value: false
|
|
49
|
+
});
|
|
50
|
+
Object.defineProperty(this, "returnIntermediateSteps", {
|
|
51
|
+
enumerable: true,
|
|
52
|
+
configurable: true,
|
|
53
|
+
writable: true,
|
|
54
|
+
value: false
|
|
55
|
+
});
|
|
56
|
+
const { graph, cypherGenerationChain, qaChain, inputKey, outputKey, topK, returnIntermediateSteps, returnDirect, } = props;
|
|
57
|
+
this.graph = graph;
|
|
58
|
+
this.cypherGenerationChain = cypherGenerationChain;
|
|
59
|
+
this.qaChain = qaChain;
|
|
60
|
+
if (inputKey) {
|
|
61
|
+
this.inputKey = inputKey;
|
|
62
|
+
}
|
|
63
|
+
if (outputKey) {
|
|
64
|
+
this.outputKey = outputKey;
|
|
65
|
+
}
|
|
66
|
+
if (topK) {
|
|
67
|
+
this.topK = topK;
|
|
68
|
+
}
|
|
69
|
+
if (returnIntermediateSteps) {
|
|
70
|
+
this.returnIntermediateSteps = returnIntermediateSteps;
|
|
71
|
+
}
|
|
72
|
+
if (returnDirect) {
|
|
73
|
+
this.returnDirect = returnDirect;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
_chainType() {
|
|
77
|
+
return "graph_cypher_chain";
|
|
78
|
+
}
|
|
79
|
+
get inputKeys() {
|
|
80
|
+
return [this.inputKey];
|
|
81
|
+
}
|
|
82
|
+
get outputKeys() {
|
|
83
|
+
return [this.outputKey];
|
|
84
|
+
}
|
|
85
|
+
static fromLLM(props) {
|
|
86
|
+
const { graph, qaPrompt = CYPHER_QA_PROMPT, cypherPrompt = CYPHER_GENERATION_PROMPT, llm, cypherLLM, qaLLM, returnIntermediateSteps = false, returnDirect = false, } = props;
|
|
87
|
+
if (!cypherLLM && !llm) {
|
|
88
|
+
throw new Error("Either 'llm' or 'cypherLLM' parameters must be provided");
|
|
89
|
+
}
|
|
90
|
+
if (!qaLLM && !llm) {
|
|
91
|
+
throw new Error("Either 'llm' or 'qaLLM' parameters must be provided");
|
|
92
|
+
}
|
|
93
|
+
if (cypherLLM && qaLLM && llm) {
|
|
94
|
+
throw new Error("You can specify up to two of 'cypherLLM', 'qaLLM', and 'llm', but not all three simultaneously.");
|
|
95
|
+
}
|
|
96
|
+
const qaChain = new LLMChain({
|
|
97
|
+
llm: (qaLLM || llm),
|
|
98
|
+
prompt: qaPrompt,
|
|
99
|
+
});
|
|
100
|
+
const cypherGenerationChain = new LLMChain({
|
|
101
|
+
llm: (cypherLLM || llm),
|
|
102
|
+
prompt: cypherPrompt,
|
|
103
|
+
});
|
|
104
|
+
return new GraphCypherQAChain({
|
|
105
|
+
cypherGenerationChain,
|
|
106
|
+
qaChain,
|
|
107
|
+
graph,
|
|
108
|
+
returnIntermediateSteps,
|
|
109
|
+
returnDirect,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
extractCypher(text) {
|
|
113
|
+
const pattern = /```(.*?)```/s;
|
|
114
|
+
const matches = text.match(pattern);
|
|
115
|
+
return matches ? matches[1] : text;
|
|
116
|
+
}
|
|
117
|
+
async _call(values, runManager) {
|
|
118
|
+
const callbacks = runManager?.getChild();
|
|
119
|
+
const question = values[this.inputKey];
|
|
120
|
+
const intermediateSteps = [];
|
|
121
|
+
const generatedCypher = await this.cypherGenerationChain.call({ question, schema: this.graph.getSchema() }, callbacks);
|
|
122
|
+
const extractedCypher = this.extractCypher(generatedCypher.text);
|
|
123
|
+
await runManager?.handleText(`Generated Cypher:\n`);
|
|
124
|
+
await runManager?.handleText(`${extractedCypher} green\n`);
|
|
125
|
+
intermediateSteps.push({ query: extractedCypher });
|
|
126
|
+
let chainResult;
|
|
127
|
+
const context = await this.graph.query(extractedCypher, {
|
|
128
|
+
topK: this.topK,
|
|
129
|
+
});
|
|
130
|
+
if (this.returnDirect) {
|
|
131
|
+
chainResult = { [this.outputKey]: context };
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
await runManager?.handleText("Full Context:\n");
|
|
135
|
+
await runManager?.handleText(`${context} green\n`);
|
|
136
|
+
intermediateSteps.push({ context });
|
|
137
|
+
const result = await this.qaChain.call({ question, context: JSON.stringify(context) }, callbacks);
|
|
138
|
+
chainResult = {
|
|
139
|
+
[this.outputKey]: result[this.qaChain.outputKey],
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
if (this.returnIntermediateSteps) {
|
|
143
|
+
chainResult[INTERMEDIATE_STEPS_KEY] = intermediateSteps;
|
|
144
|
+
}
|
|
145
|
+
return chainResult;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CYPHER_QA_PROMPT = exports.CYPHER_GENERATION_PROMPT = void 0;
|
|
4
|
+
const prompt_js_1 = require("../../prompts/prompt.cjs");
|
|
5
|
+
const CYPHER_GENERATION_TEMPLATE = `Task:Generate Cypher statement to query a graph database.
|
|
6
|
+
Instructions:
|
|
7
|
+
Use only the provided relationship types and properties in the schema.
|
|
8
|
+
Do not use any other relationship types or properties that are not provided.
|
|
9
|
+
Schema:
|
|
10
|
+
{schema}
|
|
11
|
+
Note: Do not include any explanations or apologies in your responses.
|
|
12
|
+
Do not respond to any questions that might ask anything else than for you to construct a Cypher statement.
|
|
13
|
+
Do not include any text except the generated Cypher statement.
|
|
14
|
+
|
|
15
|
+
The question is:
|
|
16
|
+
{question}`;
|
|
17
|
+
exports.CYPHER_GENERATION_PROMPT = new prompt_js_1.PromptTemplate({
|
|
18
|
+
template: CYPHER_GENERATION_TEMPLATE,
|
|
19
|
+
inputVariables: ["schema", "question"],
|
|
20
|
+
});
|
|
21
|
+
const CYPHER_QA_TEMPLATE = `You are an assistant that helps to form nice and human understandable answers.
|
|
22
|
+
The information part contains the provided information that you must use to construct an answer.
|
|
23
|
+
The provided information is authoritative, you must never doubt it or try to use your internal knowledge to correct it.
|
|
24
|
+
Make the answer sound as a response to the question. Do not mention that you based the result on the given information.
|
|
25
|
+
If the provided information is empty, say that you don't know the answer.
|
|
26
|
+
Information:
|
|
27
|
+
{context}
|
|
28
|
+
|
|
29
|
+
Question: {question}
|
|
30
|
+
Helpful Answer:`;
|
|
31
|
+
exports.CYPHER_QA_PROMPT = new prompt_js_1.PromptTemplate({
|
|
32
|
+
template: CYPHER_QA_TEMPLATE,
|
|
33
|
+
inputVariables: ["context", "question"],
|
|
34
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { PromptTemplate } from "../../prompts/prompt.js";
|
|
2
|
+
export declare const CYPHER_GENERATION_PROMPT: PromptTemplate<{
|
|
3
|
+
schema: any;
|
|
4
|
+
question: any;
|
|
5
|
+
}, any>;
|
|
6
|
+
export declare const CYPHER_QA_PROMPT: PromptTemplate<{
|
|
7
|
+
context: any;
|
|
8
|
+
question: any;
|
|
9
|
+
}, any>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { PromptTemplate } from "../../prompts/prompt.js";
|
|
2
|
+
const CYPHER_GENERATION_TEMPLATE = `Task:Generate Cypher statement to query a graph database.
|
|
3
|
+
Instructions:
|
|
4
|
+
Use only the provided relationship types and properties in the schema.
|
|
5
|
+
Do not use any other relationship types or properties that are not provided.
|
|
6
|
+
Schema:
|
|
7
|
+
{schema}
|
|
8
|
+
Note: Do not include any explanations or apologies in your responses.
|
|
9
|
+
Do not respond to any questions that might ask anything else than for you to construct a Cypher statement.
|
|
10
|
+
Do not include any text except the generated Cypher statement.
|
|
11
|
+
|
|
12
|
+
The question is:
|
|
13
|
+
{question}`;
|
|
14
|
+
export const CYPHER_GENERATION_PROMPT = /* #__PURE__ */ new PromptTemplate({
|
|
15
|
+
template: CYPHER_GENERATION_TEMPLATE,
|
|
16
|
+
inputVariables: ["schema", "question"],
|
|
17
|
+
});
|
|
18
|
+
const CYPHER_QA_TEMPLATE = `You are an assistant that helps to form nice and human understandable answers.
|
|
19
|
+
The information part contains the provided information that you must use to construct an answer.
|
|
20
|
+
The provided information is authoritative, you must never doubt it or try to use your internal knowledge to correct it.
|
|
21
|
+
Make the answer sound as a response to the question. Do not mention that you based the result on the given information.
|
|
22
|
+
If the provided information is empty, say that you don't know the answer.
|
|
23
|
+
Information:
|
|
24
|
+
{context}
|
|
25
|
+
|
|
26
|
+
Question: {question}
|
|
27
|
+
Helpful Answer:`;
|
|
28
|
+
export const CYPHER_QA_PROMPT = /* #__PURE__ */ new PromptTemplate({
|
|
29
|
+
template: CYPHER_QA_TEMPLATE,
|
|
30
|
+
inputVariables: ["context", "question"],
|
|
31
|
+
});
|
package/dist/chains/index.d.ts
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
export { BaseChain, ChainInputs } from "./base.js";
|
|
2
|
-
export { LLMChain, LLMChainInput } from "./llm_chain.js";
|
|
3
|
-
export { APIChain, APIChainInput, APIChainOptions } from "./api/api_chain.js";
|
|
1
|
+
export { BaseChain, type ChainInputs } from "./base.js";
|
|
2
|
+
export { LLMChain, type LLMChainInput } from "./llm_chain.js";
|
|
3
|
+
export { APIChain, type APIChainInput, type APIChainOptions, } from "./api/api_chain.js";
|
|
4
4
|
export { ConversationChain } from "./conversation.js";
|
|
5
|
-
export { SequentialChain, SequentialChainInput, SimpleSequentialChain, SimpleSequentialChainInput, } from "./sequential_chain.js";
|
|
6
|
-
export { StuffDocumentsChain, StuffDocumentsChainInput, MapReduceDocumentsChain, MapReduceDocumentsChainInput, RefineDocumentsChain, RefineDocumentsChainInput, } from "./combine_docs_chain.js";
|
|
7
|
-
export { ChatVectorDBQAChain, ChatVectorDBQAChainInput, } from "./chat_vector_db_chain.js";
|
|
8
|
-
export { AnalyzeDocumentChain, AnalyzeDocumentChainInput, } from "./analyze_documents_chain.js";
|
|
9
|
-
export { VectorDBQAChain, VectorDBQAChainInput } from "./vector_db_qa.js";
|
|
10
|
-
export { loadQAChain, QAChainParams, loadQAStuffChain, StuffQAChainParams, loadQAMapReduceChain, MapReduceQAChainParams, loadQARefineChain, RefineQAChainParams, } from "./question_answering/load.js";
|
|
11
|
-
export { loadSummarizationChain, SummarizationChainParams, } from "./summarization/load.js";
|
|
12
|
-
export { ConversationalRetrievalQAChain, ConversationalRetrievalQAChainInput, } from "./conversational_retrieval_chain.js";
|
|
13
|
-
export { RetrievalQAChain, RetrievalQAChainInput } from "./retrieval_qa.js";
|
|
14
|
-
export { ConstitutionalChainInput, ConstitutionalChain, } from "./constitutional_ai/constitutional_chain.js";
|
|
5
|
+
export { SequentialChain, type SequentialChainInput, SimpleSequentialChain, type SimpleSequentialChainInput, } from "./sequential_chain.js";
|
|
6
|
+
export { StuffDocumentsChain, type StuffDocumentsChainInput, MapReduceDocumentsChain, type MapReduceDocumentsChainInput, RefineDocumentsChain, type RefineDocumentsChainInput, } from "./combine_docs_chain.js";
|
|
7
|
+
export { ChatVectorDBQAChain, type ChatVectorDBQAChainInput, } from "./chat_vector_db_chain.js";
|
|
8
|
+
export { AnalyzeDocumentChain, type AnalyzeDocumentChainInput, } from "./analyze_documents_chain.js";
|
|
9
|
+
export { VectorDBQAChain, type VectorDBQAChainInput } from "./vector_db_qa.js";
|
|
10
|
+
export { loadQAChain, type QAChainParams, loadQAStuffChain, type StuffQAChainParams, loadQAMapReduceChain, type MapReduceQAChainParams, loadQARefineChain, type RefineQAChainParams, } from "./question_answering/load.js";
|
|
11
|
+
export { loadSummarizationChain, type SummarizationChainParams, } from "./summarization/load.js";
|
|
12
|
+
export { ConversationalRetrievalQAChain, type ConversationalRetrievalQAChainInput, } from "./conversational_retrieval_chain.js";
|
|
13
|
+
export { RetrievalQAChain, type RetrievalQAChainInput, } from "./retrieval_qa.js";
|
|
14
|
+
export { type ConstitutionalChainInput, ConstitutionalChain, } from "./constitutional_ai/constitutional_chain.js";
|
|
15
15
|
export { ConstitutionalPrinciple, PRINCIPLES, } from "./constitutional_ai/constitutional_principle.js";
|
|
16
|
-
export { SerializedLLMChain, SerializedSequentialChain, SerializedSimpleSequentialChain, SerializedAnalyzeDocumentChain, SerializedAPIChain, SerializedBaseChain, SerializedChatVectorDBQAChain, SerializedMapReduceDocumentsChain, SerializedStuffDocumentsChain, SerializedVectorDBQAChain, SerializedRefineDocumentsChain, } from "./serde.js";
|
|
16
|
+
export type { SerializedLLMChain, SerializedSequentialChain, SerializedSimpleSequentialChain, SerializedAnalyzeDocumentChain, SerializedAPIChain, SerializedBaseChain, SerializedChatVectorDBQAChain, SerializedMapReduceDocumentsChain, SerializedStuffDocumentsChain, SerializedVectorDBQAChain, SerializedRefineDocumentsChain, } from "./serde.js";
|
|
17
17
|
export { OpenAIModerationChain } from "./openai_moderation.js";
|
|
18
|
-
export { MultiRouteChain, MultiRouteChainInput, RouterChain, } from "./router/multi_route.js";
|
|
19
|
-
export { LLMRouterChain, LLMRouterChainInput, RouterOutputSchema, } from "./router/llm_router.js";
|
|
18
|
+
export { MultiRouteChain, type MultiRouteChainInput, RouterChain, } from "./router/multi_route.js";
|
|
19
|
+
export { LLMRouterChain, type LLMRouterChainInput, type RouterOutputSchema, } from "./router/llm_router.js";
|
|
20
20
|
export { MultiPromptChain } from "./router/multi_prompt.js";
|
|
21
21
|
export { MultiRetrievalQAChain } from "./router/multi_retrieval_qa.js";
|
|
22
|
-
export { TransformChain, TransformChainFields } from "./transform.js";
|
|
22
|
+
export { TransformChain, type TransformChainFields } from "./transform.js";
|
|
23
23
|
export { createExtractionChain, createExtractionChainFromZod, } from "./openai_functions/extraction.js";
|
|
24
|
-
export { TaggingChainOptions, createTaggingChain, createTaggingChainFromZod, } from "./openai_functions/tagging.js";
|
|
25
|
-
export { OpenAPIChainOptions, createOpenAPIChain, } from "./openai_functions/openapi.js";
|
|
24
|
+
export { type TaggingChainOptions, createTaggingChain, createTaggingChainFromZod, } from "./openai_functions/tagging.js";
|
|
25
|
+
export { type OpenAPIChainOptions, createOpenAPIChain, } from "./openai_functions/openapi.js";
|
package/dist/chains/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export { BaseChain } from "./base.js";
|
|
2
2
|
export { LLMChain } from "./llm_chain.js";
|
|
3
|
-
export { APIChain } from "./api/api_chain.js";
|
|
3
|
+
export { APIChain, } from "./api/api_chain.js";
|
|
4
4
|
export { ConversationChain } from "./conversation.js";
|
|
5
5
|
export { SequentialChain, SimpleSequentialChain, } from "./sequential_chain.js";
|
|
6
6
|
export { StuffDocumentsChain, MapReduceDocumentsChain, RefineDocumentsChain, } from "./combine_docs_chain.js";
|
|
@@ -10,7 +10,7 @@ export { VectorDBQAChain } from "./vector_db_qa.js";
|
|
|
10
10
|
export { loadQAChain, loadQAStuffChain, loadQAMapReduceChain, loadQARefineChain, } from "./question_answering/load.js";
|
|
11
11
|
export { loadSummarizationChain, } from "./summarization/load.js";
|
|
12
12
|
export { ConversationalRetrievalQAChain, } from "./conversational_retrieval_chain.js";
|
|
13
|
-
export { RetrievalQAChain } from "./retrieval_qa.js";
|
|
13
|
+
export { RetrievalQAChain, } from "./retrieval_qa.js";
|
|
14
14
|
export { ConstitutionalChain, } from "./constitutional_ai/constitutional_chain.js";
|
|
15
15
|
export { ConstitutionalPrinciple, PRINCIPLES, } from "./constitutional_ai/constitutional_principle.js";
|
|
16
16
|
export { OpenAIModerationChain } from "./openai_moderation.js";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { createExtractionChain, createExtractionChainFromZod, } from "./extraction.js";
|
|
2
|
-
export { TaggingChainOptions, createTaggingChain, createTaggingChainFromZod, } from "./tagging.js";
|
|
3
|
-
export { OpenAPIChainOptions, createOpenAPIChain } from "./openapi.js";
|
|
4
|
-
export { StructuredOutputChainInput, createStructuredOutputChain, createStructuredOutputChainFromZod, } from "./structured_output.js";
|
|
2
|
+
export { type TaggingChainOptions, createTaggingChain, createTaggingChainFromZod, } from "./tagging.js";
|
|
3
|
+
export { type OpenAPIChainOptions, createOpenAPIChain } from "./openapi.js";
|
|
4
|
+
export { type StructuredOutputChainInput, createStructuredOutputChain, createStructuredOutputChainFromZod, } from "./structured_output.js";
|
|
@@ -7,7 +7,7 @@ import { LLMChain } from "../llm_chain.js";
|
|
|
7
7
|
import { BaseLanguageModel } from "../../base_language/index.js";
|
|
8
8
|
import { AsymmetricStructuredOutputParser } from "../../output_parsers/structured.js";
|
|
9
9
|
import { AttributeInfo } from "../../schema/query_constructor.js";
|
|
10
|
-
export { QueryTransformer, TraverseType };
|
|
10
|
+
export { QueryTransformer, type TraverseType };
|
|
11
11
|
export { DEFAULT_EXAMPLES, DEFAULT_PREFIX, DEFAULT_SCHEMA, DEFAULT_SUFFIX, EXAMPLE_PROMPT, };
|
|
12
12
|
declare const queryInputSchema: z.ZodObject<{
|
|
13
13
|
query: z.ZodString;
|