从文本中提取事件详情
本教程演示了如何使用 outlines
库从文本消息中提取结构化的事件详情。我们将从如下所示的消息中提取标题、位置以及开始日期和时间
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 😘
让我们看看如何使用专用于 Apple Silicon 处理器(M 系列)的 MLX 库从消息中提取事件详情。
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())
输出将是
提取的事件信息将是
要了解有关此用例的更多信息,我们推荐 Joseph Rudoler 开发的项目 ICS Generator