-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
90 lines (74 loc) · 2.91 KB
/
Copy pathmain.py
File metadata and controls
90 lines (74 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import os
import sys
from dotenv import load_dotenv
# Fix for Windows terminal encoding issues
if sys.platform == "win32":
sys.stdout.reconfigure(encoding='utf-8')
from langchain_openai import ChatOpenAI
from langchain_anthropic import ChatAnthropic
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.messages import HumanMessage
from agent.graph import create_agent_graph
def get_llm():
"""Initializes the LLM based on available environment variables."""
if os.getenv("ANTHROPIC_API_KEY"):
return ChatAnthropic(model="claude-3-haiku-20240307")
elif os.getenv("OPENAI_API_KEY"):
return ChatOpenAI(model="gpt-4o-mini")
elif os.getenv("GOOGLE_API_KEY"):
return ChatGoogleGenerativeAI(model="gemini-flash-latest")
else:
raise ValueError("No API key found. Please set ANTHROPIC_API_KEY, OPENAI_API_KEY, or GOOGLE_API_KEY in .env")
def print_header():
print("\n" + "="*50)
print(" AUTOSTREAM CONVERSATIONAL AI AGENT")
print(" Social-to-Lead Agentic Workflow")
print("="*50)
print("Type 'exit' or 'quit' to end the session.\n")
def main():
load_dotenv()
try:
llm = get_llm()
except ValueError as e:
print(f"Error: {e}")
return
graph = create_agent_graph()
# Initialize state
state = {
"messages": [],
"intent": None,
"lead_name": None,
"lead_email": None,
"lead_platform": None,
"lead_captured": False
}
print_header()
while True:
user_input = input("USER: ")
if user_input.lower() in ["exit", "quit", "bye"]:
print("AGENT: Goodbye! Have a great day creating content.")
break
# Add user message to state
state["messages"].append(HumanMessage(content=user_input))
# Run the graph
# We pass the llm in the config so nodes can access it
config = {"configurable": {"llm": llm}}
try:
output = graph.invoke(state, config=config)
# Update state with the output from the graph
state = output
# Print the last message from the agent
if state["messages"]:
last_message = state["messages"][-1]
content = last_message.content
if isinstance(content, list):
# Handle structured content (e.g. from Gemini)
text_parts = [part.get("text", "") for part in content if isinstance(part, dict) and part.get("type") == "text"]
print(f"AGENT: {''.join(text_parts)}\n")
else:
print(f"AGENT: {content}\n")
except Exception as e:
print(f"\n[ERROR] Something went wrong: {e}")
print("Please check your API keys and internet connection.\n")
if __name__ == "__main__":
main()