97e91a83f3
Ruff / Ruff (push) Waiting to run
Test / Core Tests (push) Waiting to run
Test / Offline Coverage Tests (Python 3.10) (push) Waiting to run
Test / Offline Coverage Tests (Python 3.11) (push) Waiting to run
Test / Offline Coverage Tests (Python 3.12) (push) Waiting to run
Test / Offline Coverage Tests (Python 3.13) (push) Waiting to run
Test / Offline Coverage Tests (Python 3.9) (push) Waiting to run
Test / Full Coverage (Python 3.11) (push) Waiting to run
Test / Core Provider Tests (OpenAI) (push) Blocked by required conditions
Test / Core Provider Tests (Anthropic) (push) Blocked by required conditions
Test / Core Provider Tests (Google) (push) Blocked by required conditions
Test / Core Provider Tests (Other) (push) Blocked by required conditions
Test / Anthropic Tests (push) Blocked by required conditions
Test / Gemini Tests (push) Blocked by required conditions
Test / Google GenAI Tests (push) Blocked by required conditions
Test / Vertex AI Tests (push) Blocked by required conditions
Test / OpenAI Tests (push) Blocked by required conditions
Test / Writer Tests (push) Blocked by required conditions
Test / Auto Client Tests (push) Blocked by required conditions
ty / type-check (push) Waiting to run
53 lines
1.3 KiB
Python
53 lines
1.3 KiB
Python
import enum
|
|
from pydantic import BaseModel
|
|
from openai import OpenAI
|
|
import instructor
|
|
import logfire
|
|
|
|
|
|
class Labels(str, enum.Enum):
|
|
"""Enumeration for single-label text classification."""
|
|
|
|
SPAM = "spam"
|
|
NOT_SPAM = "not_spam"
|
|
|
|
|
|
class SinglePrediction(BaseModel):
|
|
"""
|
|
Class for a single class label prediction.
|
|
"""
|
|
|
|
class_label: Labels
|
|
|
|
|
|
openai_client = OpenAI()
|
|
logfire.configure(pydantic_plugin=logfire.PydanticPlugin(record="all"))
|
|
logfire.instrument_openai(openai_client)
|
|
client = instructor.from_openai(openai_client)
|
|
|
|
|
|
@logfire.instrument("classification", extract_args=True)
|
|
def classify(data: str) -> SinglePrediction:
|
|
"""Perform single-label classification on the input text."""
|
|
return client.chat.completions.create(
|
|
model="gpt-4o-mini",
|
|
response_model=SinglePrediction,
|
|
messages=[
|
|
{
|
|
"role": "user",
|
|
"content": f"Classify the following text: {data}",
|
|
},
|
|
],
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
emails = [
|
|
"Hello there I'm a Nigerian prince and I want to give you money",
|
|
"Meeting with Thomas has been set at Friday next week",
|
|
"Here are some weekly product updates from our marketing team",
|
|
]
|
|
|
|
for email in emails:
|
|
classify(email)
|