Getting connection refused errors with OpenAI embeddings on local

Been using OpenAI embeddings with an embedded Weaviate instance just fine. However, I just launched a local deployment with Docker and I’m now getting the following error:

weaviate.exceptions.UnexpectedStatusCodeError: Object was not added! 
Unexpected status code: 500, with response body: 
{'error': 
    [{'message': 'update vector: send POST request: Post "https://api.openai.com/v1/embeddings": dial tcp 162.159.140.245:443: connect: connection refused'}]
}.

This is my docker-compose, mostly just copied from the Weaviate website.

---
services:
  weaviate:
    command:
      - --host
      - 0.0.0.0
      - --port
      - "8080"
      - --scheme
      - http
    image: cr.weaviate.io/semitechnologies/weaviate:1.26.6
    ports:
      - 8080:8080
      - 50051:50051
    volumes:
      - weaviate_data:/var/lib/weaviate
    restart: on-failure:0
    environment:
      QUERY_DEFAULTS_LIMIT: 25
      PERSISTENCE_DATA_PATH: "/var/lib/weaviate"
      DEFAULT_VECTORIZER_MODULE: "none"
      ENABLE_API_BASED_MODULES: "true"
      CLUSTER_HOSTNAME: "node1"
      ENABLE_MODULES: "text2vec-openai"

      # Enables API key authentication.
      AUTHENTICATION_APIKEY_ENABLED: "true"

      # List one or more keys, separated by commas. Each key corresponds to a specific user identity below.
      AUTHENTICATION_APIKEY_ALLOWED_KEYS: "[API_KEY]"

      # List one or more user identities, separated by commas. Each identity corresponds to a specific key above.
      AUTHENTICATION_APIKEY_USERS: "[My email]"
volumes:
  weaviate_data:

It works fine with embedded Weaviate so it shouldn’t be an issue with the OpenAI API key. Running ping to openai.com in the docker container works so I don’t think it’s an internet connection issue.

Any help would be appreciated, thanks!

hi @Lucas_Rothman !!

Welcome to our community :hugs:

It looks like your Weaviate server cannot communicate with OpenAi for some reason.

You can check this by running a wget call directly to open ai like this:

docker compose exec -ti weaviate sh -c 'wget --header="Content-Type: application/json" --header="Authorization: Bearer '"$OPENAI_APIKEY"'" --post-data='"'"'{ "input": "Hey Ho! Weaviate, Go!", "model": "text-embedding-3-small" }'"'"' --output-document - https://api.openai.com/v1/embeddings'

Note that you should have OPENAI_APIKEY with a valid apikey inside your docker container, or replace it with an actual apikey

I don’t have the OpenAI key set in my docker env but I’ve set the OpenAI API key in my connection:

weaviate_client = weaviate.connect_to_local(
    auth_credentials=Auth.api_key(DOCKER_API_KEY), 
    headers={"X-OpenAI-Api-Key": OPENAI_API_KEY}
)

Is it still required that I set the OpenAI key in the docker container? And if so what is the point of passing the API key in the headers?

The call does fail with connection refused when I enter my API key though. Any ideas why this could potentially be happening?

Hi @Lucas_Rothman !

If you pass the environment variable to the server, you no longer need to pass it at client/query time :slight_smile:

Not sure what is happening, but certainly something on your host OS or your network connection may be blocking open ai APIs

You will probably be able to run this code in a sandbox in our cloud, for example.

Let me know if this helps.

Thanks!