-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·122 lines (103 loc) · 3.23 KB
/
index.js
File metadata and controls
executable file
·122 lines (103 loc) · 3.23 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import Anthropic from "@anthropic-ai/sdk";
import config from "./config.js";
import express from "express";
import axios from "axios";
import bodyParser from "body-parser";
// Create Express app
const app = express();
const PORT = 3000;
// Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/info", async (req, res) => {
try {
let token = req.query.token;
if (!token) {
return res.status(400).json({ success: false, remaining: 0 });
}
const response = await axios.get(config.limit_handler, {
params: {
token: token,
method: "getLimits",
},
});
if (response.data.success) {
return res.json({ success: true, remaining: response.data.requests_left });
}
return res.status(400).json({ success: false, remaining: 0 });
} catch (error) {
res.status(500).json({ success: false, remaining: 0 });
}
});
app.post("/complete", async (req, res) => {
try {
let prompt = req.body.prompt;
let system = req.body.system ?? "";
let token = req.query.token;
if (!prompt) {
return res.status(400).json({ error: "Prompt is required" });
}
if (!token) {
return res.status(400).json({ error: "Token is required" });
}
const token_response = await axios.get(config.limit_handler, {
params: {
token: token,
method: "consumeRequest",
},
});
if (!token_response.data.success) {
return res.json({ info: "Invalid Token or Out of Tokens", response: "" });
}
const anthropic = new Anthropic({
apiKey: config.tokens.claude,
});
let msg;
if(system != ""){
msg = await anthropic.messages.create({
model: "claude-3-7-sonnet-20250219",
max_tokens: 1024,
system: system,
temperature: 0.7,
messages: [
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt
}
]
}
]
});
}
else{
msg = await anthropic.messages.create({
model: "claude-3-7-sonnet-20250219",
max_tokens: 1024,
messages: [
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt
}
]
}
]
});
}
msg = msg ?? {};
console.log(msg);
return res.json({ response: msg });
} catch (error) {
console.log(error);
res.json({ info: "Internal Server Error", response: "" });
}
});
// Start the server
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});