mirror of
https://github.com/hwchase17/langchain.git
synced 2026-04-03 10:55:08 +00:00
Use docusaurus versioning with a callout, merged master as well @hwchase17 @baskaryan --------- Signed-off-by: Weichen Xu <weichen.xu@databricks.com> Signed-off-by: Rahul Tripathi <rauhl.psit.ec@gmail.com> Co-authored-by: Leonid Ganeline <leo.gan.57@gmail.com> Co-authored-by: Leonid Kuligin <lkuligin@yandex.ru> Co-authored-by: Averi Kitsch <akitsch@google.com> Co-authored-by: Erick Friis <erick@langchain.dev> Co-authored-by: Nuno Campos <nuno@langchain.dev> Co-authored-by: Nuno Campos <nuno@boringbits.io> Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com> Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com> Co-authored-by: Martín Gotelli Ferenaz <martingotelliferenaz@gmail.com> Co-authored-by: Fayfox <admin@fayfox.com> Co-authored-by: Eugene Yurtsev <eugene@langchain.dev> Co-authored-by: Dawson Bauer <105886620+djbauer2@users.noreply.github.com> Co-authored-by: Ravindu Somawansa <ravindu.somawansa@gmail.com> Co-authored-by: Dhruv Chawla <43818888+Dominastorm@users.noreply.github.com> Co-authored-by: ccurme <chester.curme@gmail.com> Co-authored-by: Bagatur <baskaryan@gmail.com> Co-authored-by: WeichenXu <weichen.xu@databricks.com> Co-authored-by: Benito Geordie <89472452+benitoThree@users.noreply.github.com> Co-authored-by: kartikTAI <129414343+kartikTAI@users.noreply.github.com> Co-authored-by: Kartik Sarangmath <kartik@thirdai.com> Co-authored-by: Sevin F. Varoglu <sfvaroglu@octoml.ai> Co-authored-by: MacanPN <martin.triska@gmail.com> Co-authored-by: Prashanth Rao <35005448+prrao87@users.noreply.github.com> Co-authored-by: Hyeongchan Kim <kozistr@gmail.com> Co-authored-by: sdan <git@sdan.io> Co-authored-by: Guangdong Liu <liugddx@gmail.com> Co-authored-by: Rahul Triptahi <rahul.psit.ec@gmail.com> Co-authored-by: Rahul Tripathi <rauhl.psit.ec@gmail.com> Co-authored-by: pjb157 <84070455+pjb157@users.noreply.github.com> Co-authored-by: Eun Hye Kim <ehkim1440@gmail.com> Co-authored-by: kaijietti <43436010+kaijietti@users.noreply.github.com> Co-authored-by: Pengcheng Liu <pcliu.fd@gmail.com> Co-authored-by: Tomer Cagan <tomer@tomercagan.com> Co-authored-by: Christophe Bornet <cbornet@hotmail.com>
413 lines
9.1 KiB
Plaintext
413 lines
9.1 KiB
Plaintext
{
|
||
"cells": [
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "a6850189",
|
||
"metadata": {},
|
||
"source": [
|
||
"# NetworkX\n",
|
||
"\n",
|
||
">[NetworkX](https://networkx.org/) is a Python package for the creation, manipulation, and study of the structure, dynamics, and functions of complex networks.\n",
|
||
"\n",
|
||
"This notebook goes over how to do question answering over a graph data structure."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "f96662d5-1b68-4b38-9da8-56bf3463b138",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Setting up\n",
|
||
"\n",
|
||
"We have to install a Python package."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "21bd86df-9717-4a27-9233-1404c89cf442",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"%pip install --upgrade --quiet networkx"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "9e516e3e",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Create the graph\n",
|
||
"\n",
|
||
"In this section, we construct an example graph. At the moment, this works best for small pieces of text."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 1,
|
||
"id": "3849873d",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"from langchain.indexes import GraphIndexCreator\n",
|
||
"from langchain_openai import OpenAI"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 2,
|
||
"id": "05d65c87",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"index_creator = GraphIndexCreator(llm=OpenAI(temperature=0))"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 3,
|
||
"id": "0a45a5b9",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"with open(\"../../../modules/state_of_the_union.txt\") as f:\n",
|
||
" all_text = f.read()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "3fca3e1b",
|
||
"metadata": {},
|
||
"source": [
|
||
"We will use just a small snippet, because extracting the knowledge triplets is a bit intensive at the moment."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 4,
|
||
"id": "80522bd6",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"text = \"\\n\".join(all_text.split(\"\\n\\n\")[105:108])"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 5,
|
||
"id": "da5aad5a",
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"'It won’t look like much, but if you stop and look closely, you’ll see a “Field of dreams,” the ground on which America’s future will be built. \\nThis is where Intel, the American company that helped build Silicon Valley, is going to build its $20 billion semiconductor “mega site”. \\nUp to eight state-of-the-art factories in one place. 10,000 new good-paying jobs. '"
|
||
]
|
||
},
|
||
"execution_count": 5,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"text"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 6,
|
||
"id": "8dad7b59",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"graph = index_creator.from_text(text)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "2118f363",
|
||
"metadata": {},
|
||
"source": [
|
||
"We can inspect the created graph."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 7,
|
||
"id": "32878c13",
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"[('Intel', '$20 billion semiconductor \"mega site\"', 'is going to build'),\n",
|
||
" ('Intel', 'state-of-the-art factories', 'is building'),\n",
|
||
" ('Intel', '10,000 new good-paying jobs', 'is creating'),\n",
|
||
" ('Intel', 'Silicon Valley', 'is helping build'),\n",
|
||
" ('Field of dreams',\n",
|
||
" \"America's future will be built\",\n",
|
||
" 'is the ground on which')]"
|
||
]
|
||
},
|
||
"execution_count": 7,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"graph.get_triples()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "e9737be1",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Querying the graph\n",
|
||
"We can now use the graph QA chain to ask question of the graph"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 8,
|
||
"id": "76edc854",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"from langchain.chains import GraphQAChain"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 9,
|
||
"id": "8e7719b4",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"chain = GraphQAChain.from_llm(OpenAI(temperature=0), graph=graph, verbose=True)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 10,
|
||
"id": "f6511169",
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"\n",
|
||
"\n",
|
||
"\u001b[1m> Entering new GraphQAChain chain...\u001b[0m\n",
|
||
"Entities Extracted:\n",
|
||
"\u001b[32;1m\u001b[1;3m Intel\u001b[0m\n",
|
||
"Full Context:\n",
|
||
"\u001b[32;1m\u001b[1;3mIntel is going to build $20 billion semiconductor \"mega site\"\n",
|
||
"Intel is building state-of-the-art factories\n",
|
||
"Intel is creating 10,000 new good-paying jobs\n",
|
||
"Intel is helping build Silicon Valley\u001b[0m\n",
|
||
"\n",
|
||
"\u001b[1m> Finished chain.\u001b[0m\n"
|
||
]
|
||
},
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"' Intel is going to build a $20 billion semiconductor \"mega site\" with state-of-the-art factories, creating 10,000 new good-paying jobs and helping to build Silicon Valley.'"
|
||
]
|
||
},
|
||
"execution_count": 10,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"chain.run(\"what is Intel going to build?\")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "410aafa0",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Save the graph\n",
|
||
"We can also save and load the graph."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 7,
|
||
"id": "bc72cca0",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"graph.write_to_gml(\"graph.gml\")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 8,
|
||
"id": "652760ad",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"from langchain.indexes.graph import NetworkxEntityGraph"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 9,
|
||
"id": "eae591fe",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"loaded_graph = NetworkxEntityGraph.from_gml(\"graph.gml\")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 10,
|
||
"id": "9439d419",
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"[('Intel', '$20 billion semiconductor \"mega site\"', 'is going to build'),\n",
|
||
" ('Intel', 'state-of-the-art factories', 'is building'),\n",
|
||
" ('Intel', '10,000 new good-paying jobs', 'is creating'),\n",
|
||
" ('Intel', 'Silicon Valley', 'is helping build'),\n",
|
||
" ('Field of dreams',\n",
|
||
" \"America's future will be built\",\n",
|
||
" 'is the ground on which')]"
|
||
]
|
||
},
|
||
"execution_count": 10,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"loaded_graph.get_triples()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "045796cf",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"loaded_graph.get_number_of_nodes()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "7cc06554",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"loaded_graph.add_node(\"NewNode\")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "f42deb48",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"loaded_graph.has_node(\"NewNode\")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "e91bc6b9",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"loaded_graph.remove_node(\"NewNode\")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "a1d1e745",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"loaded_graph.get_neighbors(\"Intel\")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "eecea586",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"loaded_graph.has_edge(\"Intel\", \"Silicon Valley\")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "f1fdc612",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"loaded_graph.remove_edge(\"Intel\", \"Silicon Valley\")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "d24b3407",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"loaded_graph.clear_edges()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "b8afda51",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"loaded_graph.clear()"
|
||
]
|
||
}
|
||
],
|
||
"metadata": {
|
||
"kernelspec": {
|
||
"display_name": "Python 3 (ipykernel)",
|
||
"language": "python",
|
||
"name": "python3"
|
||
},
|
||
"language_info": {
|
||
"codemirror_mode": {
|
||
"name": "ipython",
|
||
"version": 3
|
||
},
|
||
"file_extension": ".py",
|
||
"mimetype": "text/x-python",
|
||
"name": "python",
|
||
"nbconvert_exporter": "python",
|
||
"pygments_lexer": "ipython3",
|
||
"version": "3.10.12"
|
||
},
|
||
"vscode": {
|
||
"interpreter": {
|
||
"hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
|
||
}
|
||
}
|
||
},
|
||
"nbformat": 4,
|
||
"nbformat_minor": 5
|
||
}
|