Skip to content

Commit

Permalink
new tip
Browse files Browse the repository at this point in the history
  • Loading branch information
bbelderbos committed Jun 28, 2024
1 parent 5ba2d37 commit f556f93
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
4 changes: 4 additions & 0 deletions index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
66 changes: 66 additions & 0 deletions notes/20240628165703.md
Original file line number Diff line number Diff line change
@@ -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:
<type> <priority> <location> <description> <city> <number>
Example components:
<priority> : Priority level (e.g., 'High', 'Medium', 'Low').
<description> : Brief incident/service description (e.g., 'Accident', 'Service Call').
<location> : Could contain street name, postal code, and/or city name.
<date + time> : 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

0 comments on commit f556f93

Please sign in to comment.