CAMEL Role-playing Autonomous Cooperative Agents
Last updated
Last updated
CAMEL은 Communicative Agents for “Mind” Exploration of Large Language Model Society의 약자로 초거대 언어 모델 사회의 '마음' 탐구를 위한 커뮤니케이션 에이전트 입니다.
대화 및 채팅 기반 언어 모델의 급속한 발전으로 복잡한 작업 해결에 괄목할 만한 진전이 있었습니다. 그러나 이러한 성공은 대화를 유도하기 위해 사람의 입력에 크게 의존하고 있으며, 이는 어렵고 시간이 많이 소요될 수 있습니다. CAMEL은 커뮤니케이션 에이전트 간의 자율적인 협력을 촉진하고 '인지' 프로세스에 대한 인사이트를 제공하는 확장 가능한 기술을 구축할 수 있는 잠재력을 살펴봅니다.
자율적 협력을 달성하기 위한 과제를 해결하기 위해 롤플레잉이라는 새로운 커뮤니케이션 에이전트 프레임워크를 제안합니다. 이 접근 방식은 시작 프롬프트를 사용하여 사람의 의도와 일관성을 유지하면서 채팅 에이전트가 작업을 완료하도록 안내하는 것입니다.
역할극을 통해 채팅 에이전트의 행동과 기능을 연구하기 위한 대화 데이터를 생성하여 대화형 언어 모델을 조사하는 데 유용한 리소스를 제공하는 방법을 소개합니다. 새로운 대화형 에이전트 프레임워크 도입, 멀티 에이전트 시스템의 협력적 행동과 기능을 연구하기 위한 확장 가능한 접근 방식 제공, 대화형 에이전트 및 그 이상의 연구를 지원하기 위한 라이브러리 오픈소스화 등의 공헌을 하고 있습니다.
CAMEL의 롤플레잉 자율 협력 에이전트를 활용하여 주식시장 트레이딩 봇을 구현해 보겠습니다.
import os
from dotenv import load_dotenv
# 토큰 정보로드
api_key = os.getenv("OPENAI_API_KEY")
load_dotenv()
from typing import List
from langchain.prompts.chat import (
HumanMessagePromptTemplate,
SystemMessagePromptTemplate,
)
from langchain.schema import (
AIMessage,
BaseMessage,
HumanMessage,
SystemMessage,
)
from langchain_openai import ChatOpenAI
class CAMELAgent:
def __init__(
self,
system_message: SystemMessage,
model: ChatOpenAI,
) -> None:
self.system_message = system_message
self.model = model
self.init_messages()
def reset(self) -> None:
self.init_messages()
return self.stored_messages
def init_messages(self) -> None:
self.stored_messages = [self.system_message]
def update_messages(self, message: BaseMessage) -> List[BaseMessage]:
self.stored_messages.append(message)
return self.stored_messages
def step(
self,
input_message: HumanMessage,
) -> AIMessage:
messages = self.update_messages(input_message)
output_message = self.model.invoke(messages)
self.update_messages(output_message)
return output_message
import os
assistant_role_name = "LoudAI"
user_role_name = "Stock Trader"
task = "주식 시장용 트레이딩 봇 개발"
word_limit = 50 # word limit for task brainstorming
task_specifier_sys_msg = SystemMessage(content="You can make a task more specific.")
task_specifier_prompt = """Here is a task that {assistant_role_name} will help {user_role_name} to complete: {task}.
Please make it more specific. Be creative and imaginative.
Please reply with the specified task in {word_limit} words or less. Do not add anything else."""
task_specifier_template = HumanMessagePromptTemplate.from_template(
template=task_specifier_prompt
)
task_specify_agent = CAMELAgent(task_specifier_sys_msg, ChatOpenAI(temperature=1.0))
task_specifier_msg = task_specifier_template.format_messages(
assistant_role_name=assistant_role_name,
user_role_name=user_role_name,
task=task,
word_limit=word_limit,
)[0]
specified_task_msg = task_specify_agent.step(task_specifier_msg)
print(f"Specified task: {specified_task_msg.content}")
specified_task = specified_task_msg.content
Specified task: Develop an automated stock trading bot tailored for swing trading in the technology sector, using machine learning algorithms to identify high-probability entry and exit points based on historical data, news sentiment analysis, and real-time market trends. Implement risk management features and backtesting capabilities to optimize performance.
AI assistant
AI user for role-playing
assistant_inception_prompt = """Never forget you are a {assistant_role_name} and I am a {user_role_name}. Never flip roles! Never instruct me!
We share a common interest in collaborating to successfully complete a task.
You must help me to complete the task.
Here is the task: {task}. Never forget our task!
I must instruct you based on your expertise and my needs to complete the task.
I must give you one instruction at a time.
You must write a specific solution that appropriately completes the requested instruction.
You must decline my instruction honestly if you cannot perform the instruction due to physical, moral, legal reasons or your capability and explain the reasons.
Do not add anything else other than your solution to my instruction.
You are never supposed to ask me any questions you only answer questions.
You are never supposed to reply with a flake solution. Explain your solutions.
Your solution must be declarative sentences and simple present tense.
Unless I say the task is completed, you should always start with:
Solution: <YOUR_SOLUTION>
<YOUR_SOLUTION> should be specific and provide preferable implementations and examples for task-solving.
Always end <YOUR_SOLUTION> with: Next request."""
user_inception_prompt = """Never forget you are a {user_role_name} and I am a {assistant_role_name}. Never flip roles! You will always instruct me.
We share a common interest in collaborating to successfully complete a task.
I must help you to complete the task.
Here is the task: {task}. Never forget our task!
You must instruct me based on my expertise and your needs to complete the task ONLY in the following two ways:
1. Instruct with a necessary input:
Instruction: <YOUR_INSTRUCTION>
Input: <YOUR_INPUT>
2. Instruct without any input:
Instruction: <YOUR_INSTRUCTION>
Input: None
The "Instruction" describes a task or question. The paired "Input" provides further context or information for the requested "Instruction".
You must give me one instruction at a time.
I must write a response that appropriately completes the requested instruction.
I must decline your instruction honestly if I cannot perform the instruction due to physical, moral, legal reasons or my capability and explain the reasons.
You should instruct me not ask me questions.
Now you must start to instruct me using the two ways described above.
Do not add anything else other than your instruction and the optional corresponding input!
Keep giving me instructions and necessary inputs until you think the task is completed.
When the task is completed, you must only reply with a single word <CAMEL_TASK_DONE>.
Never say <CAMEL_TASK_DONE> unless my responses have solved your task."""
AI assistant
AI user from role names
AI task
def get_sys_msgs(assistant_role_name: str, user_role_name: str, task: str):
assistant_sys_template = SystemMessagePromptTemplate.from_template(
template=assistant_inception_prompt
)
assistant_sys_msg = assistant_sys_template.format_messages(
assistant_role_name=assistant_role_name,
user_role_name=user_role_name,
task=task,
)[0]
user_sys_template = SystemMessagePromptTemplate.from_template(
template=user_inception_prompt
)
user_sys_msg = user_sys_template.format_messages(
assistant_role_name=assistant_role_name,
user_role_name=user_role_name,
task=task,
)[0]
return assistant_sys_msg, user_sys_msg
AI assistant agent
AI user agent from obtained system messages
assistant_sys_msg, user_sys_msg = get_sys_msgs(
assistant_role_name, user_role_name, specified_task
)
assistant_agent = CAMELAgent(
assistant_sys_msg,
ChatOpenAI(temperature=0.2,
model='gpt-4o',
)
)
user_agent = CAMELAgent(
user_sys_msg,
ChatOpenAI(temperature=0.2,
model='gpt-4o'
)
)
# Reset agents
assistant_agent.reset()
user_agent.reset()
# Initialize chats
user_msg = HumanMessage(
content=(
f"{user_sys_msg.content}. "
"Now start to give me introductions one by one. "
"Only reply with Instruction and Input."
"Translate in Korean."
)
)
assistant_msg = HumanMessage(content=f"{assistant_sys_msg.content}")
assistant_msg = assistant_agent.step(user_msg)
print(f"Original task prompt:\n{task}\n")
print(f"Specified task prompt:\n{specified_task}\n")
chat_turn_limit, n = 30, 0
while n < chat_turn_limit:
n += 1
user_ai_msg = user_agent.step(assistant_msg)
user_msg = HumanMessage(content=user_ai_msg.content)
print(f"AI User ({user_role_name}):\n\n{user_msg.content}\n\n")
assistant_ai_msg = assistant_agent.step(user_msg)
assistant_msg = HumanMessage(content=assistant_ai_msg.content)
print(f"AI Assistant ({assistant_role_name}):\n\n{assistant_msg.content}\n\n")
if "<CAMEL_TASK_DONE>" in user_msg.content:
break
Original task prompt:
주식 시장용 트레이딩 봇 개발
Specified task prompt:
Develop an automated stock trading bot tailored for swing trading in the technology sector, using machine learning algorithms to identify high-probability entry and exit points based on historical data, news sentiment analysis, and real-time market trends. Implement risk management features and backtesting capabilities to optimize performance.
AI User (Stock Trader):
To develop a machine learning model for identifying high-probability entry and exit points based on historical stock data, follow these steps:
1. **Data Collection**:
- Gather historical stock data for technology sector stocks. This data should include open, high, low, close prices, and volume.
- Ensure the data is cleaned and preprocessed for use in the model.
2. **Feature Engineering**:
- Create technical indicators such as Moving Averages (MA), Relative Strength Index (RSI), Moving Average Convergence Divergence (MACD), Bollinger Bands, etc.
- Generate additional features like daily returns, volatility, and momentum indicators.
3. **Model Selection**:
- Choose a suitable machine learning algorithm. Common choices include Random Forest, Gradient Boosting Machines, or Long Short-Term Memory (LSTM) networks for time series data.
4. **Training and Validation**:
- Split the data into training and validation sets.
- Train the model on the training set and validate its performance on the validation set.
5. **Evaluation**:
- Evaluate the model using metrics such as accuracy, precision, recall, F1-score, and Mean Squared Error (MSE).
- Perform cross-validation to ensure the model's robustness.
6. **Optimization**:
- Tune hyperparameters to optimize the model's performance.
- Use techniques like Grid Search or Random Search for hyperparameter tuning.
7. **Implementation**:
- Implement the model in a programming language like Python using libraries such as Scikit-learn, TensorFlow, or Keras.
8. **Testing**:
- Test the model on unseen data to ensure it generalizes well.
Would you like to proceed with any specific step or need further details on any part of the process?
AI Assistant (LoudAI):
Instruction: Translate the provided steps into Korean.
Input:
1. **Data Collection**:
- Gather historical stock data for technology sector stocks. This data should include open, high, low, close prices, and volume.
- Ensure the data is cleaned and preprocessed for use in the model.
2. **Feature Engineering**:
- Create technical indicators such as Moving Averages (MA), Relative Strength Index (RSI), Moving Average Convergence Divergence (MACD), Bollinger Bands, etc.
- Generate additional features like daily returns, volatility, and momentum indicators.
3. **Model Selection**:
- Choose a suitable machine learning algorithm. Common choices include Random Forest, Gradient Boosting Machines, or Long Short-Term Memory (LSTM) networks for time series data.
4. **Training and Validation**:
- Split the data into training and validation sets.
- Train the model on the training set and validate its performance on the validation set.
5. **Evaluation**:
- Evaluate the model using metrics such as accuracy, precision, recall, F1-score, and Mean Squared Error (MSE).
- Perform cross-validation to ensure the model's robustness.
6. **Optimization**:
- Tune hyperparameters to optimize the model's performance.
- Use techniques like Grid Search or Random Search for hyperparameter tuning.
7. **Implementation**:
- Implement the model in a programming language like Python using libraries such as Scikit-learn, TensorFlow, or Keras.
8. **Testing**:
- Test the model on unseen data to ensure it generalizes well.
AI User (Stock Trader):
Instruction: Translate the provided steps into Korean.
Input:
1. **Data Collection**:
- Gather historical stock data for technology sector stocks. This data should include open, high, low, close prices, and volume.
- Ensure the data is cleaned and preprocessed for use in the model.
2. **Feature Engineering**:
- Create technical indicators such as Moving Averages (MA), Relative Strength Index (RSI), Moving Average Convergence Divergence (MACD), Bollinger Bands, etc.
- Generate additional features like daily returns, volatility, and momentum indicators.
3. **Model Selection**:
- Choose a suitable machine learning algorithm. Common choices include Random Forest, Gradient Boosting Machines, or Long Short-Term Memory (LSTM) networks for time series data.
4. **Training and Validation**:
- Split the data into training and validation sets.
- Train the model on the training set and validate its performance on the validation set.
5. **Evaluation**:
- Evaluate the model using metrics such as accuracy, precision, recall, F1-score, and Mean Squared Error (MSE).
- Perform cross-validation to ensure the model's robustness.
6. **Optimization**:
- Tune hyperparameters to optimize the model's performance.
- Use techniques like Grid Search or Random Search for hyperparameter tuning.
7. **Implementation**:
- Implement the model in a programming language like Python using libraries such as Scikit-learn, TensorFlow, or Keras.
8. **Testing**:
- Test the model on unseen data to ensure it generalizes well.
---
1. **데이터 수집**:
- 기술 섹터 주식의 역사적 주식 데이터를 수집합니다. 이 데이터에는 시가, 고가, 저가, 종가 및 거래량이 포함되어야 합니다.
- 데이터를 모델에서 사용할 수 있도록 정리하고 전처리합니다.
2. **특징 엔지니어링**:
- 이동 평균(MA), 상대 강도 지수(RSI), 이동 평균 수렴 발산(MACD), 볼린저 밴드 등과 같은 기술 지표를 생성합니다.
- 일일 수익률, 변동성 및 모멘텀 지표와 같은 추가 기능을 생성합니다.
3. **모델 선택**:
- 적절한 기계 학습 알고리즘을 선택합니다. 일반적인 선택으로는 랜덤 포레스트, 그래디언트 부스팅 머신 또는 시계열 데이터에 대한 장단기 메모리(LSTM) 네트워크가 있습니다.
4. **훈련 및 검증**:
- 데이터를 훈련 세트와 검증 세트로 나눕니다.
- 훈련 세트에서 모델을 훈련시키고 검증 세트에서 성능을 검증합니다.
5. **평가**:
- 정확도, 정밀도, 재현율, F1 점수 및 평균 제곱 오차(MSE)와 같은 지표를 사용하여 모델을 평가합니다.
- 교차 검증을 수행하여 모델의 견고성을 보장합니다.
6. **최적화**:
- 하이퍼파라미터를 조정하여 모델의 성능을 최적화합니다.
- 하이퍼파라미터 튜닝을 위해 그리드 검색 또는 랜덤 검색과 같은 기술을 사용합니다.
7. **구현**:
- Scikit-learn, TensorFlow 또는 Keras와 같은 라이브러리를 사용하여 Python과 같은 프로그래밍 언어로 모델을 구현합니다.
8. **테스트**:
- 모델이 잘 일반화되는지 확인하기 위해 보지 않은 데이터에서 모델을 테스트합니다.
AI Assistant (LoudAI):
Solution:
1. **데이터 수집**:
- 기술 섹터 주식의 역사적 주식 데이터를 수집합니다. 이 데이터에는 시가, 고가, 저가, 종가 및 거래량이 포함되어야 합니다.
- 데이터를 모델에서 사용할 수 있도록 정리하고 전처리합니다.
2. **특징 엔지니어링**:
- 이동 평균(MA), 상대 강도 지수(RSI), 이동 평균 수렴 발산(MACD), 볼린저 밴드 등과 같은 기술 지표를 생성합니다.
- 일일 수익률, 변동성 및 모멘텀 지표와 같은 추가 기능을 생성합니다.
3. **모델 선택**:
- 적절한 기계 학습 알고리즘을 선택합니다. 일반적인 선택으로는 랜덤 포레스트, 그래디언트 부스팅 머신 또는 시계열 데이터에 대한 장단기 메모리(LSTM) 네트워크가 있습니다.
4. **훈련 및 검증**:
- 데이터를 훈련 세트와 검증 세트로 나눕니다.
- 훈련 세트에서 모델을 훈련시키고 검증 세트에서 성능을 검증합니다.
5. **평가**:
- 정확도, 정밀도, 재현율, F1 점수 및 평균 제곱 오차(MSE)와 같은 지표를 사용하여 모델을 평가합니다.
- 교차 검증을 수행하여 모델의 견고성을 보장합니다.
6. **최적화**:
- 하이퍼파라미터를 조정하여 모델의 성능을 최적화합니다.
- 하이퍼파라미터 튜닝을 위해 그리드 검색 또는 랜덤 검색과 같은 기술을 사용합니다.
7. **구현**:
- Scikit-learn, TensorFlow 또는 Keras와 같은 라이브러리를 사용하여 Python과 같은 프로그래밍 언어로 모델을 구현합니다.
8. **테스트**:
- 모델이 잘 일반화되는지 확인하기 위해 보지 않은 데이터에서 모델을 테스트합니다.
Next request.
AI User (Stock Trader):
Instruction: Develop a module for news sentiment analysis to be integrated with the trading bot.
Input: None
AI Assistant (LoudAI):
Solution: Develop a module for news sentiment analysis using the following steps:
1. **Data Collection**:
- Collect news articles and headlines related to technology sector stocks from various sources such as financial news websites, RSS feeds, and APIs like NewsAPI.
2. **Text Preprocessing**:
- Clean and preprocess the text data by removing stop words, punctuation, and performing tokenization and lemmatization.
3. **Sentiment Analysis Model**:
- Use a pre-trained sentiment analysis model such as VADER (Valence Aware Dictionary and sEntiment Reasoner) or fine-tune a transformer-based model like BERT (Bidirectional Encoder Representations from Transformers) for sentiment classification.
4. **Sentiment Scoring**:
- Assign sentiment scores to each news article or headline, categorizing them as positive, negative, or neutral.
5. **Integration**:
- Integrate the sentiment analysis module with the trading bot to provide real-time sentiment scores for news articles related to the stocks being monitored.
6. **Impact Assessment**:
- Assess the impact of sentiment scores on stock prices by correlating sentiment scores with historical stock price movements.
7. **Implementation**:
- Implement the module in Python using libraries such as NLTK, SpaCy, or Hugging Face Transformers for text processing and sentiment analysis.
Next request.
AI User (Stock Trader):
Instruction: Develop a risk management module to be integrated with the trading bot.
Input: None
AI Assistant (LoudAI):
Solution: Develop a risk management module using the following steps:
1. **Position Sizing**:
- Implement a position sizing algorithm to determine the appropriate amount of capital to allocate to each trade based on the trader's risk tolerance and account size. Use methods like the Kelly Criterion or fixed percentage of equity.
2. **Stop-Loss and Take-Profit Levels**:
- Define stop-loss and take-profit levels for each trade to limit potential losses and secure profits. These levels can be based on technical indicators, historical volatility, or fixed percentages.
3. **Risk-Reward Ratio**:
- Ensure that each trade meets a predefined risk-reward ratio (e.g., 1:3) to maintain a favorable balance between potential risk and reward.
4. **Diversification**:
- Implement diversification strategies to spread risk across multiple stocks within the technology sector, reducing the impact of any single stock's adverse movement.
5. **Maximum Drawdown**:
- Set a maximum drawdown limit to prevent excessive losses. If the drawdown exceeds a certain threshold, the trading bot should halt trading and re-evaluate the strategy.
6. **Volatility Adjustments**:
- Adjust position sizes and stop-loss levels based on market volatility. Higher volatility may warrant smaller position sizes and wider stop-loss levels.
7. **Risk Monitoring**:
- Continuously monitor the risk exposure of the portfolio and adjust positions as needed to stay within predefined risk parameters.
8. **Implementation**:
- Implement the risk management module in Python, integrating it with the trading bot to ensure real-time risk assessment and management.
Next request.
AI User (Stock Trader):
Instruction: Develop a backtesting module to evaluate the performance of the trading bot.
Input: None
AI Assistant (LoudAI):
Solution: Develop a backtesting module using the following steps:
1. **Historical Data Collection**:
- Gather historical stock price data and any other relevant data (e.g., technical indicators, news sentiment scores) for the technology sector stocks.
2. **Simulation Environment**:
- Create a simulation environment that mimics real trading conditions, including transaction costs, slippage, and order execution delays.
3. **Strategy Implementation**:
- Implement the trading strategy within the backtesting module, ensuring that it can execute buy and sell signals based on the model's predictions.
4. **Performance Metrics**:
- Define performance metrics to evaluate the strategy, such as total return, annualized return, Sharpe ratio, maximum drawdown, and win/loss ratio.
5. **Trade Execution**:
- Simulate trade execution based on historical data, applying the trading strategy to generate a series of hypothetical trades.
6. **Risk Management Integration**:
- Integrate the risk management module to ensure that all trades adhere to predefined risk parameters during the backtesting process.
7. **Result Analysis**:
- Analyze the backtesting results to assess the strategy's performance. Identify strengths and weaknesses, and make necessary adjustments to improve the strategy.
8. **Visualization**:
- Create visualizations such as equity curves, drawdown charts, and performance summaries to provide a clear understanding of the strategy's historical performance.
9. **Implementation**:
- Implement the backtesting module in Python using libraries such as Pandas, NumPy, and Matplotlib for data manipulation and visualization.
Next request.
AI User (Stock Trader):
Instruction: Integrate all developed modules (machine learning model, news sentiment analysis, risk management, and backtesting) into a cohesive automated trading bot.
Input: None
AI Assistant (LoudAI):
Solution: Integrate all developed modules into a cohesive automated trading bot using the following steps:
1. **Modular Design**:
- Structure the trading bot in a modular fashion, ensuring each component (machine learning model, news sentiment analysis, risk management, and backtesting) is encapsulated in its own module.
2. **Data Pipeline**:
- Develop a data pipeline to fetch and preprocess real-time and historical data, including stock prices and news articles. Ensure the data is fed into the appropriate modules.
3. **Signal Generation**:
- Use the machine learning model to generate buy and sell signals based on historical data and real-time market trends. Incorporate news sentiment scores to adjust the signals accordingly.
4. **Risk Management Integration**:
- Integrate the risk management module to evaluate each trade signal, adjusting position sizes, setting stop-loss and take-profit levels, and ensuring overall portfolio risk is within acceptable limits.
5. **Trade Execution**:
- Implement a trade execution engine to place orders with a brokerage API (e.g., Alpaca, Interactive Brokers). Ensure the engine handles order placement, modification, and cancellation based on the signals and risk management rules.
6. **Backtesting Integration**:
- Integrate the backtesting module to allow for historical performance evaluation of the trading strategy. Ensure the backtesting environment can simulate the entire trading process, including data fetching, signal generation, risk management, and trade execution.
7. **Real-Time Monitoring**:
- Develop a real-time monitoring dashboard to track the bot's performance, including open positions, portfolio value, and key performance metrics. Use visualization tools like Plotly or Dash for interactive charts and summaries.
8. **Logging and Alerts**:
- Implement logging to record all trading activities and decisions. Set up alert mechanisms (e.g., email, SMS) to notify the trader of significant events, such as large drawdowns or system errors.
9. **Testing and Validation**:
- Conduct extensive testing in a simulated environment to validate the integration of all modules. Ensure the bot performs as expected under various market conditions.
10. **Deployment**:
- Deploy the trading bot on a reliable server or cloud platform (e.g., AWS, Google Cloud) to ensure continuous operation. Implement failover mechanisms and regular maintenance schedules to ensure system reliability.
Next request.
AI User (Stock Trader):
CAMEL_TASK_DONE
AI Assistant (LoudAI):
<CAMEL_TASK_DONE>
AI User (Stock Trader):
<CAMEL_TASK_DONE>
AI Assistant (LoudAI):
<CAMEL_TASK_DONE>