Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRAFT: Add Graph store and Graph QA #2514

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 156 additions & 0 deletions langchain/src/chains/graph_qa.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import { BaseChain, ChainInputs } from "./base.js";
import { GraphStore } from "../graphstores/base.js";
import { SerializedGraphQAChain } from "./serde.js";
import { BaseLanguageModel } from "../base_language/index.js";
import { CallbackManagerForChainRun } from "../callbacks/manager.js";
import { ChainValues } from "../schema/index.js";
import { loadQAStuffChain } from "./question_answering/load.js";

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type LoadValues = Record<string, any>;

/**
* Interface that extends the `ChainInputs` interface and defines the
* input fields required for a GraphQAChain. It includes properties
* such as `graphstore`, `combineDocumentsChain`,
* `returnSourceDocuments`, `k`, and `inputKey`.
*/
export interface GraphQAChainInput extends Omit<ChainInputs, "memory"> {
graphstore: GraphStore;
combineDocumentsChain: BaseChain;
returnSourceDocuments?: boolean;
k?: number;
inputKey?: string;
}

/**
* Class that represents a GraphQAChain. It extends the `BaseChain`
* class and implements the `GraphQAChainInput` interface. It performs
* a similarity search using a knowledge graph and combines the search
* results using a specified combine documents chain.
*/
export class GraphQAChain extends BaseChain implements GraphQAChainInput {
static lc_name() {
return "GraphQAChain";
}

k = 4;

inputKey = "query";

get inputKeys() {
return [this.inputKey];
}

get outputKeys() {
return this.combineDocumentsChain.outputKeys.concat(
this.returnSourceDocuments ? ["sourceDocuments"] : []
);
}

graphstore: GraphStore;

combineDocumentsChain: BaseChain;

returnSourceDocuments = false;

constructor(fields: GraphQAChainInput) {
super(fields);
this.graphstore = fields.graphstore;
this.combineDocumentsChain = fields.combineDocumentsChain;
this.inputKey = fields.inputKey ?? this.inputKey;
this.k = fields.k ?? this.k;
this.returnSourceDocuments =
fields.returnSourceDocuments ?? this.returnSourceDocuments;
}

/** @ignore */
async _call(
values: ChainValues,
runManager?: CallbackManagerForChainRun
): Promise<ChainValues> {
if (!(this.inputKey in values)) {
throw new Error(`Question key ${this.inputKey} not found.`);
}
const question: string = values[this.inputKey];
const docs = await this.graphstore.similaritySearch(
question,
this.k,
values.filter,
runManager?.getChild("graphstore")
);
const inputs = { question, input_documents: docs };
const result = await this.combineDocumentsChain.call(
inputs,
runManager?.getChild("combine_documents")
);
if (this.returnSourceDocuments) {
return {
...result,
sourceDocuments: docs,
};
}
return result;
}

_chainType() {
return "graph_qa" as const;
}

static async deserialize(
data: SerializedGraphQAChain,
values: LoadValues
) {
if (!("graphstore" in values)) {
throw new Error(
`Need to pass in a graphstore to deserialize GraphQAChain`
);
}
const { graphstore } = values;
if (!data.combine_documents_chain) {
throw new Error(
`GraphQAChain must have combine_documents_chain in serialized data`
);
}

return new GraphQAChain({
combineDocumentsChain: await BaseChain.deserialize(
data.combine_documents_chain
),
k: data.k,
graphstore,
});
}

serialize(): SerializedGraphQAChain {
return {
_type: this._chainType(),
combine_documents_chain: this.combineDocumentsChain.serialize(),
k: this.k,
};
}

/**
* Static method that creates a GraphQAChain instance from a
* BaseLanguageModel and a graph store. It also accepts optional options
* to customize the chain.
* @param llm The BaseLanguageModel instance.
* @param graphstore The graph store used for similarity search.
* @param options Optional options to customize the chain.
* @returns A new instance of GraphQAChain.
*/
static fromLLM(
llm: BaseLanguageModel,
graphstore: GraphStore,
options?: Partial<
Omit<GraphQAChainInput, "combineDocumentsChain" | "graphstore">
>
): GraphQAChain {
const qaChain = loadQAStuffChain(llm);
return new this({
graphstore,
combineDocumentsChain: qaChain,
...options,
});
}
}
1 change: 1 addition & 0 deletions langchain/src/chains/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export {
SerializedMapReduceDocumentsChain,
SerializedStuffDocumentsChain,
SerializedVectorDBQAChain,
SerializedGraphQAChain,
SerializedRefineDocumentsChain,
} from "./serde.js";
export { OpenAIModerationChain } from "./openai_moderation.js";
Expand Down
11 changes: 11 additions & 0 deletions langchain/src/chains/serde.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ export type SerializedVectorDBQAChain = {
combine_documents_chain: SerializedBaseChain;
};

/**
* Represents the serialized form of a GraphQAChain. It includes
* properties such as `_type`, `k`, and `combine_documents_chain`.
*/
export type SerializedGraphQAChain = {
_type: "graph_qa";
k: number;
combine_documents_chain: SerializedBaseChain;
};

/**
* Represents the serialized form of an APIChain. It includes properties
* such as `_type`, `api_request_chain`, `api_answer_chain`, and
Expand Down Expand Up @@ -139,6 +149,7 @@ export type SerializedBaseChain =
| SerializedSequentialChain
| SerializedSimpleSequentialChain
| SerializedVectorDBQAChain
| SerializedGraphQAChain
| SerializedAPIChain
| SerializedStuffDocumentsChain
| SerializedChatVectorDBQAChain
Expand Down
Loading