Skip to content

Commit

Permalink
feat: API to configure huggingface_token (#1699)
Browse files Browse the repository at this point in the history
* feat: API for configuring huggingface token

* chore: API docs

* chore: Token docs

* chore: docs

---------

Co-authored-by: vansangpfiev <[email protected]>
  • Loading branch information
vansangpfiev and sangjanai authored Nov 19, 2024
1 parent 9ff93c5 commit 37fecbd
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 11 deletions.
120 changes: 120 additions & 0 deletions docs/docs/configurations/token.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
---
title: Token
description: Setting up token
slug: "token"
---

import Tabs from "@theme/Tabs";
import TabItem from "@theme/TabItem";

:::warning
🚧 Cortex.cpp is currently under development. Our documentation outlines the intended behavior of Cortex, which may not yet be fully implemented in the codebase.
:::

# Token Configuration Guide

This document describes how to configure HuggingFace token settings for Cortex.

## Command Line Interface (CLI)

### Basic Usage

```bash
cortex config [OPTIONS] [COMMAND]
```

### Commands

- `status`: Display all current configurations

```bash
cortex config status
```

Example Output:

```bash
+-----------------------+------------------------+
| Config name | Value |
+-----------------------+------------------------+
| huggingface_token | |
+-----------------------+------------------------+
```

### Options

| Option | Description | Example |
| ----------------------------------- | --------------------------- | ------------------------------------------------- |
| `-h, --help` | Print help message and exit |
| `--huggingface_token <token>` | Set HuggingFace token | `cortex config --huggingface_token token` |

## Token API Configuration

### Endpoints

#### Get Current Configuration

```http
GET /v1/configs
```

Retrieves the current configuration settings.

##### Response

```json
{
"allowed_origins": [
"http://localhost:39281",
"http://127.0.0.1:39281",
"http://0.0.0.0:39281"
],
"cors": true,
"huggingface_token": ""
}
```

#### Update Configuration

```http
PATCH /v1/configs
```

Updates HuggingFace token configuration settings.

##### Request Headers

```
Content-Type: application/json
```

##### Request Body

```json
{
"huggingface_token": "token"
}
```

##### Parameters

| Field | Type | Description |
| ----------------------- | ------- | ------------------------------------------|
| `huggingface_token` | string | HuggingFace token to pull models |

##### Response

```json
{
"config": {
"allowed_origins": [
"http://localhost:39281",
"http://127.0.0.1:39281",
"http://0.0.0.0:39281"
],
"cors": true,
"huggingface_token": "token"
},
"message": "Configuration updated successfully"
}
```
5 changes: 5 additions & 0 deletions docs/sidebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ const sidebars: SidebarsConfig = {
id: "configurations/proxy",
label: "Proxy",
},
{
type: "doc",
id: "configurations/token",
label: "Token",
}
],
},
{
Expand Down
16 changes: 15 additions & 1 deletion docs/static/openapi/cortex.json
Original file line number Diff line number Diff line change
Expand Up @@ -1810,6 +1810,10 @@
"no_proxy": {
"type": "string",
"example": "localhost"
},
"huggingface_token": {
"type": "string",
"example": "your_token"
}
}
},
Expand All @@ -1826,7 +1830,8 @@
"verify_proxy_host_ssl": false,
"verify_peer_ssl": false,
"verify_host_ssl": false,
"no_proxy": "localhost"
"no_proxy": "localhost",
"huggingface_token": "your_token"
}
}
}
Expand Down Expand Up @@ -1896,6 +1901,11 @@
"type": "string",
"description": "List of hosts that should not be proxied.",
"example": "localhost"
},
"huggingface_token": {
"type": "string",
"description": "HuggingFace token to pull models.",
"example": "your_token"
}
}
}
Expand Down Expand Up @@ -1958,6 +1968,10 @@
"no_proxy": {
"type": "string",
"example": "localhost"
},
"huggingface_token": {
"type": "string",
"example": "your_token"
}
}
},
Expand Down
26 changes: 24 additions & 2 deletions engine/common/api_server_configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ static const std::unordered_map<std::string, ApiConfigurationMetadata>
.group = "Proxy",
.accept_value = "[on|off]",
.default_value = "on"}},
{"huggingface_token",
ApiConfigurationMetadata{.name = "huggingface_token",
.desc = "HuggingFace token to pull models",
.group = "Token",
.accept_value = "string",
.default_value = "",
.allow_empty = true}},
};

class ApiServerConfiguration {
Expand All @@ -99,7 +106,8 @@ class ApiServerConfiguration {
bool verify_proxy_ssl = true, bool verify_proxy_host_ssl = true,
const std::string& proxy_url = "", const std::string& proxy_username = "",
const std::string& proxy_password = "", const std::string& no_proxy = "",
bool verify_peer_ssl = true, bool verify_host_ssl = true)
bool verify_peer_ssl = true, bool verify_host_ssl = true,
const std::string& hf_token = "")
: cors{cors},
allowed_origins{allowed_origins},
verify_proxy_ssl{verify_proxy_ssl},
Expand All @@ -109,7 +117,8 @@ class ApiServerConfiguration {
proxy_password{proxy_password},
no_proxy{no_proxy},
verify_peer_ssl{verify_peer_ssl},
verify_host_ssl{verify_host_ssl} {}
verify_host_ssl{verify_host_ssl},
hf_token{hf_token} {}

// cors
bool cors{true};
Expand All @@ -127,6 +136,9 @@ class ApiServerConfiguration {
bool verify_peer_ssl{true};
bool verify_host_ssl{true};

// token
std::string hf_token{""};

Json::Value ToJson() const {
Json::Value root;
root["cors"] = cors;
Expand All @@ -142,6 +154,7 @@ class ApiServerConfiguration {
root["no_proxy"] = no_proxy;
root["verify_peer_ssl"] = verify_peer_ssl;
root["verify_host_ssl"] = verify_host_ssl;
root["huggingface_token"] = hf_token;

return root;
}
Expand Down Expand Up @@ -225,6 +238,15 @@ class ApiServerConfiguration {
return true;
}},

{"huggingface_token",
[this](const Json::Value& value) -> bool {
if (!value.isString()) {
return false;
}
hf_token = value.asString();
return true;
}},

{"cors",
[this](const Json::Value& value) -> bool {
if (!value.isBool()) {
Expand Down
18 changes: 10 additions & 8 deletions engine/services/config_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ cpp::result<ApiServerConfiguration, std::string>
ConfigService::UpdateApiServerConfiguration(const Json::Value& json) {
auto config = file_manager_utils::GetCortexConfig();
ApiServerConfiguration api_server_config{
config.enableCors, config.allowedOrigins, config.verifyProxySsl,
config.verifyProxyHostSsl, config.proxyUrl, config.proxyUsername,
config.proxyPassword, config.noProxy, config.verifyPeerSsl,
config.verifyHostSsl};
config.enableCors, config.allowedOrigins, config.verifyProxySsl,
config.verifyProxyHostSsl, config.proxyUrl, config.proxyUsername,
config.proxyPassword, config.noProxy, config.verifyPeerSsl,
config.verifyHostSsl, config.huggingFaceToken};

std::vector<std::string> updated_fields;
std::vector<std::string> invalid_fields;
Expand All @@ -35,6 +35,8 @@ ConfigService::UpdateApiServerConfiguration(const Json::Value& json) {
config.verifyPeerSsl = api_server_config.verify_peer_ssl;
config.verifyHostSsl = api_server_config.verify_host_ssl;

config.huggingFaceToken = api_server_config.hf_token;

auto result = file_manager_utils::UpdateCortexConfig(config);
return api_server_config;
}
Expand All @@ -43,8 +45,8 @@ cpp::result<ApiServerConfiguration, std::string>
ConfigService::GetApiServerConfiguration() {
auto config = file_manager_utils::GetCortexConfig();
return ApiServerConfiguration{
config.enableCors, config.allowedOrigins, config.verifyProxySsl,
config.verifyProxyHostSsl, config.proxyUrl, config.proxyUsername,
config.proxyPassword, config.noProxy, config.verifyPeerSsl,
config.verifyHostSsl};
config.enableCors, config.allowedOrigins, config.verifyProxySsl,
config.verifyProxyHostSsl, config.proxyUrl, config.proxyUsername,
config.proxyPassword, config.noProxy, config.verifyPeerSsl,
config.verifyHostSsl, config.huggingFaceToken};
}

0 comments on commit 37fecbd

Please sign in to comment.