Skip to content

Commit

Permalink
Receipe for the cookbook to explain how to extract event details from…
Browse files Browse the repository at this point in the history
… message
  • Loading branch information
scampion committed Nov 15, 2024
1 parent d842522 commit 83c7c2e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
70 changes: 70 additions & 0 deletions docs/cookbook/extract_event_details.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
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.

```python

from datetime import datetime
from pydantic import Field, BaseModel
from outlines import models, generate

# 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("%d/%m/%Y %H:%M")

# Define the prompt
prompt = f"""
Today's date and time are {datetime.now().strftime("%d/%m/%Y %H:%M")}
Given a user message, extract information of the event like date and time in iso format, location and title.
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 Monday 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("Today's date and time are", now)

# Print the extracted event information in JSON format
print(event.json())

```

The output will be:

```plaintext
Today's date and time are 15/11/2024 15:52
```

and the extracted event information will be:

```json
{
"title":"Math lessons",
"location":"3 avenue des tanneurs",
"start":"2024-11-18T14:00:00Z"
}
```
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,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 83c7c2e

Please sign in to comment.