add ollama package and support for ollama-hosted model

This commit is contained in:
kwaroran
2024-04-17 21:01:56 +09:00
parent e10723aec5
commit 4f8477538d
7 changed files with 61 additions and 0 deletions

View File

@@ -55,6 +55,7 @@
"mobile-drag-drop": "3.0.0-rc.0", "mobile-drag-drop": "3.0.0-rc.0",
"msgpackr": "^1.10.1", "msgpackr": "^1.10.1",
"node-html-parser": "^6.1.12", "node-html-parser": "^6.1.12",
"ollama": "^0.5.0",
"pdfjs-dist": "^4.0.379", "pdfjs-dist": "^4.0.379",
"peerjs": "^1.5.2", "peerjs": "^1.5.2",
"png-chunk-text": "^1.0.0", "png-chunk-text": "^1.0.0",

13
pnpm-lock.yaml generated
View File

@@ -122,6 +122,9 @@ dependencies:
node-html-parser: node-html-parser:
specifier: ^6.1.12 specifier: ^6.1.12
version: 6.1.12 version: 6.1.12
ollama:
specifier: ^0.5.0
version: 0.5.0
pdfjs-dist: pdfjs-dist:
specifier: ^4.0.379 specifier: ^4.0.379
version: 4.0.379 version: 4.0.379
@@ -4390,6 +4393,12 @@ packages:
resolution: {integrity: sha512-5qoj1RUiKOMsCCNLV1CBiPYE10sziTsnmNxkAI/rZhiD63CF7IqdFGC/XzjWjpSgLf0LxXX3bDFIh0E18f6UhQ==} resolution: {integrity: sha512-5qoj1RUiKOMsCCNLV1CBiPYE10sziTsnmNxkAI/rZhiD63CF7IqdFGC/XzjWjpSgLf0LxXX3bDFIh0E18f6UhQ==}
dev: false dev: false
/ollama@0.5.0:
resolution: {integrity: sha512-CRtRzsho210EGdK52GrUMohA2pU+7NbgEaBG3DcYeRmvQthDO7E2LHOkLlUUeaYUlNmEd8icbjC02ug9meSYnw==}
dependencies:
whatwg-fetch: 3.6.20
dev: false
/on-finished@2.4.1: /on-finished@2.4.1:
resolution: {integrity: sha512-oVlzkg3ENAhCk2zdv7IJwd/QUD4z2RxRwpkcGY8psCVcCYZNq4wYnVWALHM+brtuJjePWiYF/ClmuDr8Ch5+kg==} resolution: {integrity: sha512-oVlzkg3ENAhCk2zdv7IJwd/QUD4z2RxRwpkcGY8psCVcCYZNq4wYnVWALHM+brtuJjePWiYF/ClmuDr8Ch5+kg==}
engines: {node: '>= 0.8'} engines: {node: '>= 0.8'}
@@ -6100,6 +6109,10 @@ packages:
iconv-lite: 0.6.3 iconv-lite: 0.6.3
dev: false dev: false
/whatwg-fetch@3.6.20:
resolution: {integrity: sha512-EqhiFU6daOA8kpjOWTL0olhVOF3i7OrFzSYiGsEMB8GcXS+RrzauAERX65xMeNWVqxA6HXH2m69Z9LaKKdisfg==}
dev: false
/whatwg-mimetype@4.0.0: /whatwg-mimetype@4.0.0:
resolution: {integrity: sha512-QaKxh0eNIi2mE9p2vEdzfagOKHCcj1pJ56EEHGQOVxp8r9/iszLUUV7v89x9O1p/T+NlTM5W7jW6+cz4Fq1YVg==} resolution: {integrity: sha512-QaKxh0eNIi2mE9p2vEdzfagOKHCcj1pJ56EEHGQOVxp8r9/iszLUUV7v89x9O1p/T+NlTM5W7jW6+cz4Fq1YVg==}
engines: {node: '>=18'} engines: {node: '>=18'}

View File

@@ -203,6 +203,13 @@
<span class="text-textcolor mt-4">Risu {language.apiKey}</span> <span class="text-textcolor mt-4">Risu {language.apiKey}</span>
<TextInput marginBottom={false} size={"sm"} bind:value={$DataBase.proxyKey} /> <TextInput marginBottom={false} size={"sm"} bind:value={$DataBase.proxyKey} />
{/if} {/if}
{#if $DataBase.aiModel === 'ollama-hosted'}
<span class="text-textcolor mt-4">Ollama URL</span>
<TextInput marginBottom={false} size={"sm"} bind:value={$DataBase.ollamaURL} />
<span class="text-textcolor mt-4">Ollama Model</span>
<TextInput marginBottom={false} size={"sm"} bind:value={$DataBase.ollamaModel} />
{/if}
{#if $DataBase.aiModel === 'openrouter' || $DataBase.subModel === 'openrouter'} {#if $DataBase.aiModel === 'openrouter' || $DataBase.subModel === 'openrouter'}
<span class="text-textcolor mt-4">Openrouter Key</span> <span class="text-textcolor mt-4">Openrouter Key</span>
<TextInput marginBottom={false} size={"sm"} bind:value={$DataBase.openrouterKey} /> <TextInput marginBottom={false} size={"sm"} bind:value={$DataBase.openrouterKey} />

View File

@@ -132,6 +132,7 @@
{/each} {/each}
{/await} {/await}
</Arcodion> </Arcodion>
<button class="hover:bg-selected px-6 py-2 text-lg" on:click={() => {changeModel('ollama-hosted')}}>OLlama</button>
{#if showUnrec} {#if showUnrec}
<Arcodion name="WebLLM Local"> <Arcodion name="WebLLM Local">
<button class="hover:bg-selected px-6 py-2 text-lg" on:click={() => {changeModel('hf:::Xenova/opt-350m')}}>opt-350m</button> <button class="hover:bg-selected px-6 py-2 text-lg" on:click={() => {changeModel('hf:::Xenova/opt-350m')}}>opt-350m</button>

View File

@@ -95,6 +95,8 @@ export function getModelName(name:string){
return 'GPT-4 Turbo (20240409)' return 'GPT-4 Turbo (20240409)'
case 'gemini-1.5-pro-latest': case 'gemini-1.5-pro-latest':
return 'Gemini 1.5 Pro' return 'Gemini 1.5 Pro'
case 'ollama-hosted':
return 'Ollama'
default: default:
if(name.startsWith("horde:::")){ if(name.startsWith("horde:::")){
const split = name.split(":::") const split = name.split(":::")

View File

@@ -23,6 +23,7 @@ import { Capacitor } from "@capacitor/core";
import { getFreeOpenRouterModel } from "../model/openrouter"; import { getFreeOpenRouterModel } from "../model/openrouter";
import { runTransformers } from "./transformers"; import { runTransformers } from "./transformers";
import {createParser, type ParsedEvent, type ReconnectInterval} from 'eventsource-parser' import {createParser, type ParsedEvent, type ReconnectInterval} from 'eventsource-parser'
import {Ollama} from 'ollama/dist/browser.mjs'
@@ -1494,6 +1495,38 @@ export async function requestChatDataMain(arg:requestDataArgument, model:'model'
result: resp.replace(/\\n/g, '\n') result: resp.replace(/\\n/g, '\n')
} }
} }
case 'ollama-hosted':{
const ollama = new Ollama({host: 'http://localhost:11434'})
const response = await ollama.chat({
model: db.ollamaModel,
messages: formated.map((v) => {
return {
role: v.role,
content: v.content
}
}).filter((v) => {
return v.role === 'assistant' || v.role === 'user' || v.role === 'system'
}),
stream: true
})
const readableStream = new ReadableStream<StreamResponseChunk>({
async start(controller){
for await(const chunk of response){
controller.enqueue({
"0": chunk.message.content
})
}
controller.close()
}
})
return {
type: 'streaming',
result: readableStream
}
}
default:{ default:{
if(raiModel.startsWith('claude-3')){ if(raiModel.startsWith('claude-3')){
let replacerURL = (aiModel === 'reverse_proxy') ? (db.forceReplaceUrl) : ('https://api.anthropic.com/v1/messages') let replacerURL = (aiModel === 'reverse_proxy') ? (db.forceReplaceUrl) : ('https://api.anthropic.com/v1/messages')

View File

@@ -383,6 +383,8 @@ export function setDatabase(data:Database){
data.heightMode ??= 'normal' data.heightMode ??= 'normal'
data.antiClaudeOverload ??= false data.antiClaudeOverload ??= false
data.maxSupaChunkSize ??= 1200 data.maxSupaChunkSize ??= 1200
data.ollamaURL ??= ''
data.ollamaModel ??= ''
changeLanguage(data.language) changeLanguage(data.language)
DataBase.set(data) DataBase.set(data)
@@ -618,6 +620,8 @@ export interface Database{
noWaitForTranslate:boolean noWaitForTranslate:boolean
antiClaudeOverload:boolean antiClaudeOverload:boolean
maxSupaChunkSize:number maxSupaChunkSize:number
ollamaURL:string
ollamaModel:string
} }
export interface customscript{ export interface customscript{