fix(hermes): create custom sendMessageWithCustomHistory function
- Add sendMessageWithCustomHistory function to handle intro generation - Fix TypeError: sendMessageWithHistory is not a function - Custom function allows intro to use different system prompt than conversation - Maintains streaming response for real-time intro generation - Fixes intro message generation with proper API call handling
This commit is contained in:
parent
4110a8130b
commit
4486cec23b
1 changed files with 58 additions and 4 deletions
62
src/ui.js
62
src/ui.js
|
|
@ -5,6 +5,63 @@ import { extractWebpageContent } from "./content.js";
|
|||
import { speakText } from "./tts.js";
|
||||
import { TTS_API_URL, API_KEY, setSystemMessageAppend } from "./config.js";
|
||||
|
||||
// Send message with custom history (for intro generation)
|
||||
async function* sendMessageWithCustomHistory(messageHistory) {
|
||||
const { getSelectedModel, getSelectedModelEndpoint } = await import('./models.js');
|
||||
const apiUrl = `${getSelectedModelEndpoint()}/chat/completions`;
|
||||
|
||||
const response = await fetch(apiUrl, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${API_KEY}`,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model: getSelectedModel(),
|
||||
messages: messageHistory,
|
||||
temperature: 0.3,
|
||||
max_tokens: 8192,
|
||||
stream: true,
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
const reader = response.body.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
let buffer = "";
|
||||
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
|
||||
buffer += decoder.decode(value, { stream: true });
|
||||
const lines = buffer.split("\n");
|
||||
|
||||
for (let i = 0; i < lines.length - 1; i++) {
|
||||
const line = lines[i].trim();
|
||||
if (line.startsWith("data: ")) {
|
||||
const jsonData = line.slice(6);
|
||||
if (jsonData === "[DONE]") continue;
|
||||
|
||||
try {
|
||||
const parsedData = JSON.parse(jsonData);
|
||||
const content = parsedData.choices[0].delta.content;
|
||||
if (content) {
|
||||
yield content;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error parsing JSON:", error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
buffer = lines[lines.length - 1];
|
||||
}
|
||||
}
|
||||
|
||||
// Simple TTS function for chat messages (no preprocessing needed)
|
||||
async function speakChatText(text, voice = "alloy", rate = 1.0) {
|
||||
try {
|
||||
|
|
@ -1388,10 +1445,7 @@ Reference specific details from the page content to show understanding.`;
|
|||
|
||||
// Generate intro with specialized system prompt
|
||||
let response = '';
|
||||
const { sendMessage: sendMessageWithHistory } = await import('./translation.js');
|
||||
|
||||
// Use the translation sendMessage which accepts custom history
|
||||
for await (const chunk of sendMessageWithHistory(introHistory)) {
|
||||
for await (const chunk of sendMessageWithCustomHistory(introHistory)) {
|
||||
response += chunk;
|
||||
introMsg.innerHTML = marked.parse(response);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue