Spaces:
Running
Running
File size: 2,971 Bytes
ff0e97f |
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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
"""
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()
# Create agent
agent = await AgentFactory.create_classifier_agent()
# Default test URL if none provided
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")
# Invoke agent
result = await agent.ainvoke({
"messages": [{
"role": "user",
"content": f"What bird species is this? {image_url}"
}]
})
# Print result
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")
# Create multi-server agent with memory
agent = await AgentFactory.create_multi_server_agent(with_memory=True)
# Thread ID for conversation memory
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
# Invoke agent
result = await agent.ainvoke(
{"messages": [{"role": "user", "content": user_input}]},
config
)
# Print response
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:
# Default: run demo
asyncio.run(run_classifier_demo())
if __name__ == "__main__":
main() |