Skip to content

Commit

Permalink
Update GraphQueryLLM sample notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelnchin committed Dec 12, 2024
1 parent fb66141 commit 4cc257e
Showing 1 changed file with 73 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,86 +10,82 @@
"## Querying the Network Graph with Natural language\n",
"\n",
"This notebook shows how you can ask questions in natural language about the data in the graph database using the Neptune\n",
"Langchain integration and Amazon Bedrock anthropic claude v2 model."
"Langchain integration and Amazon Bedrock Anthropic Claude 3.5v2 model."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "60a09cea-ae76-45d0-9dc1-860fc3ad833f",
"metadata": {
"tags": []
},
"source": "!pip install langchain langchain-community langchain-aws --quiet",
"outputs": [],
"source": [
"!pip install langchain --quiet"
]
"execution_count": null
},
{
"cell_type": "code",
"execution_count": null,
"id": "fb336cc7-2316-43a3-ac3d-6984b08ef228",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import warnings\n",
"import langchain\n",
"# langchain.__version__\n",
"import boto3\n",
"from langchain.chat_models import BedrockChat\n",
"from langchain_aws import ChatBedrock\n",
"from langchain.chains import NeptuneOpenCypherQAChain\n",
"from langchain.graphs import NeptuneGraph"
]
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"execution_count": null,
"id": "2a4b753d-6e9c-4f62-86b5-259165ff9f91",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"host = \"<your_neptune_host_endpoint>\" # e.g. cluter.cluster-xxxxxxxx.region.neptune.amazonaws.com\n",
"host = \"<your_neptune_host_endpoint>\" # e.g. cluster.cluster-xxxxxxxx.region.neptune.amazonaws.com\n",
"port = 8182\n",
"use_https = True"
]
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"execution_count": null,
"id": "4bc8717c-1d03-442d-9517-6adaa8768d09",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"graph = NeptuneGraph(host=host, port=port, use_https=use_https)"
]
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"execution_count": null,
"id": "4e5bd3a5-02d5-4cc6-8caa-df08659aaed7",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"client = boto3.client('bedrock-runtime', region_name='us-west-2')\n",
"\n",
"\n",
"llm = BedrockChat(\n",
" model_id = \"anthropic.claude-v2\",\n",
" client = client\n",
"llm = ChatBedrock(\n",
" model_id = \"anthropic.claude-3-5-sonnet-20241022-v2:0\"\n",
")\n",
"\n",
"chain = NeptuneOpenCypherQAChain.from_llm(\n",
" llm = llm, graph = graph, verbose = True, top_K = 10, return_intermediate_steps=True, return_direct=False\n",
" llm = llm, \n",
" graph = graph, \n",
" verbose = True, \n",
" top_K = 10, \n",
" return_intermediate_steps=True, \n",
" return_direct=False,\n",
" allow_dangerous_requests=True\n",
")"
]
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
Expand All @@ -101,119 +97,128 @@
},
{
"cell_type": "code",
"execution_count": null,
"id": "845025be-87a9-4aaa-8855-8b0bb17010ad",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"chain.run(\"how many edges between the users and the cells in the graph?\")"
]
"res = chain.invoke(\"How many edges between the users and the cells in the graph?\")\n",
"print(res['result'])"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"execution_count": null,
"id": "6e8cfaf2-761a-48d3-b704-e058a21d679b",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"chain.run(\"how many vertexes of type user\")"
]
"res = chain.run(\"How many vertexes of type user?\")\n",
"print(res['result'])"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"execution_count": null,
"id": "5b4dabd3-6369-4853-a767-359babef7be1",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"chain.run(\"how many nodes of type cell\")"
]
"res = chain.run(\"How many nodes of type cell?\")\n",
"print(res['result'])"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"execution_count": null,
"id": "1f53149c-23ca-4702-a32d-33f4e4a662b6",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"chain.run(\"how many vertexes of type gnodeb\")"
]
"res = chain.run(\"How many vertexes of type gnodeb?\")\n",
"print(res['result'])"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"execution_count": null,
"id": "0d658504-0c87-4fe5-8e7c-0581d828f5b8",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"chain.run(\"list the two types of edges\")"
]
"res = chain.run(\"List the two types of edges.\")\n",
"print(res['result'])"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"execution_count": null,
"id": "69b7fc44-b5a1-464f-b414-363ac7e48ac1",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"chain.run(\"count the edges between the cell and gnodeb ?\")"
]
"res = chain.run(\"Count the edges between the cell and gnodeb.\")\n",
"print(res['result'])"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"execution_count": null,
"id": "75711c07-ee2b-44c1-be42-bd2bd514c622",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"chain.run(\"count how many edges for cell_relatedto_gnodeb and user_live_cell\")"
]
"res = chain.run(\"Count how many edges there are for cell_relatedto_gnodeb and user_live_cell.\")\n",
"print(res['result'])"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"execution_count": null,
"id": "dafb88cc-6969-45ec-bc6c-4329dd5ca092",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"chain.run(\"what are the types of vertex ?\")"
]
"res = chain.run(\"What are the types of vertexes?\")\n",
"print(res['result'])"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"execution_count": null,
"id": "acb64772-b929-45dd-8402-4ba82ae93cfe",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"chain.run(\"to which cells the user_1500 is connceted\")"
]
"res = chain.run(\"To which cells is user_1500 connected?\")\n",
"print(res['result'])"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"execution_count": null,
"id": "aee5eb2b-c543-4b86-94bb-86254c097fa6",
"metadata": {},
"source": [],
"outputs": [],
"source": []
"execution_count": null
}
],
"metadata": {
Expand Down

0 comments on commit 4cc257e

Please sign in to comment.