# @indexnetwork/sdk

> ## Quick start

Latest version **0.1.26** (published 2024-07-22) · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @indexnetwork/sdk
pnpm add @indexnetwork/sdk
yarn add @indexnetwork/sdk
bun add @indexnetwork/sdk
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.1.26 |
| Published | 2024-07-22 |
| First published | 2024-03-18 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 15 |
| Unpacked size | 1.5 MB |
| Known vulnerabilities | 0 (+10 in 5 direct dependencies) |
| Install scripts | no |
| Maintainers | indexnetwork |

## Links

- npm: https://www.npmjs.com/package/@indexnetwork/sdk
- npm.io page: https://npm.io/package/@indexnetwork/sdk

## Dependencies (15)

- [uuid](https://npm.io/package/uuid.md) ^9.0.1
- [vite](https://npm.io/package/vite.md) ^5.2.13
- [ethers](https://npm.io/package/ethers.md) ^6.12.0
- [openai](https://npm.io/package/openai.md) ^4.38.5
- [langchain](https://npm.io/package/langchain.md) ^0.1.36
- [did-session](https://npm.io/package/did-session.md) ^3.1.0
- [eventsource](https://npm.io/package/eventsource.md) ^2.0.2
- [uint8arrays](https://npm.io/package/uint8arrays.md) ^5.1.0
- [multiformats](https://npm.io/package/multiformats.md) ^13.1.0
- [@didtools/cacao](https://npm.io/package/@didtools/cacao.md) ^3.0.1
- [@langchain/core](https://npm.io/package/@langchain/core.md) ^0.2.18
- [@langchain/openai](https://npm.io/package/@langchain/openai.md) ^0.0.28
- [cross-eventsource](https://npm.io/package/cross-eventsource.md) ^1.0.0
- [vite-tsconfig-paths](https://npm.io/package/vite-tsconfig-paths.md) ^4.3.2
- [@langchain/community](https://npm.io/package/@langchain/community.md) ^0.0.53

## Recent versions

- 0.1.26 (latest) — 2024-07-22
- 0.1.25 — 2024-07-22
- 0.1.24 — 2024-07-04
- 0.1.23 — 2024-07-04
- 0.1.22 — 2024-06-24
- 0.1.21 — 2024-06-24
- 0.1.20 — 2024-06-24
- 0.1.19 — 2024-06-24
- 0.1.18 — 2024-06-24
- 0.1.10 — 2024-06-24
- 0.1.9 — 2024-06-24
- 0.1.8 — 2024-06-24
- 0.1.7 — 2024-06-24
- 0.1.6 — 2024-06-24
- 0.1.5 — 2024-06-19
- … 22 more at https://npm.io/package/@indexnetwork/sdk/versions

## README

# Index Network Client SDK

## Quick start


Index is a discovery protocol that eliminates the need for intermediaries in finding knowledge, products, and like-minded people through direct, composable discovery across the web. As the first decentralized semantic index, it leverages Web3 and AI and offers an open layer for discovery.

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 the protocol. In this example, we'll demonstrate how to authenticate, create an Index, and add an Item to it.

> [**Index**](https://docs.index.network/docs/getting-started/data-models#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**](https://docs.index.network/docs/getting-started/data-models#indexitem) represents a graph node within an Index. It provides a standardized approach to representing and managing various types of data.

First, install the index-client via your preferred package manager:

```shell
yarn add @indexnetwork/sdk
```

Next, import it in your project:

```typescript
import IndexClient from "@indexnetwork/sdk";
```

Create an instance of `IndexClient`:

```typescript
// Init your wallet
const wallet = new Wallet(process.env.PRIVATE_KEY);

const indexClient = new IndexClient({
  wallet,
  domain: "https://dev.index.network",
  network: "dev", // or mainnet
});
```

For authentication, you need a `DIDSession`. You can either sign in using a wallet or pass an existing session. Check [Authentication](https://docs.index.network/docs/api-reference/identity/authentication) for details explanation on how to initiate a session.

```typescript
await indexClient.authenticate();
```

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

```typescript
const index = await indexClient.createIndex("Future of publishing");
```

Great, now you have a truly decentralized index to interact with! Though it's empty, which means we need to create and add an [`Item`](https://docs.index.network/docs/api-reference/indexing/index) into it so we can interact. Let's do that.

```typescript
const webPage = await indexClient.crawlWebPage("http://www.paulgraham.com/publishing.html");

await indexClient.addItemToIndex(index.id, webPage.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.

```typescript

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
  }
`);

```

#### Deploying a Custom Model
After creating a custom model, use the deployModel method to deploy it.

```typescript
await indexClient.deployModel(modelResponse.models[0]);
```

#### Using Your Model
To use it, create a node with your model and required data.

```typescript
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,
);
```

## Interact with your index
Your index is now ready for interaction! To start a conversation and interact with the data, follow these steps:


```typescript
// 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);
console.log(messages);
```

The response should look something like this:

```typescript
{
  "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:

```typescript
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,
);
```

### 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:

```typescript
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,
);
```

## Vector Search
Index allows you to search by vectors, by providing a LangChain plugin. This document will guide you through the steps to use this feature.


### Search by Vector using IndexVectorStore
First install and import necessary libraries.

```typescript
import IndexClient, {
  IndexVectorStore,
} from "@indexnetwork/sdk";
import { Wallet } from "ethers";
import { ChatOpenAI, OpenAIEmbeddings } from "@langchain/openai";
```

Then, create the IndexVectorStore instance with your configuration. The sources are the IDs of the Indexes that the search will operate on.

```typescript
const wallet = new Wallet(process.env.PRIVATE_KEY);
const indexClient = new IndexClient({
  network: "dev", // or mainnet
  wallet, // or session
  domain: "index.network",
});

const embeddings = new OpenAIEmbeddings({
  apiKey: process.env.OPENAI_API_KEY,
  model: "text-embedding-ada-002",
});

const sourceIndexId = "kjzl6kcym7w8y7lvuklrt4mmon5h9u3wpkm9jd9rtdbghl9df2ujsyid8d0qxj4";

const vectorStore = new IndexVectorStore(embeddings, {
  client: indexClient,
  sources: [sourceIndexId],
});
```

### Perform operations using the VectorStore
VectorStore object provides the same Langchain methods. Here are a few examples:

Similarity Search

```typescript
const question = "What is mesh.xyz?";
const response = await vectorStore.similaritySearch(question, 1);
```

It should print as below. Remember that pageContent is the stringified item data JSON.
```typescript
[
    {
    "pageContent": "{...}",
    "metadata": {},
    "id": "kjzl6kcym7w8y5slja1yq8upkixhskarjd8292qsa9bmhh15xci1ndlzkhju9ie"
 },
]
```

Conversational Retrieval

```typescript
const model = new ChatOpenAI({
  apiKey: process.env.OPENAI_API_KEY,
  model: "gpt-3.5-turbo",
});

const chain = ConversationalRetrievalQAChain.fromLLM(
  model,
  vectorStore.asRetriever(),
);

/* Ask it a question */
const response = await chain.invoke({ question, chat_history: [] });const message = "hello world";
```

Response would be as following:

```typescript
{
   "text": "Mesh.xyz is a platform that connects people, projects, and protocols building Web3. Founded in 2015 by Ethereum co-founder Joseph Lubin, Mesh has four core components..."
}
```

---
_Source: https://npm.io/package/@indexnetwork/sdk · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
