Spaces:
Running
Running
use authorization header instead of env token
Browse files- src/routes/api/+server.ts +17 -20
src/routes/api/+server.ts
CHANGED
|
@@ -1,34 +1,31 @@
|
|
| 1 |
import { json, type RequestEvent } from '@sveltejs/kit';
|
| 2 |
import { InferenceClient } from '@huggingface/inference';
|
| 3 |
|
| 4 |
-
import { env } from '$env/dynamic/private';
|
| 5 |
-
|
| 6 |
export async function POST({ request }: RequestEvent) {
|
| 7 |
const { model, messages, options, provider = 'auto' } = await request.json();
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
if (!model) {
|
| 10 |
return json({ error: 'Model and prompt are required' }, { status: 400 });
|
| 11 |
}
|
| 12 |
|
| 13 |
-
const client = new InferenceClient(
|
| 14 |
|
| 15 |
-
const stream = client.chatCompletionStream(
|
| 16 |
-
{
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
},
|
| 28 |
-
{
|
| 29 |
-
billTo: 'huggingface'
|
| 30 |
-
}
|
| 31 |
-
);
|
| 32 |
|
| 33 |
const readable = new ReadableStream({
|
| 34 |
async start(controller) {
|
|
|
|
| 1 |
import { json, type RequestEvent } from '@sveltejs/kit';
|
| 2 |
import { InferenceClient } from '@huggingface/inference';
|
| 3 |
|
|
|
|
|
|
|
| 4 |
export async function POST({ request }: RequestEvent) {
|
| 5 |
const { model, messages, options, provider = 'auto' } = await request.json();
|
| 6 |
+
const token = request.headers.get('Authorization')?.split(' ')[1];
|
| 7 |
+
if (!token) {
|
| 8 |
+
return json({ error: 'Unauthorized' }, { status: 401 });
|
| 9 |
+
}
|
| 10 |
|
| 11 |
if (!model) {
|
| 12 |
return json({ error: 'Model and prompt are required' }, { status: 400 });
|
| 13 |
}
|
| 14 |
|
| 15 |
+
const client = new InferenceClient(token);
|
| 16 |
|
| 17 |
+
const stream = client.chatCompletionStream({
|
| 18 |
+
model: model + (provider !== 'auto' ? `:${provider}` : ''),
|
| 19 |
+
...(options ?? {}),
|
| 20 |
+
messages: [
|
| 21 |
+
{
|
| 22 |
+
role: 'system',
|
| 23 |
+
content:
|
| 24 |
+
"You are a helpful assistant. You are very helpful and friendly. Use markdown to format your responses, but don't include array start and end markers."
|
| 25 |
+
},
|
| 26 |
+
...(messages ?? [])
|
| 27 |
+
]
|
| 28 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
const readable = new ReadableStream({
|
| 31 |
async start(controller) {
|