QnA SDK
The @or-sdk/qna
package provides a client for working with the QnA (Question and Answer) system in OneReach.ai. With this client, you can perform operations such as loading documents, creating collections, searching in collections, and asking questions to generate answers based on the loaded documents.
Installation
To install the package, run the following command:
$ npm install @or-sdk/qna
Usage
To use the QnA
client, you need to create an instance of the class with the appropriate configuration options. You can either provide the direct API URL or the service discovery URL. Here is an example:
import { QnA } from '@or-sdk/qna';
const qna = new QnA({
token: 'my-account-token-string',
serviceUrl: 'http://example.qna/endpoint'
});
const qna = new QnA({
token: 'my-account-token-string',
discoveryUrl: 'http://example.qna/endpoint'
});
Once you have an instance of the QnA
client, you can use its methods to interact with the OneReach.ai QnA system. The available methods are:
loadDocument
Load a document into a collection.
Params
[0]: string
- Collection name.[1]: string
- Document name.[2]: CreateDocument
- Document creation parameters.
Example
await qna.loadDocument('myCollection', 'myDocument', {
url: 'https://example.com/document.pdf'
});
removeDocument
Remove a document from a collection.
Params
[0]: string
- Collection name.[1]: string
- Document name.
Example
await qna.removeDocument('myCollection', 'myDocument');
createCollection
Create a new collection.
Params
[0]: CreateCollection
- Collection creation parameters.
Example
await qna.createCollection({ collection: 'myCollection' });
deleteCollection
Delete a collection.
Params
[0]: string
- Collection name.
Example
await qna.deleteCollection('myCollection');
search
Search in the collection.
Params
[0]: string
- Collection name.[1]: Search
- Search parameters.[2]?: number
- Optional limit of search results (defaults to 5).
Example
const searchResults = await qna.search('myCollection', { term: 'coffee' });
console.log(searchResults.documents);
ask
Answer a question.
Params
[0]: string
- Collection name.[1]: Ask
- Ask question parameters.[2]?: number
- Optional limit of search results (defaults to 5).
Example
const askResults = await qna.ask('myCollection', {
question: 'What are the health benefits of coffee?',
messages: [
{ role: 'user', content: 'Tell me about coffee' },
{ role: 'assistant', content: 'Coffee is a popular beverage...' }
]
});
console.log(askResults.result.content);