θΏ™ζ˜―indexlocζδΎ›ηš„ζœεŠ‘οΌŒδΈθ¦θΎ“ε…₯任何密码
Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 51 additions & 26 deletions server/utils/AiProviders/ollama/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,14 @@ class OllamaAILLM {
});
const textResponse = await model
.pipe(new StringOutputParser())
.invoke(this.#convertToLangchainPrototypes(messages));
.invoke(this.#convertToLangchainPrototypes(messages))
.catch((e) => {
throw new Error(
`Ollama::getChatCompletion failed to communicate with Ollama. ${e.message}`
);
});

if (!textResponse.length)
if (!textResponse || !textResponse.length)
throw new Error(`Ollama::sendChat text response was empty.`);

return textResponse;
Expand Down Expand Up @@ -150,9 +155,14 @@ class OllamaAILLM {
const model = this.#ollamaClient({ temperature });
const textResponse = await model
.pipe(new StringOutputParser())
.invoke(this.#convertToLangchainPrototypes(messages));
.invoke(this.#convertToLangchainPrototypes(messages))
.catch((e) => {
throw new Error(
`Ollama::getChatCompletion failed to communicate with Ollama. ${e.message}`
);
});

if (!textResponse.length)
if (!textResponse || !textResponse.length)
throw new Error(`Ollama::getChatCompletion text response was empty.`);

return textResponse;
Expand All @@ -170,34 +180,49 @@ class OllamaAILLM {
const { uuid = uuidv4(), sources = [] } = responseProps;

return new Promise(async (resolve) => {
let fullText = "";
for await (const chunk of stream) {
if (chunk === undefined)
throw new Error(
"Stream returned undefined chunk. Aborting reply - check model provider logs."
);

const content = chunk.hasOwnProperty("content") ? chunk.content : chunk;
fullText += content;
try {
let fullText = "";
for await (const chunk of stream) {
if (chunk === undefined)
throw new Error(
"Stream returned undefined chunk. Aborting reply - check model provider logs."
);

const content = chunk.hasOwnProperty("content")
? chunk.content
: chunk;
fullText += content;
writeResponseChunk(response, {
uuid,
sources: [],
type: "textResponseChunk",
textResponse: content,
close: false,
error: false,
});
}

writeResponseChunk(response, {
uuid,
sources: [],
sources,
type: "textResponseChunk",
textResponse: content,
close: false,
textResponse: "",
close: true,
error: false,
});
resolve(fullText);
} catch (error) {
writeResponseChunk(response, {
uuid,
sources: [],
type: "textResponseChunk",
textResponse: "",
close: true,
error: `Ollama:streaming - could not stream chat. ${
error?.cause ?? error.message
}`,
});
}

writeResponseChunk(response, {
uuid,
sources,
type: "textResponseChunk",
textResponse: "",
close: true,
error: false,
});
resolve(fullText);
});
}

Expand Down