Skip to content

Commit

Permalink
fix: used _http methods
Browse files Browse the repository at this point in the history
  • Loading branch information
asafgardin committed Dec 12, 2023
1 parent 5b753ea commit e7babfc
Show file tree
Hide file tree
Showing 14 changed files with 31 additions and 43 deletions.
13 changes: 7 additions & 6 deletions ai21/clients/bedrock/resources/bedrock_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ def create(
model_id: str,
prompt: str,
*,
max_tokens: int = 64,
num_results: int = 1,
min_tokens=0,
temperature=0.7,
top_p=1,
top_k_return=0,
max_tokens: Optional[int] = None,
num_results: Optional[int] = 1,
min_tokens: Optional[int] = 0,
temperature: Optional[float] = 0.7,
top_p: Optional[int] = 1,
top_k_return: Optional[int] = 0,
stop_sequences: Optional[List[str]] = None,
frequency_penalty: Optional[Dict[str, Any]] = None,
presence_penalty: Optional[Dict[str, Any]] = None,
count_penalty: Optional[Dict[str, Any]] = None,
**kwargs,
) -> CompletionsResponse:
body = {
"prompt": prompt,
Expand Down
1 change: 1 addition & 0 deletions ai21/clients/sagemaker/ai21_sagemaker_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def __init__(
timeout_sec: Optional[float] = None,
num_retries: Optional[int] = None,
env_config: _AI21EnvConfig = AI21EnvConfig,
**kwargs,
):
super().__init__(
api_key=api_key,
Expand Down
1 change: 1 addition & 0 deletions ai21/clients/studio/ai21_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def __init__(
headers: Optional[Dict[str, Any]] = None,
timeout_sec: Optional[float] = None,
num_retries: Optional[int] = None,
**kwargs,
):
super().__init__(
api_key=api_key,
Expand Down
5 changes: 2 additions & 3 deletions ai21/clients/studio/resources/studio_answer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ def create(
mode: Optional[str] = None,
**kwargs,
) -> AnswerResponse:
url = self._client.get_base_url()
url = f"{url}/{self._MODULE_NAME}"
url = f"{self._client.get_base_url()}/{self._MODULE_NAME}"

params = {
"context": context,
Expand All @@ -25,6 +24,6 @@ def create(
"mode": mode,
}

response = self._invoke(url=url, body=params)
response = self._post(url=url, body=params)

return self._json_to_response(response)
2 changes: 1 addition & 1 deletion ai21/clients/studio/resources/studio_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ def create(
"countPenalty": count_penalty,
}
url = f"{self._client.get_base_url()}/{model}/{self._module_name}"
response = self._invoke(url=url, body=params)
response = self._post(url=url, body=params)
return self._json_to_response(response)
14 changes: 7 additions & 7 deletions ai21/clients/studio/resources/studio_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ def create(
model: str,
prompt: str,
*,
max_tokens: int = 64,
num_results: int = 1,
min_tokens=0,
temperature=0.7,
top_p=1,
top_k_return=0,
max_tokens: Optional[int] = None,
num_results: Optional[int] = 1,
min_tokens: Optional[int] = None,
temperature: Optional[float] = None,
top_p: Optional[int] = None,
top_k_return: Optional[int] = None,
custom_model: Optional[str] = None,
experimental_mode: bool = False,
stop_sequences: Optional[List[str]] = None,
Expand Down Expand Up @@ -54,4 +54,4 @@ def create(
"countPenalty": count_penalty,
"epoch": epoch,
}
return self._json_to_response(self._invoke(url=url, body=body))
return self._json_to_response(self._post(url=url, body=body))
6 changes: 3 additions & 3 deletions ai21/clients/studio/resources/studio_custom_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ def create(
"learning_rate": learning_rate,
"num_epochs": num_epochs,
}
self._invoke(url=url, body=body)
self._post(url=url, body=body)

def list(self) -> List[CustomModelResponse]:
url = f"{self._client.get_base_url()}/{self._module_name}"
response = self._invoke(url=url, http_method="GET")
response = self._get(url=url)

return [self._json_to_response(r) for r in response]

def get(self, resource_id: str) -> CustomModelResponse:
url = f"{self._client.get_base_url()}/{self._module_name}/{resource_id}"
return self._json_to_response(self._invoke(url=url, http_method="GET"))
return self._json_to_response(self._get(url=url))
6 changes: 3 additions & 3 deletions ai21/clients/studio/resources/studio_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ def upload(
"split_ratio": split_ratio,
}

return self._invoke(
return self._post(
url=self._base_url(),
body=body,
files=files,
)

def list(self) -> List[DatasetResponse]:
response = self._invoke(url=self._base_url(), http_method="GET")
response = self._get(url=self._base_url())
return [self._json_to_response(r) for r in response]

def get(self, dataset_pid: str) -> DatasetResponse:
url = f"{self._base_url()}/{dataset_pid}"
response = self._invoke(url=url, http_method="GET")
response = self._get(url=url)

return self._json_to_response(response)

Expand Down
2 changes: 1 addition & 1 deletion ai21/clients/studio/resources/studio_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
class StudioEmbed(StudioResource, Embed):
def create(self, texts: List[str], type: Optional[str] = None, **kwargs) -> EmbedResponse:
url = f"{self._client.get_base_url()}/{self._module_name}"
response = self._invoke(url=url, body={"texts": texts, "type": type})
response = self._post(url=url, body={"texts": texts, "type": type})

return self._json_to_response(response)
2 changes: 1 addition & 1 deletion ai21/clients/studio/resources/studio_gec.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ def create(self, text: str, **kwargs) -> GECResponse:
"text": text,
}
url = f"{self._client.get_base_url()}/{self._module_name}"
response = self._invoke(url=url, body=body)
response = self._post(url=url, body=body)

return self._json_to_response(response)
2 changes: 1 addition & 1 deletion ai21/clients/studio/resources/studio_improvements.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ def create(self, text: str, types: List[str], **kwargs) -> ImprovementsResponse:

url = f"{self._client.get_base_url()}/{self._module_name}"

response = self._invoke(url=url, body={"text": text, "types": types})
response = self._post(url=url, body={"text": text, "types": types})

return self._json_to_response(response)
4 changes: 2 additions & 2 deletions ai21/clients/studio/resources/studio_paraphrase.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ def create(
) -> ParaphraseResponse:
body = {
"text": text,
# 'style': style, # found a bug in the API where `style` is set to allow_reuse=True
"style": style,
"startIndex": start_index,
"endIndex": end_index,
}
url = f"{self._client.get_base_url()}/{self._module_name}"
response = self._invoke(url=url, body=body)
response = self._post(url=url, body=body)

return self._json_to_response(response)
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ def create(
"focus": focus,
}
url = f"{self._client.get_base_url()}/{self._module_name}"
response = self._invoke(url=url, body=body)
response = self._post(url=url, body=body)
return self._json_to_response(response)
14 changes: 0 additions & 14 deletions ai21/resources/studio_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,6 @@ class StudioResource(ABC):
def __init__(self, client: AI21StudioClient):
self._client = client

def _invoke(
self,
url: str,
body: Optional[Dict[str, Any]] = None,
http_method: str = "POST",
files: Optional[Dict[str, Any]] = None,
) -> Dict[str, Any]:
return self._client.execute_http_request(
method=http_method,
url=url,
params=body or {},
files=files,
)

def _post(
self,
url: str,
Expand Down

0 comments on commit e7babfc

Please sign in to comment.