diff --git a/.gitignore b/.gitignore index 567994ceb..8aff5e694 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ __pycache__ *.egg-info/ +venv diff --git a/comps/knowledgegraphs/langchain/README.md b/comps/knowledgegraphs/langchain/README.md index d4af41e8a..8f0c747fd 100644 --- a/comps/knowledgegraphs/langchain/README.md +++ b/comps/knowledgegraphs/langchain/README.md @@ -8,139 +8,10 @@ The service contains three modes: - "rag": Apply similarity search on embeddings of knowledge graph - "query": An LLM agent will automatically choose tools (RAG or CypherChain) to enhance the question answering -Here is the overall workflow: +# Knowledge Graph Microservice with Neo4J -![Workflow](doc/workflow.png) +For details, please refer to this [readme](langchain/neo4j/README.md) -A prerequisite for using this microservice is that users must have a knowledge gragh database already running, and currently we have support [Neo4J](https://neo4j.com/) for quick deployment. Users need to set the graph service's endpoint into an environment variable and microservie utilizes it for data injestion and retrieve. If user want to use "rag" and "query" mode, still need a LLM text generation service (etc., TGI, vLLM and Ray) already running. +# Knowledge Graph Microservice with FalkorDB -Overall, this microservice provides efficient support for applications related with graph dataset, especially for answering multi-part questions, or any other conditions including comples relationship between entities. - -## 🚀1. Start Microservice with Docker - -### 1.1 Setup Environment Variables - -```bash -export NEO4J_ENDPOINT="neo4j://${your_ip}:7687" -export NEO4J_USERNAME="neo4j" -export NEO4J_PASSWORD=${define_a_password} -export HUGGINGFACEHUB_API_TOKEN=${your_huggingface_api_token} -export LLM_ENDPOINT="http://${your_ip}:8080" -export LLM_MODEL="meta-llama/Llama-2-7b-hf" -export AGENT_LLM="HuggingFaceH4/zephyr-7b-beta" -``` - -### 1.2 Start Neo4j Service - -```bash -docker pull neo4j - -docker run --rm \ - --publish=7474:7474 --publish=7687:7687 \ - --env NEO4J_AUTH=$NEO4J_USER/$NEO4J_PASSWORD \ - --volume=$PWD/neo4j_data:"/data" \ - --env='NEO4JLABS_PLUGINS=["apoc"]' \ - neo4j -``` - -### 1.3 Start LLM Service for "rag"/"query" mode - -You can start any LLM microserve, here we take TGI as an example. - -```bash -docker run -p 8080:80 \ - -v $PWD/llm_data:/data --runtime=habana \ - -e HABANA_VISIBLE_DEVICES=all \ - -e OMPI_MCA_btl_vader_single_copy_mechanism=none \ - -e HUGGING_FACE_HUB_TOKEN=$HUGGINGFACEHUB_API_TOKEN \ - --cap-add=sys_nice \ - --ipc=host \ - ghcr.io/huggingface/tgi-gaudi:2.0.0 \ - --model-id $LLM_MODEL \ - --max-input-tokens 1024 \ - --max-total-tokens 2048 -``` - -Verify LLM service. - -```bash -curl $LLM_ENDPOINT/generate \ - -X POST \ - -d '{"inputs":"What is Deep Learning?","parameters":{"max_new_tokens":32}}' \ - -H 'Content-Type: application/json' -``` - -### 1.4 Start Microservice - -```bash -cd ../.. -docker build -t opea/knowledge_graphs:latest \ - --build-arg https_proxy=$https_proxy \ - --build-arg http_proxy=$http_proxy \ - -f comps/knowledgegraphs/langchain/Dockerfile . - -docker run --rm \ - --name="knowledge-graph-server" \ - -p 8060:8060 \ - --ipc=host \ - -e http_proxy=$http_proxy \ - -e https_proxy=$https_proxy \ - -e NEO4J_ENDPOINT=$NEO4J_ENDPOINT \ - -e NEO4J_USERNAME=$NEO4J_USERNAME \ - -e NEO4J_PASSWORD=$NEO4J_PASSWORD \ - -e HUGGINGFACEHUB_API_TOKEN=$HUGGINGFACEHUB_API_TOKEN \ - -e LLM_ENDPOINT=$LLM_ENDPOINT \ - opea/knowledge_graphs:latest -``` - -## 🚀2. Consume Knowledge Graph Service - -### 2.1 Cypher mode - -```bash -curl http://${your_ip}:8060/v1/graphs \ - -X POST \ - -d "{\"text\":\"MATCH (t:Task {status:'open'}) RETURN count(*)\",\"strtype\":\"cypher\"}" \ - -H 'Content-Type: application/json' -``` - -Example output: -![Cypher Output](doc/output_cypher.png) - -### 2.2 Rag mode - -```bash -curl http://${your_ip}:8060/v1/graphs \ - -X POST \ - -d "{\"text\":\"How many open tickets there are?\",\"strtype\":\"rag\", \"max_new_tokens\":128}" \ - -H 'Content-Type: application/json' -``` - -Example output: -![Cypher Output](doc/output_rag.png) - -### 2.3 Query mode - -First example: - -```bash -curl http://${your_ip}:8060/v1/graphs \ - -X POST \ - -d "{\"text\":\"Which tasks have optimization in their description?\",\"strtype\":\"query\"}" \ - -H 'Content-Type: application/json' -``` - -Example output: -![Cypher Output](doc/output_query1.png) - -Second example: - -```bash -curl http://${your_ip}:8060/v1/graphs \ - -X POST \ - -d "{\"text\":\"Which team is assigned to maintain PaymentService?\",\"strtype\":\"query\"}" \ - -H 'Content-Type: application/json' -``` - -Example output: -![Cypher Output](doc/output_query2.png) +For details, please refer to this [readme](langchain/falkordb/README.md) diff --git a/comps/knowledgegraphs/langchain/falkordb/README.md b/comps/knowledgegraphs/langchain/falkordb/README.md new file mode 100755 index 000000000..302e58c1b --- /dev/null +++ b/comps/knowledgegraphs/langchain/falkordb/README.md @@ -0,0 +1,135 @@ +# Knowledge Graph Microservice + +This microservice, designed for efficiently handling and retrieving informantion from knowledge graph. The microservice integrates text retriever, knowledge graph quick search and LLM agent, which can be combined to enhance question answering. + +The service contains three modes: + +- "cypher": Query knowledge graph directly with cypher +- "query": An LLM agent will automatically choose tools (RAG or CypherChain) to enhance the question answering + +Here is the overall workflow: + +![Workflow](doc/workflow.png) + +A prerequisite for using this microservice is that users must have a knowledge gragh database already running, and currently we have support [FalkorDB](https://falkordb.com/) for quick deployment. Users need to set the graph service's endpoint into an environment variable and microservie utilizes it for data injestion and retrieve. If user want to use "query" mode, still need a LLM text generation service (etc., TGI, vLLM and Ray) already running. + +Overall, this microservice provides efficient support for applications related with graph dataset, especially for answering multi-part questions, or any other conditions including comples relationship between entities. + +# 🚀1. Start Microservice with Docker + +## 1.1 Setup Environment Variables + +```bash +export FALKORDB_HOST="${your_ip}" +export FALKORDB_PORT="6379" +export FALKORDB_DATABASE="falkordb" +export FALKORDB_USERNAME="" +export FALKORDB_PASSWORD=${define_a_password} +export HUGGINGFACEHUB_API_TOKEN=${your_huggingface_api_token} +export LLM_ENDPOINT="http://${your_ip}:8080" +export LLM_MODEL="meta-llama/Llama-2-7b-hf" +export AGENT_LLM="HuggingFaceH4/zephyr-7b-beta" +``` + +## 1.2 Start FalkorDB Service + +```bash +docker pull falkordb/falkordb + +docker run --rm \ + --publish=3000:3000 --publish=6379:6379 \ + --volume=$PWD/falkordb_data:"/data" \ + falkordb/falkordb +``` + +## 1.3 Start LLM Service for "query" mode + +You can start any LLM microserve, here we take TGI as an example. + +```bash +docker run -p 8080:80 \ + -v $PWD/llm_data:/data --runtime=habana \ + -e HABANA_VISIBLE_DEVICES=all \ + -e OMPI_MCA_btl_vader_single_copy_mechanism=none \ + -e HUGGING_FACE_HUB_TOKEN=$HUGGINGFACEHUB_API_TOKEN \ + --cap-add=sys_nice \ + --ipc=host \ + ghcr.io/huggingface/tgi-gaudi:2.0.0 \ + --model-id $LLM_MODEL \ + --max-input-tokens 1024 \ + --max-total-tokens 2048 +``` + +Verify LLM service. + +```bash +curl $LLM_ENDPOINT/generate \ + -X POST \ + -d '{"inputs":"What is Deep Learning?","parameters":{"max_new_tokens":32}}' \ + -H 'Content-Type: application/json' +``` + +## 1.4 Start Microservice + +```bash +cd ../.. +docker build -t opea/knowledge_graphs:latest \ + --build-arg https_proxy=$https_proxy \ + --build-arg http_proxy=$http_proxy \ + -f comps/knowledgegraphs/langchain/docker/Dockerfile . + +docker run --rm \ + --name="knowledge-graph-server" \ + -p 8060:8060 \ + --ipc=host \ + -e http_proxy=$http_proxy \ + -e https_proxy=$https_proxy \ + -e FALKORDB_HOST=$FALKORDB_HOST \ + -e FALKORDB_PORT=$FALKORDB_PORT \ + -e FALKORDB_DATABASE=$FALKORDB_DATABASE \ + -e FALKORDB_USERNAME=$FALKORDB_USERNAME \ + -e FALKORDB_PASSWORD=$FALKORDB_PASSWORD \ + -e HUGGINGFACEHUB_API_TOKEN=$HUGGINGFACEHUB_API_TOKEN \ + -e LLM_ENDPOINT=$LLM_ENDPOINT \ + opea/knowledge_graphs:latest +``` + +# 🚀2. Consume Knowledge Graph Service + +## 2.1 Cypher mode + +```bash +curl http://${your_ip}:8060/v1/graphs \ + -X POST \ + -d "{\"text\":\"MATCH (t:Task {status:'open'}) RETURN count(*)\",\"strtype\":\"cypher\"}" \ + -H 'Content-Type: application/json' +``` + +Example output: +![Cypher Output](doc/output_cypher.png) + +## 2.3 Query mode + +First example: + +```bash +curl http://${your_ip}:8060/v1/graphs \ + -X POST \ + -d "{\"text\":\"Which tasks have optimization in their description?\",\"strtype\":\"query\"}" \ + -H 'Content-Type: application/json' +``` + +Example output: +![Cypher Output](doc/output_query1.png) + +Second example: + +```bash +curl http://${your_ip}:8060/v1/graphs \ + -X POST \ + -d "{\"text\":\"Which team is assigned to maintain PaymentService?\",\"strtype\":\"query\"}" \ + -H 'Content-Type: application/json' +``` + +Example output: +![Cypher Output](doc/output_query2.png) diff --git a/comps/knowledgegraphs/langchain/falkordb/__init__.py b/comps/knowledgegraphs/langchain/falkordb/__init__.py new file mode 100755 index 000000000..916f3a44b --- /dev/null +++ b/comps/knowledgegraphs/langchain/falkordb/__init__.py @@ -0,0 +1,2 @@ +# Copyright (C) 2024 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 diff --git a/comps/knowledgegraphs/langchain/falkordb/ingest.py b/comps/knowledgegraphs/langchain/falkordb/ingest.py new file mode 100755 index 000000000..373fb228e --- /dev/null +++ b/comps/knowledgegraphs/langchain/falkordb/ingest.py @@ -0,0 +1,23 @@ +# Copyright (C) 2024 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 + +import json +import os + +from langchain_community.graphs import FalkorDBGraph + +falkordb_host = os.getenv("FALKORDB_HOST", "localhost") +falkordb_port = int(os.getenv("FALKORDB_PORT", "6379")) +falkordb_database = int(os.getenv("FALKORDB_DATABASE", "falkordb")) +falkordb_username = os.getenv("FALKORDB_USERNAME", "") +falkordb_password = os.getenv("FALKORDB_PASSWORD", "") +graph = FalkorDBGraph(falkordb_database, falkordb_host, falkordb_port, falkordb_username, falkordb_password) + +# remove all nodes +graph.query("MATCH (n) DETACH DELETE n") + +# ingest +import_query = json.load(open("../data/microservices.json", "r"))["query"] +graph.query(import_query) +print("Total nodes: ", graph.query("MATCH (n) RETURN count(n)")) +print("Total edges: ", graph.query("MATCH ()-->() RETURN count(*)")) diff --git a/comps/knowledgegraphs/langchain/falkordb/knowledge_graph.py b/comps/knowledgegraphs/langchain/falkordb/knowledge_graph.py new file mode 100755 index 000000000..fe899733e --- /dev/null +++ b/comps/knowledgegraphs/langchain/falkordb/knowledge_graph.py @@ -0,0 +1,138 @@ +# Copyright (C) 2024 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 + +import os +import pathlib +import sys + +cur_path = pathlib.Path(__file__).parent.resolve() +comps_path = os.path.join(cur_path, "../../../") +sys.path.append(comps_path) +import json + +from langchain import hub +from langchain.agents import AgentExecutor, Tool +from langchain.agents.format_scratchpad import format_log_to_str +from langchain.agents.output_parsers import ReActJsonSingleInputOutputParser +from langchain.chains import GraphCypherQAChain +from langchain.tools.render import render_text_description +from langchain_community.chat_models.huggingface import ChatHuggingFace +from langchain_community.graphs import FalkorDBGraph +from langchain_community.llms import HuggingFaceEndpoint +from langchain_community.vectorstores.chroma import Chroma + +from comps import GeneratedDoc, GraphDoc, opea_microservices, register_microservice + + +def get_cypherchain(graph, cypher_llm, qa_llm): + graph.refresh_schema() + cypher_chain = GraphCypherQAChain.from_llm(cypher_llm=cypher_llm, qa_llm=qa_llm, graph=graph, verbose=True) + return cypher_chain + + +def get_agent(vector_qa, cypher_chain, llm_repo_id): + # define two tools + tools = [ + Tool( + name="Tasks", + func=vector_qa.invoke, + description="""Useful when you need to answer questions about descriptions of tasks. + Not useful for counting the number of tasks. + Use full question as input. + """, + ), + Tool( + name="Graph", + func=cypher_chain.invoke, + description="""Useful when you need to answer questions about microservices, + their dependencies or assigned people. Also useful for any sort of + aggregation like counting the number of tasks, etc. + Use full question as input. + """, + ), + ] + + # setup ReAct style prompt + prompt = hub.pull("hwchase17/react-json") + prompt = prompt.partial( + tools=render_text_description(tools), + tool_names=", ".join([t.name for t in tools]), + ) + + # define chat model + llm = HuggingFaceEndpoint(repo_id=llm_repo_id, max_new_tokens=512) + chat_model = ChatHuggingFace(llm=llm) + chat_model_with_stop = chat_model.bind(stop=["\nObservation"]) + + # define agent + agent = ( + { + "input": lambda x: x["input"], + "agent_scratchpad": lambda x: format_log_to_str(x["intermediate_steps"]), + } + | prompt + | chat_model_with_stop + | ReActJsonSingleInputOutputParser() + ) + + # instantiate AgentExecutor + agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True) + return agent_executor + + +@register_microservice( + name="opea_service@knowledge_graph_falkordb", + endpoint="/v1/graphs", + host="0.0.0.0", + port=8060, +) +def graph_query(input: GraphDoc) -> GeneratedDoc: + print(input) + + ## Connect to FalkorDB + falkordb_host = os.getenv("FALKORDB_HOST", "localhost") + falkordb_port = int(os.getenv("FALKORDB_PORT", "6379")) + falkordb_database = int(os.getenv("FALKORDB_DATABASE", "falkordb")) + falkordb_username = os.getenv("FALKORDB_USERNAME", "") + falkordb_password = os.getenv("FALKORDB_PASSWORD", "") + graph = FalkorDBGraph(falkordb_database, falkordb_host, falkordb_port, falkordb_username, falkordb_password) + + ## keep for multiple tests, will remove later + graph.query("MATCH (n) DETACH DELETE n") + import_query = json.load(open("data/microservices.json", "r"))["query"] + graph.query(import_query) + + ## get tool flag + flag_agent = True if input.strtype == "query" else False + + ## define LLM + if flag_agent: + llm_endpoint = os.getenv("LLM_ENDPOINT", "http://localhost:8080") + llm = HuggingFaceEndpoint( + endpoint_url=llm_endpoint, + timeout=600, + max_new_tokens=input.max_new_tokens, + ) + + ## define an agent + if flag_agent: + llm_repo_id = os.getenv("AGENT_LLM", "HuggingFaceH4/zephyr-7b-beta") + cypher_chain = get_cypherchain(graph, llm, llm) # define a cypher generator + agent_executor = get_agent(cypher_chain, llm_repo_id) + + ## process input query + if input.strtype == "cypher": + result_dicts = graph.query(input.text) + result = "" + for result_dict in result_dicts: + for key in result_dict: + result += str(key) + ": " + str(result_dict[key]) + elif input.strtype == "query": + result = agent_executor.invoke({"input": input.text})["output"] + else: + result = "Please specify strtype as one of cypher, rag, query." + return GeneratedDoc(text=result, prompt=input.text) + + +if __name__ == "__main__": + opea_microservices["opea_service@knowledge_graph"].start() diff --git a/comps/knowledgegraphs/langchain/neo4j/README.md b/comps/knowledgegraphs/langchain/neo4j/README.md new file mode 100755 index 000000000..248e46eba --- /dev/null +++ b/comps/knowledgegraphs/langchain/neo4j/README.md @@ -0,0 +1,146 @@ +# Knowledge Graph Microservice + +This microservice, designed for efficiently handling and retrieving informantion from knowledge graph. The microservice integrates text retriever, knowledge graph quick search and LLM agent, which can be combined to enhance question answering. + +The service contains three modes: + +- "cypher": Query knowledge graph directly with cypher +- "rag": Apply similarity search on embeddings of knowledge graph +- "query": An LLM agent will automatically choose tools (RAG or CypherChain) to enhance the question answering + +Here is the overall workflow: + +![Workflow](doc/workflow.png) + +A prerequisite for using this microservice is that users must have a knowledge gragh database already running, and currently we have support [Neo4J](https://neo4j.com/) for quick deployment. Users need to set the graph service's endpoint into an environment variable and microservie utilizes it for data injestion and retrieve. If user want to use "rag" and "query" mode, still need a LLM text generation service (etc., TGI, vLLM and Ray) already running. + +Overall, this microservice provides efficient support for applications related with graph dataset, especially for answering multi-part questions, or any other conditions including comples relationship between entities. + +# 🚀1. Start Microservice with Docker + +## 1.1 Setup Environment Variables + +```bash +export NEO4J_ENDPOINT="neo4j://${your_ip}:7687" +export NEO4J_USERNAME="neo4j" +export NEO4J_PASSWORD=${define_a_password} +export HUGGINGFACEHUB_API_TOKEN=${your_huggingface_api_token} +export LLM_ENDPOINT="http://${your_ip}:8080" +export LLM_MODEL="meta-llama/Llama-2-7b-hf" +export AGENT_LLM="HuggingFaceH4/zephyr-7b-beta" +``` + +## 1.2 Start Neo4j Service + +```bash +docker pull neo4j + +docker run --rm \ + --publish=7474:7474 --publish=7687:7687 \ + --env NEO4J_AUTH=$NEO4J_USER/$NEO4J_PASSWORD \ + --volume=$PWD/neo4j_data:"/data" \ + --env='NEO4JLABS_PLUGINS=["apoc"]' \ + neo4j +``` + +## 1.3 Start LLM Service for "rag"/"query" mode + +You can start any LLM microserve, here we take TGI as an example. + +```bash +docker run -p 8080:80 \ + -v $PWD/llm_data:/data --runtime=habana \ + -e HABANA_VISIBLE_DEVICES=all \ + -e OMPI_MCA_btl_vader_single_copy_mechanism=none \ + -e HUGGING_FACE_HUB_TOKEN=$HUGGINGFACEHUB_API_TOKEN \ + --cap-add=sys_nice \ + --ipc=host \ + ghcr.io/huggingface/tgi-gaudi:2.0.0 \ + --model-id $LLM_MODEL \ + --max-input-tokens 1024 \ + --max-total-tokens 2048 +``` + +Verify LLM service. + +```bash +curl $LLM_ENDPOINT/generate \ + -X POST \ + -d '{"inputs":"What is Deep Learning?","parameters":{"max_new_tokens":32}}' \ + -H 'Content-Type: application/json' +``` + +## 1.4 Start Microservice + +```bash +cd ../.. +docker build -t opea/knowledge_graphs:latest \ + --build-arg https_proxy=$https_proxy \ + --build-arg http_proxy=$http_proxy \ + -f comps/knowledgegraphs/langchain/docker/Dockerfile . + +docker run --rm \ + --name="knowledge-graph-server" \ + -p 8060:8060 \ + --ipc=host \ + -e http_proxy=$http_proxy \ + -e https_proxy=$https_proxy \ + -e NEO4J_ENDPOINT=$NEO4J_ENDPOINT \ + -e NEO4J_USERNAME=$NEO4J_USERNAME \ + -e NEO4J_PASSWORD=$NEO4J_PASSWORD \ + -e HUGGINGFACEHUB_API_TOKEN=$HUGGINGFACEHUB_API_TOKEN \ + -e LLM_ENDPOINT=$LLM_ENDPOINT \ + opea/knowledge_graphs:latest +``` + +# 🚀2. Consume Knowledge Graph Service + +## 2.1 Cypher mode + +```bash +curl http://${your_ip}:8060/v1/graphs \ + -X POST \ + -d "{\"text\":\"MATCH (t:Task {status:'open'}) RETURN count(*)\",\"strtype\":\"cypher\"}" \ + -H 'Content-Type: application/json' +``` + +Example output: +![Cypher Output](doc/output_cypher.png) + +## 2.2 Rag mode + +```bash +curl http://${your_ip}:8060/v1/graphs \ + -X POST \ + -d "{\"text\":\"How many open tickets there are?\",\"strtype\":\"rag\", \"max_new_tokens\":128}" \ + -H 'Content-Type: application/json' +``` + +Example output: +![Cypher Output](doc/output_rag.png) + +## 2.3 Query mode + +First example: + +```bash +curl http://${your_ip}:8060/v1/graphs \ + -X POST \ + -d "{\"text\":\"Which tasks have optimization in their description?\",\"strtype\":\"query\"}" \ + -H 'Content-Type: application/json' +``` + +Example output: +![Cypher Output](doc/output_query1.png) + +Second example: + +```bash +curl http://${your_ip}:8060/v1/graphs \ + -X POST \ + -d "{\"text\":\"Which team is assigned to maintain PaymentService?\",\"strtype\":\"query\"}" \ + -H 'Content-Type: application/json' +``` + +Example output: +![Cypher Output](doc/output_query2.png) diff --git a/comps/knowledgegraphs/langchain/neo4j/__init__.py b/comps/knowledgegraphs/langchain/neo4j/__init__.py new file mode 100755 index 000000000..916f3a44b --- /dev/null +++ b/comps/knowledgegraphs/langchain/neo4j/__init__.py @@ -0,0 +1,2 @@ +# Copyright (C) 2024 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 diff --git a/comps/knowledgegraphs/langchain/neo4j/doc/output_cypher.png b/comps/knowledgegraphs/langchain/neo4j/doc/output_cypher.png new file mode 100644 index 000000000..c77f54611 Binary files /dev/null and b/comps/knowledgegraphs/langchain/neo4j/doc/output_cypher.png differ diff --git a/comps/knowledgegraphs/langchain/neo4j/doc/output_query1.png b/comps/knowledgegraphs/langchain/neo4j/doc/output_query1.png new file mode 100644 index 000000000..cc470e1a5 Binary files /dev/null and b/comps/knowledgegraphs/langchain/neo4j/doc/output_query1.png differ diff --git a/comps/knowledgegraphs/langchain/neo4j/doc/output_query2.png b/comps/knowledgegraphs/langchain/neo4j/doc/output_query2.png new file mode 100644 index 000000000..af9e3dae9 Binary files /dev/null and b/comps/knowledgegraphs/langchain/neo4j/doc/output_query2.png differ diff --git a/comps/knowledgegraphs/langchain/neo4j/doc/output_rag.png b/comps/knowledgegraphs/langchain/neo4j/doc/output_rag.png new file mode 100644 index 000000000..25bb1e409 Binary files /dev/null and b/comps/knowledgegraphs/langchain/neo4j/doc/output_rag.png differ diff --git a/comps/knowledgegraphs/langchain/neo4j/doc/workflow.png b/comps/knowledgegraphs/langchain/neo4j/doc/workflow.png new file mode 100644 index 000000000..770870708 Binary files /dev/null and b/comps/knowledgegraphs/langchain/neo4j/doc/workflow.png differ diff --git a/comps/knowledgegraphs/langchain/ingest.py b/comps/knowledgegraphs/langchain/neo4j/ingest.py similarity index 89% rename from comps/knowledgegraphs/langchain/ingest.py rename to comps/knowledgegraphs/langchain/neo4j/ingest.py index 7e7e1d8b3..aaf608671 100755 --- a/comps/knowledgegraphs/langchain/ingest.py +++ b/comps/knowledgegraphs/langchain/neo4j/ingest.py @@ -15,7 +15,7 @@ graph.query("MATCH (n) DETACH DELETE n") # ingest -import_query = json.load(open("data/microservices.json", "r"))["query"] +import_query = json.load(open("../data/microservices.json", "r"))["query"] graph.query(import_query) print("Total nodes: ", graph.query("MATCH (n) RETURN count(n)")) print("Total edges: ", graph.query("MATCH ()-->() RETURN count(*)")) diff --git a/comps/knowledgegraphs/langchain/knowledge_graph.py b/comps/knowledgegraphs/langchain/neo4j/knowledge_graph.py similarity index 96% rename from comps/knowledgegraphs/langchain/knowledge_graph.py rename to comps/knowledgegraphs/langchain/neo4j/knowledge_graph.py index 3b5b2882e..5dda8c6b0 100755 --- a/comps/knowledgegraphs/langchain/knowledge_graph.py +++ b/comps/knowledgegraphs/langchain/neo4j/knowledge_graph.py @@ -10,9 +10,8 @@ sys.path.append(comps_path) import json -import requests from langchain import hub -from langchain.agents import AgentExecutor, Tool, load_tools +from langchain.agents import AgentExecutor, Tool from langchain.agents.format_scratchpad import format_log_to_str from langchain.agents.output_parsers import ReActJsonSingleInputOutputParser from langchain.chains import GraphCypherQAChain, RetrievalQA @@ -23,7 +22,7 @@ from langchain_community.llms import HuggingFaceEndpoint from langchain_community.vectorstores.neo4j_vector import Neo4jVector -from comps import CustomLogger, GeneratedDoc, GraphDoc, ServiceType, opea_microservices, register_microservice +from comps import CustomLogger, GeneratedDoc, GraphDoc, opea_microservices, register_microservice logger = CustomLogger("knowledge_graph") logflag = os.getenv("LOGFLAG", False) @@ -102,7 +101,7 @@ def get_agent(vector_qa, cypher_chain, llm_repo_id): @register_microservice( - name="opea_service@knowledge_graph", + name="opea_service@knowledge_graph_neo4j", endpoint="/v1/graphs", host="0.0.0.0", port=8060, diff --git a/comps/knowledgegraphs/langchain/requirements.txt b/comps/knowledgegraphs/langchain/requirements.txt index 39b351530..96899e537 100644 --- a/comps/knowledgegraphs/langchain/requirements.txt +++ b/comps/knowledgegraphs/langchain/requirements.txt @@ -2,6 +2,7 @@ beautifulsoup4 docarray docarray[full] easyocr +falkordb fastapi huggingface_hub langchain