Source code for langchain_community.graphs.neptune_rdf_graph
importjsonfromtypesimportSimpleNamespacefromtypingimportAny,Dict,Optional,Sequenceimportrequestsfromlangchain_core._api.deprecationimportdeprecated# Query to find OWL datatype propertiesDTPROP_QUERY="""SELECT DISTINCT ?elem WHERE { ?elem a owl:DatatypeProperty . }"""# Query to find OWL object propertiesOPROP_QUERY="""SELECT DISTINCT ?elem WHERE { ?elem a owl:ObjectProperty . }"""ELEM_TYPES={"classes":None,"rels":None,"dtprops":DTPROP_QUERY,"oprops":OPROP_QUERY,}
[docs]@deprecated(since="0.3.15",removal="1.0",alternative_import="langchain_aws.NeptuneRdfGraph",)classNeptuneRdfGraph:"""Neptune wrapper for RDF graph operations. Args: host: endpoint for the database instance port: port number for the database instance, default is 8182 use_iam_auth: boolean indicating IAM auth is enabled in Neptune cluster use_https: whether to use secure connection, default is True client: optional boto3 Neptune client credentials_profile_name: optional AWS profile name region_name: optional AWS region, e.g., us-west-2 service: optional service name, default is neptunedata sign: optional, whether to sign the request payload, default is True Example: .. code-block:: python graph = NeptuneRdfGraph( host='<SPARQL host'>, port=<SPARQL port> ) schema = graph.get_schema() OR graph = NeptuneRdfGraph( host='<SPARQL host'>, port=<SPARQL port> ) schema_elem = graph.get_schema_elements() #... change schema_elements ... graph.load_schema(schema_elem) *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. """
[docs]def__init__(self,host:str,port:int=8182,use_https:bool=True,use_iam_auth:bool=False,client:Any=None,credentials_profile_name:Optional[str]=None,region_name:Optional[str]=None,service:str="neptunedata",sign:bool=True,)->None:self.use_iam_auth=use_iam_authself.region_name=region_nameself.query_endpoint=f"https://{host}:{port}/sparql"try:ifclientisnotNone:self.client=clientelse:importboto3ifcredentials_profile_nameisnotNone:self.session=boto3.Session(profile_name=credentials_profile_name)else:# use default credentialsself.session=boto3.Session()client_params={}ifregion_name:client_params["region_name"]=region_nameprotocol="https"ifuse_httpselse"http"client_params["endpoint_url"]=f"{protocol}://{host}:{port}"ifsign:self.client=self.session.client(service,**client_params)else:frombotocoreimportUNSIGNEDfrombotocore.configimportConfigself.client=self.session.client(service,**client_params,config=Config(signature_version=UNSIGNED),)exceptImportError:raiseImportError("Could not import boto3 python package. ""Please install it with `pip install boto3`.")exceptExceptionase:iftype(e).__name__=="UnknownServiceError":raiseImportError("NeptuneGraph requires a boto3 version 1.28.38 or greater.""Please install it with `pip install -U boto3`.")fromeelse:raiseValueError("Could not load credentials to authenticate with AWS client. ""Please check that credentials in the specified ""profile name are valid.")frome# Set schemaself.schema=""self.schema_elements:Dict[str,Any]={}self._refresh_schema()
@propertydefget_schema(self)->str:""" Returns the schema of the graph database. """returnself.schema@propertydefget_schema_elements(self)->Dict[str,Any]:returnself.schema_elements
[docs]defget_summary(self)->Dict[str,Any]:""" Obtain Neptune statistical summary of classes and predicates in the graph. """returnself.client.get_rdf_graph_summary(mode="detailed")
[docs]defquery(self,query:str,)->Dict[str,Any]:""" Run Neptune query. """request_data={"query":query}data=request_datarequest_hdr=Noneifself.use_iam_auth:credentials=self.session.get_credentials()credentials=credentials.get_frozen_credentials()access_key=credentials.access_keysecret_key=credentials.secret_keyservice="neptune-db"session_token=credentials.tokenparams=Nonecreds=SimpleNamespace(access_key=access_key,secret_key=secret_key,token=session_token,region=self.region_name,)frombotocore.awsrequestimportAWSRequestrequest=AWSRequest(method="POST",url=self.query_endpoint,data=data,params=params)frombotocore.authimportSigV4AuthSigV4Auth(creds,service,self.region_name).add_auth(request)request.headers["Content-Type"]="application/x-www-form-urlencoded"request_hdr=request.headerselse:request_hdr={}request_hdr["Content-Type"]="application/x-www-form-urlencoded"queryres=requests.request(method="POST",url=self.query_endpoint,headers=request_hdr,data=data)json_resp=json.loads(queryres.text)returnjson_resp
[docs]defload_schema(self,schema_elements:Dict[str,Any])->None:""" Generates and sets schema from schema_elements. Helpful in cases where introspected schema needs pruning. """elem_str={}foreleminELEM_TYPES:res_list=[]forelem_recinschema_elements[elem]:uri=elem_rec["uri"]local=elem_rec["local"]res_str=f"<{uri}> ({local})"res_list.append(res_str)elem_str[elem]=", ".join(res_list)self.schema=("In the following, each IRI is followed by the local name and ""optionally its description in parentheses. \n""The graph supports the following node types:\n"f"{elem_str['classes']}\n""The graph supports the following relationships:\n"f"{elem_str['rels']}\n""The graph supports the following OWL object properties:\n"f"{elem_str['dtprops']}\n""The graph supports the following OWL data properties:\n"f"{elem_str['oprops']}")
def_get_local_name(self,iri:str)->Sequence[str]:""" Split IRI into prefix and local """if"#"iniri:tokens=iri.split("#")return[f"{tokens[0]}#",tokens[-1]]elif"/"iniri:tokens=iri.split("/")return[f"{'/'.join(tokens[0:len(tokens)-1])}/",tokens[-1]]else:raiseValueError(f"Unexpected IRI '{iri}', contains neither '#' nor '/'.")def_refresh_schema(self)->None:""" Query Neptune to introspect schema. """self.schema_elements["distinct_prefixes"]={}# get summary and build list of classes and relssummary=self.get_summary()reslist=[]forcinsummary["payload"]["graphSummary"]["classes"]:uri=ctokens=self._get_local_name(uri)elem_record={"uri":uri,"local":tokens[1]}reslist.append(elem_record)iftokens[0]notinself.schema_elements["distinct_prefixes"]:self.schema_elements["distinct_prefixes"][tokens[0]]="y"self.schema_elements["classes"]=reslistreslist=[]forrinsummary["payload"]["graphSummary"]["predicates"]:forpinr:uri=ptokens=self._get_local_name(uri)elem_record={"uri":uri,"local":tokens[1]}reslist.append(elem_record)iftokens[0]notinself.schema_elements["distinct_prefixes"]:self.schema_elements["distinct_prefixes"][tokens[0]]="y"self.schema_elements["rels"]=reslist# get dtprops and oprops tooforeleminELEM_TYPES:q=ELEM_TYPES.get(elem)ifnotq:continueitems=self.query(q)reslist=[]forrinitems["results"]["bindings"]:uri=r["elem"]["value"]tokens=self._get_local_name(uri)elem_record={"uri":uri,"local":tokens[1]}reslist.append(elem_record)iftokens[0]notinself.schema_elements["distinct_prefixes"]:self.schema_elements["distinct_prefixes"][tokens[0]]="y"self.schema_elements[elem]=reslistself.load_schema(self.schema_elements)