Skip to content

Commit

Permalink
Prevent external service localhost question (#497)
Browse files Browse the repository at this point in the history
* Prevent external service localhost question

* add 0.0.0.0 to docker-invalid URL

* clarify hint
  • Loading branch information
timothycarambat authored Dec 28, 2023
1 parent e0a0a89 commit d748167
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"cSpell.words": [
"Dockerized",
"Ollama",
"openai",
"Qdrant",
Expand Down
1 change: 1 addition & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ RUN cd ./server && npx prisma migrate deploy --schema=./prisma/schema.prisma

# Setup the environment
ENV NODE_ENV=production
ENV ANYTHING_LLM_RUNTIME=docker

# Expose the server port
EXPOSE 3001
Expand Down
25 changes: 18 additions & 7 deletions server/utils/helpers/updateENV.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const KEY_MAPPING = {
// LMStudio Settings
LMStudioBasePath: {
envKey: "LMSTUDIO_BASE_PATH",
checks: [isNotEmpty, validLLMExternalBasePath],
checks: [isNotEmpty, validLLMExternalBasePath, validDockerizedUrl],
},
LMStudioTokenLimit: {
envKey: "LMSTUDIO_MODEL_TOKEN_LIMIT",
Expand All @@ -66,7 +66,7 @@ const KEY_MAPPING = {
// LocalAI Settings
LocalAiBasePath: {
envKey: "LOCAL_AI_BASE_PATH",
checks: [isNotEmpty, validLLMExternalBasePath],
checks: [isNotEmpty, validLLMExternalBasePath, validDockerizedUrl],
},
LocalAiModelPref: {
envKey: "LOCAL_AI_MODEL_PREF",
Expand All @@ -83,7 +83,7 @@ const KEY_MAPPING = {

OllamaLLMBasePath: {
envKey: "OLLAMA_BASE_PATH",
checks: [isNotEmpty, validOllamaLLMBasePath],
checks: [isNotEmpty, validOllamaLLMBasePath, validDockerizedUrl],
},
OllamaLLMModelPref: {
envKey: "OLLAMA_MODEL_PREF",
Expand All @@ -106,7 +106,7 @@ const KEY_MAPPING = {
},
EmbeddingBasePath: {
envKey: "EMBEDDING_BASE_PATH",
checks: [isNotEmpty, validLLMExternalBasePath],
checks: [isNotEmpty, validLLMExternalBasePath, validDockerizedUrl],
},
EmbeddingModelPref: {
envKey: "EMBEDDING_MODEL_PREF",
Expand All @@ -126,7 +126,7 @@ const KEY_MAPPING = {
// Chroma Options
ChromaEndpoint: {
envKey: "CHROMA_ENDPOINT",
checks: [isValidURL, validChromaURL],
checks: [isValidURL, validChromaURL, validDockerizedUrl],
},
ChromaApiHeader: {
envKey: "CHROMA_API_HEADER",
Expand All @@ -140,7 +140,7 @@ const KEY_MAPPING = {
// Weaviate Options
WeaviateEndpoint: {
envKey: "WEAVIATE_ENDPOINT",
checks: [isValidURL],
checks: [isValidURL, validDockerizedUrl],
},
WeaviateApiKey: {
envKey: "WEAVIATE_API_KEY",
Expand All @@ -150,7 +150,7 @@ const KEY_MAPPING = {
// QDrant Options
QdrantEndpoint: {
envKey: "QDRANT_ENDPOINT",
checks: [isValidURL],
checks: [isValidURL, validDockerizedUrl],
},
QdrantApiKey: {
envKey: "QDRANT_API_KEY",
Expand Down Expand Up @@ -318,6 +318,17 @@ function isDownloadedModel(input = "") {
return files.includes(input);
}

function validDockerizedUrl(input = "") {
if (process.env.ANYTHING_LLM_RUNTIME !== "docker") return null;
try {
const { hostname } = new URL(input);
if (["localhost", "127.0.0.1", "0.0.0.0"].includes(hostname.toLowerCase()))
return "Localhost, 127.0.0.1, or 0.0.0.0 origins cannot be reached from inside the AnythingLLM container. Please use host.docker.internal, a real machine ip, or domain to connect to your service.";
return null;
} catch {}
return null;
}

// This will force update .env variables which for any which reason were not able to be parsed or
// read from an ENV file as this seems to be a complicating step for many so allowing people to write
// to the process will at least alleviate that issue. It does not perform comprehensive validity checks or sanity checks
Expand Down

0 comments on commit d748167

Please sign in to comment.