Skip to content

Commit

Permalink
Merge branch 'master' into pprados/02-pymupdf
Browse files Browse the repository at this point in the history
  • Loading branch information
pprados authored Jan 14, 2025
2 parents 4762fab + d9b856a commit 6121005
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 59 deletions.
46 changes: 23 additions & 23 deletions docs/docs/integrations/graphs/amazon_neptune_open_cypher.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
">[openCypher](https://opencypher.org/) is an open-source implementation of Cypher.# Neptune Open Cypher QA Chain\n",
"This QA chain queries Amazon Neptune using openCypher and returns human readable response\n",
"\n",
"LangChain supports both [Neptune Database](https://docs.aws.amazon.com/neptune/latest/userguide/intro.html) and [Neptune Analytics](https://docs.aws.amazon.com/neptune-analytics/latest/userguide/what-is-neptune-analytics.html) with `NeptuneOpenCypherQAChain` \n",
"\n",
"LangChain supports both [Neptune Database](https://docs.aws.amazon.com/neptune/latest/userguide/intro.html) and [Neptune Analytics](https://docs.aws.amazon.com/neptune-analytics/latest/userguide/what-is-neptune-analytics.html) with `create_neptune_opencypher_qa_chain`.\n",
"\n",
"Neptune Database is a serverless graph database designed for optimal scalability and availability. It provides a solution for graph database workloads that need to scale to 100,000 queries per second, Multi-AZ high availability, and multi-Region deployments. You can use Neptune Database for social networking, fraud alerting, and Customer 360 applications.\n",
"\n",
"Neptune Analytics is an analytics database engine that can quickly analyze large amounts of graph data in memory to get insights and find trends. Neptune Analytics is a solution for quickly analyzing existing graph databases or graph datasets stored in a data lake. It uses popular graph analytic algorithms and low-latency analytic queries.\n",
"\n",
"\n",
"\n",
"## Using Neptune Database"
]
},
Expand All @@ -31,7 +32,7 @@
"metadata": {},
"outputs": [],
"source": [
"from langchain_community.graphs import NeptuneGraph\n",
"from langchain_aws.graphs import NeptuneGraph\n",
"\n",
"host = \"<neptune-host>\"\n",
"port = 8182\n",
Expand All @@ -53,7 +54,7 @@
"metadata": {},
"outputs": [],
"source": [
"from langchain_community.graphs import NeptuneAnalyticsGraph\n",
"from langchain_aws.graphs import NeptuneAnalyticsGraph\n",
"\n",
"graph = NeptuneAnalyticsGraph(graph_identifier=\"<neptune-analytics-graph-id>\")"
]
Expand All @@ -62,36 +63,35 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Using NeptuneOpenCypherQAChain\n",
"## Using the Neptune openCypher QA Chain\n",
"\n",
"This QA chain queries Neptune graph database using openCypher and returns human readable response."
"This QA chain queries the Neptune graph database using openCypher and returns a human-readable response."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'The Austin airport has 98 outgoing routes.'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"from langchain.chains import NeptuneOpenCypherQAChain\n",
"from langchain_openai import ChatOpenAI\n",
"from langchain_aws import ChatBedrockConverse\n",
"from langchain_aws.chains import create_neptune_opencypher_qa_chain\n",
"\n",
"llm = ChatOpenAI(temperature=0, model=\"gpt-4\")\n",
"MODEL_ID = \"anthropic.claude-3-5-sonnet-20241022-v2:0\"\n",
"llm = ChatBedrockConverse(\n",
" model=MODEL_ID,\n",
" temperature=0,\n",
")\n",
"\n",
"chain = NeptuneOpenCypherQAChain.from_llm(llm=llm, graph=graph)\n",
"chain = create_neptune_opencypher_qa_chain(\n",
" llm=llm,\n",
" graph=graph,\n",
")\n",
"\n",
"chain.invoke(\"how many outgoing routes does the Austin airport have?\")"
"result = chain.invoke(\n",
" {\"query\": \"How many outgoing routes does the Austin airport have?\"}\n",
")\n",
"print(result[\"result\"].content)"
]
}
],
Expand Down
82 changes: 51 additions & 31 deletions docs/docs/integrations/graphs/amazon_neptune_sparql.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"\n",
"\n",
"This example uses a `NeptuneRdfGraph` class that connects with the Neptune database and loads its schema. \n",
"The `NeptuneSparqlQAChain` is used to connect the graph and LLM to ask natural language questions.\n",
"The `create_neptune_sparql_qa_chain` is used to connect the graph and LLM to ask natural language questions.\n",
"\n",
"This notebook demonstrates an example using organizational data.\n",
"\n",
Expand Down Expand Up @@ -60,6 +60,11 @@
"STAGE_BUCKET = \"<bucket-name>\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": ""
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -118,7 +123,7 @@
"metadata": {},
"outputs": [],
"source": [
"!pip install --upgrade --quiet langchain langchain-community langchain-aws"
"!pip install --upgrade --quiet langchain-aws"
]
},
{
Expand Down Expand Up @@ -238,48 +243,37 @@
"\"\"\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": "### Create the Neptune Database RDF Graph"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import boto3\n",
"from langchain_aws import ChatBedrock\n",
"from langchain_community.chains.graph_qa.neptune_sparql import NeptuneSparqlQAChain\n",
"from langchain_community.graphs import NeptuneRdfGraph\n",
"from langchain_aws.graphs import NeptuneRdfGraph\n",
"\n",
"host = \"<your host>\"\n",
"port = 8182 # change if different\n",
"region = \"us-east-1\" # change if different\n",
"graph = NeptuneRdfGraph(host=host, port=port, use_iam_auth=True, region_name=region)\n",
"\n",
"# Optionally change the schema\n",
"# Optionally, change the schema\n",
"# elems = graph.get_schema_elements\n",
"# change elems ...\n",
"# graph.load_schema(elems)\n",
"\n",
"MODEL_ID = \"anthropic.claude-v2\"\n",
"bedrock_client = boto3.client(\"bedrock-runtime\")\n",
"llm = ChatBedrock(model_id=MODEL_ID, client=bedrock_client)\n",
"\n",
"chain = NeptuneSparqlQAChain.from_llm(\n",
" llm=llm,\n",
" graph=graph,\n",
" examples=EXAMPLES,\n",
" verbose=True,\n",
" top_K=10,\n",
" return_intermediate_steps=True,\n",
" return_direct=False,\n",
")"
"# graph.load_schema(elems)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Ask questions\n",
"Depends on the data we ingested above"
"## Using the Neptune SPARQL QA Chain\n",
"\n",
"This QA chain queries the Neptune graph database using SPARQL and returns a human-readable response."
]
},
{
Expand All @@ -288,7 +282,31 @@
"metadata": {},
"outputs": [],
"source": [
"chain.invoke(\"\"\"How many organizations are in the graph\"\"\")"
"from langchain_aws import ChatBedrockConverse\n",
"from langchain_aws.chains import create_neptune_sparql_qa_chain\n",
"\n",
"MODEL_ID = \"anthropic.claude-3-5-sonnet-20241022-v2:0\"\n",
"llm = ChatBedrockConverse(\n",
" model_id=MODEL_ID,\n",
" temperature=0,\n",
")\n",
"\n",
"chain = create_neptune_sparql_qa_chain(\n",
" llm=llm,\n",
" graph=graph,\n",
" examples=EXAMPLES,\n",
")\n",
"\n",
"result = chain.invoke({\"query\": \"How many organizations are in the graph?\"})\n",
"print(result[\"result\"].content)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Extra questions\n",
"Here are a few more prompts to try on the graph data that was ingested.\n"
]
},
{
Expand All @@ -297,7 +315,7 @@
"metadata": {},
"outputs": [],
"source": [
"chain.invoke(\"\"\"Are there any mergers or acquisitions\"\"\")"
"chain.invoke({\"query\": \"Are there any mergers or acquisitions?\"})"
]
},
{
Expand All @@ -306,7 +324,7 @@
"metadata": {},
"outputs": [],
"source": [
"chain.invoke(\"\"\"Find organizations\"\"\")"
"chain.invoke({\"query\": \"Find organizations.\"})"
]
},
{
Expand All @@ -315,7 +333,7 @@
"metadata": {},
"outputs": [],
"source": [
"chain.invoke(\"\"\"Find sites of MegaSystems or MegaFinancial\"\"\")"
"chain.invoke({\"query\": \"Find sites of MegaSystems or MegaFinancial.\"})"
]
},
{
Expand All @@ -324,7 +342,7 @@
"metadata": {},
"outputs": [],
"source": [
"chain.invoke(\"\"\"Find a member who is manager of one or more members.\"\"\")"
"chain.invoke({\"query\": \"Find a member who is a manager of one or more members.\"})"
]
},
{
Expand All @@ -333,7 +351,7 @@
"metadata": {},
"outputs": [],
"source": [
"chain.invoke(\"\"\"Find five members and who their manager is.\"\"\")"
"chain.invoke({\"query\": \"Find five members and their managers.\"})"
]
},
{
Expand All @@ -343,7 +361,9 @@
"outputs": [],
"source": [
"chain.invoke(\n",
" \"\"\"Find org units or suborganizations of The Mega Group. What are the sites of those units?\"\"\"\n",
" {\n",
" \"query\": \"Find org units or suborganizations of The Mega Group. What are the sites of those units?\"\n",
" }\n",
")"
]
}
Expand Down
21 changes: 16 additions & 5 deletions docs/docs/integrations/providers/aws.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -310,23 +310,34 @@ from langchain_community.chat_message_histories import DynamoDBChatMessageHistor

## Graphs

### Amazon Neptune

>[Amazon Neptune](https://aws.amazon.com/neptune/)
> is a high-performance graph analytics and serverless database for superior scalability and availability.
For the Cypher and SPARQL integrations below, we need to install the `langchain-aws` library.

```bash
pip install langchain-aws
```

### Amazon Neptune with Cypher

See a [usage example](/docs/integrations/graphs/amazon_neptune_open_cypher).

```python
from langchain_community.graphs import NeptuneGraph
from langchain_community.graphs import NeptuneAnalyticsGraph
from langchain_community.chains.graph_qa.neptune_cypher import NeptuneOpenCypherQAChain
from langchain_aws.graphs import NeptuneGraph
from langchain_aws.graphs import NeptuneAnalyticsGraph
from langchain_aws.chains import create_neptune_opencypher_qa_chain
```

### Amazon Neptune with SPARQL

See a [usage example](/docs/integrations/graphs/amazon_neptune_sparql).

```python
from langchain_community.graphs import NeptuneRdfGraph
from langchain_community.chains.graph_qa.neptune_sparql import NeptuneSparqlQAChain
from langchain_aws.graphs import NeptuneRdfGraph
from langchain_aws.chains import create_neptune_sparql_qa_chain
```


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from langchain.chains.base import Chain
from langchain.chains.llm import LLMChain
from langchain.chains.prompt_selector import ConditionalPromptSelector
from langchain_core._api.deprecation import deprecated
from langchain_core.callbacks import CallbackManagerForChainRun
from langchain_core.language_models import BaseLanguageModel
from langchain_core.prompts.base import BasePromptTemplate
Expand Down Expand Up @@ -82,6 +83,11 @@ def use_simple_prompt(llm: BaseLanguageModel) -> bool:
)


@deprecated(
since="0.3.15",
removal="1.0",
alternative_import="langchain_aws.create_neptune_opencypher_qa_chain",
)
class NeptuneOpenCypherQAChain(Chain):
"""Chain for question-answering against a Neptune graph
by generating openCypher statements.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from langchain.chains.base import Chain
from langchain.chains.llm import LLMChain
from langchain_core._api.deprecation import deprecated
from langchain_core.callbacks.manager import CallbackManagerForChainRun
from langchain_core.language_models import BaseLanguageModel
from langchain_core.prompts.base import BasePromptTemplate
Expand Down Expand Up @@ -75,6 +76,11 @@ def extract_sparql(query: str) -> str:
return query


@deprecated(
since="0.3.15",
removal="1.0",
alternative_import="langchain_aws.create_neptune_sparql_qa_chain",
)
class NeptuneSparqlQAChain(Chain):
"""Chain for question-answering against a Neptune graph
by generating SPARQL statements.
Expand Down
12 changes: 12 additions & 0 deletions libs/community/langchain_community/graphs/neptune_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from abc import ABC, abstractmethod
from typing import Any, Dict, List, Optional, Tuple, Union

from langchain_core._api.deprecation import deprecated


class NeptuneQueryException(Exception):
"""Exception for the Neptune queries."""
Expand Down Expand Up @@ -139,6 +141,11 @@ def _refresh_schema(self) -> None:
"""


@deprecated(
since="0.3.15",
removal="1.0",
alternative_import="langchain_aws.NeptuneAnalyticsGraph",
)
class NeptuneAnalyticsGraph(BaseNeptuneGraph):
"""Neptune Analytics wrapper for graph operations.
Expand Down Expand Up @@ -269,6 +276,11 @@ def _get_summary(self) -> Dict:
return summary


@deprecated(
since="0.3.15",
removal="1.0",
alternative_import="langchain_aws.NeptuneGraph",
)
class NeptuneGraph(BaseNeptuneGraph):
"""Neptune wrapper for graph operations.
Expand Down
Loading

0 comments on commit 6121005

Please sign in to comment.