Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] OpenAI is not accessible #3822

Closed
unitythemaker opened this issue Sep 8, 2024 · 19 comments · Fixed by #3837
Closed

[Bug] OpenAI is not accessible #3822

unitythemaker opened this issue Sep 8, 2024 · 19 comments · Fixed by #3837
Labels
🐛 Bug Something isn't working | 缺陷

Comments

@unitythemaker
Copy link

📦 Environment

Docker

📌 Version

v1.15.19

💻 Operating System

Ubuntu

🌐 Browser

Firefox

🐛 Bug Description

The LobeChat is unable to access the OpenAI API, despite the server having successful access. This issue appears to be isolated to the LobeChat application, as other AI models (e.g., Claude, Perplexity) are functioning correctly. The server can successfully access the OpenAI API. The nextjs Docker container itself can access the OpenAI API. LobeChat frontend displays an error as shown. I am using a production docker-compose installation

image

{
  "error": {
    "cause": {
      "errno": -3008,
      "code": "ENOTFOUND",
      "syscall": "getaddrinfo",
      "hostname": "api.openai.com"
    }
  },
  "endpoint": "https://api.openai.com/v1",
  "provider": "openai"
}

I am able to access OpenAI from both VM and nextjs container:
image

📷 Recurrence Steps

  1. Install LobeChat production.
  2. Create and configure an OpenAI API key.
  3. Attempt to use OpenAI-based features in LobeChat.

🚦 Expected Behavior

LobeChat should be able to communicate with the OpenAI API successfully, as the server and Docker container can.

📝 Additional Information

No response

@unitythemaker unitythemaker added the 🐛 Bug Something isn't working | 缺陷 label Sep 8, 2024
@github-project-automation github-project-automation bot moved this to Roadmap - Chat 1.x in Lobe Chat Routine Sep 8, 2024
@lobehubbot
Copy link
Member

👀 @unitythemaker

Thank you for raising an issue. We will investigate into the matter and get back to you as soon as possible.
Please make sure you have given us as much context as possible.
非常感谢您提交 issue。我们会尽快调查此事,并尽快回复您。 请确保您已经提供了尽可能多的背景信息。

Copy link

dosubot bot commented Sep 8, 2024

Hey there, @unitythemaker! I'm here to help you with any bugs, questions, or contributions you have. Let's tackle this issue together!

I found some similar issues that might be relevant to your problem:

  1. [Bug] The openai agent parameters in the Docker startup method cannot be switched to the official API direct connection mode: This issue was closed and last updated on August 07, 2024. You can check the details here [1].

  2. [Bug] 显示ConnectTimeoutError,应该如何解决: This issue was closed and last updated on July 10, 2024. It was referenced by another issue and suggested that the problem might be due to a slow first token. You can check the details here [2].

  3. 无法链接 openai 并且 timeout: This issue was closed and last updated on May 25, 2024. The suggested solution was to use a third-party proxy website instead of the official OpenAI API key. You can check the details here [3].

Regarding your specific issue, the ENOTFOUND error indicates a DNS resolution problem. Here are some steps you can take to troubleshoot and resolve this issue:

  1. Check DNS Configuration: Ensure that the DNS settings in your Docker environment are correctly configured. The network-service container in the Docker Compose setup does not have specific DNS settings configured, so it relies on the default DNS settings of the host machine [4].

  2. Verify OPENAI_PROXY_URL: Make sure that the OPENAI_PROXY_URL environment variable is correctly set, including the proper /v1 suffix if needed. An incorrect configuration can lead to connectivity issues [5].

  3. Debugging Tools: Enable the DEBUG_ZEROONE_CHAT_COMPLETION environment variable by setting it to 1. This will call the debugStream function and provide more detailed information about the API connectivity issues [6].

  4. Environment Variables: Ensure that all required environment variables for configuring the OpenAI API in LobeChat are correctly set. This includes OPENAI_API_KEY, OPENAI_PROXY_URL, and others as needed [7].

By following these steps, you should be able to identify and resolve the DNS resolution issue affecting your LobeChat's connectivity to the OpenAI API.

To continue talking to Dosu, mention @dosu.

@unitythemaker
Copy link
Author

I tried to manually add api.openai.com to container's /etc/hosts and it worked.

# Start a sh shell in container as root user
docker exec -u 0 -it lobe-database sh
# Open hosts file
vi /etc/hosts
# I added this line:
# 104.18.6.192 api.openai.com

Tried to check the connection again, it worked:
image

@arvinxx
Copy link
Contributor

arvinxx commented Sep 8, 2024

@hezhijie0327 is this same as before?

@unitythemaker
Copy link
Author

I got the same error for api.deepseek.com too right now. Claude and Perplexity still works fine though.

@hezhijie0327
Copy link
Contributor

hezhijie0327 commented Sep 8, 2024

@unitythemaker have a try to mapping host's /etc/resolv.conf to container

  lobe:
    image: lobehub/lobe-chat-database
    container_name: lobe-database
    network_mode: 'service:network-service'
    volumes:
      - '/etc/resolv.conf:/etc/resolv.conf'
    depends_on:
      postgresql:
        condition: service_healthy
      network-service:
        condition: service_started
      minio:
        condition: service_started
      logto:
        condition: service_started

    environment:
      - 'APP_URL=http://localhost:3210'
      - 'NEXT_AUTH_SSO_PROVIDERS=logto'
      - 'KEY_VAULTS_SECRET=Kix2wcUONd4CX51E/ZPAd36BqM4wzJgKjPtz2sGztqQ='
      - 'NEXT_AUTH_SECRET=NX2kaPE923dt6BL2U8e9oSre5RfoT7hg'
      - 'NEXTAUTH_URL=http://localhost:${LOBE_PORT}/api/auth'
      - 'LOGTO_ISSUER=http://localhost:${LOGTO_PORT}/oidc'
      - 'DATABASE_URL=postgresql://postgres:${POSTGRES_PASSWORD}@postgresql:5432/${LOBE_DB_NAME}'
      - 'S3_ENDPOINT=http://localhost:${MINIO_PORT}'
      - 'S3_BUCKET=${MINIO_LOBE_BUCKET}'
      - 'S3_PUBLIC_DOMAIN=http://localhost:${MINIO_PORT}'
      - 'S3_ENABLE_PATH_STYLE=1'
    env_file:
      - .env
    restart: always

@unitythemaker
Copy link
Author

unitythemaker commented Sep 8, 2024

Umm, server uses systemd-resolved (Ubuntu 24), so that wouldn't work because host's resolv.conf looks like this, the nameserver is localhost:

# This is /run/systemd/resolve/stub-resolv.conf managed by man:systemd-resolved(8).
# Do not edit.
#
# This file might be symlinked as /etc/resolv.conf. If you're looking at
# /etc/resolv.conf and seeing this text, you have followed the symlink.
#
# This is a dynamic resolv.conf file for connecting local clients to the
# internal DNS stub resolver of systemd-resolved. This file lists all
# configured search domains.
#
# Run "resolvectl status" to see details about the uplink DNS servers
# currently in use.
#
# Third party programs should typically not access this file directly, but only
# through the symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a
# different way, replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

nameserver 127.0.0.53
options edns0 trust-ad
search .

When I try nslookup with 127.0.0.53:

docker exec -u 0 -it lobe-database sh
/ # nslookup google.com
Server:		127.0.0.11
Address:	127.0.0.11#53

Non-authoritative answer:
Name:	google.com
Address: 142.250.184.238
Name:	google.com
Address: 142.250.184.238
Name:	google.com
Address: 2a00:1450:4001:813::200e
Name:	google.com
Address: 2a00:1450:4001:813::200e

/ # nslookup google.com 127.0.0.53
;; communications error to 127.0.0.53#53: connection refused
;; communications error to 127.0.0.53#53: connection refused
;; communications error to 127.0.0.53#53: connection refused
;; no servers could be reached


/ #

However, I believe the issue is not about DNS configuration but also I am confused. Because, host can resolve it, container can resolve it, even the node can resolve it but for both deepseek and openai lobe app can't.

This is an image from container where I tried to fetch OpenAI API:
image

I am not sure if that could be a problem but I've changed the docker-compose a little:
services:
  postgresql:
    image: pgvector/pgvector:pg16
    container_name: lobe-postgres
    ports:
      - '5432:5432'
    volumes:
      - './data:/var/lib/postgresql/data'
    environment:
      - 'POSTGRES_DB=${LOBE_DB_NAME}'
      - 'POSTGRES_PASSWORD=${POSTGRES_PASSWORD}'
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -U postgres']
      interval: 5s
      timeout: 5s
      retries: 5
    restart: always
    networks:
      - lobe-network

  minio:
    image: minio/minio
    container_name: lobe-minio
    ports:
      - '${MINIO_PORT}:${MINIO_PORT}'  # MinIO API
      - '9001:9001'  # MinIO Console
    volumes:
      - './s3_data:/etc/minio/data'
    environment:
      - 'MINIO_ROOT_USER=${MINIO_ROOT_USER}'
      - 'MINIO_ROOT_PASSWORD=${MINIO_ROOT_PASSWORD}'
      - 'MINIO_DOMAIN=${MINIO_DOMAIN}'
      - 'MINIO_API_CORS_ALLOW_ORIGIN=${MINIO_API_CORS_ALLOW_ORIGIN}'
    restart: always
    command: >
      server /etc/minio/data --address ":${MINIO_PORT}" --console-address ":9001"
    networks:
      - lobe-network

  logto:
    image: svhd/logto
    container_name: lobe-logto
    ports:
      - '${LOGTO_PORT}:${LOGTO_PORT}' # Logto
      - '3002:3002'  # Logto Admin
    depends_on:
      postgresql:
        condition: service_healthy
    environment:
      - 'TRUST_PROXY_HEADER=1'
      - 'PORT=${LOGTO_PORT}'
      - 'DB_URL=postgresql://postgres:${POSTGRES_PASSWORD}@postgresql:5432/logto'
      - 'ENDPOINT=${LOGTO_ENDPOINT}'
      - 'ADMIN_ENDPOINT=${LOGTO_ADMIN_ENDPOINT}'
    entrypoint: ['sh', '-c', 'npm run cli db seed -- --swe && npm start']
    networks:
      - lobe-network


  lobe:
    image: lobehub/lobe-chat-database
    container_name: lobe-database
    ports:
      - '${LOBE_PORT}:3210' # LobeChat
    depends_on:
      postgresql:
        condition: service_healthy
      minio:
        condition: service_started
      logto:
        condition: service_started
    env_file:
      - .env
    restart: always
    networks:
      - lobe-network

volumes:
  data:
    driver: local
  s3_data:
    driver: local

networks:
  lobe-network:

@hezhijie0327
Copy link
Contributor

More like related to this alpinelinux/docker-alpine#403

Could you run cat /etc/resolv.conf in container?
I assume that these following text are in that file, and Alpine not support these options

options edns0 trust-ad
search .

if yes, run the following code in container, then restart the container.

echo '8.8.4.4' > /etc/resolv.conf
echo '8.8.8.8' >> /etc/resolv.conf

@unitythemaker
Copy link
Author

unitythemaker commented Sep 8, 2024

Unfortunately, container has a different one. Here you go:

# cat /etc/resolv.conf
# Generated by Docker Engine.
# This file can be edited; Docker Engine will not make further changes once it
# has been modified.

nameserver 127.0.0.11
options ndots:0

# Based on host file: '/etc/resolv.conf' (internal resolver)
# ExtServers: [192.168.65.1]
# Overrides: []
# Option ndots from: internal

@hezhijie0327
Copy link
Contributor

hezhijie0327 commented Sep 8, 2024

then could you run the code to overwrite /etc/resolv.conf in container?

@hezhijie0327
Copy link
Contributor

found in docker/for-linux#755 , caused by options ndots:0 when using private dns server (127.0.0.11)

@unitythemaker
Copy link
Author

I tried it, it couldn't reach database via postgresql DNS so I replaced it with IP address of postgres container in .env then it could reach the db, but I guess, as container couldn't access host's DNS server via 127.0.0.53 it got this error now:

{
  "error": {
    "cause": {
      "errno": -3001,
      "code": "EAI_AGAIN",
      "syscall": "getaddrinfo",
      "hostname": "api.openai.com"
    }
  },
  "endpoint": "https://api.openai.com/v1",
  "provider": "openai"
}

@hezhijie0327
Copy link
Contributor

nonono... I mean run this to replace, or using text-editor to modify

echo 'nameserver 8.8.4.4' > /etc/resolv.conf
echo 'nameserver 8.8.8.8' >> /etc/resolv.conf

@unitythemaker
Copy link
Author

Oh, okay.

@unitythemaker
Copy link
Author

It worked!
image

@unitythemaker
Copy link
Author

Interesting... The thing I don't understand is how app can't reach but tools like nslookup, curl even nodejs in the container is able to reach.

@unitythemaker
Copy link
Author

I added this lines in docker-compose and now the solution is permanent!

    dns:
      - 8.8.4.4
      - 8.8.8.8

Btw, I checked /etc/resolv.conf after adding dns option and interestingly it's the same but see the comment (ExtServers):

# cat /etc/resolv.conf
# Generated by Docker Engine.
# This file can be edited; Docker Engine will not make further changes once it
# has been modified.

nameserver 127.0.0.11
options ndots:0

# Based on host file: '/etc/resolv.conf' (internal resolver)
# ExtServers: [8.8.4.4 8.8.8.8]
# Overrides: [nameservers]
# Option ndots from: internal

@github-project-automation github-project-automation bot moved this from Roadmap - Chat 1.x to Done in Lobe Chat Routine Sep 8, 2024
@lobehubbot
Copy link
Member

@unitythemaker

This issue is closed, If you have any questions, you can comment and reply.
此问题已经关闭。如果您有任何问题,可以留言并回复。

@hezhijie0327
Copy link
Contributor

hezhijie0327 commented Sep 8, 2024

create a file to save dns configuration like /your_path_to_lobechat/resolv.conf then mapping it to container /etc/resolv.conf

nameserver 8.8.4.4
nameserver 8.8.8.8
    volumes:
      - '/your_path_to_lobechat/resolv.conf:/etc/resolv.conf'

@lobehub lobehub locked and limited conversation to collaborators Sep 8, 2024
@arvinxx arvinxx converted this issue into discussion #3828 Sep 8, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
🐛 Bug Something isn't working | 缺陷
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants