Skip to content
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

implements mapping for ai_fn and ai_model #330

Merged
merged 3 commits into from
May 26, 2023
Merged

implements mapping for ai_fn and ai_model #330

merged 3 commits into from
May 26, 2023

Conversation

zzstoatzz
Copy link
Collaborator

@zzstoatzz zzstoatzz commented May 26, 2023

uses sync_compatible to enable sync or async mapping, depending on the caller's context

and adds basic test coverage

ai functions

In [1]: from marvin import ai_fn, ai_model

In [2]: @ai_fn(llm_model_name="gpt-3.5-turbo")
   ...: def opposite(thing: str) -> str:
   ...:     """returns the opposite of 'thing'"""
   ...:

In [3]: opposite.map(["up", "happy", "night"])
15:11:04.107 | INFO    | prefect.engine - Created flow run 'famous-spider' for flow 'opposite'
15:11:04.785 | INFO    | Flow run 'famous-spider' - Created task run 'opposite-1' for task 'opposite'
15:11:04.786 | INFO    | Flow run 'famous-spider' - Submitted task run 'opposite-1' for execution.
15:11:04.804 | INFO    | Flow run 'famous-spider' - Created task run 'opposite-2' for task 'opposite'
15:11:04.805 | INFO    | Flow run 'famous-spider' - Submitted task run 'opposite-2' for execution.
15:11:04.810 | INFO    | Flow run 'famous-spider' - Created task run 'opposite-0' for task 'opposite'
15:11:04.811 | INFO    | Flow run 'famous-spider' - Submitted task run 'opposite-0' for execution.
15:11:06.336 | INFO    | Task run 'opposite-1' - Finished in state Completed()
15:11:06.685 | INFO    | Task run 'opposite-2' - Finished in state Completed()
15:11:06.739 | INFO    | Task run 'opposite-0' - Finished in state Completed()
15:11:06.868 | INFO    | Flow run 'famous-spider' - Finished in state Completed('All states completed.')

Out[3]: ['down', 'sad', 'day']

In [5]: from prefect.tasks import task_input_hash

In [6]: opposite.map(["up", "happy", "night"], task_kwargs={"cache_key_fn": task_input_hash})

15:13:32.148 | INFO    | prefect.engine - Created flow run 'cute-urchin' for flow 'opposite'
15:13:32.850 | INFO    | Flow run 'cute-urchin' - Created task run 'opposite-0' for task 'opposite'
15:13:32.852 | INFO    | Flow run 'cute-urchin' - Submitted task run 'opposite-0' for execution.
15:13:32.869 | INFO    | Flow run 'cute-urchin' - Created task run 'opposite-1' for task 'opposite'
15:13:32.870 | INFO    | Flow run 'cute-urchin' - Submitted task run 'opposite-1' for execution.
15:13:32.874 | INFO    | Flow run 'cute-urchin' - Created task run 'opposite-2' for task 'opposite'
15:13:32.874 | INFO    | Flow run 'cute-urchin' - Submitted task run 'opposite-2' for execution.
15:13:33.148 | INFO    | Task run 'opposite-2' - Finished in state Cached(type=COMPLETED)
15:13:33.151 | INFO    | Task run 'opposite-1' - Finished in state Cached(type=COMPLETED)
15:13:33.342 | INFO    | Task run 'opposite-0' - Finished in state Cached(type=COMPLETED)
15:13:33.478 | INFO    | Flow run 'cute-urchin' - Finished in state Completed('All states completed.')
Out[6]: ['down', 'sad', 'day']

ai models

In [10]: @ai_model(llm_model_name='gpt-3.5-turbo')
    ...: class Location(BaseModel):
    ...:     city: str
    ...:     state: str
    ...:

In [11]: Location.map(["windy city", "big apple", "mile high city"])
15:14:48.856 | INFO    | prefect.engine - Created flow run 'successful-axolotl' for flow 'Location'
15:14:49.681 | INFO    | Flow run 'successful-axolotl' - Created task run 'Location-0' for task 'Location'
15:14:49.684 | INFO    | Flow run 'successful-axolotl' - Submitted task run 'Location-0' for execution.
15:14:49.701 | INFO    | Flow run 'successful-axolotl' - Created task run 'Location-2' for task 'Location'
15:14:49.702 | INFO    | Flow run 'successful-axolotl' - Submitted task run 'Location-2' for execution.
15:14:49.799 | INFO    | Flow run 'successful-axolotl' - Created task run 'Location-1' for task 'Location'
15:14:49.799 | INFO    | Flow run 'successful-axolotl' - Submitted task run 'Location-1' for execution.

15:14:53.640 | INFO    | Task run 'Location-1' - Finished in state Completed()
15:14:53.651 | INFO    | Task run 'Location-2' - Finished in state Completed()
15:14:54.793 | INFO    | Task run 'Location-0' - Finished in state Completed()
15:14:54.967 | INFO    | Flow run 'successful-axolotl' - Finished in state Completed('All states completed.')

Out[11]:
[Location(city='Chicago', state='Illinois'),
 Location(city='New York', state='New York'),
 Location(city='Denver', state='Colorado')]

@zzstoatzz zzstoatzz requested review from aaazzam and jlowin May 26, 2023 21:40
@zzstoatzz zzstoatzz merged commit bf7bb2b into main May 26, 2023
@zzstoatzz zzstoatzz deleted the mapping branch May 26, 2023 22:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants