Devchuz/llm_code
0
1from fastapi import FastAPI, HTTPException2from pydantic import BaseModel, Field3from typing import List4import os5from dotenv import load_dotenv6import warnings7import json8from langchain_anthropic import ChatAnthropic9 10# Ignorar todos los warnings11warnings.filterwarnings("ignore")12 13# Cargar variables de entorno14load_dotenv()15 16# Configurar LLM con Anthropic17llm = ChatAnthropic(model="claude-3-haiku-20240307", temperature=0)18 19# Definiciones de clases Pydantic20 21class Title(BaseModel):22 text: str = Field(description="Title of the notification")23 24class Message(BaseModel):25 content: str = Field(description="Main content or message of the notification")26 27class Number(BaseModel):28 value: int = Field(description="Relevant number or identifier within the notification")29 30class NotificationAnalysis(BaseModel):31 title: List[Title] = Field(default=[], description="Title of the push notification")32 message: List[Message] = Field(default=[], description="Main message or content")33 number: List[Number] = Field(default=[], description="Relevant number or ID within the notification")34 35class NotificationRequest(BaseModel):36 noti_text: str37 38 class Config:39 schema_extra = {40 "example": {41 "text": "New update available: Version 2.0 has been released with improved features. Your device ID is 12345. Tap to update now."42 }43 }44 45# Instancia de FastAPI46app = FastAPI()47 48# Función para procesar el texto de notificación49def extract_from_notification(llm, notification_text: str):50 prompt = f"""51 Given the following push notification text, structure the content into JSON format under the sections "title", "message", and "number":52 - Extract the title of the notification and add it to "title".53 - Extract the main message or content and add it to "message".54 - If there is any relevant number or identifier, add it to "number".55 - Do not include information that is not directly related to title, message, or relevant number.56 57 Notification to process:58 {notification_text}59 60 Expected format:61 {{62 "title": [{{"text": "Title of the notification"}}],63 "message": [{{"content": "Main message or content"}}],64 "number": [{{"value": 123}}]65 }}66 """67 structured_llm = llm.with_structured_output(NotificationAnalysis)68 response = structured_llm.invoke(prompt)69 response = response.dict()70 71 return response72 73@app.get("/")74def greet_json():75 return {"Hello": "World!"}76 77 78# Endpoint que recibe una notificación y devuelve el análisis79@app.post("/notification/")80def analyze_noti(notification: NotificationRequest):81 try:82 response = extract_from_notification(llm, notification.noti_text)83 return response84 except json.JSONDecodeError as e:85 raise HTTPException(status_code=400, detail=f"JSON decode error: {str(e)}")86 