Processamento de linguagem natural avançado com LLMs

Processamento de linguagem natural avançado com LLMs

Introdução

Os modelos de linguagem podem ser usados para executar tarefas avançadas de processamento de linguagem natural (NLP). Este tutorial explora as diferentes formas de implementar essas funcionalidades com LLMs.

Tipos de tarefas de NLP

1. Análise de texto

2. Classificação

3. Extração de informações

Implementação

1. Analisador de texto

from openai import OpenAI
import json
from typing import List, Dict, Any
import numpy as np

class TextAnalyzer:
    def __init__(self, api_key: str):
        self.client = OpenAI(api_key=api_key)
    
    def analyze_text(self, text: str, analysis_type: str = "sentiment") -> Dict[str, Any]:
        prompt = self._create_analysis_prompt(text, analysis_type)
        
        response = self.client.chat.completions.create(
            model="gpt-3.5-turbo",
            messages=[
                {"role": "system", "content": "Tu es un expert en analyse de texte."},
                {"role": "user", "content": prompt}
            ],
            temperature=0.3
        )
        
        return json.loads(response.choices[0].message.content)
    
    def _create_analysis_prompt(self, text: str, analysis_type: str) -> str:
        return f"""
        Analyse le texte suivant de type {analysis_type} :
        
        Texte : {text}
        
        Fournis :
        1. L'analyse détaillée
        2. Les points clés
        3. Les insights
        4. Les recommandations
        
        Réponds au format JSON.
        """