LangchainJS
LangChain.js 是 LangChain 库的 JavaScript/TypeScript 实现。它使用类似的概念,包括 Prompt、Chain、Transformer、文档加载器、Agent 等。
以下是 图集成 的概览。
Neo4j 集成使得 Neo4j 向量 索引以及 Cypher 的生成和执行在 LangChain.js 库中可用。
了解如何使用 LangChain.js 在 TypeScript 中构建聊天机器人,参与我们的全新 GraphAcademy 课程。
功能包括
Neo4jVector
Neo4j 向量集成支持多种操作:
-
从 LangChain 文档创建向量
-
向量查询
-
结合额外图检索 Cypher 查询的向量查询
-
从现有图数据构建向量实例
-
混合搜索
import { OpenAIEmbeddings } from "@langchain/openai";
import { Neo4jVectorStore } from "@langchain/community/vectorstores/neo4j_vector";
// Configuration object for Neo4j connection and other related settings
const config = {
url: "bolt://:7687", // URL for the Neo4j instance
username: "neo4j", // Username for Neo4j authentication
password: "pleaseletmein", // Password for Neo4j authentication
indexName: "vector", // Name of the vector index
keywordIndexName: "keyword", // Name of the keyword index if using hybrid search
searchType: "vector" as const, // Type of search (e.g., vector, hybrid)
nodeLabel: "Chunk", // Label for the nodes in the graph
textNodeProperty: "text", // Property of the node containing text
embeddingNodeProperty: "embedding", // Property of the node containing embedding
};
const documents = [
{ pageContent: "what's this", metadata: { a: 2 } },
{ pageContent: "Cat drinks milk", metadata: { a: 1 } },
];
const neo4jVectorIndex = await Neo4jVectorStore.fromDocuments(
documents,
new OpenAIEmbeddings(),
config
);
const results = await neo4jVectorIndex.similaritySearch("water", 1);
console.log(results);
/*
[ Document { pageContent: 'Cat drinks milk', metadata: { a: 1 } } ]
*/
await neo4jVectorIndex.close();
Neo4j Graph
Neo4j Graph 集成是 Neo4j Python 驱动的包装器。它允许从 LangChain 以简化的方式查询和更新 Neo4j 数据库。许多集成允许将 Neo4j Graph 作为 LangChain 的数据源。
import { Neo4jGraph } from "@langchain/community/graphs/neo4j_graph";
const graph = await Neo4jGraph.initialize({ NEO4J_URL, NEO4J_USERNAME, NEO4J_PASSWORD });
QUERY = """
"MATCH (m:Movie)-[:IN_GENRE]->(:Genre {name:$genre})
RETURN m.title, m.plot
ORDER BY m.imdbRating DESC LIMIT 5"
"""
await graph.query(QUERY, genre="action")
CypherQAChain
CypherQAChain 是一个 LangChain 组件,允许您使用自然语言与 Neo4j 图数据库进行交互。它使用 LLM 和图谱模式将用户问题转换为 Cypher 查询,针对图谱执行该查询,并利用返回的上下文信息和原始问题,通过第二个 LLM 生成自然语言回复。
import { Neo4jGraph } from "@langchain/community/graphs/neo4j_graph";
import { OpenAI } from "@langchain/openai";
import { GraphCypherQAChain } from "langchain/chains/graph_qa/cypher";
const graph = await Neo4jGraph.initialize({ NEO4J_URL, NEO4J_USERNAME, NEO4J_PASSWORD });
const model = new OpenAI({ temperature: 0 });
// Populate the database with two nodes and a relationship
await graph.query(`
CREATE (a:Actor {name:'Bruce Willis'})
-[:ACTED_IN]->(:Movie {title: 'Pulp Fiction'})
`);
await graph.refreshSchema();
const chain = GraphCypherQAChain.fromLLM({ llm: model, graph });
const res = await chain.run("Who acted in Pulp Fiction?");
// Bruce Willis acted in Pulp Fiction.
知识图谱构建
从 PDF 文档等非结构化数据创建知识图谱曾经是一项复杂且耗时的任务,需要训练和使用专门的大型 NLP 模型。
Graph Transformers(图转换器)是用于从非结构化文档中提取结构化数据并将其转换为知识图谱的工具。
