Spaces:
Sleeping
Sleeping
Priyansh Saxena commited on
Commit ·
3240afb
1
Parent(s): acd7de3
Migration Phase 6: Autonomous agent core modules
Browse files- agent/nodes.py +25 -0
agent/nodes.py
CHANGED
|
@@ -5,6 +5,7 @@ from langchain_core.prompts import ChatPromptTemplate
|
|
| 5 |
from agent.state import AgentState
|
| 6 |
from rag.retriever import retrieve_documents
|
| 7 |
from tools.lead_capture import mock_lead_capture
|
|
|
|
| 8 |
from langchain_huggingface import HuggingFacePipeline
|
| 9 |
from transformers import pipeline
|
| 10 |
import os
|
|
@@ -26,6 +27,10 @@ def get_llm():
|
|
| 26 |
)
|
| 27 |
_local_llm = HuggingFacePipeline(pipeline=pipe)
|
| 28 |
return _local_llm
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
return ChatOpenAI(model="gpt-4o-mini", temperature=0)
|
| 30 |
|
| 31 |
class IntentResponse(BaseModel):
|
|
@@ -44,6 +49,7 @@ def detect_intent(state: AgentState) -> AgentState:
|
|
| 44 |
("user", "{message}")
|
| 45 |
])
|
| 46 |
|
|
|
|
| 47 |
if hasattr(llm, "with_structured_output"):
|
| 48 |
chain = prompt | llm.with_structured_output(IntentResponse)
|
| 49 |
else:
|
|
@@ -56,14 +62,21 @@ def detect_intent(state: AgentState) -> AgentState:
|
|
| 56 |
])
|
| 57 |
|
| 58 |
chain = prompt | llm | parser
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
history_str = "\n".join([f"{msg['role']}: {msg['content']}" for msg in state.get("conversation_history", [])[-3:]])
|
| 61 |
context_message = f"Recent history:\n{history_str}\n\nCurrent message:\n{state['current_message']}"
|
| 62 |
|
|
|
|
| 63 |
if hasattr(llm, "with_structured_output"):
|
| 64 |
response = chain.invoke({"message": context_message})
|
| 65 |
else:
|
| 66 |
response = chain.invoke({"message": context_message, "format_instructions": parser.get_format_instructions()})
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
return {"detected_intent": response.intent}
|
| 69 |
|
|
@@ -92,9 +105,13 @@ def generate_rag_response(state: AgentState) -> AgentState:
|
|
| 92 |
"message": state["current_message"]
|
| 93 |
})
|
| 94 |
|
|
|
|
| 95 |
content = response.content if hasattr(response, "content") else str(response)
|
| 96 |
|
| 97 |
return {"response": content}
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
def process_lead(state: AgentState) -> AgentState:
|
| 100 |
llm = get_llm()
|
|
@@ -103,6 +120,7 @@ def process_lead(state: AgentState) -> AgentState:
|
|
| 103 |
("system", "Extract the user's name, email, and creator platform (e.g. YouTube, TikTok, Instagram) from the message if present. Return null for fields not found."),
|
| 104 |
("user", "{message}")
|
| 105 |
])
|
|
|
|
| 106 |
|
| 107 |
if hasattr(llm, "with_structured_output"):
|
| 108 |
extract_chain = extract_prompt | llm.with_structured_output(LeadExtractionResponse)
|
|
@@ -114,14 +132,21 @@ def process_lead(state: AgentState) -> AgentState:
|
|
| 114 |
("user", "{message}")
|
| 115 |
])
|
| 116 |
extract_chain = extract_prompt | llm | parser
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
history_str = "\n".join([f"{msg['role']}: {msg['content']}" for msg in state.get("conversation_history", [])[-3:]])
|
| 119 |
context_message = f"Recent history:\n{history_str}\n\nCurrent message:\n{state['current_message']}"
|
| 120 |
|
|
|
|
| 121 |
if hasattr(llm, "with_structured_output"):
|
| 122 |
extracted = extract_chain.invoke({"message": context_message})
|
| 123 |
else:
|
| 124 |
extracted = extract_chain.invoke({"message": context_message, "format_instructions": parser.get_format_instructions()})
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
updates = {}
|
| 127 |
if extracted.user_name and not state.get("user_name"):
|
|
|
|
| 5 |
from agent.state import AgentState
|
| 6 |
from rag.retriever import retrieve_documents
|
| 7 |
from tools.lead_capture import mock_lead_capture
|
| 8 |
+
<<<<<<< HEAD
|
| 9 |
from langchain_huggingface import HuggingFacePipeline
|
| 10 |
from transformers import pipeline
|
| 11 |
import os
|
|
|
|
| 27 |
)
|
| 28 |
_local_llm = HuggingFacePipeline(pipeline=pipe)
|
| 29 |
return _local_llm
|
| 30 |
+
=======
|
| 31 |
+
|
| 32 |
+
def get_llm():
|
| 33 |
+
>>>>>>> 128a106 (Migration Phase 6: Autonomous agent core modules)
|
| 34 |
return ChatOpenAI(model="gpt-4o-mini", temperature=0)
|
| 35 |
|
| 36 |
class IntentResponse(BaseModel):
|
|
|
|
| 49 |
("user", "{message}")
|
| 50 |
])
|
| 51 |
|
| 52 |
+
<<<<<<< HEAD
|
| 53 |
if hasattr(llm, "with_structured_output"):
|
| 54 |
chain = prompt | llm.with_structured_output(IntentResponse)
|
| 55 |
else:
|
|
|
|
| 62 |
])
|
| 63 |
|
| 64 |
chain = prompt | llm | parser
|
| 65 |
+
=======
|
| 66 |
+
chain = prompt | llm.with_structured_output(IntentResponse)
|
| 67 |
+
>>>>>>> 128a106 (Migration Phase 6: Autonomous agent core modules)
|
| 68 |
|
| 69 |
history_str = "\n".join([f"{msg['role']}: {msg['content']}" for msg in state.get("conversation_history", [])[-3:]])
|
| 70 |
context_message = f"Recent history:\n{history_str}\n\nCurrent message:\n{state['current_message']}"
|
| 71 |
|
| 72 |
+
<<<<<<< HEAD
|
| 73 |
if hasattr(llm, "with_structured_output"):
|
| 74 |
response = chain.invoke({"message": context_message})
|
| 75 |
else:
|
| 76 |
response = chain.invoke({"message": context_message, "format_instructions": parser.get_format_instructions()})
|
| 77 |
+
=======
|
| 78 |
+
response = chain.invoke({"message": context_message})
|
| 79 |
+
>>>>>>> 128a106 (Migration Phase 6: Autonomous agent core modules)
|
| 80 |
|
| 81 |
return {"detected_intent": response.intent}
|
| 82 |
|
|
|
|
| 105 |
"message": state["current_message"]
|
| 106 |
})
|
| 107 |
|
| 108 |
+
<<<<<<< HEAD
|
| 109 |
content = response.content if hasattr(response, "content") else str(response)
|
| 110 |
|
| 111 |
return {"response": content}
|
| 112 |
+
=======
|
| 113 |
+
return {"response": response.content}
|
| 114 |
+
>>>>>>> 128a106 (Migration Phase 6: Autonomous agent core modules)
|
| 115 |
|
| 116 |
def process_lead(state: AgentState) -> AgentState:
|
| 117 |
llm = get_llm()
|
|
|
|
| 120 |
("system", "Extract the user's name, email, and creator platform (e.g. YouTube, TikTok, Instagram) from the message if present. Return null for fields not found."),
|
| 121 |
("user", "{message}")
|
| 122 |
])
|
| 123 |
+
<<<<<<< HEAD
|
| 124 |
|
| 125 |
if hasattr(llm, "with_structured_output"):
|
| 126 |
extract_chain = extract_prompt | llm.with_structured_output(LeadExtractionResponse)
|
|
|
|
| 132 |
("user", "{message}")
|
| 133 |
])
|
| 134 |
extract_chain = extract_prompt | llm | parser
|
| 135 |
+
=======
|
| 136 |
+
extract_chain = extract_prompt | llm.with_structured_output(LeadExtractionResponse)
|
| 137 |
+
>>>>>>> 128a106 (Migration Phase 6: Autonomous agent core modules)
|
| 138 |
|
| 139 |
history_str = "\n".join([f"{msg['role']}: {msg['content']}" for msg in state.get("conversation_history", [])[-3:]])
|
| 140 |
context_message = f"Recent history:\n{history_str}\n\nCurrent message:\n{state['current_message']}"
|
| 141 |
|
| 142 |
+
<<<<<<< HEAD
|
| 143 |
if hasattr(llm, "with_structured_output"):
|
| 144 |
extracted = extract_chain.invoke({"message": context_message})
|
| 145 |
else:
|
| 146 |
extracted = extract_chain.invoke({"message": context_message, "format_instructions": parser.get_format_instructions()})
|
| 147 |
+
=======
|
| 148 |
+
extracted = extract_chain.invoke({"message": context_message})
|
| 149 |
+
>>>>>>> 128a106 (Migration Phase 6: Autonomous agent core modules)
|
| 150 |
|
| 151 |
updates = {}
|
| 152 |
if extracted.user_name and not state.get("user_name"):
|