Skip to content

Adding Embeddings endpoint, and reconfiguring env variables. - #130

Open
Enielect wants to merge 17 commits into
thepersonalaicompany:mainfrom
Enielect:self-hosting-web-app
Open

Adding Embeddings endpoint, and reconfiguring env variables.#130
Enielect wants to merge 17 commits into
thepersonalaicompany:mainfrom
Enielect:self-hosting-web-app

Conversation

@Enielect

Copy link
Copy Markdown

This pull request introduces significant updates to the .env.example configuration, environment-specific behavior, and the integration of a local AI model for generating embeddings and tags. It also includes enhancements to API routes, dependency updates, and minor logging improvements. Below is a categorized summary of the most important changes:

Environment Configuration Updates

  • Added new environment variables to .env.example and README.md for local AI model integration, including NEXT_PUBLIC_LOCAL_AI_MODEL_URL, MODEL_NAME, and CLIENT_MODE to enable switching between local and cloud-based models. [1] [2]
  • Introduced BRAIN_API_KEY for linking platforms. [1] [2]

AI Model Integration

  • Implemented a Singleton pattern in src/app/api/embed/pipeline.js for lazy initialization of a feature-extraction pipeline using the @xenova/transformers library.
  • Created a new API route in src/app/api/embed/route.js to generate embeddings using the pipeline.
  • Added local AI model fallback for generating tags and completions in various API routes (google/import, notion/import, obsidian/upload, and search). These routes now conditionally use the local Ollama model when CLIENT_MODE is set to local. [1] [2] [3] [4]

Dependency and Configuration Updates

  • Added @xenova/transformers as a dependency in package.json for handling AI model pipelines.
  • Updated next.config.mjs to include experimental support for external packages (sharp and onnxruntime-node).

Code Simplification and Bug Fixes

  • Replaced incorrect environment variable references (GOOGLE_REDIRECT_URI_NEW) with the correct variable GOOGLE_REDIRECT_URI in multiple Google OAuth-related API routes. [1] [2] [3]
  • Fixed a misplaced comment in src/app/api/google/callback/route.js for clarity.

Minor Enhancements

These changes collectively enhance the application's flexibility, enabling local AI model usage, improving configuration clarity, and refining API behavior.

@vercel

vercel Bot commented Jul 29, 2025

Copy link
Copy Markdown

@Enielect is attempting to deploy a commit to the The Personal AI Company Team on Vercel.

A member of the Team first needs to authorize it.

@Enielect

Copy link
Copy Markdown
Author

@sansyrox here is a PR, please can you help review this? I should get the merge conflict fixed soon, just realized it was cause by something added after I had forked the repo

@dwi11harsh dwi11harsh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the substantial improvements and local-model support!
I’m requesting a few changes to improve robustness and deployment safety before merge.

Comment thread .env.example Outdated
Comment on lines +14 to +20
NEXT_PUBLIC_BASE_URL=http://0.0.0.0:8080
NEXT_PUBLIC_APP_URL=http://localhost:3000
NEXT_PUBLIC_LOCAL_AI_MODEL_URL=http://localhost:11434/api
NEXT_PUBLIC_BASE_URL_EMBEDDINGS="https://api.mistral.ai/v1/"

MODEL_NAME=llama3.1:8b
CLIENT_MODE=local

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to add the values here
suggesting on README is sufficient

Comment thread src/app/api/embed/pipeline.js Outdated
};

let PipelineSingleton;
if (process.env.NODE_ENV !== "production") {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

validate env variable before using

Comment thread src/app/api/google/import/route.js Outdated
process.env.GOOGLE_CLIENT_ID_NEW,
process.env.GOOGLE_CLIENT_SECRET_NEW,
process.env.GOOGLE_REDIRECT_URI_NEW
process.env.GOOGLE_REDIRECT_URI

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's not change this as we would also need to fix this in server

Comment thread src/app/api/google/import/route.js Outdated
if (process.env.CLIENT_MODE === 'local') {
// Use local Ollama model as fallback
try {
const response = await fetch('http://localhost:11434/api/generate', {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this url is already present in environment variable, using it here might be a better option

if (process.env.CLIENT_MODE === 'local' && process.env.MODEL_NAME) {
try {
// Use local Ollama API
const response = await fetch('http://localhost:11434/api/generate', {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use environment value instead of hardcoded url

Comment thread src/app/api/search/route.js Outdated

// Function to check which model to use and make the appropriate API call
async function generateCompletion(messages, modelName) {
const clientMode = process.env.CLIENT_MODE;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not validating could cause runtime failure

Comment thread src/app/api/upload/route.js Outdated
if (misttralApiKey?.length == 0) {
embeddingResponse = await fetch("/api/embed", {
method: "GET",
body: JSON.stringify({text: truncatedText}),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

truncatedText is not defined

Comment thread src/app/search/page.js Outdated

// fetching user's sessions
const fetchUserThreads = async () => {
console.log(session?.user?.id, 'user Id from session in fetchUserThreads')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove log after using

Comment thread src/app/settings/page.js
.select("analytics_enabled")
.eq("id", userId)
.single();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider using
if (!analyticsEnabled) return;
to save additional api calls

Comment thread src/app/api/google/auth/route.js Outdated
client.client_id,
client.client_secret,
process.env.GOOGLE_REDIRECT_URI_NEW
process.env.GOOGLE_REDIRECT_URI // why is there new in front of it???

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets leave this as is and maybe fix this later (in some other PR)

Comment thread src/app/api/google/auth/route.js Outdated
client.client_id,
client.client_secret,
process.env.GOOGLE_REDIRECT_URI // why is there new in front of it???
process.env.GOOGLE_REDIRECT_URI_NEW // why is there new in front of it???

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants