@@ -7,6 +7,20 @@ const paramDefaults = {
77
88let generation_settings = null ;
99
10+ // Returns a new URL that starts with `urlPrefix` and ends with `path`. The
11+ // `path` must not begin with a slash. This is more robust than `new URL(path,
12+ // urlPrefix)` because it preserves the prefix's entire path, even when the
13+ // prefix has no trailing slash.
14+ function buildUrl ( urlPrefix , path ) {
15+ if ( path . startsWith ( '/' ) ) {
16+ throw new Error ( "path must not have a leading slash" ) ;
17+ }
18+ const base = new URL ( urlPrefix ) ;
19+ if ( ! base . pathname . endsWith ( '/' ) ) {
20+ base . pathname += '/' ;
21+ }
22+ return new URL ( path , base ) ;
23+ }
1024
1125// Completes the prompt as a generator. Recommended for most use cases.
1226//
@@ -28,7 +42,7 @@ export async function* llama(prompt, params = {}, config = {}) {
2842
2943 const completionParams = { ...paramDefaults , ...params , prompt } ;
3044
31- const response = await fetch ( ` ${ url_prefix } / completion` , {
45+ const response = await fetch ( buildUrl ( url_prefix , ' completion' ) , {
3246 method : 'POST' ,
3347 body : JSON . stringify ( completionParams ) ,
3448 headers : {
@@ -196,7 +210,7 @@ export const llamaComplete = async (params, controller, callback) => {
196210export const llamaModelInfo = async ( config = { } ) => {
197211 if ( ! generation_settings ) {
198212 const url_prefix = config . url_prefix || "" ;
199- const props = await fetch ( ` ${ url_prefix } / props` ) . then ( r => r . json ( ) ) ;
213+ const props = await fetch ( buildUrl ( url_prefix , ' props' ) ) . then ( r => r . json ( ) ) ;
200214 generation_settings = props . default_generation_settings ;
201215 }
202216 return generation_settings ;
0 commit comments