UNPKG

aptai

Version:

Node.js client for aptAI APIs

187 lines (135 loc) 6.28 kB
# aptAI aptAI is a powerful Node.js package for interacting with aptAI API, providing you with the ability to generate language-based responses for complex queries. ## Prerequisites Before you begin, ensure you have met the following requirements: - Node.js 14.0.0 or later - npm 7.0.0 or later ## Installation To use aptAI, you must first install the package. Use the following command to do this: ```bash npm install aptai ``` ## Initialization After you've installed aptAI, you need to initialize it with your API key before making any requests. Here's how to do it: For CommonJS (CJS): ```javascript const aptai = require("aptai"); aptai.initializeConfig({ apiKey: "your-api-key" }); ``` For ES6 Modules (ESM): ```javascript import aptai from "aptai"; aptai.initializeConfig({ apiKey: "your-api-key" }); ``` Replace 'your-api-key' with your actual API key. ## aptAI LLM V3 You can use the `llmV3` function to make requests to the API. This function expects an options object as an argument. This object must have the following property: - `q`: The query you want to send to the API. In addition, it can optionally have these properties: - `group_id`: Used to create a session for a conversation. Allows the API to keep track of the context and provide relevant responses for follow-up conversation. - `followUp`: Indicates whether you want to have a follow-up conversation. Set it to `0` for no follow-up, and `1` for follow-up. If you set it to `1`, you must also provide a `group_id`. - `limit`: Sets the maximum length of the response. Set it to `0` to let the system automatically calculate the limit. - `regenerate_id`: Allows you to regenerate the last response from a session. - `timeout`: Sets the maximum timeout for fetching the response from the API, in milliseconds. - `model`: The language model you want to use for generating the response. ### Making a Basic Request Here's a basic example of making a request without any optional parameters: ```javascript try { const response = await aptai.llmV3({ q: "How you can help me?" }); console.log(response); } catch (error) { console.error("An error occurred:", error.message); } ``` In this example, `llmV3` will use default values for all optional parameters. ### Using Optional Parameters You can provide any optional parameters you want in the options object. Here are examples for each optional parameter: **group_id and followUp:** ```javascript try { const options = { q: "How you can help me?", group_id: "my-group-id", followUp: 1, }; const response = await aptai.llmV3(options); console.log(response); } catch (error) { console.error("An error occurred:", error.message); } ``` **limit:** ```javascript try { const options = { q: "How you can help me?", limit: 100, }; const response = await aptai.llmV3(options); console.log(response); } catch (error) { console.error("An error occurred:", error.message); } ``` **regenerate_id:** ```javascript try { const options = { q: "How you can help me?", regenerate_id: "my-regenerate-id", }; const response = await aptai.llmV3(options); console.log(response); } catch (error) { console.error("An error occurred:", error.message); } ``` **timeout:** ```javascript try { const options = { q: "How you can help me?", timeout: 5000, // 5 seconds }; const response = await aptai.llmV3(options); console.log(response); } catch (error) { console.error("An error occurred:", error.message); } ``` ## Using the Translate Latest Endpoint aptAI provides a `translateLatest` function that you can use to translate text into a different language. This function expects an options object with the following properties: - `text`: The original text that you want to translate. This is a required property. - `target_language`: Specify a language code for the desired target language. This is not a requirement and by default, the language is set to English ("en"). You can retrieve a list of supported languages using the supportedLanguage API. - `timeout`: The maximum timeout for fetching the response from the API, in milliseconds. This is optional, and the default is 120000 (120 seconds). Here is an example of using this function: ```javascript try { const response = await aptai.translateLatest({ text: "Hello, I am aptAI", target_language: "en", }); console.log(response); } catch (error) { console.error("An error occurred:", error.message); } ``` In this example, if "Hello, I am aptAI" is successfully translated into English, the translated text will be logged to the console. If an error occurs, the error message will be logged to the console. **Supported Language API (translateLatest):** aptAI provides a `supportedLanguage` function which you can use to get a list of supported languages. These are the languages you can pass as the `target_language` when using the `translateLatest` function. The `supportedLanguage` function is a GET API and does not require any parameters. Here is an example of how to use it: ```javascript try { const response = await aptai.supportedLanguage(); console.log(response); } catch (error) { console.error("An error occurred:", error.message); } ``` In this example, if the request is successful, the list of supported languages will be logged to the console. If an error occurs, the error message will be logged to the console. Remember that you should always handle errors in your actual application. ## Documentation For more detailed information about aptAI, check out the [full documentation](https://documenter.getpostman.com/view/12525200/2s93m7Wgu5#2148617d-218e-4b6a-b4a0-075ce25e24e0). ## Error Handling You should always handle any potential errors when making requests. If the `APIs` function throws an error, it will always be an instance of `Error` and the error message will provide details about what went wrong. This might be a message from the API itself or a message about a network error. The examples above show basic error handling with a try-catch block. This will catch any errors thrown by `APIs` and log the error message to the console. In a real application, you should adjust this to handle errors in a way that makes sense for your use case.