معالجة اللغة الطبيعية المتقدمة باستخدام نماذج اللغة الكبيرة

معالجة اللغة الطبيعية المتقدمة باستخدام نماذج اللغة الكبيرة

مقدمة

يمكن استخدام نماذج اللغة لتنفيذ مهام متقدمة في معالجة اللغة الطبيعية (NLP). يستعرض هذا الدرس التعليمي الطرق المختلفة لتنفيذ هذه الوظائف باستخدام نماذج اللغة الكبيرة (LLM).

أنواع مهام NLP

1. تحليل النصوص

2. التصنيف

3. استخراج المعلومات

التنفيذ

1. محلل النصوص

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.
        """