-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add Prompts/AsyncPrompts resource for fetching DIAL prompts #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,70 @@ | ||
| from pathlib import PurePosixPath | ||
| from typing import Optional, Union | ||
| from urllib.parse import urljoin | ||
|
|
||
| import httpx | ||
|
|
||
| from aidial_client._constants import API_PREFIX | ||
| from aidial_client._exception import DialException, ResourceNotFoundError | ||
| from aidial_client._internal_types._http_request import FinalRequestOptions | ||
| from aidial_client.helpers.storage_resource import DialStorageResourceMixin | ||
| from aidial_client.resources.base import AsyncResource, Resource | ||
| from aidial_client.resources.metadata import AsyncMetadata, Metadata | ||
| from aidial_client.types.metadata import PromptMetadata | ||
| from aidial_client.types.prompt import Prompt | ||
|
|
||
|
|
||
| def _prompts_error_processor( | ||
| http_status_error: httpx.HTTPStatusError, | ||
| ) -> Optional[DialException]: | ||
| if http_status_error.response.status_code == 404: | ||
| return ResourceNotFoundError( | ||
| message=http_status_error.response.text, | ||
| ) | ||
| return None | ||
|
|
||
|
|
||
| class Prompts(Resource, DialStorageResourceMixin): | ||
| metadata: Metadata | ||
| resource_type: str = "prompts" | ||
|
|
||
| def get(self, url: Union[str, PurePosixPath]) -> Prompt: | ||
| """Fetch a single prompt by its storage path.""" | ||
| return self.http_client.request( | ||
| cast_to=Prompt, | ||
| options=FinalRequestOptions( | ||
| method="GET", | ||
| url=urljoin(API_PREFIX, self.get_api_path(str(url))), | ||
| ), | ||
| on_http_error=_prompts_error_processor, | ||
| ) | ||
|
|
||
| def get_metadata(self, url: Union[str, PurePosixPath]) -> PromptMetadata: | ||
| return self.metadata.get( | ||
| resource="prompts", | ||
| relative_url=self.get_api_path(str(url)), | ||
| ) | ||
|
|
||
|
|
||
| class AsyncPrompts(AsyncResource, DialStorageResourceMixin): | ||
| metadata: AsyncMetadata | ||
| resource_type: str = "prompts" | ||
|
|
||
| async def get(self, url: Union[str, PurePosixPath]) -> Prompt: | ||
| """Fetch a single prompt by its storage path.""" | ||
| return await self.http_client.request( | ||
| cast_to=Prompt, | ||
| options=FinalRequestOptions( | ||
| method="GET", | ||
| url=urljoin(API_PREFIX, self.get_api_path(str(url))), | ||
| ), | ||
| on_http_error=_prompts_error_processor, | ||
| ) | ||
|
|
||
| async def get_metadata( | ||
| self, url: Union[str, PurePosixPath] | ||
| ) -> PromptMetadata: | ||
| return await self.metadata.get( | ||
| resource="prompts", | ||
| relative_url=self.get_api_path(str(url)), | ||
| ) |
This file contains hidden or 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,25 @@ | ||
| from typing import Optional | ||
|
|
||
| from aidial_client._compatibility.pydantic import PYDANTIC_V2 | ||
| from aidial_client._internal_types._model import ExtraAllowModel | ||
| from aidial_client._utils._alias import to_camel | ||
|
|
||
|
|
||
| class Prompt(ExtraAllowModel): | ||
| """A DIAL prompt resource.""" | ||
|
|
||
| if PYDANTIC_V2: | ||
| model_config = { | ||
| "alias_generator": to_camel, | ||
| "populate_by_name": True, | ||
| } | ||
| else: | ||
|
|
||
| class Config: | ||
| alias_generator = to_camel | ||
| allow_population_by_field_name = True | ||
|
|
||
| id: str | ||
| name: str | ||
| folder_id: str | ||
| content: Optional[str] = None |
This file contains hidden or 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,148 @@ | ||
| import pytest | ||
|
|
||
| from aidial_client._exception import DialException, ResourceNotFoundError | ||
| from aidial_client.types.metadata import PromptMetadata | ||
| from aidial_client.types.prompt import Prompt | ||
| from tests.client_mock import get_async_client_mock, get_client_mock | ||
|
|
||
| PROMPT_MOCK = { | ||
| "id": "prompts/test-bucket/my-folder/my-prompt", | ||
| "name": "my-prompt", | ||
| "folderId": "my-folder", | ||
| "content": "You are a helpful assistant.", | ||
| } | ||
|
|
||
| PROMPT_NO_CONTENT_MOCK = { | ||
| "id": "prompts/test-bucket/my-folder/my-prompt", | ||
| "name": "my-prompt", | ||
| "folderId": "my-folder", | ||
| } | ||
|
|
||
| PROMPT_METADATA_MOCK = { | ||
| "name": "my-prompt", | ||
| "parentPath": "my-folder", | ||
| "bucket": "test-bucket", | ||
| "url": "prompts/test-bucket/my-folder/my-prompt", | ||
| "nodeType": "ITEM", | ||
| "resourceType": "PROMPT", | ||
| "items": [], | ||
| } | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # prompts.get() | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| def test_get_prompt(): | ||
| client = get_client_mock(status_code=200, json_mock=PROMPT_MOCK) | ||
| result = client.prompts.get("prompts/test-bucket/my-folder/my-prompt") | ||
| assert isinstance(result, Prompt) | ||
| assert result.id == "prompts/test-bucket/my-folder/my-prompt" | ||
| assert result.name == "my-prompt" | ||
| assert result.folder_id == "my-folder" | ||
| assert result.content == "You are a helpful assistant." | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_async_get_prompt(): | ||
| client = get_async_client_mock(status_code=200, json_mock=PROMPT_MOCK) | ||
| result = await client.prompts.get("prompts/test-bucket/my-folder/my-prompt") | ||
| assert isinstance(result, Prompt) | ||
| assert result.id == "prompts/test-bucket/my-folder/my-prompt" | ||
| assert result.name == "my-prompt" | ||
| assert result.folder_id == "my-folder" | ||
| assert result.content == "You are a helpful assistant." | ||
|
|
||
|
|
||
| def test_get_prompt_no_content(): | ||
| client = get_client_mock(status_code=200, json_mock=PROMPT_NO_CONTENT_MOCK) | ||
| result = client.prompts.get("prompts/test-bucket/my-folder/my-prompt") | ||
| assert isinstance(result, Prompt) | ||
| assert result.content is None | ||
|
|
||
|
|
||
| def test_get_prompt_not_found(): | ||
| client = get_client_mock( | ||
| status_code=404, | ||
| json_mock={ | ||
| "error": { | ||
| "message": "Not found", | ||
| "type": "not_found", | ||
| } | ||
| }, | ||
| ) | ||
| with pytest.raises(ResourceNotFoundError): | ||
| client.prompts.get("prompts/test-bucket/nonexistent/prompt") | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_async_get_prompt_not_found(): | ||
| client = get_async_client_mock( | ||
| status_code=404, | ||
| json_mock={ | ||
| "error": { | ||
| "message": "Not found", | ||
| "type": "not_found", | ||
| } | ||
| }, | ||
| ) | ||
| with pytest.raises(ResourceNotFoundError): | ||
| await client.prompts.get("prompts/test-bucket/nonexistent/prompt") | ||
|
|
||
|
|
||
| def test_get_prompt_http_error(): | ||
| client = get_client_mock( | ||
| status_code=401, | ||
| json_mock={ | ||
| "error": { | ||
| "message": "Unauthorized", | ||
| "type": "auth_error", | ||
| } | ||
| }, | ||
| ) | ||
| with pytest.raises(DialException): | ||
| client.prompts.get("prompts/test-bucket/my-folder/my-prompt") | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_async_get_prompt_http_error(): | ||
| client = get_async_client_mock( | ||
| status_code=401, | ||
| json_mock={ | ||
| "error": { | ||
| "message": "Unauthorized", | ||
| "type": "auth_error", | ||
| } | ||
| }, | ||
| ) | ||
| with pytest.raises(DialException): | ||
| await client.prompts.get("prompts/test-bucket/my-folder/my-prompt") | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # prompts.get_metadata() | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| def test_get_prompt_metadata(): | ||
| client = get_client_mock(status_code=200, json_mock=PROMPT_METADATA_MOCK) | ||
| result = client.prompts.get_metadata( | ||
| "prompts/test-bucket/my-folder/my-prompt" | ||
| ) | ||
| assert isinstance(result, PromptMetadata) | ||
| assert result.node_type == "ITEM" | ||
| assert result.bucket == "test-bucket" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_async_get_prompt_metadata(): | ||
| client = get_async_client_mock( | ||
| status_code=200, json_mock=PROMPT_METADATA_MOCK | ||
| ) | ||
| result = await client.prompts.get_metadata( | ||
| "prompts/test-bucket/my-folder/my-prompt" | ||
| ) | ||
| assert isinstance(result, PromptMetadata) | ||
| assert result.node_type == "ITEM" | ||
| assert result.bucket == "test-bucket" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.