-
Notifications
You must be signed in to change notification settings - Fork 541
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
Cookbook receipe on extract event details from text #1269
Merged
rlouf
merged 2 commits into
dottxt-ai:main
from
scampion:cookbook_receipe_on_extract_event_details_from_text_without_mlx_patches
Nov 27, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will that inline the content of the python script?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes and that python script will be evaluated by the precommit hooks.
the mkdoc.yml need also the
pymdownx.snippets:
configThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, didn't know about this