@@ -18,21 +18,27 @@ export default {
1818 }
1919 } ;
2020 const { pathname } = new URL ( request . url ) ;
21- switch ( true ) {
22- case pathname . endsWith ( "/chat/completions" ) :
23- assert ( request . method === "POST" ) ;
24- return handleCompletions ( await request . json ( ) , apiKey )
25- . catch ( errHandler ) ;
26- case pathname . endsWith ( "/embeddings" ) :
27- assert ( request . method === "POST" ) ;
28- return handleEmbeddings ( await request . json ( ) , apiKey )
29- . catch ( errHandler ) ;
30- case pathname . endsWith ( "/models" ) :
31- assert ( request . method === "GET" ) ;
32- return handleModels ( apiKey )
33- . catch ( errHandler ) ;
34- default :
35- throw new HttpError ( "404 Not Found" , 404 ) ;
21+ const routeRegex = / ^ \/ ( [ \w . - ] + ) \/ v 1 \/ ( c h a t \/ c o m p l e t i o n s | e m b e d d i n g s ) $ / ;
22+ const match = pathname . match ( routeRegex ) ;
23+
24+ if ( ! match ) {
25+ throw new HttpError ( "404 Not Found. Use format: /<model-name>/v1/chat/completions" , 404 ) ;
26+ }
27+
28+ const modelFromUrl = match [ 1 ] ;
29+ const endpoint = match [ 2 ] ;
30+ let body ;
31+ try {
32+ body = await request . json ( ) ;
33+ } catch ( e ) {
34+ body = { } ;
35+ }
36+ body . model = modelFromUrl ;
37+
38+ if ( endpoint === "chat/completions" ) {
39+ return handleCompletions ( body , apiKey ) . catch ( errHandler ) ;
40+ } else if ( endpoint === "embeddings" ) {
41+ return handleEmbeddings ( body , apiKey ) . catch ( errHandler ) ;
3642 }
3743 } catch ( err ) {
3844 return errHandler ( err ) ;
0 commit comments