---
title: Azure Cosmos DB for Apache Gremlin
---

>[Azure Cosmos DB for Apache Gremlin](https://learn.microsoft.com/en-us/azure/cosmos-db/gremlin/introduction)은 수십억 개의 vertex와 edge를 가진 대규모 graph를 저장하는 데 사용할 수 있는 graph database 서비스입니다. 밀리초 단위의 지연 시간으로 graph를 쿼리하고 graph 구조를 쉽게 발전시킬 수 있습니다.
>
>[Gremlin](https://en.wikipedia.org/wiki/Gremlin_(query_language))은 `Apache Software Foundation``Apache TinkerPop`에서 개발한 graph traversal 언어이자 virtual machine입니다.

이 notebook은 LLM을 사용하여 `Gremlin` 쿼리 언어로 쿼리할 수 있는 graph database에 자연어 인터페이스를 제공하는 방법을 보여줍니다.

## 설정하기

라이브러리 설치:

```python
!pip3 install gremlinpython
Azure CosmosDB Graph database 인스턴스가 필요합니다. 한 가지 옵션은 Azure에서 무료 CosmosDB Graph database 인스턴스를 생성하는 것입니다. Cosmos DB 계정과 Graph를 생성할 때 partition key로 /type을 사용하세요.
cosmosdb_name = "mycosmosdb"
cosmosdb_db_id = "graphtesting"
cosmosdb_db_graph_id = "mygraph"
cosmosdb_access_Key = "longstring=="
import nest_asyncio
from langchain_community.chains.graph_qa.gremlin import GremlinQAChain
from langchain_community.graphs import GremlinGraph
from langchain_community.graphs.graph_document import GraphDocument, Node, Relationship
from langchain_core.documents import Document
from langchain_openai import AzureChatOpenAI
graph = GremlinGraph(
    url=f"wss://{cosmosdb_name}.gremlin.cosmos.azure.com:443/",
    username=f"/dbs/{cosmosdb_db_id}/colls/{cosmosdb_db_graph_id}",
    password=cosmosdb_access_Key,
)

Database에 데이터 채우기

database가 비어 있다고 가정하면 GraphDocuments를 사용하여 채울 수 있습니다. Gremlin의 경우 각 Node에 대해 항상 ‘label’이라는 property를 추가하세요. label이 설정되지 않으면 Node.type이 label로 사용됩니다. Cosmos의 경우 자연스러운 id를 사용하는 것이 합리적입니다. graph explorer에서 볼 수 있기 때문입니다.
source_doc = Document(
    page_content="Matrix is a movie where Keanu Reeves, Laurence Fishburne and Carrie-Anne Moss acted."
)
movie = Node(id="The Matrix", properties={"label": "movie", "title": "The Matrix"})
actor1 = Node(id="Keanu Reeves", properties={"label": "actor", "name": "Keanu Reeves"})
actor2 = Node(
    id="Laurence Fishburne", properties={"label": "actor", "name": "Laurence Fishburne"}
)
actor3 = Node(
    id="Carrie-Anne Moss", properties={"label": "actor", "name": "Carrie-Anne Moss"}
)
rel1 = Relationship(
    id=5, type="ActedIn", source=actor1, target=movie, properties={"label": "ActedIn"}
)
rel2 = Relationship(
    id=6, type="ActedIn", source=actor2, target=movie, properties={"label": "ActedIn"}
)
rel3 = Relationship(
    id=7, type="ActedIn", source=actor3, target=movie, properties={"label": "ActedIn"}
)
rel4 = Relationship(
    id=8,
    type="Starring",
    source=movie,
    target=actor1,
    properties={"label": "Strarring"},
)
rel5 = Relationship(
    id=9,
    type="Starring",
    source=movie,
    target=actor2,
    properties={"label": "Strarring"},
)
rel6 = Relationship(
    id=10,
    type="Straring",
    source=movie,
    target=actor3,
    properties={"label": "Strarring"},
)
graph_doc = GraphDocument(
    nodes=[movie, actor1, actor2, actor3],
    relationships=[rel1, rel2, rel3, rel4, rel5, rel6],
    source=source_doc,
)
# The underlying python-gremlin has a problem when running in notebook
# The following line is a workaround to fix the problem
nest_asyncio.apply()

# Add the document to the CosmosDB graph.
graph.add_graph_documents([graph_doc])

Graph schema 정보 새로고침

Database의 schema가 변경되면(업데이트 후) schema 정보를 새로고침할 수 있습니다.
graph.refresh_schema()
print(graph.schema)

Graph 쿼리하기

이제 gremlin QA chain을 사용하여 graph에 질문할 수 있습니다.
chain = GremlinQAChain.from_llm(
    AzureChatOpenAI(
        temperature=0,
        azure_deployment="gpt-4-turbo",
    ),
    graph=graph,
    verbose=True,
)
chain.invoke("Who played in The Matrix?")
chain.run("How many people played in The Matrix?")

---

<Callout icon="pen-to-square" iconType="regular">
    [Edit the source of this page on GitHub.](https://github.com/langchain-ai/docs/edit/main/src/oss/python/integrations/graphs/azure_cosmosdb_gremlin.mdx)
</Callout>
<Tip icon="terminal" iconType="regular">
    [Connect these docs programmatically](/use-these-docs) to Claude, VSCode, and more via MCP for    real-time answers.
</Tip>
I