Update README.md
Browse files
README.md
CHANGED
|
@@ -1,100 +1,110 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
{
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: nvidia-chat-proxy
|
| 3 |
+
emoji: 💬
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
+
sdk: docker
|
| 7 |
+
sdk_version: "1.0"
|
| 8 |
+
app_port: 8000
|
| 9 |
+
pinned: false
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
## NVIDIA Chat Proxy API
|
| 13 |
+
|
| 14 |
+
This is a small FastAPI server that proxies requests from your static website to the NVIDIA/`OpenAI` compatible endpoint, so your API key stays on the server and is never exposed in the browser.
|
| 15 |
+
|
| 16 |
+
### 1. Setup
|
| 17 |
+
|
| 18 |
+
Create and activate a virtual environment (optional but recommended), then install dependencies:
|
| 19 |
+
|
| 20 |
+
```bash
|
| 21 |
+
pip install -r requirements.txt
|
| 22 |
+
```
|
| 23 |
+
|
| 24 |
+
Create a `.env` file in this folder:
|
| 25 |
+
|
| 26 |
+
```bash
|
| 27 |
+
echo NVIDIA_API_KEY=your_real_nvidia_key_here > .env
|
| 28 |
+
```
|
| 29 |
+
|
| 30 |
+
> **Important**: Never commit your real key to git or paste it in client-side code.
|
| 31 |
+
|
| 32 |
+
### 2. Run the server
|
| 33 |
+
|
| 34 |
+
```bash
|
| 35 |
+
python server.py
|
| 36 |
+
```
|
| 37 |
+
|
| 38 |
+
The API will be available at `http://localhost:8000`.
|
| 39 |
+
|
| 40 |
+
### 3. HTTP API
|
| 41 |
+
|
| 42 |
+
**Endpoint**: `POST /chat`
|
| 43 |
+
|
| 44 |
+
**Request body**:
|
| 45 |
+
|
| 46 |
+
```json
|
| 47 |
+
{
|
| 48 |
+
"messages": [
|
| 49 |
+
{ "role": "user", "content": "Hello!" }
|
| 50 |
+
],
|
| 51 |
+
"temperature": 1.0,
|
| 52 |
+
"top_p": 1.0,
|
| 53 |
+
"max_tokens": 512
|
| 54 |
+
}
|
| 55 |
+
```
|
| 56 |
+
|
| 57 |
+
**Response body**:
|
| 58 |
+
|
| 59 |
+
```json
|
| 60 |
+
{
|
| 61 |
+
"content": "Model reply here..."
|
| 62 |
+
}
|
| 63 |
+
```
|
| 64 |
+
|
| 65 |
+
### 4. Example usage from a static website
|
| 66 |
+
|
| 67 |
+
```html
|
| 68 |
+
<!DOCTYPE html>
|
| 69 |
+
<html>
|
| 70 |
+
<head>
|
| 71 |
+
<meta charset="UTF-8" />
|
| 72 |
+
<title>Chat with NVIDIA Model</title>
|
| 73 |
+
</head>
|
| 74 |
+
<body>
|
| 75 |
+
<textarea id="input" placeholder="Ask something..."></textarea>
|
| 76 |
+
<button id="send">Send</button>
|
| 77 |
+
<pre id="output"></pre>
|
| 78 |
+
|
| 79 |
+
<script>
|
| 80 |
+
const API_URL = "http://localhost:8000/chat"; // or your deployed URL
|
| 81 |
+
|
| 82 |
+
document.getElementById("send").addEventListener("click", async () => {
|
| 83 |
+
const userText = document.getElementById("input").value;
|
| 84 |
+
|
| 85 |
+
const body = {
|
| 86 |
+
messages: [{ role: "user", content: userText }],
|
| 87 |
+
temperature: 1,
|
| 88 |
+
top_p: 1,
|
| 89 |
+
max_tokens: 512,
|
| 90 |
+
};
|
| 91 |
+
|
| 92 |
+
const res = await fetch(API_URL, {
|
| 93 |
+
method: "POST",
|
| 94 |
+
headers: { "Content-Type": "application/json" },
|
| 95 |
+
body: JSON.stringify(body),
|
| 96 |
+
});
|
| 97 |
+
|
| 98 |
+
if (!res.ok) {
|
| 99 |
+
document.getElementById("output").textContent =
|
| 100 |
+
"Error: " + (await res.text());
|
| 101 |
+
return;
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
const data = await res.json();
|
| 105 |
+
document.getElementById("output").textContent = data.content;
|
| 106 |
+
});
|
| 107 |
+
</script>
|
| 108 |
+
</body>
|
| 109 |
+
</html>
|
| 110 |
+
```
|