Add initial project setup with environment variables, server logic, and memory handling
This commit is contained in:
37
index.js
Normal file
37
index.js
Normal file
@@ -0,0 +1,37 @@
|
||||
fetch('http://localhost:8000', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
messages: [{ role: 'user', content: 'Hello, tell me a joke' }]
|
||||
})
|
||||
})
|
||||
.then(response => {
|
||||
const reader = response.body.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
let fullText = '';
|
||||
|
||||
function read() {
|
||||
reader.read().then(({ done, value }) => {
|
||||
if (done) {
|
||||
console.log('\n--- Stream complete ---');
|
||||
console.log('Full response:', fullText);
|
||||
return;
|
||||
}
|
||||
|
||||
const chunk = decoder.decode(value);
|
||||
const lines = chunk.split('\n').filter(line => line.startsWith('data: '));
|
||||
|
||||
lines.forEach(line => {
|
||||
const json = JSON.parse(line.replace('data: ', ''));
|
||||
if (json.content) {
|
||||
fullText += json.content;
|
||||
console.clear();
|
||||
console.log(fullText);
|
||||
}
|
||||
});
|
||||
|
||||
read();
|
||||
});
|
||||
}
|
||||
read();
|
||||
});
|
||||
Reference in New Issue
Block a user