Index Network API allows you to interact with our platform and build products leveraging our services. Below, you will find detailed information about the available endpoints, their purposes, and how to use them.
You can either use the API directly or the client available. Here is a quick start to discover it.
Using Index Client SDK
The Index Network offers an SDK to facilitate various operations on our platform. In this example, we'll demonstrate how to authenticate, create an Index, and add an Item to it.
Index is a fundamental component of the Index Network, designed to facilitate context management and enable semantic interoperability within your system. It serves as a structured way to organize and store data related to specific contexts.
Item represents a graph node within an Index. It provides a standardized approach to representing and managing various types of data.
constindexClient=newIndexClient({ network:"dev",// Or mainnet wallet,// Wallet that interacts});
index_client =IndexClient( wallet=your_wallet_object, # Provide your wallet instance network="dev"# Or mainnet)
Authenticate it. Check out Authentication for details and explanation on how to initiate a session.
awaitindexClient.authenticate();
index_client.authenticate()
Creating an Index
We're ready. Now, let's create an Index with a title.
constindexId=awaitindexClient.createIndex("Future of publishing");
index_id = client.create_index("Future of publishing")
Now you have a truly decentralized index to interact with! Though it's empty, which means we need to create and add an Item into it so we can interact. Let's do that.
To use it, create a node with your model and required data.
constsampleNodeData= { } // Fill with your dataconstcreatedNode=awaitindexClient.createNode(modelResponse.models[0], sampleNodeData,);constnewIndex=awaitindexClient.createIndex("Index with your model");constaddedItem=awaitindexClient.addItemToIndex(newIndex.id,createdNode.id,);
message ="hello world"print(message)
Interact with your index
Your index is now ready for interaction! To start a conversation and interact with the data, follow these steps:
// Create a conversationconstconversationParams= { sources: [index.id], summary:"Mock summary",};constconversation=awaitindexClient.createConversation(conversationParams);// Add a message to the conversationconstmessageParams= { role:"user", content:"How do you do this?",};constmessage=awaitindexClient.createMessage(conversation.id, messageParams);// Retrieve messages from the conversationconst { messages } =awaitindexClient.getConversation(conversation.id);
conversation_params ={"sources": [index["id"]],"summary":"Mock summary"}conversation = index_client.create_conversation(conversation_params)message_params ={"role":"user","content":"How do you do this?"}message = index_client.create_message(conversation["id"], message_params)messages = index_client.get_conversation(conversation["id"])print("Retrieved Messages:", messages)
The response should look something like this:
{"id": "message-id","content": "How do you do this?","role": "user","createdAt": "timestamp"}
Listening to Conversation Updates
The Index Client SDK allows you to listen for updates to a conversation in real-time. This is useful for applications that need to react to new messages or changes in a conversation.
Here is an example of how you can use the listenToConversationUpdates method to handle real-time updates in a conversation:
constconversationId="your-conversation-id";consthandleMessage= (data:any) => {console.log("New message received:", data);// Handle the new message data};consthandleError= (error:any) => {console.error("Error receiving updates:", error);// Handle the error};conststopListening=indexClient.listenToConversationUpdates( conversationId, handleMessage, handleError,);
The Index Client SDK allows you to listen for updates to miltiple indexes in real-time. This is useful for applications that need to react to new data events, using natural language.
Here is an example of how you can use the listenToIndexUpdates method to handle real-time updates in a conversation:
constsources= ["did:pkh:eip155:1:0x1b9Aceb609a62bae0c0a9682A9268138Faff4F5f"];constquery="if it is relevant to decentralized AI";consthandleMessage= (data:any) => {console.log("New event received:", data);// Handle the new message data};consthandleError= (error:any) => {console.error("Error receiving updates:", error);// Handle the error};conststopListening=indexClient.listenToIndexUpdates( sources, query handleMessage, handleError,);
sources = ["did:pkh:eip155:1:0x1b9Aceb609a62bae0c0a9682A9268138Faff4F5f"]query ="if it is relevant to decentralized AI"defhandle_message(data):print("New event received:", data)defhandle_error(error):print("Error receiving updates:", error)index_client.listen_to_index_updates( sources=sources, query=query, handle_message=handle_message, handle_error=handle_error)