Text Classification using OpenAI API
Introduction
Text classification with the OpenAI API allows you to categorize text into predefined categories or tags using advanced machine learning models. This tutorial covers how to use the text classification endpoint effectively, its parameters, and examples in JavaScript and Python.
Endpoint Overview
The text classification endpoint leverages state-of-the-art AI models to assign one or more labels to a piece of text based on its content.
Using the Text Classification Endpoint
API Request
To classify text, send a POST request to the endpoint URL with your API key and the text to classify.
POST /v1/classifications HTTP/1.1 Host: api.openai.com Content-Type: application/json Authorization: Bearer YOUR_API_KEY { "model": "text-classification", "query": "Is this sentence positive or negative?" }
In this example, a sentence is classified using the "text-classification" model.
API Response
The API responds with the classification results based on the provided input.
HTTP/1.1 200 OK Content-Type: application/json { "labels": [ { "label": "Positive", "confidence": 0.95 }, { "label": "Neutral", "confidence": 0.04 }, { "label": "Negative", "confidence": 0.01 } ] }
The classification results include a list of labels with associated confidence scores.
Parameters
Here are some common parameters you can use with the text classification endpoint:
- query: The text to classify.
- model: The model to use for classification (e.g., "text-classification").
Examples in JavaScript
Here's how you can use the text classification endpoint in JavaScript:
const axios = require('axios'); const apiKey = 'YOUR_API_KEY'; const endpoint = 'https://api.openai.com/v1/classifications'; async function getTextClassification(query) { try { const response = await axios.post(endpoint, { model: 'text-classification', query: query }, { headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${apiKey}` } }); console.log(response.data.labels); } catch (error) { console.error('Error:', error); } } getTextClassification('Is this sentence positive or negative?');
Examples in Python
Here's how you can use the text classification endpoint in Python:
import requests import json api_key = 'YOUR_API_KEY' endpoint = 'https://api.openai.com/v1/classifications' def get_text_classification(query): headers = { 'Content-Type': 'application/json', 'Authorization': f'Bearer {api_key}' } data = { 'model': 'text-classification', 'query': query } response = requests.post(endpoint, headers=headers, data=json.dumps(data)) if response.status_code == 200: labels = response.json()['labels'] for label in labels: print(f"{label['label']}: {label['confidence']}") else: print(f"Error: {response.status_code}, {response.text}") get_text_classification('Is this sentence positive or negative?');
Conclusion
The text classification endpoint in the OpenAI API provides powerful capabilities to categorize text into relevant labels based on its content. By understanding its usage, parameters, and seeing examples in JavaScript and Python, you can integrate text classification seamlessly into various applications for improved content management and analysis.