|
|
""" |
|
|
Main entry point for LangGraph bird classification agents. |
|
|
""" |
|
|
import asyncio |
|
|
import sys |
|
|
from typing import Optional |
|
|
|
|
|
from .config import AgentConfig |
|
|
from .agents import AgentFactory |
|
|
|
|
|
async def run_classifier_demo(image_url: Optional[str] = None): |
|
|
"""Run basic classifier demo""" |
|
|
|
|
|
AgentConfig.print_config() |
|
|
|
|
|
|
|
|
agent = await AgentFactory.create_classifier_agent() |
|
|
|
|
|
|
|
|
if not image_url: |
|
|
image_url = "https://images.unsplash.com/photo-1445820200644-69f87d946277?w=400" |
|
|
|
|
|
print("="*70) |
|
|
print("Testing bird classification...") |
|
|
print(f"[IMAGE URL]: {image_url}\n") |
|
|
|
|
|
|
|
|
result = await agent.ainvoke({ |
|
|
"messages": [{ |
|
|
"role": "user", |
|
|
"content": f"What bird species is this? {image_url}" |
|
|
}] |
|
|
}) |
|
|
|
|
|
|
|
|
print("\n[AGENT RESPONSE]:") |
|
|
print(result["messages"][-1].content) |
|
|
print("\n[DEMO COMPLETE!]\n") |
|
|
|
|
|
async def run_interactive_chat(): |
|
|
"""Run interactive chat with agent.""" |
|
|
|
|
|
print("\n"+"="*70) |
|
|
print("Bird Classification Agent - Interactive Mode") |
|
|
print("="*70) |
|
|
print("Commands:") |
|
|
print(". - Type 'quit' or 'exit' to end session") |
|
|
print(" - Paste image URLs to classify birds") |
|
|
print(". - Ask about bird locations, sightings, hotspots") |
|
|
print("="*70+"\n") |
|
|
|
|
|
|
|
|
agent = await AgentFactory.create_multi_server_agent(with_memory=True) |
|
|
|
|
|
|
|
|
config = {"configurable": {"thread_id": "interactive_session"}} |
|
|
|
|
|
while True: |
|
|
try: |
|
|
user_input = input("\nYou: ").strip() |
|
|
|
|
|
if user_input.lower() in ['quit', 'exit', 'q']: |
|
|
print("\nGoodbye! Happy birding!\n") |
|
|
break |
|
|
|
|
|
if not user_input: |
|
|
continue |
|
|
|
|
|
|
|
|
result = await agent.ainvoke( |
|
|
{"messages": [{"role": "user", "content": user_input}]}, |
|
|
config |
|
|
) |
|
|
|
|
|
|
|
|
print(f"\nAgent: {result['messages'][-1].content}") |
|
|
|
|
|
except KeyboardInterrupt: |
|
|
print("\nGoodbye! Happy birding!\n") |
|
|
break |
|
|
except Exception as e: |
|
|
print(f"\n[ERROR]: {e}") |
|
|
|
|
|
def main(): |
|
|
"""Main entry point""" |
|
|
|
|
|
if len(sys.argv) > 1: |
|
|
command = sys.argv[1] |
|
|
|
|
|
if command == "interactive": |
|
|
asyncio.run(run_interactive_chat()) |
|
|
elif command == "demo": |
|
|
url = sys.argv[2] if len(sys.argv) > 2 else None |
|
|
asyncio.run(run_classifier_demo(url)) |
|
|
else: |
|
|
print(f"Unknown command: {command}") |
|
|
print("Usage:") |
|
|
print(" python -m langgraph_agent demo [url]") |
|
|
print(" python -m langgraph_agent interactive") |
|
|
else: |
|
|
|
|
|
asyncio.run(run_classifier_demo()) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |