Groq and LLM: Transforming Text into Structured Tabular Data
Le brief IA que les pros lisent chaque soir
Les 7 actus IA du jour, décryptées en 5 min. Gratuit.
Inclus dès l'inscription : notre sélection des meilleurs guides & comparatifs IA.
Choisis ton rythme
Gratuit · Pas de spam · Désabonnement en 1 clic
LLM and Feature Engineering: A New Approach
Large language models (LLMs), well-known for their conversational capabilities, can also be leveraged for complex tasks such as feature engineering. In particular, pre-trained models from providers like Groq enable the transformation of unstructured textual data into structured tabular data. This data is then ready to be integrated into predictive machine learning models.
Setup and Imports
To illustrate this process, it is necessary to import several libraries, including pandas, scikit-learn, and the OpenAI class. The latter allows interaction with various providers and access to a wide range of LLMs via a single client, including Groq's Llama models.
import pandas as pd
import json
from pydantic import BaseModel, Field
from openai import OpenAI
from google.colab import userdata
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.preprocessing import StandardScaler
To use these models, it is crucial to set up a Groq client with an API key. This key must be set in Google Colab via the "Secrets" icon in the left sidebar. You need to sign up on Groq's website to obtain this key, which should be named 'GROQ_API_KEY'.
groq_api_key = userdata.get('GROQ_API_KEY')
client = OpenAI(
base_url="https://api.groq.com/openai/v1",
api_key=groq_api_key
)
Creating a Synthetic Dataset
A synthetic dataset is generated to demonstrate the practical application of this technology. This dataset includes customer support tickets, combining textual descriptions with structured numerical features such as account age and the number of prior tickets.
import random
data = []
for _ in range(100):
cat = random.choice(categories)
text = random.choice(templates[cat]).format(days=random.randint(1, 14))
data.append({
"text": text,
"account_age_days": random.randint(1, 2000),
"prior_tickets": random.choices([0, 1, 2, 3, 4, 5], weights=[40, 30, 15, 10, 3, 2])[0],
"label": cat
})
df = pd.DataFrame(data)
The generated dataset contains customer support tickets, combining textual descriptions with structured numerical features such as account age and the number of prior tickets, along with a class label covering multiple ticket categories. These labels will later be used to train and evaluate a classification model at the end of the process.
Feature Extraction with LLM
The tabular features to be extracted from the text are defined based on the application domain. For example, urgency and frustration are key indicators that can be extracted to help classify customer support tickets more effectively.
class TicketFeatures(BaseModel):
urgency_score: int = Field(description="Urgency of the ticket on a scale of 1 to 5")
is_frustrated: int = Field(description="1 if the user expresses frustration, 0 otherwise")
A crucial function in the process uses the LLM to transform the text into a JSON object that matches the defined schema, thereby facilitating the integration of textual data into machine learning models.
def extract_features(text: str) -> dict:
time.sleep(2.5)
schema_instructions = json.dumps(TicketFeatures.model_json_schema())
response = client.chat.completions.create(
model="llama-3.3-70b-versatile",
messages=[
{
"role": "system",
"content": f"You are an extraction assistant. Output ONLY valid JSON matching this schema: {schema_instructions}"
},
{"role": "user", "content": text}
],
response_format={"type": "json_object"},
[temperature](/glossaire/temperature)=0.0
)
return json.loads(response.choices[0].message.content)
Training and Evaluating the Model
After feature extraction, the next step is to train a classification model. The RandomForestClassifier from scikit-learn is used here to demonstrate how the extracted features can improve classification accuracy compared to using raw text alone.
X = df.drop('label', axis=1)
y = df['label']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train_scaled, y_train)
predictions = model.predict(X_test_scaled)
print(classification_report(y_test, predictions))
This process demonstrates how integrating LLMs into feature engineering can transform textual data into actionable insights, thereby enhancing the performance of machine learning models.
Brief IA — L'actualité IA en français
L'essentiel de l'actualité de l'intelligence artificielle, décrypté et expliqué chaque jour.