All the parameters above are optional, but you may need to use them to create great answer sessions for your users. Let’s see them in detail.
userContext
userContext is a string or an object that describes the user’s context. This is useful when you want to provide a more personalized experience to your users. For example, if you know that the user is a beginner in programming, you can provide a more detailed explanation of the concepts you’re talking about. This is optional, but it’s a good practice to provide it.
If we pretend to be an online music store, and we know who the logged in user is, we could provide the following user context:
inferenceType refers to the type of inference that runs on your data. Right now, only documentation is supported - therefore it’s set as a default value - but we’ll enable more inference types soon (website, blog, ecommerce, generic, and more)
initialMessages
An array of initial messages for your conversational experience with Orama Cloud. By default, this parameter is an empty array, but it can be an array of objects in the following format:
The events to subscribe to. You will get notified whenever a new event gets triggered. More on this in the next section.
Answer Session Events
Answers are a highly dynamic part of Orama. They work in an asynchronous manner and requires you to subscribe to events to catch any new change in the answer flow and react to it.
At the time of writing, Orama supports the following events:
onStateChange
Runs everytime the state of the answer session changes. In this context, the state refers to a list of interactions between the user and Orama Cloud.
The onStateChange event gets triggered everytime there’s a change in the state, which means that it will also get triggered when the server streams a new chunk of the answer back to the client.
This is particularly useful when creating reactive interfaces that need to update the UI as the answer gets streamed back from the server with frameworks like React, Vue, or Angular.
You can always access a static version of the state by using the state getter:
Runs everytime a message changes. This gets triggered with each chunk that gets streamed from the server, allowing you to re-render the messages in your page as they gets updated.
Orama Cloud will return the results of the inference process as a standard set of Orama search results. You will have access to those as soon as they gets computed:
Gets triggered when Orama successfully translates a natural language query into a search query. This is useful when you want to know how Orama interprets the user’s input. It adapts to your index schema and returns the most relevant search query.
With the Orama JavaScript SDK, you can ask questions in two different ways:
using the .ask function
using the .askStream function
Both methods accept the search parameters you already use to perform search, so you can influence the inference process by filtering, sorting, and grouping the results as you prefer!
They both trigger the exact same events, but the only difference is how they return the answer.
The .ask function will return the entire answer once it has been entirely streamed from the server, therefore, you can use this method as follows:
The .askStream method returns an async iterator that yields chunks of the answer as they gets streamed back from the server. Therefore, this is how you would implement it:
When you ask a question, you can provide additional user data to the answer session. This is useful when you want to provide a more personalized experience to your users. For example, if you know that the user is a beginner in programming, you can provide a more detailed explanation of the concepts you’re talking about:
The main difference between userContext and userData is that the userData is specific to a single answer request, while the userContext is global to the entire answer session.
Of course, Orama will remember the userData in subsequent answer requests, but will tend to forget it after a while. So, if that data is important to you, you should either provide it in every answer request, or use the userContext instead.
Aborting Answers Generation
In any moment in time (when a user cancels an answer request, for example) you can abort an answer by using the .abortAnswer function:
When creating answer sessions, you may want to provide your users with a list of related queries that they can use to get more information about a specific topic. You can do this by using the related parameter:
When asking for "question" (default) related queries, Orama will return an array of related queries in a question format. For example, if the user asks for: “What is Orama?”, Orama will return an array of related queries like: ["How does Orama work?", "Why is Orama the best?", "How do I run vector search?"].
If the format is "query", Orama will return an array of related queries in a query format. For example, if the user asks for: “What is Orama?”, Orama will return an array of related queries like: ["How Orama works", "Why Orama is the best", "Vector search with Orama"].