-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from parea-ai/update-pkg
update naming and imports
- Loading branch information
Showing
11 changed files
with
153 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
API_KEY=<API_KEY> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from typing import Any, Optional | ||
|
||
import httpx | ||
|
||
|
||
class HTTPClient: | ||
_instance = None | ||
base_url = "http://localhost:8000/api/parea/v1" | ||
|
||
def __new__(cls, *args, **kwargs): | ||
if cls._instance is None: | ||
cls._instance = super().__new__(cls) | ||
cls._instance.sync_client = httpx.Client(base_url=cls.base_url, timeout=60 * 3.0) | ||
cls._instance.async_client = httpx.AsyncClient(base_url=cls.base_url, timeout=60 * 3.0) | ||
return cls._instance | ||
|
||
def request( | ||
self, | ||
method: str, | ||
endpoint: str, | ||
data: Optional[dict[str, Any]] = None, | ||
params: Optional[dict[str, Any]] = None, | ||
authorization: Optional[str] = None, | ||
) -> httpx.Response: | ||
""" | ||
Makes an HTTP request to the specified endpoint. | ||
""" | ||
headers = {"Authorization": f"Bearer {authorization}"} if authorization else None | ||
response = self.sync_client.request(method, endpoint, json=data, headers=headers, params=params) | ||
response.raise_for_status() | ||
return response | ||
|
||
async def request_async( | ||
self, | ||
method: str, | ||
endpoint: str, | ||
data: Optional[dict[str, Any]] = None, | ||
params: Optional[dict[str, Any]] = None, | ||
authorization: Optional[str] = None, | ||
) -> httpx.Response: | ||
""" | ||
Makes an asynchronous HTTP request to the specified endpoint. | ||
""" | ||
headers = {"Authorization": f"Bearer {authorization}"} if authorization else None | ||
response = await self.async_client.request(method, endpoint, json=data, headers=headers, params=params) | ||
response.raise_for_status() | ||
return response | ||
|
||
def close(self): | ||
""" | ||
Closes the synchronous HTTP client. | ||
""" | ||
self.sync_client.close() | ||
|
||
async def close_async(self): | ||
""" | ||
Closes the asynchronous HTTP client. | ||
""" | ||
await self.async_client.aclose() | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
self.close() | ||
|
||
async def __aenter__(self): | ||
return self | ||
|
||
async def __aexit__(self, exc_type, exc_val, exc_tb): | ||
await self.close_async() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,49 @@ | ||
from typing import Any, Optional | ||
|
||
import os | ||
|
||
import httpx | ||
from dotenv import load_dotenv | ||
|
||
load_dotenv() | ||
|
||
|
||
class HTTPClient: | ||
_instance = None | ||
base_url = os.getenv("PAREA_API_URL") | ||
|
||
def __new__(cls, *args, **kwargs): | ||
if cls._instance is None: | ||
cls._instance = super().__new__(cls) | ||
cls._instance.sync_client = httpx.Client(base_url=cls.base_url, timeout=60 * 3.0) | ||
cls._instance.async_client = httpx.AsyncClient(base_url=cls.base_url, timeout=60 * 3.0) | ||
return cls._instance | ||
|
||
def request( | ||
self, | ||
method: str, | ||
endpoint: str, | ||
data: Optional[dict[str, Any]] = None, | ||
params: Optional[dict[str, Any]] = None, | ||
authorization: Optional[str] = None, | ||
) -> httpx.Response: | ||
""" | ||
Makes an HTTP request to the specified endpoint. | ||
""" | ||
headers = {"Authorization": f"Bearer {authorization}"} if authorization else None | ||
response = self.sync_client.request(method, endpoint, json=data, headers=headers, params=params) | ||
response.raise_for_status() | ||
return response | ||
|
||
async def request_async( | ||
self, | ||
method: str, | ||
endpoint: str, | ||
data: Optional[dict[str, Any]] = None, | ||
params: Optional[dict[str, Any]] = None, | ||
authorization: Optional[str] = None, | ||
) -> httpx.Response: | ||
""" | ||
Makes an asynchronous HTTP request to the specified endpoint. | ||
""" | ||
headers = {"Authorization": f"Bearer {authorization}"} if authorization else None | ||
response = await self.async_client.request(method, endpoint, json=data, headers=headers, params=params) | ||
response.raise_for_status() | ||
return response | ||
|
||
def close(self): | ||
""" | ||
Closes the synchronous HTTP client. | ||
""" | ||
self.sync_client.close() | ||
|
||
async def close_async(self): | ||
""" | ||
Closes the asynchronous HTTP client. | ||
""" | ||
await self.async_client.aclose() | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
self.close() | ||
|
||
async def __aenter__(self): | ||
return self | ||
|
||
async def __aexit__(self, exc_type, exc_val, exc_tb): | ||
await self.close_async() | ||
from attrs import asdict, define, field | ||
|
||
from parea.api_client import HTTPClient | ||
from parea.schemas.models import Completion, CompletionResponse, UseDeployedPrompt, UseDeployedPromptResponse | ||
|
||
COMPLETION_ENDPOINT = "/completion" | ||
DEPLOYED_PROMPT_ENDPOINT = "/deployed-prompt" | ||
|
||
|
||
@define | ||
class Parea: | ||
api_key: str = field(init=True, default="") | ||
_client: HTTPClient = field(init=False, default=HTTPClient()) | ||
|
||
def completion(self, data: Completion) -> CompletionResponse: | ||
r = self._client.request( | ||
"POST", | ||
COMPLETION_ENDPOINT, | ||
data=asdict(data), | ||
authorization=self.api_key, | ||
) | ||
return CompletionResponse(**r.json()) | ||
|
||
async def acompletion(self, data: Completion) -> CompletionResponse: | ||
r = await self._client.request_async( | ||
"POST", | ||
COMPLETION_ENDPOINT, | ||
data=asdict(data), | ||
authorization=self.api_key, | ||
) | ||
return CompletionResponse(**r.json()) | ||
|
||
def get_prompt(self, data: UseDeployedPrompt) -> UseDeployedPromptResponse: | ||
r = self._client.request( | ||
"POST", | ||
DEPLOYED_PROMPT_ENDPOINT, | ||
data=asdict(data), | ||
authorization=self.api_key, | ||
) | ||
return UseDeployedPromptResponse(**r.json()) | ||
|
||
async def aget_prompt(self, data: UseDeployedPrompt) -> UseDeployedPromptResponse: | ||
r = await self._client.request_async( | ||
"POST", | ||
DEPLOYED_PROMPT_ENDPOINT, | ||
data=asdict(data), | ||
authorization=self.api_key, | ||
) | ||
return UseDeployedPromptResponse(**r.json()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ build-backend = "poetry.core.masonry.api" | |
|
||
[tool.poetry] | ||
name = "parea" | ||
version = "0.1.0" | ||
version = "0.1.1a0" | ||
description = "Parea python sdk" | ||
readme = "README.md" | ||
authors = ["parea-ai <[email protected]>"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
attrs | ||
httpx | ||
python-dotenv |
2 changes: 1 addition & 1 deletion
2
tests/test_parea/test_main.py → tests/test_example/test_example.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters