Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules/
apiKeys.db
*.conf
.env
119 changes: 50 additions & 69 deletions APIMyLlama.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,50 @@
const express = require('express');
const { initializeDatabase, closeDatabase, getDb } = require('./db');
const { startServer, askForPort, askForOllamaURL, startCLI } = require('./utils');
const { setupRoutes } = require('./api');
const fs = require('fs');

const app = express();
app.use(express.json());

console.log('APIMyLlama V2 is being started. Thanks for choosing Gimer Studios.');

// Middleware for logging requests
app.use((req, res, next) => {
console.log(`Received a ${req.method} request at ${req.url}`);
next();
});

// Initialize the database
const db = initializeDatabase();

// Setup API routes
setupRoutes(app, db);

// Close the database connection when the application is closed
process.on('SIGINT', () => {
closeDatabase();
});

// Start the server
setTimeout(() => {
if (fs.existsSync('port.conf')) {
fs.readFile('port.conf', 'utf8', (err, data) => {
if (err) {
console.error('Error reading port number from file:', err.message);
askForPort(app, startServer, askForOllamaURL, startCLI, db);
} else {
const port = parseInt(data.trim());
if (isNaN(port)) {
console.error('Invalid port number in port.conf');
askForPort(app, startServer, askForOllamaURL, startCLI, db);
} else {
if (fs.existsSync('ollamaURL.conf')) {
fs.readFile('ollamaURL.conf', 'utf8', (err, data) => {
if (err) {
console.error('Error reading Ollama url from file:', err.message);
askForOllamaURL(app, startServer, startCLI, port, db);
} else {
const ollamaURL = data.trim();
if (typeof ollamaURL !== 'string' || ollamaURL === '') {
console.error('Invalid Ollama url in ollamaURL.conf');
askForOllamaURL(app, startServer, startCLI, port, db);
} else {
startServer(port, app);
startCLI(db);
}
}
});
} else {
askForOllamaURL(app, startServer, startCLI, port, db);
}
}
}
});
} else {
askForPort(app, startServer, askForOllamaURL, startCLI, db);
}
}, 1000);

module.exports = app;
const express = require('express');
const db = require('./db');
const { startServer, resolveConfig, startCLI } = require('./utils');
const { setupRoutes } = require('./api');

const app = express();

async function main() {
console.log('APIMyLlama V2 is being started. Thanks for choosing Gimer Studios.');

app.use(express.json({ limit: '10mb' }));

app.use((req, res, next) => {
console.log(`Received a ${req.method} request at ${req.url}`);
next();
});

app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, DELETE');
if (req.method === 'OPTIONS') return res.sendStatus(200);
next();
});

await db.initialize();

setupRoutes(app);

const port = await resolveConfig('port number', 'PORT', 'port.conf', '3000');
await resolveConfig('Ollama server URL', 'OLLAMA_URL', 'ollamaURL.conf', 'http://localhost:11434');

startServer(parseInt(port), app);
startCLI();
}

process.on('SIGINT', async () => {
console.log('\nShutting down...');
try {
await db.close();
} catch {}
process.exit(0);
});

main().catch(err => {
console.error('Failed to start:', err);
process.exit(1);
});

module.exports = app;
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,13 @@ prompt: Text prompt to generate a response.
model: Machine learning model to use for text generation.
stream: Boolean indicating whether to stream the response.
```
## curl Use Case
```
curl -X POST http://localhost:<Port>/generate \
-H "Authorization: Bearer <APIKey>" \
-H "Content-Type: application/json" \
-d '{"prompt":"<Message>","model":"<Model>","stream":true}'
```
# Support
If there are any issues please make a Github Issue Report. To get quicker support join our discord server.
-[Discord Server](https://discord.gg/r6XazGtKg7) If there are any feature requests you may request them in the discord server. PLEASE NOTE this project is still in EARLY BETA.
Expand Down
Loading