LLM Host API

Endpoints

1. Register Endpoint

POST/register

This endpoint is used to register the username and password.

cURL Request:

curl -X POST 'https://humblebeeai-llm-host.hf.space/register' \
                  -H "Content-Type: application/json" \
                  -d '{
                        "username": "testuser",
                        "password": "testpassword"
                      }'

Response:

{"message":"User registered successfully","user":"bahodir"}

2. Login Endpoint

POST/login

This endpoint is used to authenticate a user and retrieve an access token.

cURL Request:

curl -X POST 'https://humblebeeai-llm-host.hf.space/login' \
                 -H "Content-Type: application/x-www-form-urlencoded" \
                 -d 'username=testuser&password=testpassword'

Response:

{
                    "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
                    "token_type": "bearer",
                    "refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
                }

3. Refresh Token Endpoint

POST/refresh

This endpoint is used to refresh the access token using a valid refresh token.

cURL Request:

curl -X POST 'https://humblebeeai-llm-host.hf.space/refresh' \
                 -H "Content-Type: application/json" \
                 -d '{"refresh_token": "your-refresh-token"}'

Response:

{
                    "access_token": "new-access-token",
                    "refresh_token": "your-refresh-token",
                    "token_type": "bearer"
                }

4. Generate Endpoint

POST/generate

This endpoint is used to generate text based on a query. It requires a valid access token and supports optional streaming.

cURL Request:

curl -X POST 'https://humblebeeai-llm-host.hf.space/generate?stream=true' \
                 -H "Content-Type: application/json" \
                 -H "Authorization: Bearer your-access-token" \
                 -d '{"query": "test query"}'

Response:

{
                    "content": "Generated response text based on the query."
                }

Streaming Response:


                data: {"content": "First streamed response chunk"}
                data: {"content": "Second streamed response chunk"}
                

Workflow Example

Here's a complete workflow demonstrating how to use the API:

cURL Implementation

Step 1: Register

curl -X POST 'https://humblebeeai-llm-host.hf.space/register' \
                  -H "Content-Type: application/json" \
                  -d '{
                        "username": "testuser",
                        "password": "testpassword"
                      }'

Step 2: Login

curl -X POST 'https://humblebeeai-llm-host.hf.space/login' \
                 -H "Content-Type: application/x-www-form-urlencoded" \
                 -d 'username=testuser&password=testpassword'

Step 3: Generate

curl -X POST 'https://humblebeeai-llm-host.hf.space/generate?stream=true' \
                 -H "Content-Type: application/json" \
                 -H "Authorization: Bearer your-access-token" \
                 -d '{"query": "test query"}'

Step 4: Refresh Token

curl -X POST 'https://humblebeeai-llm-host.hf.space/refresh' \
                 -H "Content-Type: application/json" \
                 -d '{"refresh_token": "your-refresh-token"}'

Python Implementation

Complete Python Example


        import requests
        import json
        
        API_BASE_URL = "https://humblebeeai-llm-host.hf.space"
        
        class APIClient:
            """Handles the full API workflow"""
        
            def __init__(self):
                self.access_token = None
                self.refresh_token = None
        
            def register(self, username: str, password: str):
                """Registers a new user."""
                url = f"{API_BASE_URL}/register"
                payload = {"username": username, "password": password}
                headers = {"Content-Type": "application/json"}
        
                response = requests.post(url, json=payload, headers=headers)
                if response.status_code == 200:
                    print("✅ Registration successful!")
                else:
                    raise Exception(f"⚠️ Registration failed: {response.text}")
        
            def login(self, username: str, password: str):
                """Authenticates the user and retrieves tokens."""
                url = f"{API_BASE_URL}/login"
                payload = {"username": username, "password": password}
                headers = {"Content-Type": "application/x-www-form-urlencoded"}
        
                response = requests.post(url, data=payload, headers=headers)
                if response.status_code == 200:
                    tokens = response.json()
                    self.access_token = tokens.get("access_token")
                    self.refresh_token = tokens.get("refresh_token")
                    print(f"✅ Login successful! Token: {self.access_token}")
                else:
                    raise Exception(f"⚠️ Login failed: {response.text}")
        
            def generate(self, query: str, stream: bool = False):
                """Generates content based on query."""
                url = f"{API_BASE_URL}/generate?stream={str(stream).lower()}"
                payload = {"query": query}
                headers = {
                    "Content-Type": "application/json",
                    "Authorization": f"Bearer {self.access_token}",
                }
        
                response = requests.post(url, json=payload, headers=headers, stream=stream)
        
                if response.status_code == 200:
                    if stream:
                        for line in response.iter_lines():
                            if line:
                                print(json.loads(line.decode("utf-8")).get("content"))
                    else:
                        return response.json()
                else:
                    raise Exception(f"⚠️ Request failed: {response.text}")
        
            def refresh_token(self):
                """Refreshes access token."""
                url = f"{API_BASE_URL}/refresh"
                payload = {"refresh_token": self.refresh_token}
                headers = {"Content-Type": "application/json"}
        
                response = requests.post(url, json=payload, headers=headers)
                if response.status_code == 200:
                    tokens = response.json()
                    self.access_token = tokens.get("access_token")
                    print(f"✅ Token refreshed! New Token: {self.access_token}")
                else:
                    raise Exception(f"⚠️ Token refresh failed: {response.text}")
        
        # Example Usage
        client = APIClient()
        
        try:
            # Step 1: Register
            client.register("testuser", "testpassword")
        
            # Step 2: Login
            client.login("testuser", "testpassword")
        
            # Step 3: Generate response
            response = client.generate("test query", stream=True)
            if response:
                print(f"✅ Response: {response}")
        
            # Step 4: Refresh Token
            client.refresh_token()
        
        except Exception as e:
            print(f"⚠️ Error: {e}")
                

Example Responses:

{
                "access_token": "your-access-token",
                "refresh_token": "your-refresh-token",
                "token_type": "bearer"
            }
{
                "content": "Generated response text based on the query."
            }

            data: {"content": "First streamed response chunk"}
            data: {"content": "Second streamed response chunk"}