Fast API endpoint for Text Classification using GPT 4

 
import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel, Field
from typing import List
from abilities import llm_prompt
from concurrent.futures import ThreadPoolExecutor, TimeoutError
import json

app = FastAPI()

class Item(BaseModel):
    text: str = Field(..., description="The text item to be classified")

class Category(BaseModel):
    name: str = Field(..., description="The category name")

def classify_item(item, categories):
    try:
        prompt = f"Classify the following item: [{item.text}], into one of the following categories: [{', '.join([category.name for category in categories])}]. Respond with only the name of the category, leave empty if nothing matches."
        response = llm_prompt(prompt, model="gpt-4", temperature=0.1)
        print(response)
        try:
            response = json.loads(response)

About this template

This API will classify incoming text items into categories using the GPT 4 model. If the model is unsure about the category of a text item, it will respond with an empty string. The categories are parameters that the API endpoint accepts. The GPT 4 model will classify the items on its own with a prompt like this: "Classify the following item {item} into one of these categories {categories}". There is no maximum number of categories a text item can belong to in the multiple categories classification. The API will use the llm_prompt ability to ask the LLM to classify the item and respond with the category. The API will take the LLM's response as is and will not handle situations where the model identifies multiple categories for a text item in the single category classification. If the model is unsure about the category of a text item in the multiple categories classification, it will respond with an empty string for that item. The API will use Python's concurrent.futures module to parallelize the classification of text items. The API will handle timeouts and exceptions by leaving the items unclassified. The API will parse the LLM's response for the multiple categories classification and match it to the list of categories provided in the API parameters. The API will convert the LLM's response and the categories to lowercase before matching them. The API will split the LLM's response on both ':' and ',' to remove the "Category" word from the response. The temperature of the GPT model is set to a minimal value to make the output more deterministic. The API will return all matching categories for a text item in the multiple categories classification. The API will strip any leading or trailing whitespace from the categories in the LLM's response before matching them to the list of categories provided in the API parameters. The API will accept lists as answers from the LLM. If the LLM responds with a string that's formatted like a list, the API will parse it and match it to the list of categories provided in the API parameters.

Introduction to the FastAPI Text Classification Template

Welcome to the FastAPI Text Classification Template using GPT-4! This template is designed to help you quickly set up an API that can classify text items into categories using the power of GPT-4. Whether you're building a content categorization tool, a customer support automation system, or any other application that requires text classification, this template will get you started without the hassle of environment setup or deployment concerns.

Clicking Start with this Template

To begin using this template, simply click on the "Start with this Template" button. This will set up the template in your Lazy builder interface, pre-populating the code and allowing you to customize it to your needs.

Initial Setup

There are no environment secrets to set up for this template, as all necessary modules and functionalities are built-in within the Lazy platform. This means you can proceed without any additional configuration.

Test: Pressing the Test Button

Once you have started with the template, you can test the functionality by pressing the "Test" button. This will deploy your application and launch the Lazy CLI. The CLI will prompt you for any required user input, if necessary.

Entering Input

For this template, user input through the CLI is not required as the API endpoints are designed to receive input through HTTP requests. Therefore, you can skip this section and move on to using the app.

Using the App

After testing, Lazy will provide you with a dedicated server link to use the API. Additionally, since this template uses FastAPI, you will also receive a link to the automatically generated documentation for your API endpoints. This documentation will guide you on how to interact with the API, detailing the request formats and available endpoints.

Integrating the App

If you need to integrate this API into an external service or frontend, you can use the server link provided by Lazy. Here's how you can make a sample request to the "/classify_single" endpoint:


import requests

# Replace 'your_server_link' with the actual server link provided by Lazy
url = 'your_server_link/classify_single'

# Sample data to classify
data = {
    "items": [{"text": "Sample text to classify"}],
    "categories": [{"name": "Category1"}, {"name": "Category2"}]
}

# Make a POST request to the API
response = requests.post(url, json=data)

# Print the response from the API
print(response.json())

And here's an example of what the response might look like:


[
    {
        "item": "Sample text to classify",
        "category": "Category1"
    }
]

Remember to replace 'your_server_link' with the actual link provided after deployment. Use the provided server link to integrate the API into your application or service as needed. If your integration requires specific scopes or code placement, ensure you follow the guidelines of the external tool you are integrating with.

By following these steps, you should be able to successfully set up and integrate the FastAPI Text Classification Template into your project. Happy building!

Category
Technology
Last published
July 26, 2024

More templates like this

Backend Server

This skeleton is streamlined for creating backend services using FastAPI. It's an excellent choice for building microservices or APIs with minimal frontend requirements.

Fast API
Python

Free Unlimited DALL-E-3 - v2

Demo at https://app--266a4010-2c1e-410b-a9e6-93252d88a540.app.getlazy.ai/ - You get the api key at https://api.discord.rocks

OpenAI

GPT-4 Exam Generator

This app uses GPT-4 to generate a PDF of a new exam based on an uploaded PDF of a past exam.

PDF
OpenAI
Python
Home
/
Fast API endpoint for Text Classification using GPT 4