fix: PEP8 Style
This commit is contained in:
@@ -1,17 +1,12 @@
|
||||
|
||||
import os
|
||||
from naliiabot.bot.agent.agent import Agent, AgentConfig
|
||||
from naliiabot.bot.tools.naliia_tools import NaliiaTools
|
||||
from naliiabot.bot.factories.llm_factory import LLMFactory
|
||||
from naliiabot.bot.prompts.load_prompt import get_prompt_template
|
||||
from langgraph.checkpoint.postgres.aio import AsyncPostgresSaver
|
||||
from naliiabot.bot.agent.agent import Agent
|
||||
from psycopg_pool import AsyncConnectionPool
|
||||
from psycopg.rows import dict_row
|
||||
from ..logger import logger
|
||||
from .settings import _settings
|
||||
from .settings import _settings as st
|
||||
|
||||
|
||||
_connection_string = f"postgresql://{_settings.DB_USER}:{_settings.DB_PASSWORD}@localhost:5432/{_settings.DB_NAME}"
|
||||
_protocol = "postgresql://"
|
||||
_connection_string =\
|
||||
f"{_protocol}{st.DB_USER}:{st.DB_PASSWORD}@localhost:5432/{st.DB_NAME}"
|
||||
agent_instance: Agent | None = None
|
||||
db_pool = None
|
||||
|
||||
|
||||
@@ -4,8 +4,9 @@ from langchain_core.messages import HumanMessage
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.post("/chat")
|
||||
def chat(messages: dict, agent = Depends(get_agent)):
|
||||
def chat(messages: dict, agent=Depends(get_agent)):
|
||||
"""
|
||||
Simulate a chat response based on the input message.
|
||||
|
||||
@@ -16,7 +17,12 @@ def chat(messages: dict, agent = Depends(get_agent)):
|
||||
dict: A dictionary containing the response message.
|
||||
"""
|
||||
|
||||
messages = [HumanMessage(content=messages["messages"])]
|
||||
response_message = agent.invoke({"messages": messages})
|
||||
|
||||
return {"response": response_message}
|
||||
messages = [
|
||||
HumanMessage(
|
||||
content=messages["messages"]
|
||||
)]
|
||||
response_message = agent.invoke(
|
||||
{"messages": messages}
|
||||
)
|
||||
|
||||
return {"response": response_message}
|
||||
|
||||
@@ -9,6 +9,7 @@ import json
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
async def send_whatsapp_message(request_data: SendMessageScheme):
|
||||
"""
|
||||
Envía un mensaje de texto a través de la API de WhatsApp.
|
||||
@@ -32,11 +33,16 @@ async def send_whatsapp_message(request_data: SendMessageScheme):
|
||||
"delay": delay,
|
||||
"linkPreview": False
|
||||
}
|
||||
|
||||
|
||||
async with httpx.AsyncClient() as client:
|
||||
try:
|
||||
response = await client.post(endpoint, json=payload, headers=headers)
|
||||
response = await client.post(
|
||||
endpoint,
|
||||
json=payload,
|
||||
headers=headers
|
||||
)
|
||||
response.raise_for_status()
|
||||
|
||||
return response.json()
|
||||
except Exception as e:
|
||||
logger.error(f"Error enviando mensaje a WhatsApp: {e}")
|
||||
@@ -44,7 +50,7 @@ async def send_whatsapp_message(request_data: SendMessageScheme):
|
||||
|
||||
|
||||
@router.post("/webhook")
|
||||
async def webhook_chat(request: Request, agent = Depends(get_agent)):
|
||||
async def webhook_chat(request: Request, agent=Depends(get_agent)):
|
||||
logger.info("Received webhook request")
|
||||
body = await request.json()
|
||||
logger.info(f"Webhook payload: {json.dumps(body)}")
|
||||
@@ -74,9 +80,11 @@ async def webhook_chat(request: Request, agent = Depends(get_agent)):
|
||||
|
||||
messages = [HumanMessage(content=user_message)]
|
||||
|
||||
agent_response = await agent.ainvoke({"messages": messages}, config=config)
|
||||
agent_response = await agent.ainvoke(
|
||||
{"messages": messages}, config=config
|
||||
)
|
||||
agent_response_content = agent_response["messages"][-1].content
|
||||
|
||||
|
||||
clean_jid = thread_id.split('@')[0]
|
||||
|
||||
await send_whatsapp_message(
|
||||
@@ -87,10 +95,10 @@ async def webhook_chat(request: Request, agent = Depends(get_agent)):
|
||||
jid=clean_jid,
|
||||
text=agent_response_content,
|
||||
delay=1200
|
||||
))
|
||||
))
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error processing webhook: {e}")
|
||||
return {"error": str(e)}
|
||||
|
||||
|
||||
return {"status": "sent", "reply": agent_response_content}
|
||||
|
||||
@@ -16,10 +16,10 @@ async def lifespan(app: FastAPI):
|
||||
await checkpointer.setup()
|
||||
|
||||
model_name = "anthropic"
|
||||
model = deps.LLMFactory(model_name).get_model()
|
||||
naliia_prompt = deps.get_prompt_template("NALIIA_PROMPT")
|
||||
naliia_tools = deps.NaliiaTools().get_tools()
|
||||
config = deps.AgentConfig(system_prompt=naliia_prompt)
|
||||
model = deps.LLMFactory(model_name).get_model()
|
||||
|
||||
deps.agent_instance = deps.Agent(
|
||||
model=model,
|
||||
@@ -35,7 +35,9 @@ async def lifespan(app: FastAPI):
|
||||
|
||||
app = FastAPI(
|
||||
title="NaliiaBot API",
|
||||
description="API for NaliiaBot, a chatbot that provides customer service and related topics.",
|
||||
description=(
|
||||
"API for NaliiaBot, a chatbot that provides "
|
||||
"customer service and related topics."),
|
||||
version="1.0.0",
|
||||
lifespan=lifespan
|
||||
)
|
||||
@@ -43,10 +45,12 @@ app = FastAPI(
|
||||
app.include_router(chat_router)
|
||||
app.include_router(chat_hook_router)
|
||||
|
||||
|
||||
@app.get("/")
|
||||
def read_root():
|
||||
return {"Hello": "World"}
|
||||
|
||||
|
||||
@app.get("/health")
|
||||
def health_check():
|
||||
return {"status": "ok"}
|
||||
|
||||
@@ -7,4 +7,4 @@ class SendMessageScheme(TypedDict):
|
||||
apikey: str
|
||||
jid: str
|
||||
delay: int = 1200
|
||||
text: str
|
||||
text: str
|
||||
|
||||
Reference in New Issue
Block a user