lm-sys / lm-sys/FastChat

Proposal: Use api keys with openai server api: A simple example with gitlab tokens

Open
#2,591 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
39.5k
Forks
4.8k
PR merge metrics
No merged PRs in 30d

Description

So, I would like to have ANY way of making sure that my users are authenticated before they use the openai api server.

My rationale is: If they have our server gitlab account, and they have an api key there, they can use the openai_server_api of fastchat.

This patch below adds support for gitlab keys.

With that, I can use continue.dev, for example, with FastChat being the inference server. So the same system provides me the chat web interface and the code completion/explain for VSCode.

My coding skills don't take me much further than that - but we could do some auth scheme for different providers, what do you think?

diff --git a/fastchat/serve/openai_api_server.py b/fastchat/serve/openai_api_server.py
index 9743bde..88e890c 100644
--- a/fastchat/serve/openai_api_server.py
+++ b/fastchat/serve/openai_api_server.py
@@ -88,6 +88,7 @@ async def fetch_remote(url, pload=None, name=None):
 class AppSettings(BaseSettings):
     # The address of the model controller.
     controller_address: str = "http://localhost:21001"
+    authenticator_url = "https://gitlab.com/api/v4/user"
     api_keys: Optional[List[str]] = None
 
 
@@ -115,6 +116,25 @@ async def check_api_key(
             )
         return token
     else:
+        if auth is not None and auth.credentials is not None:
+            # gitlab expect the token bearer with the string PRIVATE-TOKEN
+            headers = {"PRIVATE-TOKEN": auth.credentials}
+            response = httpx.get(app_settings.authenticator_url, headers=headers)
+            print(response.status_code)
+            if response.status_code == 200:
+                    return auth.credentials
+            raise HTTPException(
+                status_code=401,
+                detail={
+                    "error": {
+                        "message": "",
+                        "type": "invalid_request_error",
+                        "param": None,
+                        "code": "invalid_api_key",
+                    }
+                },
+            )

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in fastchat/serve/openai_api_server.py, reading AppSettings and check_api_key, then trace how authentication is applied to the OpenAI-compatible endpoints. Compare the proposed GitLab token validation with the existing api_keys behavior. Done means the supported authentication flow is defined and users with valid GitLab credentials can access the server while invalid credentials are rejected.

Written by the indexing model from the issue text.

Assessment

Tech stack
gitlab, python
Domain
api, authentication, backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.