[docs]classGremlinGraph(GraphStore):"""Gremlin wrapper for graph operations. Parameters: url (Optional[str]): The URL of the Gremlin database server or env GREMLIN_URI username (Optional[str]): The collection-identifier like '/dbs/database/colls/graph' or env GREMLIN_USERNAME if none provided password (Optional[str]): The connection-key for database authentication or env GREMLIN_PASSWORD if none provided traversal_source (str): The traversal source to use for queries. Defaults to 'g'. message_serializer (Optional[Any]): The message serializer to use for requests. Defaults to serializer.GraphSONSerializersV2d0() *Security note*: Make sure that the database connection uses credentials that are narrowly-scoped to only include necessary permissions. Failure to do so may result in data corruption or loss, since the calling code may attempt commands that would result in deletion, mutation of data if appropriately prompted or reading sensitive data if such data is present in the database. The best way to guard against such negative outcomes is to (as appropriate) limit the permissions granted to the credentials used with this tool. See https://python.langchain.com/docs/security for more information. *Implementation details*: The Gremlin queries are designed to work with Azure CosmosDB limitations """@propertydefget_structured_schema(self)->Dict[str,Any]:returnself.structured_schema
[docs]def__init__(self,url:Optional[str]=None,username:Optional[str]=None,password:Optional[str]=None,traversal_source:str="g",message_serializer:Optional[Any]=None,)->None:"""Create a new Gremlin graph wrapper instance."""try:importasynciofromgremlin_python.driverimportclient,serializerifsys.platform=="win32":asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())exceptImportError:raiseImportError("Please install gremlin-python first: ""`pip3 install gremlinpython")self.client=client.Client(url=get_from_env("url","GREMLIN_URI",url),traversal_source=traversal_source,username=get_from_env("username","GREMLIN_USERNAME",username),password=get_from_env("password","GREMLIN_PASSWORD",password),message_serializer=message_serializerifmessage_serializerelseserializer.GraphSONSerializersV2d0(),)self.schema:str=""
@propertydefget_schema(self)->str:"""Returns the schema of the Gremlin database"""iflen(self.schema)==0:self.refresh_schema()returnself.schema
[docs]defrefresh_schema(self)->None:""" Refreshes the Gremlin graph schema information. """vertex_schema=self.client.submit("g.V().label().dedup()").all().result()edge_schema=self.client.submit("g.E().label().dedup()").all().result()vertex_properties=(self.client.submit("g.V().group().by(label).by(properties().label().dedup().fold())").all().result()[0])self.structured_schema={"vertex_labels":vertex_schema,"edge_labels":edge_schema,"vertice_props":vertex_properties,}self.schema="\n".join(["Vertex labels are the following:",",".join(vertex_schema),"Edge labes are the following:",",".join(edge_schema),f"Vertices have following properties:\n{vertex_properties}",])
[docs]defadd_graph_documents(self,graph_documents:List[GraphDocument],include_source:bool=False)->None:""" Take GraphDocument as input as uses it to construct a graph. """node_cache:Dict[Union[str,int],Node]={}fordocumentingraph_documents:ifinclude_source:# Create document vertexdoc_props={"page_content":document.source.page_content,"metadata":document.source.metadata,}doc_id=hashlib.md5(document.source.page_content.encode()).hexdigest()doc_node=self.add_node(Node(id=doc_id,type="Document",properties=doc_props),node_cache)# Import nodes to verticesfornindocument.nodes:node=self.add_node(n)ifinclude_source:# Add Edge to document for each nodeself.add_edge(Relationship(type="contains information about",source=doc_node,target=node,properties={},))self.add_edge(Relationship(type="is extracted from",source=node,target=doc_node,properties={},))# Edgesforelindocument.relationships:# Find or create the source vertexself.add_node(el.source,node_cache)# Find or create the target vertexself.add_node(el.target,node_cache)# Find or create the edgeself.add_edge(el)
[docs]defadd_node(self,node:Node,node_cache:dict={})->Node:# if properties does not have label, add type as labelif"label"notinnode.properties:node.properties["label"]=node.typeifnode.idinnode_cache:returnnode_cache[node.id]else:query=self.build_vertex_query(node)_=self.client.submit(query).all().result()[0]node_cache[node.id]=nodereturnnode