mudler / LocalAI

:robot: The free, Open Source OpenAI alternative. Self-hosted, community-driven and local-first. Drop-in replacement for OpenAI running on consumer-grade hardware. No GPU required. Runs gguf, transformers, diffusers and many more models architectures. It allows to generate Text, Audio, Video, Images. Also with voice cloning capabilities.
https://localai.io
MIT License
21.75k stars 1.66k forks source link

document use of api_key / API_KEY #1869

Open sbe-arg opened 3 months ago

sbe-arg commented 3 months ago

I cannot figure out how to set and use API_KEY for nextcloud or http/curl commands

I tried adding the environment var API_KEY='some-value-crypt' in my docker compose

They using it to authenticate in the nextcloud API settings but the same api_key value does not work either on the http enpoint via curl

Does it need any specif requirements or encryption?

version: '3.8'

services:

  localai:
    container_name: local-ai
    hostname: local-ai
    image: docker.io/localai/localai:v2.10.1
    ports:
      - 8080:8080
    environment:
      - MODELS_PATH=/models
      - REBUILD=true
      - API_KEY='somevaluehere'
      - BUILD_API_ONLY=false # rebuild everything
      - BUILD_GRPC_FOR_BACKEND_LLAMA=true
      - BUILD_TYPE=openblas
      - GO_TAGS=stablediffusion,tts
      - GALLERIES='[{"name":"model-gallery","url":"github:go-skynet/model-gallery/index.yaml"},{"url":"github:go-skynet/model-gallery/huggingface.yaml","name":"huggingface"}]'
      - THREADS=2
    volumes:
      - localai_models:/models:cached
      - localai_images:/tmp/generated/images/
    command:
      - phi-2
      - tinyllama-chat
      # https://localai.io/basics/getting_started/index.html#running-models
    restart: unless-stopped

volumes:
  localai_models: {}
  localai_images: {}

or it needs something on the lines of '["key1","key2"]' or '["{key1"},"{key2}"}' nothing I tried works.

chenxinlong commented 3 months ago

For me, the following setting works. Set the API_KEY environment, when you have multiple api keys just seperate it with commas, like below :

...
        env:
        - name: DEBUG
          value: "true"
        - name: API_KEY
          value: "aaa,bbb"
...

Then send request with Authorization http header, value start with Bearer prefix :

curl --location 'http://localhost:8080/v1/chat/completions' \
--header 'Authorization: Bearer aaa' \
--header 'Content-Type: application/json' \
--data '{
     "model": "phi-2",
     "messages": [{"role": "user", "content": "how are you?"}],
     "temperature": 0.3
}'

I found it in the source code, not document.

localai-bot commented 2 months ago

I see that you're having trouble setting and using the API_KEY for Nextcloud or HTTP/curl commands in the LocalAI docker-compose setup. As mentioned by @sbe-arg, you can set multiple API keys by separating them with commas in the API_KEY environment variable. Here's an example from their docker-compose file:

...
        env:
        - name: DEBUG
          value: "true"
        - name: API_KEY
          value: "aaa,bbb"
...

To send a request with the Authorization header, you must start the value with the Bearer prefix. Here's an example using curl:

curl --location 'http://localhost:8080/v1/chat/completions' \
--header 'Authorization: Bearer aaa' \
--header 'Content-Type: application/json' \
--data '{"model": "phi-2",
     "messages": [{"role": "user", "content": "how are you?"}],
     "temperature": 0.3
}'

Regarding the documentation, I understand that you found the information in the source code and not in the official documentation. This is an experiment, and the documentation might not cover all the details yet. You can find more information about this feature in the LocalAI source code ( https://github.com/mudler/LocalAI/blob/main/docs/content/docs/features/openai-functions.md ).

(I am a bot, an experiment of @mudler, and I audited this issue from the LocalAI Github project.)

JackBekket commented 2 months ago

Can I add something like api_keys.json and make local-ai check this file for the keys?

jtwolfe commented 2 months ago

related #2046