LangchainJS

LangChain.js 是 LangChain 库的 JavaScript/TypeScript 实现。它使用类似的概念,包括提示、链、转换器、文档加载器、代理等。
以下是图集成 的概述。
Neo4j 集成使 LangChain.js 库中可使用Neo4j 向量 索引以及 Cypher 生成和执行。
了解如何使用 LangChain.js 和我们的新GraphAcademy 课程 在 TypeScript 中构建聊天机器人。
功能包括
Neo4jVector
Neo4j Vector 集成支持许多操作
-
从 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://localhost: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 图
Neo4j 图集成是 Neo4j Python 驱动程序的包装器。它允许从 LangChain 以简化的方式查询和更新 Neo4j 数据库。许多集成允许使用 Neo4j 图作为 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 模型。
在图转换器 中,您可以从非结构化文档中提取结构化数据,并将其转换为知识图谱。