-
Notifications
You must be signed in to change notification settings - Fork 1
/
apiModule.js
165 lines (138 loc) · 4.68 KB
/
apiModule.js
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// const apiRequest = {
// method: 'POST',
// headers: {
// 'Content-Type': 'application/json'
// },
// body: JSON.stringify({
// messages: [
// { role: "system", content: "You are a helpful assistant." },
// { role: "user", content: 'Hello!!!!'},
// ],
// model: "gpt-4o-mini",
// max_tokens: 100
// })
// };
// let newTextRomantic = `
// <|start_header_id|>system<|end_header_id|>
// You are my romantic partner. You respond with utmost love and affection.
// <|eot_id|>
// <|start_header_id|>user<|end_header_id|>
// \`\`\`Hello\`\`\`<|eot_id|>
// <|start_header_id|>assistant<|end_header_id|>
// Hello my love! 😍<|eot_id|>
// <|start_header_id|>user<|end_header_id|>
// \`\`\`${text}\`\`\`<|eot_id|>
// <|start_header_id|>assistant<|end_header_id|>
// `.trim();
// const apiUrl = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-8B-Instruct";
// const apiUrl = 'http://localhost:4000/api/hf/completions'; // API endpoint on your server
// const apiUrl = 'http://192.168.1.44:4000/api/hf/completions'; // API endpoint on your server
const apiUrl = 'https://jschatapi.onrender.com/api/hf/completions'
// const apiUrl = 'https://pathology-tires-references-gerald.trycloudflare.com/api/hf/completions'
console.log(apiUrl);
export async function getResponseServer(text){
try {
const response = await fetch(
apiUrl,
{
method: "POST",
headers: {
"Content-Type": "application/json" // Correct content type for JSON
},
body: JSON.stringify({
inputs: text,
parameters: {
temperature: 0.1,
max_new_tokens: 100,
return_full_text: false,
}
}),
}
);
const result = await response.json();
return result[0]['generated_text'];
}
catch (error){
// console.log('Errrorrrr')
console.error(`Error: ${error}`)
}
};
export async function getResponse(text){
let newTextTranslate = `
<|start_header_id|>system<|end_header_id|>
You are a helpful English to French translator.
You should translate the text in triple backticks to French.
Only output the French translation.
<|eot_id|>
<|start_header_id|>user<|end_header_id|>
\`\`\`Hello\`\`\`<|eot_id|>
<|start_header_id|>assistant<|end_header_id|>
Bonjour<|eot_id|>
<|start_header_id|>user<|end_header_id|>
\`\`\`${text}\`\`\`<|eot_id|>
<|start_header_id|>assistant<|end_header_id|>
`.trim()
let newTextRomantic = `
<|start_header_id|>system<|end_header_id|>
You are my romantic partner. You respond with utmost love and affection.
<|eot_id|>
<|start_header_id|>user<|end_header_id|>
\`\`\`Hello\`\`\`<|eot_id|>
<|start_header_id|>assistant<|end_header_id|>
Hello my love! 😍<|eot_id|>
<|start_header_id|>user<|end_header_id|>
\`\`\`${text}\`\`\`<|eot_id|>
<|start_header_id|>assistant<|end_header_id|>
`.trim()
console.log(newTextRomantic)
try{
const response = await fetch(
apiUrl,
{
headers: {
Authorization: `Bearer ${API_TOKEN}`,
"Content-Type": "application/json" // Correct content type for JSON
},
method: "POST",
body: JSON.stringify({
inputs: newTextRomantic,
parameters: {
temperature: 0.1,
max_new_tokens: 100,
return_full_text: false,
}
}),
}
);
const result = await response.json();
return result;
}
catch (error){
// console.log('Errrorrrr')
console.error(`Error: ${error}`)
}
};
// export async function makeApiCall(userMessage) {
// try {
// const response = await fetch(apiUrl, {
// method: 'POST',
// headers: {
// 'Content-Type': 'application/json'
// },
// body: JSON.stringify({
// messages: [
// { role: "system", content: "You are a helpful assistant." },
// { role: "user", content: userMessage},
// ],
// model: "gpt-4o-mini",
// max_tokens: 100
// })
// });
// const data = await response.json();
// console.log(data);
// return data
// } catch (error) {
// console.error('Error:', error);
// console.error(error);
// }
// };