|
| 1 | +# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. ========= |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | +# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. ========= |
| 14 | + |
| 15 | +""" |
| 16 | +Multi-turn Workforce Conversation Example |
| 17 | +
|
| 18 | +This example demonstrates a multi-turn conversation where: |
| 19 | +- User provides input via terminal |
| 20 | +- Previous task and summary are passed as context to each new turn |
| 21 | +- Two agents (Researcher and Writer) collaborate on tasks |
| 22 | +
|
| 23 | +Example conversation to try: |
| 24 | + Turn 1: "What is machine learning?" |
| 25 | + Turn 2: "Can you give me an example application?" |
| 26 | + Turn 3: "How does that relate to what you explained earlier?" |
| 27 | +
|
| 28 | +Or try: |
| 29 | + Turn 1: "Tell me about Paris" |
| 30 | + Turn 2: "What are the top 3 attractions there?" |
| 31 | + Turn 3: "Which one would you recommend based on our discussion?" |
| 32 | +""" |
| 33 | + |
| 34 | +from camel.agents.chat_agent import ChatAgent |
| 35 | +from camel.messages.base import BaseMessage |
| 36 | +from camel.models import ModelFactory |
| 37 | +from camel.societies.workforce import Workforce |
| 38 | +from camel.tasks.task import Task |
| 39 | +from camel.types import ModelPlatformType, ModelType |
| 40 | + |
| 41 | +# Set up agents |
| 42 | +research_agent = ChatAgent( |
| 43 | + system_message=BaseMessage.make_assistant_message( |
| 44 | + role_name="Researcher", |
| 45 | + content="You are a research specialist who gathers and " |
| 46 | + "analyzes information. You focus on finding facts and " |
| 47 | + "providing detailed context.", |
| 48 | + ), |
| 49 | + model=ModelFactory.create( |
| 50 | + model_platform=ModelPlatformType.DEFAULT, |
| 51 | + model_type=ModelType.DEFAULT, |
| 52 | + ), |
| 53 | +) |
| 54 | + |
| 55 | +writer_agent = ChatAgent( |
| 56 | + system_message=BaseMessage.make_assistant_message( |
| 57 | + role_name="Writer", |
| 58 | + content="You are a professional writer who creates clear, " |
| 59 | + "concise responses. You synthesize information into " |
| 60 | + "well-structured answers.", |
| 61 | + ), |
| 62 | + model=ModelFactory.create( |
| 63 | + model_platform=ModelPlatformType.DEFAULT, |
| 64 | + model_type=ModelType.DEFAULT, |
| 65 | + ), |
| 66 | +) |
| 67 | + |
| 68 | +# Create workforce with 2 agents |
| 69 | +workforce = Workforce('Multi-turn Assistant Team') |
| 70 | +workforce.add_single_agent_worker( |
| 71 | + "A researcher who gathers and analyzes information", worker=research_agent |
| 72 | +).add_single_agent_worker( |
| 73 | + "A writer who synthesizes information into clear responses", |
| 74 | + worker=writer_agent, |
| 75 | +) |
| 76 | + |
| 77 | +# Initialize conversation history for all rounds |
| 78 | +conversation_history = [] |
| 79 | +turn_number = 0 |
| 80 | + |
| 81 | +print("=== Multi-turn Workforce Conversation ===") |
| 82 | +print("Type your task/question (or 'quit' to exit)") |
| 83 | +print("=" * 50) |
| 84 | + |
| 85 | +# Multi-turn conversation loop |
| 86 | +while True: |
| 87 | + turn_number += 1 |
| 88 | + print(f"\n--- Turn {turn_number} ---") |
| 89 | + |
| 90 | + # Get user input from terminal |
| 91 | + user_input = input("You: ").strip() |
| 92 | + |
| 93 | + if user_input.lower() in ['quit', 'exit', 'q']: |
| 94 | + print("Exiting conversation. Goodbye!") |
| 95 | + break |
| 96 | + |
| 97 | + if not user_input: |
| 98 | + print("Please enter a valid task or question.") |
| 99 | + continue |
| 100 | + |
| 101 | + # Build task content with context from previous rounds |
| 102 | + history_context = "" |
| 103 | + for i in range(len(conversation_history)): |
| 104 | + item = conversation_history[i] |
| 105 | + history_context += f"Round {i+1}:\n" |
| 106 | + history_context += f"Task: {item['task']}\n" |
| 107 | + history_context += f"Result: {item['result']}\n\n" |
| 108 | + |
| 109 | + task_content = ( |
| 110 | + f"{history_context}{'='*50}\nNew task: {user_input}" |
| 111 | + if history_context |
| 112 | + else user_input |
| 113 | + ) |
| 114 | + |
| 115 | + # Create and process task |
| 116 | + task = Task(content=task_content, id=str(turn_number)) |
| 117 | + task_result = workforce.process_task(task) |
| 118 | + |
| 119 | + # Display response |
| 120 | + print(f"\nAssistant: {task_result.result}") |
| 121 | + |
| 122 | + # Store all information from this round for future context |
| 123 | + round_info = { |
| 124 | + 'task': user_input, |
| 125 | + 'result': task_result.result, |
| 126 | + } |
| 127 | + |
| 128 | + conversation_history.append(round_info) |
| 129 | + |
| 130 | +print("\n--- Conversation Complete ---") |
| 131 | +print(f"Total turns: {turn_number}") |
0 commit comments