@@ -7,6 +7,8 @@ import { escapeShell, concatPath } from "./utils.js"
7
7
import { useOpenai , useOpenaiChat } from "./apis/openai/api.js"
8
8
import { useBing } from "./apis/bing/api.js"
9
9
import { useHuggingface } from "./apis/huggingface/api.js"
10
+ import { $ } from "zx"
11
+ $ . verbose = false
10
12
dotenv . config ( )
11
13
12
14
// directory of this file
@@ -81,38 +83,57 @@ export async function useLlm(args) {
81
83
case "bing-precise" :
82
84
completion = await useBing ( { print, args } )
83
85
break
86
+ case "wlm" :
87
+ case "wizardlm-7b-uncensored" :
88
+ if ( ! args . modelWasSet ) {
89
+ args . model = "WizardLM-7B-Uncensored/ggml-model-q4_0.gguf"
90
+ args . modelContextSize = 4096
91
+ args . modelWasSet = true
92
+ }
93
+ case "wlm13" :
94
+ case "wizardlm-13b" :
95
+ if ( ! args . modelWasSet ) {
96
+ args . model =
97
+ "WizardLM-1.0-Uncensored-Llama2-13b/ggml-model-q4_0.gguf"
98
+ args . modelContextSize = 2048
99
+ args . modelWasSet = true
100
+ }
101
+ case "__wizardlm-anymodel__" :
102
+ const randInt = Math . floor ( Math . random ( ) * 1000000 )
103
+ const promptPath = `/tmp/llm-prompt.tmp.${ randInt } `
104
+ await fs . promises . writeFile ( promptPath , args . prompt )
105
+ const basePath = "/Users/snwfdhmp/Dev/workspaces/ai"
106
+ completion =
107
+ await $ `${ basePath } /llama.cpp-custom/main -f "${ promptPath } " -m ${ basePath } /models/${ args . model } -n -2 -c ${ args . modelContextSize } -ngl 1 2>/dev/null`
108
+ completion = completion . stdout
109
+ . slice ( 1 + args . prompt . length )
110
+ . trimStart ( )
111
+ print ( completion )
112
+ await fs . promises . unlink ( promptPath )
113
+ break
84
114
default :
85
115
console . log ( `model ${ args . model } is known but not supported yet` )
86
116
process . exit ( 1 )
87
117
break
88
118
}
89
119
} catch ( e ) {
90
- // if 429 error
91
- if ( e . message . includes ( "429" ) ) {
92
- if ( ! args . quiet )
93
- console . log (
94
- `getCompletion: too many requests (429), waiting ${ args . backoff } ms`
95
- )
96
- await new Promise ( ( resolve ) => setTimeout ( resolve , args . backoff ) )
97
- return await getCompletion ( { ...args , backoff : args . backoff * 2 } )
98
- }
99
- if ( e . message . includes ( "503" ) ) {
100
- if ( ! args . quiet )
101
- console . log (
102
- `getCompletion: service unavailable (503), waiting ${ args . backoff } ms`
103
- )
104
- await new Promise ( ( resolve ) => setTimeout ( resolve , args . backoff ) )
105
- return await getCompletion ( { ...args , backoff : args . backoff * 2 } )
106
- }
107
- if ( e . message . includes ( "502" ) ) {
120
+ // handle network errors with backoff
121
+ const errorHandlers = [
122
+ { code : 429 , message : "too many requests" } ,
123
+ { code : 503 , message : "service unavailable" } ,
124
+ { code : 502 , message : "bad gateway" } ,
125
+ ]
126
+ for ( const errorHandler of errorHandlers ) {
127
+ if ( ! e . message . includes ( `${ errorHandler . code } ` ) ) continue
108
128
if ( ! args . quiet )
109
129
console . log (
110
- `getCompletion: bad gateway (502 ), waiting ${ args . backoff } ms`
130
+ `getCompletion: ${ errorHandler . message } ( ${ errorHandler . code } ), waiting ${ args . backoff } ms`
111
131
)
112
132
await new Promise ( ( resolve ) => setTimeout ( resolve , args . backoff ) )
113
133
return await getCompletion ( { ...args , backoff : args . backoff * 2 } )
114
134
}
115
135
136
+ // default error handler
116
137
console . error ( `Error: ${ e . message } ` )
117
138
console . log ( e )
118
139
return
0 commit comments