-
Couldn't load subscription status.
- Fork 10
fix: Adapt assistant sdk to hlp #242
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
Parent:
feat: Assistant API Support
MichalAI21
merged 13 commits into
rc_assistant_api_support
from
adapt-assistant-sdk-to-hlp
Dec 16, 2024
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
18c8cd4
fix: add schemas to assistant plans
MichalAI21 3101f90
fix: support func type in code and class in schemas
MichalAI21 feb0a6d
fix: add schemas to api call
MichalAI21 28c2fbb
fix: raise an error on code parsing failed
MichalAI21 b7add35
fix: change types
MichalAI21 f76f23f
fix: add tests
MichalAI21 2006f58
fix: adapt code to 3.8
MichalAI21 34decba
fix: adapt code to 3.8
MichalAI21 0c58392
fix: support model schemas of python 3.11
MichalAI21 2cd5541
fix: support model schemas of python 3.11
MichalAI21 54c8e0e
fix: PR comments
MichalAI21 05e836e
fix: PR comments
MichalAI21 c63f557
fix: add user defined plan example to integration tests
MichalAI21 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
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,25 @@ | ||
| from ai21 import AI21Client | ||
| from pydantic import BaseModel | ||
|
|
||
| TIMEOUT = 20 | ||
|
|
||
|
|
||
| def test_func(): | ||
| pass | ||
|
|
||
|
|
||
| class ExampleSchema(BaseModel): | ||
| name: str | ||
| id: str | ||
|
|
||
|
|
||
| def main(): | ||
| ai21_client = AI21Client() | ||
|
|
||
| assistant = ai21_client.beta.assistants.create(name="My Assistant") | ||
|
|
||
| plan = ai21_client.beta.assistants.plans.create(assistant_id=assistant.id, code=test_func, schemas=[ExampleSchema]) | ||
| route = ai21_client.beta.assistants.routes.create( | ||
| assistant_id=assistant.id, plan_id=plan.id, name="My Route", examples=["hi"], description="My Route Description" | ||
| ) | ||
| print(f"Route: {route}") |
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
Empty file.
Empty file.
130 changes: 130 additions & 0 deletions
130
tests/unittests/clients/studio/resources/assistant/plans/test_plan_body_creation.py
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,130 @@ | ||
| from typing import Callable, List, Dict, Any, Type, Union | ||
|
|
||
| from pydantic import BaseModel | ||
| from ai21.clients.common.beta.assistant.plans import BasePlans | ||
| from ai21.errors import CodeParsingError | ||
| from ai21.models.responses.plan_response import PlanResponse, ListPlanResponse | ||
| from ai21.types import NotGiven, NOT_GIVEN | ||
| import pytest | ||
|
|
||
|
|
||
| class PlanTestClass(BasePlans): | ||
| def create( | ||
| self, | ||
| *, | ||
| assistant_id: str, | ||
| code: Union[str, Callable], | ||
| schemas: Union[List[Dict[str, Any]], List[Type[BaseModel]], NotGiven] = NOT_GIVEN, | ||
| **kwargs, | ||
| ) -> PlanResponse: | ||
| pass | ||
|
|
||
| def list(self, *, assistant_id: str) -> ListPlanResponse: | ||
| pass | ||
|
|
||
| def retrieve(self, *, assistant_id: str, plan_id: str) -> PlanResponse: | ||
| pass | ||
|
|
||
| def modify( | ||
| self, *, assistant_id: str, plan_id: str, code: str, schemas: Union[List[Dict[str, Any]], NotGiven] = NOT_GIVEN | ||
| ) -> PlanResponse: | ||
| pass | ||
|
|
||
|
|
||
| def test_create_body__when_pass_code_str__should_return_dict(): | ||
| # Arrange | ||
| code = "code" | ||
|
|
||
| # Act | ||
| result = PlanTestClass()._create_body(code=code) | ||
|
|
||
| # Assert | ||
| assert result == {"code": code} | ||
|
|
||
|
|
||
| def test_create_body__when_pass_code_callable__should_return_dict(): | ||
| # Arrange | ||
| def code(): | ||
| return "code" | ||
|
|
||
| # Act | ||
| result = PlanTestClass()._create_body(code=code) | ||
|
|
||
| # Assert | ||
| assert result == {"code": 'def code():\n return "code"'} | ||
|
|
||
|
|
||
| def test_create_body__when_pass_code_and_dict_schemas__should_return_dict_with_schemas(): | ||
| # Arrange | ||
| code = "code" | ||
| schemas = [{"type": "object", "properties": {"name": {"type": "string"}}}] | ||
|
|
||
| # Act | ||
| result = PlanTestClass()._create_body(code=code, schemas=schemas) | ||
Josephasafg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Assert | ||
| assert result == {"code": code, "schemas": schemas} | ||
|
|
||
|
|
||
| class TestSchema(BaseModel): | ||
| name: str | ||
| age: int | ||
|
|
||
|
|
||
| def test_create_body__when_pass_code_and_pydantic_schemas__should_return_dict_with_converted_schemas(): | ||
| # Arrange | ||
| code = "code" | ||
| schemas = [TestSchema] | ||
|
|
||
| # Act | ||
| result = PlanTestClass()._create_body(code=code, schemas=schemas) | ||
|
|
||
| # Assert | ||
| expected_schema = { | ||
| "properties": {"age": {"title": "Age", "type": "integer"}, "name": {"title": "Name", "type": "string"}}, | ||
| "required": ["name", "age"], | ||
| "title": "TestSchema", | ||
| "type": "object", | ||
| } | ||
| assert result == {"code": code, "schemas": [expected_schema]} | ||
|
|
||
|
|
||
| def test_create_body__when_pass_code_and_not_given_schemas__should_return_dict_without_schemas(): | ||
| # Arrange | ||
| code = "code" | ||
|
|
||
| # Act | ||
| result = PlanTestClass()._create_body(code=code, schemas=NOT_GIVEN) | ||
|
|
||
| # Assert | ||
| assert result == {"code": code} | ||
|
|
||
|
|
||
| def test_create_body__when_pass_empty_schemas_list__should_return_dict_with_empty_schemas(): | ||
| # Arrange | ||
| code = "code" | ||
| schemas = [] | ||
|
|
||
| # Act | ||
| result = PlanTestClass()._create_body(code=code, schemas=schemas) | ||
|
|
||
| # Assert | ||
| assert result == {"code": code, "schemas": schemas} | ||
|
|
||
|
|
||
| def test_create_body__when_cannot_get_source_code__should_raise_code_parsing_error(): | ||
| # Arrange | ||
| class CallableWithoutSource: | ||
| def __call__(self): | ||
| return "result" | ||
|
|
||
| # Override __code__ to simulate a built-in function or method | ||
| @property | ||
| def __code__(self): | ||
| raise AttributeError("'CallableWithoutSource' object has no attribute '__code__'") | ||
|
|
||
| code = CallableWithoutSource() | ||
|
|
||
| # Act & Assert | ||
| with pytest.raises(CodeParsingError): | ||
| PlanTestClass()._create_body(code=code) | ||
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.