Spaces:
Running
Running
DeFactOfficial
commited on
Commit
•
63e9d2a
1
Parent(s):
f84190a
Add html generation
Browse files
llm.js
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Default configuration
|
2 |
+
const DEFAULT_CONFIG = {
|
3 |
+
model: 'deepseek-ai/DeepSeek-V2.5',
|
4 |
+
temperature: 0.7,
|
5 |
+
max_tokens: 2048,
|
6 |
+
top_p: 0.95
|
7 |
+
};
|
8 |
+
const { HfInference } = require('@huggingface/inference');
|
9 |
+
|
10 |
+
// Initialize HuggingFace client
|
11 |
+
const hf = new HfInference(process.env.HF_API_TOKEN);
|
12 |
+
|
13 |
+
const generateHtml = async(req, res) => {
|
14 |
+
try {
|
15 |
+
const userPrompt = req.query.prompt;
|
16 |
+
|
17 |
+
if (!userPrompt) {
|
18 |
+
return res.status(400).json({
|
19 |
+
error: 'Missing required "prompt" query parameter'
|
20 |
+
});
|
21 |
+
}
|
22 |
+
|
23 |
+
// Build configuration by merging defaults with any provided query parameters
|
24 |
+
const config = {
|
25 |
+
...DEFAULT_CONFIG,
|
26 |
+
...(req.query.model && { model: req.query.model }),
|
27 |
+
...(req.query.temperature && { temperature: parseFloat(req.query.temperature) }),
|
28 |
+
...(req.query.max_tokens && { max_tokens: parseInt(req.query.max_tokens) }),
|
29 |
+
...(req.query.top_p && { top_p: parseFloat(req.query.top_p) })
|
30 |
+
};
|
31 |
+
|
32 |
+
// Create the system and user messages
|
33 |
+
const messages = [
|
34 |
+
{
|
35 |
+
role: "system",
|
36 |
+
content: `You are a specialized AI model for generating webpage content.
|
37 |
+
Your responses must always be valid HTML that can be directly rendered in a browser.
|
38 |
+
You can include JavaScript and CSS within appropriate tags if the request calls for interactive elements.
|
39 |
+
Ensure all HTML is properly structured and follows the specified framework requirements.
|
40 |
+
Generate only the HTML content - do not include any explanations or markdown.
|
41 |
+
Do not wrap the response in code blocks or quotes - respond with pure HTML.`
|
42 |
+
},
|
43 |
+
{
|
44 |
+
role: "user",
|
45 |
+
content: userPrompt
|
46 |
+
}
|
47 |
+
];
|
48 |
+
|
49 |
+
// Make chat completion call to HuggingFace with merged configuration
|
50 |
+
const result = await hf.chatCompletion({
|
51 |
+
...config,
|
52 |
+
messages: messages
|
53 |
+
});
|
54 |
+
|
55 |
+
// Extract generated HTML from the response
|
56 |
+
const generatedHtml = result.generated_text.trim();
|
57 |
+
|
58 |
+
// Validate that the response contains HTML
|
59 |
+
|
60 |
+
/* Not a valid check...
|
61 |
+
if (!generatedHtml.includes('<')) {
|
62 |
+
throw new Error('Generated content does not appear to be valid HTML');
|
63 |
+
} */
|
64 |
+
|
65 |
+
// Set content type to HTML and send response
|
66 |
+
res.setHeader('Content-Type', 'text/html');
|
67 |
+
res.send(generatedHtml);
|
68 |
+
|
69 |
+
} catch (error) {
|
70 |
+
console.error('HTML generation error:', error);
|
71 |
+
res.status(500).json({
|
72 |
+
error: error.message,
|
73 |
+
details: error.message
|
74 |
+
});
|
75 |
+
}
|
76 |
+
});
|
77 |
+
|
78 |
+
module.exports = {generateHtml}
|