Skip to content

Commit

Permalink
Add cookbook recipe to extract event details from text (#1269)
Browse files Browse the repository at this point in the history
  • Loading branch information
scampion authored Nov 27, 2024
1 parent 66f6627 commit 568f252
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
34 changes: 34 additions & 0 deletions docs/cookbook/extract_event_details.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
This recipe demonstrates how to use the `outlines` library to extract structured event details from a text message.
We will extract the title, location, and start date and time from messages like the following:

```plaintext
Hello Kitty, my grandmother will be here, I think it's better to postpone
our appointment to review math lessons to next Monday at 2pm at the same
place, 3 avenue des tanneurs, one hour will be enough see you 😘
```

Let see how to extract the event details from the message with the MLX
library dedicated to Apple Silicon processor (M series).

```python
--8<-- "docs/cookbook/extract_event_details.py"
```

The output will be:

```plaintext
Today: Saturday 16 November 2024 and it's 10:55
```

and the extracted event information will be:

```json
{
"title":"Math Review",
"location":"3 avenue des tanneurs",
"start":"2024-11-22T14:00:00Z"
}
```


To find out more about this use case, we recommend the project developped by [Joseph Rudoler](https://x.com/JRudoler) the [ICS Generator](https://github.com/jrudoler/ics-generator)
46 changes: 46 additions & 0 deletions docs/cookbook/extract_event_details.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from datetime import datetime

from pydantic import BaseModel, Field

from outlines import generate, models

# Load the model
model = models.mlxlm("mlx-community/Hermes-3-Llama-3.1-8B-8bit")


# Define the event schema using Pydantic
class Event(BaseModel):
title: str = Field(description="title of the event")
location: str
start: datetime = Field(
default=None, description="date of the event if available in iso format"
)


# Get the current date and time
now = datetime.now().strftime("%A %d %B %Y and it's %H:%M")

# Define the prompt
prompt = f"""
Today's date and time are {now}
Given a user message, extract information of the event like date and time in iso format, location and title.
If the given date is relative, think step by step to find the right date.
Here is the message:
"""

# Sample message
message = """Hello Kitty, my grandmother will be here , I think it's better to postpone our
appointment to review math lessons to next Friday at 2pm at the same place, 3 avenue des tanneurs, I think that one hour will be enough
see you 😘 """

# Create the generator
generator = generate.json(model, Event)

# Extract the event information
event = generator(prompt + message)

# Print the current date and time
print(f"Today: {now}")

# Print the extracted event information in JSON format
print(event.json())
2 changes: 2 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ markdown_extensions:
- pymdownx.emoji:
emoji_index: !!python/name:material.extensions.emoji.twemoji
emoji_generator: !!python/name:material.extensions.emoji.to_svg
- pymdownx.snippets:


extra_css:
Expand Down Expand Up @@ -123,6 +124,7 @@ nav:
- Structured Generation from PDFs: cookbook/read-pdfs.md
- Earnings reports to CSV: cookbook/earnings-reports.md
- Digitizing receipts with vision models: cookbook/receipt-digitization.md
- Extract events details from text: cookbook/extract_event_details.md
- Run on the cloud:
- BentoML: cookbook/deploy-using-bentoml.md
- Cerebrium: cookbook/deploy-using-cerebrium.md
Expand Down

0 comments on commit 568f252

Please sign in to comment.