Skip to content

Keyword Extraction Task

openaivec.task.nlp.keyword_extraction

Keyword extraction task for OpenAI API.

This module provides a predefined task for keyword extraction that identifies important keywords and phrases from text using OpenAI's language models.

Example

Basic usage with BatchResponses:

from openai import OpenAI
from openaivec.responses import BatchResponses
from openaivec.task import nlp

client = OpenAI()
analyzer = BatchResponses.of_task(
    client=client,
    model_name="gpt-4o-mini",
    task=nlp.KEYWORD_EXTRACTION
)

texts = ["Machine learning is transforming the technology industry.", 
         "Climate change affects global weather patterns."]
analyses = analyzer.parse(texts)

for analysis in analyses:
    print(f"Keywords: {analysis.keywords}")
    print(f"Key phrases: {analysis.keyphrases}")
    print(f"Topics: {analysis.topics}")

With pandas integration:

import pandas as pd
from openaivec import pandas_ext  # Required for .ai accessor
from openaivec.task import nlp

df = pd.DataFrame({"text": ["Machine learning is transforming the technology industry.", 
                           "Climate change affects global weather patterns."]})
df["keywords"] = df["text"].ai.task(nlp.KEYWORD_EXTRACTION)

# Extract keyword components
extracted_df = df.ai.extract("keywords")
print(extracted_df[["text", "keywords_keywords", "keywords_topics", "keywords_summary"]])

Attributes:

Name Type Description
KEYWORD_EXTRACTION PreparedTask

A prepared task instance configured for keyword extraction with temperature=0.0 and top_p=1.0 for deterministic output.