Skip to content

The agent engineering platform.

Update graph docs (#21414)

Tomaz Bratanicdd70f2f47315
diff --git a/docs/docs/use_cases/graph/constructing.ipynb b/docs/docs/use_cases/graph/constructing.ipynb
index 3a63893c0e..83dd01d5af 100644
--- a/docs/docs/use_cases/graph/constructing.ipynb
+++ b/docs/docs/use_cases/graph/constructing.ipynb
@@ -65,7 +65,7 @@
"metadata": {},
"outputs": [
{
- "name": "stdout",
+ "name": "stdin",
"output_type": "stream",
"text": [
" ········\n"
@@ -128,7 +128,7 @@
"from langchain_experimental.graph_transformers import LLMGraphTransformer\n",
"from langchain_openai import ChatOpenAI\n",
"\n",
- "llm = ChatOpenAI(temperature=0, model_name=\"gpt-4-0125-preview\")\n",
+ "llm = ChatOpenAI(temperature=0, model_name=\"gpt-4-turbo\")\n",
"\n",
"llm_transformer = LLMGraphTransformer(llm=llm)"
]
@@ -149,8 +149,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "Nodes:[Node(id='Marie Curie', type='Person'), Node(id='Polish', type='Nationality'), Node(id='French', type='Nationality'), Node(id='Physicist', type='Occupation'), Node(id='Chemist', type='Occupation'), Node(id='Radioactivity', type='Field'), Node(id='Nobel Prize', type='Award'), Node(id='Pierre Curie', type='Person'), Node(id='University Of Paris', type='Organization')]\n",
- "Relationships:[Relationship(source=Node(id='Marie Curie', type='Person'), target=Node(id='Polish', type='Nationality'), type='NATIONALITY'), Relationship(source=Node(id='Marie Curie', type='Person'), target=Node(id='French', type='Nationality'), type='NATIONALITY'), Relationship(source=Node(id='Marie Curie', type='Person'), target=Node(id='Physicist', type='Occupation'), type='OCCUPATION'), Relationship(source=Node(id='Marie Curie', type='Person'), target=Node(id='Chemist', type='Occupation'), type='OCCUPATION'), Relationship(source=Node(id='Marie Curie', type='Person'), target=Node(id='Radioactivity', type='Field'), type='RESEARCH_FIELD'), Relationship(source=Node(id='Marie Curie', type='Person'), target=Node(id='Nobel Prize', type='Award'), type='AWARD_WINNER'), Relationship(source=Node(id='Pierre Curie', type='Person'), target=Node(id='Nobel Prize', type='Award'), type='AWARD_WINNER'), Relationship(source=Node(id='Marie Curie', type='Person'), target=Node(id='University Of Paris', type='Organization'), type='PROFESSOR')]\n"
+ "Nodes:[Node(id='Marie Curie', type='Person'), Node(id='Pierre Curie', type='Person'), Node(id='University Of Paris', type='Organization')]\n",
+ "Relationships:[Relationship(source=Node(id='Marie Curie', type='Person'), target=Node(id='Pierre Curie', type='Person'), type='MARRIED'), Relationship(source=Node(id='Marie Curie', type='Person'), target=Node(id='University Of Paris', type='Organization'), type='PROFESSOR')]\n"
]
}
],
@@ -158,7 +158,7 @@
"from langchain_core.documents import Document\n",
"\n",
"text = \"\"\"\n",
- "Marie Curie, was a Polish and naturalised-French physicist and chemist who conducted pioneering research on radioactivity.\n",
+ "Marie Curie, born in 1867, was a Polish and naturalised-French physicist and chemist who conducted pioneering research on radioactivity.\n",
"She was the first woman to win a Nobel Prize, the first person to win a Nobel Prize twice, and the only person to win a Nobel Prize in two scientific fields.\n",
"Her husband, Pierre Curie, was a co-winner of her first Nobel Prize, making them the first-ever married couple to win the Nobel Prize and launching the Curie family legacy of five Nobel Prizes.\n",
"She was, in 1906, the first woman to become a professor at the University of Paris.\n",
@@ -191,8 +191,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "Nodes:[Node(id='Marie Curie', type='Person'), Node(id='Polish', type='Country'), Node(id='French', type='Country'), Node(id='Pierre Curie', type='Person'), Node(id='University Of Paris', type='Organization')]\n",
- "Relationships:[Relationship(source=Node(id='Marie Curie', type='Person'), target=Node(id='Polish', type='Country'), type='NATIONALITY'), Relationship(source=Node(id='Marie Curie', type='Person'), target=Node(id='French', type='Country'), type='NATIONALITY'), Relationship(source=Node(id='Pierre Curie', type='Person'), target=Node(id='Marie Curie', type='Person'), type='SPOUSE'), Relationship(source=Node(id='Marie Curie', type='Person'), target=Node(id='University Of Paris', type='Organization'), type='WORKED_AT')]\n"
+ "Nodes:[Node(id='Marie Curie', type='Person'), Node(id='Pierre Curie', type='Person'), Node(id='University Of Paris', type='Organization')]\n",
+ "Relationships:[Relationship(source=Node(id='Marie Curie', type='Person'), target=Node(id='Pierre Curie', type='Person'), type='SPOUSE'), Relationship(source=Node(id='Marie Curie', type='Person'), target=Node(id='University Of Paris', type='Organization'), type='WORKED_AT')]\n"
]
}
],
@@ -218,6 +218,41 @@
"![graph_construction2.png](../../../static/img/graph_construction2.png)"
]
},
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "The `node_properties` parameter enables the extraction of node properties, allowing the creation of a more detailed graph.\n",
+ "When set to `True`, LLM autonomously identifies and extracts relevant node properties.\n",
+ "Conversely, if `node_properties` is defined as a list of strings, the LLM selectively retrieves only the specified properties from the text."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Nodes:[Node(id='Marie Curie', type='Person', properties={'born_year': '1867'}), Node(id='Pierre Curie', type='Person'), Node(id='University Of Paris', type='Organization')]\n",
+ "Relationships:[Relationship(source=Node(id='Marie Curie', type='Person'), target=Node(id='Pierre Curie', type='Person'), type='SPOUSE'), Relationship(source=Node(id='Marie Curie', type='Person'), target=Node(id='University Of Paris', type='Organization'), type='WORKED_AT')]\n"
+ ]
+ }
+ ],
+ "source": [
+ "llm_transformer_props = LLMGraphTransformer(\n",
+ " llm=llm,\n",
+ " allowed_nodes=[\"Person\", \"Country\", \"Organization\"],\n",
+ " allowed_relationships=[\"NATIONALITY\", \"LOCATED_IN\", \"WORKED_AT\", \"SPOUSE\"],\n",
+ " node_properties=[\"born_year\"],\n",
+ ")\n",
+ "graph_documents_props = llm_transformer_props.convert_to_graph_documents(documents)\n",
+ "print(f\"Nodes:{graph_documents_props[0].nodes}\")\n",
+ "print(f\"Relationships:{graph_documents_props[0].relationships}\")"
+ ]
+ },
{
"cell_type": "markdown",
"metadata": {},
@@ -229,11 +264,11 @@
},
{
"cell_type": "code",
- "execution_count": 7,
+ "execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
- "graph.add_graph_documents(graph_documents_filtered)"
+ "graph.add_graph_documents(graph_documents_props)"
]
}
],
diff --git a/docs/docs/use_cases/graph/mapping.ipynb b/docs/docs/use_cases/graph/mapping.ipynb
index 4978ec0d4d..67ec5fb3d1 100644
--- a/docs/docs/use_cases/graph/mapping.ipynb
+++ b/docs/docs/use_cases/graph/mapping.ipynb
@@ -28,18 +28,10 @@
},
{
"cell_type": "code",
- "execution_count": 1,
+ "execution_count": null,
"id": "18294435-182d-48da-bcab-5b8945b6d9cf",
"metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Note: you may need to restart the kernel to use updated packages.\n"
- ]
- }
- ],
+ "outputs": [],
"source": [
"%pip install --upgrade --quiet langchain langchain-community langchain-openai neo4j"
]
@@ -166,20 +158,10 @@
"execution_count": 5,
"id": "e1a19424-6046-40c2-81d1-f3b88193a293",
"metadata": {},
- "outputs": [
- {
- "name": "stderr",
- "output_type": "stream",
- "text": [
- "/Users/tomazbratanic/anaconda3/lib/python3.11/site-packages/langchain_core/_api/deprecation.py:117: LangChainDeprecationWarning: The function `create_structured_output_chain` was deprecated in LangChain 0.1.1 and will be removed in 0.2.0. Use create_structured_output_runnable instead.\n",
- " warn_deprecated(\n"
- ]
- }
- ],
+ "outputs": [],
"source": [
"from typing import List, Optional\n",
"\n",
- "from langchain.chains.openai_functions import create_structured_output_chain\n",
"from langchain_core.prompts import ChatPromptTemplate\n",
"from langchain_core.pydantic_v1 import BaseModel, Field\n",
"from langchain_openai import ChatOpenAI\n",
@@ -211,7 +193,7 @@
")\n",
"\n",
"\n",
- "entity_chain = create_structured_output_chain(Entities, llm, prompt)"
+ "entity_chain = prompt | llm.with_structured_output(Entities)"
]
},
{
@@ -231,8 +213,7 @@
{
"data": {
"text/plain": [
- "{'question': 'Who played in Casino movie?',\n",
- " 'function': Entities(names=['Casino'])}"
+ "Entities(names=['Casino'])"
]
},
"execution_count": 6,
@@ -278,9 +259,9 @@
"\"\"\"\n",
"\n",
"\n",
- "def map_to_database(values):\n",
+ "def map_to_database(entities: Entities) -> Optional[str]:\n",
" result = \"\"\n",
- " for entity in values.names:\n",
+ " for entity in entities.names:\n",
" response = graph.query(match_query, {\"value\": entity})\n",
" try:\n",
" result += f\"{entity} maps to {response[0]['result']} {response[0]['type']} in database\\n\"\n",
@@ -289,7 +270,7 @@
" return result\n",
"\n",
"\n",
- "map_to_database(entities[\"function\"])"
+ "map_to_database(entities)"
]
},
{
@@ -334,7 +315,7 @@
"cypher_response = (\n",
" RunnablePassthrough.assign(names=entity_chain)\n",
" | RunnablePassthrough.assign(\n",
- " entities_list=lambda x: map_to_database(x[\"names\"][\"function\"]),\n",
+ " entities_list=lambda x: map_to_database(x[\"names\"]),\n",
" schema=lambda _: graph.get_schema,\n",
" )\n",
" | cypher_prompt\n",
@@ -429,7 +410,7 @@
{
"data": {
"text/plain": [
- "'Joe Pesci, Robert De Niro, Sharon Stone, and James Woods played in the movie \"Casino\".'"
+ "'Robert De Niro, James Woods, Joe Pesci, and Sharon Stone played in the movie \"Casino\".'"
]
},
"execution_count": 11,
@@ -466,7 +447,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.9.1"
+ "version": "3.9.18"
}
},
"nbformat": 4,