Index Network Documentation
  • Overview
    • What is Index Network
    • Architecture
  • Getting Started
    • Quick start
    • Data Models
  • API Reference
    • Identity
      • Authentication
      • Profile
    • Semantic Index
      • Index
        • Creator Roles
        • Privacy
      • IndexItem
      • Embedding
    • Discovery Protocol
      • Completions
      • Private Conversations
      • Semantic Listener
      • Vector Search
  • Resources
    • Libraries
      • Node.js SDK
      • Python SDK
      • React SDK
      • Langchain
  • More
    • Contact
Powered by GitBook
On this page
  • Using Index Client SDK
  • Installation
  • Usage
  • Creating an Index
  • Using Custom Schemas
  • Using Your Model
  • Interact with your index
Edit on GitHub
  1. Getting Started

Quick start

Last updated 10 months ago

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.

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.

represents a graph node within an Index. It provides a standardized approach to representing and managing various types of data.

Installation

First, install the sdk via your package manager:

|

Usage

Next, import it in your project:

import IndexClient from "@indexnetwork/sdk"
from indexnetwork-sdk import IndexClient

Create an instance of IndexClient:

const indexClient = new IndexClient({
  network: "dev", // Or mainnet
  wallet, // Wallet that interacts
});
index_client = IndexClient(
    wallet=your_wallet_object,  # Provide your wallet instance
    network="dev"  # Or mainnet
)
await indexClient.authenticate(); 
index_client.authenticate()

Creating an Index

We're ready. Now, let's create an Index with a title.

const indexId = await indexClient.createIndex("Future of publishing");
index_id = client.create_index("Future of publishing")
const webPageId = await indexClient.crawlWebPage("http://www.paulgraham.com/publishing.html");

await indexClient.addItemToIndex(indexId, webPageId);
web_page = index_client.crawl_web_page("http://www.paulgraham.com/publishing.html")
index_client.add_item(index_id, web_page["id"])

Using Custom Schemas

If you want to use your own schema, you can do so by creating and deploying a custom model. Below are the methods and examples of how to use them.

Creating a Custom Model

Use the createModel method to create a custom model using a GraphQL schema.

const modelResponse = await indexClient.createModel(`
  type CustomObject {
    title: String! @string(maxLength: 50)
  }

  type YourModel @createModel(accountRelation: LIST, description: "Full schema for models") {
    id: ID!
    booleanValue: Boolean!
    intValue: Int!
    floatValue: Float!
    did: DID!
    streamId: StreamID!
    commitId: CommitID!
    cid: CID!
    chainId: ChainID!
    accountId: AccountID!
    uri: URI! @string(maxLength: 2000)
    date: Date!
    dateTime: DateTime!
    time: Time!
    localDate: LocalDate!
    localTime: LocalTime!
    timeZone: TimeZone!
    utcOffset: UTCOffset!
    duration: Duration!
    stringValue: String! @string(maxLength: 10)
    objectArray: [CustomObject!] @list(maxLength: 30)
    singleObject: CustomObject
  }
`);
graphQLSchema = """
type CustomObject {
    title: String! @string(maxLength: 50)
}

type YourModel @createModel(accountRelation: LIST, description: "Full schema for models") {
    id: ID!
    booleanValue: Boolean!
    intValue: Int!
    floatValue: Float!
    did: DID!
    streamId: StreamID!
    commitId: CommitID!
    cid: CID!
    chainId: ChainID!
    accountId: AccountID!
    uri: URI! @string(maxLength: 2000)
    date: Date!
    dateTime: DateTime!
    time: Time!
    localDate: LocalDate!
    localTime: LocalTime!
    timeZone: TimeZone!
    utcOffset: UTCOffset!
    duration: Duration!
    stringValue: String! @string(maxLength: 10)
    objectArray: [CustomObject!] @list(maxLength: 30)
    singleObject: CustomObject
}
"""

model_response = index_client.create_model(graphQLSchema)

Deploying a Custom Model

After creating a custom model, use the deployModel method to deploy it.

await indexClient.deployModel(modelResponse.models[0]);
index_client.deploy_model(model_response["models"][0]["id"])

Using Your Model

To use it, create a node with your model and required data.

const sampleNodeData = { } // Fill with your data

const createdNode = await indexClient.createNode(
  modelResponse.models[0],
  sampleNodeData,
);

const newIndex = await indexClient.createIndex("Index with your model");

const addedItem = await indexClient.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 conversation
const conversationParams = {
  sources: [index.id],
  summary: "Mock summary",
};
const conversation = await indexClient.createConversation(conversationParams);

// Add a message to the conversation
const messageParams = {
  role: "user",
  content: "How do you do this?",
};
const message = await indexClient.createMessage(conversation.id, messageParams);

// Retrieve messages from the conversation
const { messages } = await indexClient.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:

const conversationId = "your-conversation-id";

const handleMessage = (data: any) => {
  console.log("New message received:", data);
  // Handle the new message data
};

const handleError = (error: any) => {
  console.error("Error receiving updates:", error);
  // Handle the error
};

const stopListening = indexClient.listenToConversationUpdates(
  conversationId,
  handleMessage,
  handleError,
);
conversation_id = "your-conversation-id"

def handle_message(data):
  print("New message received:", data)

def handle_error(error):
  print("Error receiving updates:", error)

index_client.listen_to_conversation_updates(
  conversation_id=conversation_id,
  handle_message=handle_message,
  handle_error=handle_error
)

Listening to Index Updates

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:

const sources = ["did:pkh:eip155:1:0x1b9Aceb609a62bae0c0a9682A9268138Faff4F5f"];

const query = "if it is relevant to decentralized AI";

const handleMessage = (data: any) => {
  console.log("New event received:", data);
  // Handle the new message data
};

const handleError = (error: any) => {
  console.error("Error receiving updates:", error);
  // Handle the error
};

const stopListening = indexClient.listenToIndexUpdates(
  sources,
  query
  handleMessage,
  handleError,
);
sources = ["did:pkh:eip155:1:0x1b9Aceb609a62bae0c0a9682A9268138Faff4F5f"]
query = "if it is relevant to decentralized AI"

def handle_message(data):
  print("New event received:", data)

def handle_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
)

Authenticate it. Check out for details and explanation on how to initiate a session.

Now you have a truly decentralized index to interact with! Though it's empty, which means we need to create and add an into it so we can interact. Let's do that.

Index
Item
Node.js
Python
Authentication
Item