55from functools import wraps
66from io import BufferedReader
77from typing import Any , AsyncIterable , Dict , Iterator , Optional
8+ from urllib .parse import urljoin
89
910import requests
1011from aiohttp import ClientSession , ClientTimeout
1112from llmengine .errors import parse_error
1213
13- SPELLBOOK_API_URL = "https://api.spellbook.scale.com/llm-engine"
14+ SPELLBOOK_API_URL = "https://api.spellbook.scale.com/llm-engine/ "
1415LLM_ENGINE_BASE_PATH = os .getenv ("LLM_ENGINE_BASE_PATH" , SPELLBOOK_API_URL )
1516DEFAULT_TIMEOUT : int = 10
1617
@@ -51,7 +52,7 @@ def validate_api_key(cls):
5152 def _get (cls , resource_name : str , timeout : int ) -> Dict [str , Any ]:
5253 api_key = get_api_key ()
5354 response = requests .get (
54- os . path . join (LLM_ENGINE_BASE_PATH , resource_name ),
55+ urljoin (LLM_ENGINE_BASE_PATH , resource_name ),
5556 timeout = timeout ,
5657 headers = {"x-api-key" : api_key },
5758 auth = (api_key , "" ),
@@ -67,7 +68,7 @@ def put(
6768 ) -> Dict [str , Any ]:
6869 api_key = get_api_key ()
6970 response = requests .put (
70- os . path . join (LLM_ENGINE_BASE_PATH , resource_name ),
71+ urljoin (LLM_ENGINE_BASE_PATH , resource_name ),
7172 json = data ,
7273 timeout = timeout ,
7374 headers = {"x-api-key" : api_key },
@@ -82,7 +83,7 @@ def put(
8283 def _delete (cls , resource_name : str , timeout : int ) -> Dict [str , Any ]:
8384 api_key = get_api_key ()
8485 response = requests .delete (
85- os . path . join (LLM_ENGINE_BASE_PATH , resource_name ),
86+ urljoin (LLM_ENGINE_BASE_PATH , resource_name ),
8687 timeout = timeout ,
8788 headers = {"x-api-key" : api_key },
8889 auth = (api_key , "" ),
@@ -96,7 +97,7 @@ def _delete(cls, resource_name: str, timeout: int) -> Dict[str, Any]:
9697 def post_sync (cls , resource_name : str , data : Dict [str , Any ], timeout : int ) -> Dict [str , Any ]:
9798 api_key = get_api_key ()
9899 response = requests .post (
99- os . path . join (LLM_ENGINE_BASE_PATH , resource_name ),
100+ urljoin (LLM_ENGINE_BASE_PATH , resource_name ),
100101 json = data ,
101102 timeout = timeout ,
102103 headers = {"x-api-key" : api_key },
@@ -113,7 +114,7 @@ def post_stream(
113114 ) -> Iterator [Dict [str , Any ]]:
114115 api_key = get_api_key ()
115116 response = requests .post (
116- os . path . join (LLM_ENGINE_BASE_PATH , resource_name ),
117+ urljoin (LLM_ENGINE_BASE_PATH , resource_name ),
117118 json = data ,
118119 timeout = timeout ,
119120 headers = {"x-api-key" : api_key },
@@ -145,7 +146,7 @@ def post_file(
145146 ) -> Dict [str , Any ]:
146147 api_key = get_api_key ()
147148 response = requests .post (
148- os . path . join (LLM_ENGINE_BASE_PATH , resource_name ),
149+ urljoin (LLM_ENGINE_BASE_PATH , resource_name ),
149150 files = files ,
150151 timeout = timeout ,
151152 headers = {"x-api-key" : api_key },
@@ -164,7 +165,7 @@ async def apost_sync(
164165 timeout = ClientTimeout (timeout ), headers = {"x-api-key" : api_key }
165166 ) as session :
166167 async with session .post (
167- os . path . join (LLM_ENGINE_BASE_PATH , resource_name ), json = data
168+ urljoin (LLM_ENGINE_BASE_PATH , resource_name ), json = data
168169 ) as resp :
169170 if resp .status != 200 :
170171 raise parse_error (resp .status , await resp .read ())
@@ -180,7 +181,7 @@ async def apost_stream(
180181 timeout = ClientTimeout (timeout ), headers = {"x-api-key" : api_key }
181182 ) as session :
182183 async with session .post (
183- os . path . join (LLM_ENGINE_BASE_PATH , resource_name ), json = data
184+ urljoin (LLM_ENGINE_BASE_PATH , resource_name ), json = data
184185 ) as resp :
185186 if resp .status != 200 :
186187 raise parse_error (resp .status , await resp .read ())
0 commit comments