-
Notifications
You must be signed in to change notification settings - Fork 750
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Ollama native API to support keep alive parameters (#748)
* Add Chinese translation. * Optimize style. * Add Ollama native API to support keep alive parameters. * Optimized popup page style. * fix: Fixed data type for Ollama keep_alive parameter forever
- Loading branch information
Showing
13 changed files
with
241 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// ollama api version | ||
|
||
// There is a lot of duplicated code here, but it is very easy to refactor. | ||
// The current state is mainly convenient for making targeted changes at any time, | ||
// and it has not yet had a negative impact on maintenance. | ||
// If necessary, I will refactor. | ||
|
||
import { getUserConfig } from '../../config/index.mjs' | ||
import { fetchSSE } from '../../utils/fetch-ollama.mjs' | ||
import { getConversationPairs } from '../../utils/get-conversation-pairs.mjs' | ||
import { isEmpty } from 'lodash-es' | ||
import { pushRecord, setAbortController } from './shared.mjs' | ||
|
||
/** | ||
* @param {Browser.Runtime.Port} port | ||
* @param {string} question | ||
* @param {Session} session | ||
* @param {string} apiKey | ||
* @param {string} modelName | ||
*/ | ||
export async function generateAnswersWithOllamaApi(port, question, session, apiKey, modelName) { | ||
const { controller, messageListener, disconnectListener } = setAbortController(port) | ||
|
||
const config = await getUserConfig() | ||
const prompt = getConversationPairs( | ||
session.conversationRecords.slice(-config.maxConversationContextLength), | ||
false, | ||
) | ||
// prompt.unshift({ role: 'system', content: await getOllamaApiPromptBase() }) | ||
prompt.push({ role: 'user', content: question }) | ||
const apiUrl = config.ollamaEndpoint | ||
|
||
let answer = '' | ||
let finished = false | ||
const finish = () => { | ||
finished = true | ||
pushRecord(session, question, answer) | ||
console.debug('conversation history', { content: session.conversationRecords }) | ||
port.postMessage({ answer: null, done: true, session: session }) | ||
} | ||
await fetchSSE(`${apiUrl}/api/chat`, { | ||
method: 'POST', | ||
signal: controller.signal, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${apiKey}`, | ||
}, | ||
body: JSON.stringify({ | ||
messages: prompt, | ||
model: modelName, | ||
stream: true, | ||
keep_alive: config.keepAliveTime === '-1' ? -1 : config.keepAliveTime, | ||
}), | ||
onMessage(message) { | ||
console.debug('sse message', message) | ||
if (finished) return | ||
let data = message | ||
const delta = data.message?.content | ||
if (delta) { | ||
answer += delta | ||
port.postMessage({ answer: answer, done: false, session: null }) | ||
} | ||
if (data.done_reason) { | ||
finish() | ||
return | ||
} | ||
}, | ||
async onStart() {}, | ||
async onEnd() { | ||
port.postMessage({ done: true }) | ||
port.onMessage.removeListener(messageListener) | ||
port.onDisconnect.removeListener(disconnectListener) | ||
}, | ||
async onError(resp) { | ||
port.onMessage.removeListener(messageListener) | ||
port.onDisconnect.removeListener(disconnectListener) | ||
if (resp instanceof Error) throw resp | ||
const error = await resp.json().catch(() => ({})) | ||
throw new Error(!isEmpty(error) ? JSON.stringify(error) : `${resp.status} ${resp.statusText}`) | ||
}, | ||
}) | ||
} |
Oops, something went wrong.