Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ from langchain_openai import OpenAIEmbeddings
|
|
| 6 |
import os
|
| 7 |
from openai import OpenAI
|
| 8 |
import zipfile
|
|
|
|
| 9 |
|
| 10 |
# Inicializar FastAPI
|
| 11 |
app = FastAPI()
|
|
@@ -34,6 +35,49 @@ def obtener_extractos(pregunta: str):
|
|
| 34 |
docs_relevantes = retriever.invoke(pregunta)
|
| 35 |
return [(doc.page_content, doc.metadata.get("url", "URL no disponible")) for doc in docs_relevantes]
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
# Modelo de datos para la solicitud
|
| 38 |
class ChatRequest(BaseModel):
|
| 39 |
message: str
|
|
@@ -59,20 +103,77 @@ async def chat(request: ChatRequest):
|
|
| 59 |
{"role": "system", "content": system_message_final},
|
| 60 |
{"role": "user", "content": request.message}
|
| 61 |
]
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
try:
|
| 65 |
# Llamar a la API de OpenAI
|
| 66 |
response = client.chat.completions.create(
|
| 67 |
model="gpt-4o-mini",
|
| 68 |
messages=messages,
|
|
|
|
|
|
|
| 69 |
max_tokens=request.max_tokens,
|
| 70 |
temperature=request.temperature,
|
| 71 |
top_p=request.top_p
|
| 72 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
|
| 75 |
-
return {"response": completion, "context": contexto}
|
| 76 |
|
| 77 |
except Exception as e:
|
| 78 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
| 6 |
import os
|
| 7 |
from openai import OpenAI
|
| 8 |
import zipfile
|
| 9 |
+
from typing import Literal
|
| 10 |
|
| 11 |
# Inicializar FastAPI
|
| 12 |
app = FastAPI()
|
|
|
|
| 35 |
docs_relevantes = retriever.invoke(pregunta)
|
| 36 |
return [(doc.page_content, doc.metadata.get("url", "URL no disponible")) for doc in docs_relevantes]
|
| 37 |
|
| 38 |
+
def enviar_contacto(access_token: str, phone_number_id: str, recipient_number: str, formatted_name: str, first_name: str):
|
| 39 |
+
url = f"https://graph.facebook.com/v19.0/{phone_number_id}/messages"
|
| 40 |
+
headers = {
|
| 41 |
+
'Authorization': f'Bearer {access_token}',
|
| 42 |
+
'Content-Type': 'application/json'
|
| 43 |
+
}
|
| 44 |
+
data = {
|
| 45 |
+
"messaging_product": "whatsapp",
|
| 46 |
+
"to": recipient_number,
|
| 47 |
+
"type": "contacts",
|
| 48 |
+
"contacts": [
|
| 49 |
+
{
|
| 50 |
+
"name": {
|
| 51 |
+
"formatted_name": formatted_name,
|
| 52 |
+
"first_name": first_name
|
| 53 |
+
}
|
| 54 |
+
}
|
| 55 |
+
]
|
| 56 |
+
}
|
| 57 |
+
response = requests.post(url, headers=headers, json=data)
|
| 58 |
+
print(response.json)
|
| 59 |
+
return response.json()
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def enviar_ubicacion(access_token: str, phone_number_id: str, recipient_number: str, latitude: str, longitude: str):
|
| 63 |
+
url = f"https://graph.facebook.com/v19.0/{phone_number_id}/messages"
|
| 64 |
+
headers = {
|
| 65 |
+
'Authorization': f'Bearer {access_token}',
|
| 66 |
+
'Content-Type': 'application/json'
|
| 67 |
+
}
|
| 68 |
+
data = {
|
| 69 |
+
"messaging_product": "whatsapp",
|
| 70 |
+
"recipient_type": "individual",
|
| 71 |
+
"to": recipient_number,
|
| 72 |
+
"type": "location",
|
| 73 |
+
"location": {
|
| 74 |
+
"latitude": latitude,
|
| 75 |
+
"longitude": longitude
|
| 76 |
+
}
|
| 77 |
+
}
|
| 78 |
+
response = requests.post(url, headers=headers, json=data)
|
| 79 |
+
return response.json()
|
| 80 |
+
|
| 81 |
# Modelo de datos para la solicitud
|
| 82 |
class ChatRequest(BaseModel):
|
| 83 |
message: str
|
|
|
|
| 103 |
{"role": "system", "content": system_message_final},
|
| 104 |
{"role": "user", "content": request.message}
|
| 105 |
]
|
| 106 |
+
|
| 107 |
+
tools = [
|
| 108 |
+
{
|
| 109 |
+
"type": "function",
|
| 110 |
+
"function": {
|
| 111 |
+
"name": "enviar_contacto",
|
| 112 |
+
"description": "Envía un contacto de WhatsApp",
|
| 113 |
+
"parameters": {
|
| 114 |
+
"type": "object",
|
| 115 |
+
"properties": {
|
| 116 |
+
"formatted_name": {"type": "string", "description": "Nombre completo del contacto"},
|
| 117 |
+
"first_name": {"type": "string", "description": "Primer nombre del contacto"}
|
| 118 |
+
},
|
| 119 |
+
"required": ["formatted_name", "first_name"]
|
| 120 |
+
}
|
| 121 |
+
}
|
| 122 |
+
},
|
| 123 |
+
{
|
| 124 |
+
"type": "function",
|
| 125 |
+
"function": {
|
| 126 |
+
"name": "enviar_ubicacion",
|
| 127 |
+
"description": "Envía una ubicación a un contacto de WhatsApp",
|
| 128 |
+
"parameters": {
|
| 129 |
+
"type": "object",
|
| 130 |
+
"properties": {
|
| 131 |
+
"latitude": {"type": "string"},
|
| 132 |
+
"longitude": {"type": "string"}
|
| 133 |
+
},
|
| 134 |
+
"required": ["latitude", "longitude"]
|
| 135 |
+
}
|
| 136 |
+
}
|
| 137 |
+
}
|
| 138 |
+
]
|
| 139 |
+
|
| 140 |
|
| 141 |
try:
|
| 142 |
# Llamar a la API de OpenAI
|
| 143 |
response = client.chat.completions.create(
|
| 144 |
model="gpt-4o-mini",
|
| 145 |
messages=messages,
|
| 146 |
+
tools=tools,
|
| 147 |
+
tool_choice="auto"
|
| 148 |
max_tokens=request.max_tokens,
|
| 149 |
temperature=request.temperature,
|
| 150 |
top_p=request.top_p
|
| 151 |
)
|
| 152 |
+
|
| 153 |
+
choice = response.choices[0]
|
| 154 |
+
|
| 155 |
+
# Si el modelo decide llamar a una función
|
| 156 |
+
if choice.message.tool_calls:
|
| 157 |
+
for tool_call in choice.message.tool_calls:
|
| 158 |
+
name = tool_call.function.name
|
| 159 |
+
arguments = json.loads(tool_call.function.arguments)
|
| 160 |
+
|
| 161 |
+
if name == "enviar_contacto":
|
| 162 |
+
result = enviar_contacto(
|
| 163 |
+
ACCESS_TOKEN, PHONE_NUMBER_ID, RECIPIENT_NUMBER,
|
| 164 |
+
formatted_name="Pedro J. Johnson",
|
| 165 |
+
first_name="Pedro"
|
| 166 |
+
)
|
| 167 |
+
elif name == "enviar_ubicacion":
|
| 168 |
+
result = enviar_ubicacion(
|
| 169 |
+
ACCESS_TOKEN, PHONE_NUMBER_ID, RECIPIENT_NUMBER,
|
| 170 |
+
latitude="37.44216251868683",
|
| 171 |
+
longitude="-122.16153582049394"
|
| 172 |
+
)
|
| 173 |
+
|
| 174 |
+
return {"response": f"✅ Acción ejecutada: {name}", "result": result}
|
| 175 |
|
| 176 |
+
return {"response": choice.message.content, "context": contexto}
|
|
|
|
| 177 |
|
| 178 |
except Exception as e:
|
| 179 |
raise HTTPException(status_code=500, detail=str(e))
|