Skip to content

Commit

Permalink
[AGENT][LiteLLM FIX] [API FIX]
Browse files Browse the repository at this point in the history
  • Loading branch information
kyegomez committed Dec 18, 2024
1 parent a54785c commit 321000a
Show file tree
Hide file tree
Showing 9 changed files with 477 additions and 288 deletions.
6 changes: 3 additions & 3 deletions api/agent_api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
import time
from typing import Dict, Optional, Tuple
from uuid import UUID
import sys

BASE_URL = "http://localhost:8000/v1"
BASE_URL = "http://0.0.0.0:8000/v1"


def check_api_server() -> bool:
Expand Down Expand Up @@ -199,6 +198,7 @@ def test_completion(session: TestSession, agent_id: UUID) -> bool:

if response.status_code == 200:
completion_data = response.json()
print(completion_data)
logger.success(
f"Got completion, used {completion_data['token_usage']['total_tokens']} tokens"
)
Expand Down Expand Up @@ -317,4 +317,4 @@ def run_test_workflow():

if __name__ == "__main__":
success = run_test_workflow()
sys.exit(0 if success else 1)
print(success)
2 changes: 1 addition & 1 deletion api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json
from time import sleep

BASE_URL = "http://api.swarms.ai:8000"
BASE_URL = "http://swarms-api-893767232.us-east-2.elb.amazonaws.com"


def make_request(method, endpoint, data=None):
Expand Down
55 changes: 27 additions & 28 deletions async_workflow_example.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import asyncio
from typing import List

Expand All @@ -18,16 +17,16 @@

async def create_specialized_agents() -> List[Agent]:
"""Create a set of specialized agents for financial analysis"""

# Base model configuration
model = OpenAIChat(model_name="gpt-4o")

# Financial Analysis Agent
financial_agent = Agent(
agent_name="Financial-Analysis-Agent",
agent_description="Personal finance advisor agent",
system_prompt=FINANCIAL_AGENT_SYS_PROMPT +
"Output the <DONE> token when you're done creating a portfolio of etfs, index, funds, and more for AI",
system_prompt=FINANCIAL_AGENT_SYS_PROMPT
+ "Output the <DONE> token when you're done creating a portfolio of etfs, index, funds, and more for AI",
max_loops=1,
llm=model,
dynamic_temperature_enabled=True,
Expand All @@ -42,7 +41,7 @@ async def create_specialized_agents() -> List[Agent]:
saved_state_path="financial_agent.json",
interactive=False,
)

# Risk Assessment Agent
risk_agent = Agent(
agent_name="Risk-Assessment-Agent",
Expand All @@ -60,7 +59,7 @@ async def create_specialized_agents() -> List[Agent]:
saved_state_path="risk_agent.json",
interactive=False,
)

# Market Research Agent
research_agent = Agent(
agent_name="Market-Research-Agent",
Expand All @@ -78,49 +77,50 @@ async def create_specialized_agents() -> List[Agent]:
saved_state_path="research_agent.json",
interactive=False,
)

return [financial_agent, risk_agent, research_agent]


async def main():
# Create specialized agents
agents = await create_specialized_agents()

# Create workflow with group chat enabled
workflow = create_default_workflow(
agents=agents,
name="AI-Investment-Analysis-Workflow",
enable_group_chat=True
enable_group_chat=True,
)

# Configure speaker roles
workflow.speaker_system.add_speaker(
SpeakerConfig(
role=SpeakerRole.COORDINATOR,
agent=agents[0], # Financial agent as coordinator
priority=1,
concurrent=False,
required=True
required=True,
)
)

workflow.speaker_system.add_speaker(
SpeakerConfig(
role=SpeakerRole.CRITIC,
agent=agents[1], # Risk agent as critic
priority=2,
concurrent=True
concurrent=True,
)
)

workflow.speaker_system.add_speaker(
SpeakerConfig(
role=SpeakerRole.EXECUTOR,
agent=agents[2], # Research agent as executor
priority=2,
concurrent=True
concurrent=True,
)
)

# Investment analysis task
investment_task = """
Create a comprehensive investment analysis for a $40k portfolio focused on AI growth opportunities:
Expand All @@ -130,32 +130,30 @@ async def main():
4. Provide market trend analysis
Present the results in a structured markdown format.
"""

try:
# Run workflow with retry
result = await run_workflow_with_retry(
workflow=workflow,
task=investment_task,
max_retries=3
workflow=workflow, task=investment_task, max_retries=3
)

print("\nWorkflow Results:")
print("================")

# Process and display agent outputs
for output in result.agent_outputs:
print(f"\nAgent: {output.agent_name}")
print("-" * (len(output.agent_name) + 8))
print(output.output)

# Display group chat history if enabled
if workflow.enable_group_chat:
print("\nGroup Chat Discussion:")
print("=====================")
for msg in workflow.speaker_system.message_history:
print(f"\n{msg.role} ({msg.agent_name}):")
print(msg.content)

# Save detailed results
if result.metadata.get("shared_memory_keys"):
print("\nShared Insights:")
Expand All @@ -165,13 +163,14 @@ async def main():
if value:
print(f"\n{key}:")
print(value)

except Exception as e:
print(f"Workflow failed: {str(e)}")

finally:
await workflow.cleanup()


if __name__ == "__main__":
# Run the example
asyncio.run(main())
asyncio.run(main())
Loading

0 comments on commit 321000a

Please sign in to comment.