diff --git a/vortex/ai/agents/__init__.py b/vortex/ai/agents/__init__.py index 19b200b..3c72c73 100644 --- a/vortex/ai/agents/__init__.py +++ b/vortex/ai/agents/__init__.py @@ -1,9 +1,9 @@ # %% from langchain.agents import AgentExecutor -from langchain.agents.format_scratchpad.openai_tools import \ - format_to_openai_tool_messages -from langchain.agents.output_parsers.openai_tools import \ - OpenAIToolsAgentOutputParser +from langchain.agents.format_scratchpad.openai_tools import ( + format_to_openai_tool_messages, +) +from langchain.agents.output_parsers.openai_tools import OpenAIToolsAgentOutputParser from langchain.prompts import MessagesPlaceholder from langchain_core.messages import AIMessage, HumanMessage from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder @@ -40,7 +40,7 @@ def __init__( tools: list = tools, hub_prompt: str = "hwchase17/openai-tools-agent", agent_type="vortex_tools_agent", - context: list = [] # represents the chat history, can be pulled from a db + context: list = [], # represents the chat history, can be pulled from a db ): self.llm: LLM = llm self.tools: list = tools diff --git a/vortex/api/__init__.py b/vortex/api/__init__.py index fac2f17..f297921 100644 --- a/vortex/api/__init__.py +++ b/vortex/api/__init__.py @@ -10,6 +10,7 @@ app = FastAPI() + @app.get("/api/check") def hello_world(): return {"message": "Vortex is Running!"} diff --git a/vortex/api/data_models/__init__.py b/vortex/api/data_models/__init__.py index 0ea2153..3c81397 100644 --- a/vortex/api/data_models/__init__.py +++ b/vortex/api/data_models/__init__.py @@ -2,8 +2,7 @@ import os from datetime import datetime -from sqlalchemy import (JSON, Column, DateTime, Integer, String, Text, - create_engine) +from sqlalchemy import JSON, Column, DateTime, Integer, String, Text, create_engine from sqlalchemy.engine import URL from sqlalchemy.orm import declarative_base, sessionmaker @@ -32,19 +31,24 @@ class Conversation(Base): response = Column(String) created_at = Column(DateTime, default=datetime.utcnow) + class ChatsHistory(Base): - __tablename__ = 'chats_history' + __tablename__ = "chats_history" # id = Column(Integer, primary_key=True, index=True) sender = Column(String, primary_key=True, index=True) history = Column(Text) updated_at = Column(DateTime, default=datetime.utcnow) + Base.metadata.create_all(engine) + def get_db(): try: db = SessionLocal() yield db finally: db.close() + + # %% diff --git a/vortex/io/twilio.py b/vortex/io/twilio.py index ef54d4c..2c05825 100644 --- a/vortex/io/twilio.py +++ b/vortex/io/twilio.py @@ -1,4 +1,4 @@ -#%% +# %% import base64 import logging import os @@ -14,8 +14,7 @@ from twilio.rest import Client from vortex.ai.agents import VortexAgent -from vortex.api.data_models import (ChatsHistory, Conversation, SessionLocal, - get_db) +from vortex.api.data_models import ChatsHistory, Conversation, SessionLocal, get_db load_dotenv() @@ -32,18 +31,19 @@ agents: Dict[str, weakref.ref] = weakref.WeakValueDictionary() + def get_or_create_agent(phone_number: str, db) -> VortexAgent: agent = agents.get(phone_number) chat_history = get_chat_history(db, phone_number) - if agent is not None and chat_history: # Same session stil kept - print(f'Using existing agent {agent}') + if agent is not None and chat_history: # Same session stil kept + print(f"Using existing agent {agent}") ... - elif agent is None and chat_history: # New session but existing user + elif agent is None and chat_history: # New session but existing user agent = VortexAgent(context=chat_history) # Initialize a new agent instance - print(f'using reloaded agent with history {chat_history}') + print(f"using reloaded agent with history {chat_history}") elif agent is None and not chat_history: agent = VortexAgent() - print('using a new agent') + print("using a new agent") agents[phone_number] = agent return agent @@ -90,7 +90,7 @@ def get_chat_history(db_session, phone_number: str) -> list: chat_history = str(history[0]) print(chat_history) loaded = pickle.loads(chat_history) - print(f'loaded history {loaded}') + print(f"loaded history {loaded}") return loaded @@ -127,4 +127,5 @@ async def handle_wapp_message( send_message(whatsapp_number, langchain_response) return {"response": langchain_response} + # %%