MYSTRO6IX commited on
Commit
45a414e
·
verified ·
1 Parent(s): 4d678a9

Create Server.js.

Browse files
Files changed (1) hide show
  1. Server.js. +65 -0
Server.js. ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ export default {
3
+ async fetch(request, env) {
4
+ const corsHeaders = {
5
+ "Access-Control-Allow-Origin": "*",
6
+ "Access-Control-Allow-Methods": "POST, OPTIONS",
7
+ "Access-Control-Allow-Headers": "Content-Type"
8
+ };
9
+
10
+ if (request.method === "OPTIONS") {
11
+ return new Response(null, { headers: corsHeaders });
12
+ }
13
+
14
+ if (request.method !== "POST") {
15
+ return new Response("CreateAI server is running!", {
16
+ headers: corsHeaders
17
+ });
18
+ }
19
+
20
+ try {
21
+ const { prompt } = await request.json();
22
+
23
+ if (!prompt) {
24
+ return Response.json(
25
+ { error: "Please enter a prompt." },
26
+ { status: 400, headers: corsHeaders }
27
+ );
28
+ }
29
+
30
+ const response = await fetch(
31
+ "https://router.huggingface.co/v1/chat/completions",
32
+ {
33
+ method: "POST",
34
+ headers: {
35
+ "Authorization": `Bearer ${env.HF_TOKEN}`,
36
+ "Content-Type": "application/json"
37
+ },
38
+ body: JSON.stringify({
39
+ model: "Qwen/Qwen2.5-7B-Instruct",
40
+ messages: [
41
+ {
42
+ role: "user",
43
+ content: prompt
44
+ }
45
+ ],
46
+ max_tokens: 800
47
+ })
48
+ }
49
+ );
50
+
51
+ const data = await response.json();
52
+
53
+ return Response.json(data, {
54
+ status: response.status,
55
+ headers: corsHeaders
56
+ });
57
+
58
+ } catch (error) {
59
+ return Response.json(
60
+ { error: error.message },
61
+ { status: 500, headers: corsHeaders }
62
+ );
63
+ }
64
+ }
65
+ };