add model streaming

This commit is contained in:
Christian Rute 2024-10-30 19:21:33 +01:00
parent 0b8028635a
commit 78c854439a
4 changed files with 71 additions and 37 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
/.env.server /.env.server
/nginx.conf

View File

@ -1,35 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chat Interface</title>
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<div class="main-container">
<!-- Linke Seitenleiste für Chat-Sessions -->
<div class="sidebar" id="chatSidebar">
<h3>Gespeicherte Chats</h3>
<button id="newChatButton">Neuer Chat</button>
<div id="chatList"></div>
</div>
<!-- Chatbereich -->
<div class="chat-container">
<div class="chat-box" id="chatBox">
<!-- Nachrichten werden hier angezeigt -->
</div>
<div class="chat-input-container">
<textarea id="chatInput" class="chat-input" placeholder="Schreibe eine Nachricht..."></textarea>
<button id="sendButton" class="chat-send-button">Senden</button>
</div>
</div>
</div>
<script src="js/chat.js"></script>
</body>
</html>

View File

@ -170,5 +170,73 @@ chatInput.addEventListener('keydown', (event) => {
// Shift + Enter: Zeilenumbruch // Shift + Enter: Zeilenumbruch
chatInput.value += '\n'; // Zeilenumbruch in das Textfeld einfügen chatInput.value += '\n'; // Zeilenumbruch in das Textfeld einfügen
} }
let a = stream_api_open_ai()
console.log(a)
} }
}); });
/* OpenAI zeug */
async function getModels() {
try {
const response = await fetch(`http://localhost:8015/v1/models`, {
method: 'GET',
headers: {
'Authorization': `Bearer YOUR_API_KEY`,
'Content-Type': 'application/json',
}
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const models = await response.json();
return models.data[0].id
} catch (error) {
console.error('Error fetching models:', error);
}
}
async function stream_api_open_ai() {
const response = await fetch('http://localhost:8015/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer YOUR_API_KEY`,
},
body: JSON.stringify({
model: await getModels(),
messages: [
{ role: 'system', content: 'You are a knowledgeable assistant.' },
{ role: 'user', content: 'Hi, i have to go, bye.' }
],
stream: true,
temperature: 0.3
})
});
const reader = response.body.getReader();
const decoder = new TextDecoder('utf-8');
let result = '';
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value, { stream: true });
const lines = chunk.split('\n');
for (const line of lines) {
if (line && line.includes('data: ')) {
const json = JSON.parse(line.replace('data: ', ''));
if (json.choices[0].delta.content) {
result += json.choices[0].delta.content;
console.log(json.choices[0].delta.content); // Ausgabe in der Konsole
}
}
}
}
return result;
}

View File

@ -16,7 +16,7 @@ server {
# Weiterleitung für v1/models # Weiterleitung für v1/models
location /v1/models { location /v1/models {
proxy_pass http://ip:port/v1/models; # Ersetze 'anderer-server.com' durch die tatsächliche Domain oder IP proxy_pass http://ip:port/v1/models;
proxy_set_header Host $host; proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
@ -25,7 +25,7 @@ server {
# Weiterleitung für v1/chat/completions # Weiterleitung für v1/chat/completions
location /v1/chat/completions { location /v1/chat/completions {
proxy_pass http://ip:port/v1/chat/completions; # Ersetze 'anderer-server.com' durch die tatsächliche Domain oder IP proxy_pass http://ip:port/v1/chat/completions;
proxy_set_header Host $host; proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;