leumas-embeddings-api
v1.0.0
Published
API for generating and managing text and file embeddings.
Maintainers
Readme
@leumas/embeddings-api
API for generating and managing text and file embeddings. This package provides helper functions and Express.js routes to convert text and file content into embedding vectors.
Installation
To install the package, use npm:
npm install @leumas/embeddings-apiUsage
As an npm package (programmatic usage)
You can use the helper functions directly in your Node.js application:
const { textToEmbedding, fileToEmbedding } = require('@leumas/embeddings-api');
// Example: Convert text to embedding
const text = "Hello, world!";
const textEmbedding = textToEmbedding(text);
console.log("Text Embedding:", textEmbedding);
// Example: Convert file content to embedding
// Make sure 'path/to/your/file.txt' exists
const filePath = 'path/to/your/file.txt';
const fileEmbedding = fileToEmbedding(filePath);
if (fileEmbedding) {
console.log("File Embedding:", fileEmbedding);
} else {
console.log("Could not generate embedding for file.");
}Integrating with an existing Express.js server
You can mount the provided Express.js router to your application:
Install dependencies:
npm install express @leumas/embeddings-apiIn your main application file (e.g.,
app.jsorserver.js):const express = require('express'); const embeddingsRouter = require('@leumas/embeddings-api/routes'); // Note: points to the routes.js file const app = express(); app.use(express.json()); // Middleware to parse JSON request bodies // Mount the embeddings router under a specific path, e.g., '/api' app.use('/api', embeddingsRouter); const PORT = process.env.PORT || 3000; app.listen(PORT, () => { console.log(`Server running on port ${PORT}`); });
API Endpoints
The following endpoints are available when you integrate the router into your Express.js application:
POST /embedding/code
Convert a text string into an embedding vector.
- Request Body:
{ "text": "Your text string here." } - Response:
{ "embedding": [0.1, 0.2, 0.3, ...] } - Errors:
400 Bad Request: Iftextis not provided in the request body.500 Internal Server Error: If an error occurs during embedding generation.
POST /embedding/path
Convert file content into an embedding vector.
- Request Body:
{ "filePath": "/path/to/your/file.txt" } - Response:
{ "embedding": [0.1, 0.2, 0.3, ...] } - Errors:
400 Bad Request: IffilePathis not provided in the request body.404 Not Found: If the specified file does not exist or cannot be read.500 Internal Server Error: If an error occurs during embedding generation.
Helper Functions
textToEmbedding(text)
Generates an embedding vector from a given text string.
- Parameters:
text(String): The input text string.
- Returns:
Array: An array of numbers representing the embedding vector.
fileToEmbedding(filePath)
Generates an embedding vector from the content of a specified file.
- Parameters:
filePath(String): The absolute path to the file.
- Returns:
Arrayornull: An array of numbers representing the embedding vector, ornullif the file is not found or cannot be processed.
Development
Project Structure
.
├── index.js # Main entry point for the npm package (exports helper functions)
├── README.md # This documentation file
├── routes.js # Express.js router for API endpoints
└── functions/
├── embed-file.js # Helper function to embed file content
└── embed-text.js # Helper function to embed text contentRunning Tests
(Add your test command here if you have one, e.g., npm test)
License
This project is licensed under the MIT License.
