Chatstract is a tool that progressively extracts structured information from multi-turn user chats using pydantic schemas and LLMs. Powered by Instructor.
Basic Example
First, create the pydantic model you wish to fill out through a conversational method.
from datetime import date
from pydantic import BaseModel, Field
from typing import Optional
class Task(BaseModel):
str = Field(description="A short title of the task.")
title: = Field(description="The deadline for the task. Current year: 2024")
due_date: date str = Field(description="The person assigned to solve the task")
responsable: str = Field(description="The location where the task will be performed")
location: str] = Field(description="The status of the task", default=None) status: Optional[
Then, use Chatstract to populate the data model with values from sequential messages provided by the user.
from chatstract.core import Chat
= Chat(data_model = Task)
chat
= ["Jason has to clean his desk",
messages "The deadline is september 7th",
"This task is for the Downtown office",
"I messed up, the task is for Dorothy at the Uptown office",
"The status for this task is 'Done'"]
for message in messages:
= chat.chat_ai(message)
info print(f"User message: {message}")
print(f"Data: {chat.data_values}")
if len(info.answer) == 0:
print("Missing information: None")
print("Follow-up question: None")
print("\n===\n")
continue
for question in info.answer:
print(f"Missing information: {question.missing_information_key}")
print(f"Follow-up question: {question.question}")
print("\n===\n")
Below is the output you can expect:
User message: Jason has to clean his desk
Data: {'title': 'Clean the desk', 'due_date': None, 'responsable': 'Jason', 'location': None, 'status': None}
Missing information: due_date
Follow-up question: What is the due date for the task 'Clean the desk'?
Missing information: location
Follow-up question: Where should the task 'Clean the desk' be performed?
===
User message: The deadline is september 7th
Data: {'title': 'Clean the desk', 'due_date': datetime.date(2024, 9, 7), 'responsable': 'Jason', 'location': None, 'status': None}
Missing information: location
Follow-up question: What is the location for the task 'Clean the desk'?
===
User message: This task is for the Downtown office
Data: {'title': 'Clean the desk', 'due_date': datetime.date(2024, 9, 7), 'responsable': 'Jason', 'location': 'Downtown office', 'status': None}
Missing information: None
Follow-up question: None
===
User message: I messed up, the task is for Dorothy at the Uptown office
Data: {'title': 'Clean the desk', 'due_date': datetime.date(2024, 9, 7), 'responsable': 'Dorothy', 'location': 'Uptown office', 'status': None}
Missing information: None
Follow-up question: None
===
User message: The status for this task is 'Done'
Data: {'title': 'Clean the desk', 'due_date': datetime.date(2024, 9, 7), 'responsable': 'Dorothy', 'location': 'Uptown office', 'status': 'Done'}
Missing information: None
Follow-up question: None
===
Citation
For attribution, please cite this work as:
Astroza, Alonso. 2024. “Chatstract.” May 12, 2024. https://www.alonsoastroza.com/projects/chatstract/.