From f556f930db430e706eafe59c8387b605fda117e8 Mon Sep 17 00:00:00 2001 From: Bob Belderbos Date: Fri, 28 Jun 2024 20:15:28 +0200 Subject: [PATCH] new tip --- index.md | 4 +++ notes/20240628165703.md | 66 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 notes/20240628165703.md diff --git a/index.md b/index.md index a945448..9bea161 100644 --- a/index.md +++ b/index.md @@ -272,6 +272,10 @@ This file gets generated by [this script](index.py). - [There is a module for everything in python!](notes/20220904164714.md) +## Marvinai + +- [Using marvin ai to extract geo locations](notes/20240628165703.md) + ## Matchcase - [Use match/case over if/elisf/else](notes/20240614124008.md) diff --git a/notes/20240628165703.md b/notes/20240628165703.md new file mode 100644 index 0000000..c0c82c2 --- /dev/null +++ b/notes/20240628165703.md @@ -0,0 +1,66 @@ +# Using marvin ai to extract geo locations + +I just used Marvin AI to get geo locations based on a fuzzy location string 💡🎉 + +I like the combination of using a Pydantic 🐍 base model + typing + a prompt in the docstring of a function, feels really clean. 😍 📈 + +``` +import marvin +from pydantic import BaseModel, Field + + +class Location(BaseModel): + fuzzy_input_name: str = Field(..., description="The original input string") + location_string_extracted: str = Field(..., description="The extracted location string") + latitude: float = Field(..., description="Latitude of the location") + longitude: float = Field(..., description="Longitude of the location") + + +@marvin.fn +def get_locations(location_strings: list[str]) -> list[Location]: + """ + Estimate geo locations from fuzzy input strings. + + Args: + location_strings (list[str]): List of location strings. + + Returns: + list[Location]: List of Location objects with extracted details and geo coordinates. + + Format for each input string: + + + Example components: + : Priority level (e.g., 'High', 'Medium', 'Low'). + : Brief incident/service description (e.g., 'Accident', 'Service Call'). + : Could contain street name, postal code, and/or city name. + : Indicates the time of the incident/service. + + Note: + - Street names or postal codes are the most specific identifiers. + - Use the city name if no street name or postal code is found. + """ + + +if __name__ == "__main__": + location_strings = [ + "High Accident 123 Main St, New York 2024-06-28 12:59:00", + "Medium Service Call Los Angeles 2024-06-28 13:00:00", + "Low Fire Incident 456 Elm St, 60614, Chicago 2024-06-28 13:01:00", + "High Medical Emergency Uptown, Boston 2024-06-28 13:02:00", + "Medium Road Block 789 Pine St, San Francisco 2024-06-28 13:03:00", + ] + locations = get_locations(location_strings) + + for location in locations: + print(location) + + # Output: + # fuzzy_input_name='High Accident 123 Main St, New York 2024-06-28 12:59:00' location_string_extracted='123 Main St, New York' latitude=40.7127753 longitude=-74.0059728 + # fuzzy_input_name='Medium Service Call Los Angeles 2024-06-28 13:00:00' location_string_extracted='Los Angeles' latitude=34.0522342 longitude=-118.2436849 + # fuzzy_input_name='Low Fire Incident 456 Elm St, 60614, Chicago 2024-06-28 13:01:00' location_string_extracted='456 Elm St, 60614, Chicago' latitude=41.8493 longitude=-87.648 + # fuzzy_input_name='High Medical Emergency Uptown, Boston 2024-06-28 13:02:00' location_string_extracted='Uptown, Boston' latitude=42.3600825 longitude=-71.0588801 + # fuzzy_input_name='Medium Road Block 789 Pine St, San Francisco 2024-06-28 13:03:00' location_string_extracted='789 Pine St, San Francisco' latitude=37.7749295 longitude=-122.4194155 +``` + +#marvinai