In Index Network, you can engage in conversations with one or multiple Indexes. This interaction is facilitated by our storage system, which processes and stores information to allow natural, meaningful dialogue. Whether it's querying for specific information or engaging in an ongoing conversation, the Index Network enables seamless communication with its indexed data.
To start a conversation and interact with the data, follow these steps:
// Create a conversationconstconversationParams= { sources: [index.id], summary:"Mock summary", members: [indexClient.wallet.address],};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);console.log(messages);
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:```python{"id":"message-id","content":"How do you do this?","role":"user","createdAt":"timestamp"}
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,);
constresponse=awaitfetch('https://dev.index.network/api/api/conversations/{conversationId}/messages', { method:'POST', headers: {"Authorization":"Bearer DID_SESSION_SERIALIZED","Content-Type":"application/json" }, body:JSON.stringify({"role":"user","content":"give me a very long hello world python code" }),});constdata=awaitresponse.json();
constresponse=awaitfetch('https://dev.index.network/api/api/conversations/{conversationId}/messages/{messageId}', { method:'PUT', headers: {"Authorization":"Bearer DID_SESSION_SERIALIZED","Content-Type":"application/json" }, body:JSON.stringify({"role":"user","content":"What is Index Network and how is it related to Ceramic?" }),});constdata=awaitresponse.json();
constresponse=awaitfetch('https://dev.index.network/api/api/conversations/{conversationId}', { method:'PUT', headers: {"Authorization":"Bearer DID_SESSION_SERIALIZED","Content-Type":"application/json" }, body:JSON.stringify({"sources": ["did:pkh:eip155:1:0x1b9Aceb609a62bae0c0a9682A9268138Faff4F5f" ],"summary":"summary of the conversation, just for listing purposes." }),});constdata=awaitresponse.json();