使用 Conversable Agent Autogen 时遇到问题

问题描述 投票:0回答:1

这是Conversable Agent功能:

def create_scoring_agent() -> ConversableAgent:
    """Creates an LLM-powered agent for extracting food and service scores from reviews."""
    scoring_agent_system_message = """
    You are an assistant that evaluates restaurant reviews. Your task is to:
    1. Identify adjectives describing food and customer service in the review.
    2. Assign scores based on the following rules:
       - Score 1: awful, horrible, disgusting
       - Score 2: bad, unpleasant, offensive
       - Score 3: average, uninspiring, forgettable
       - Score 4: good, enjoyable, satisfying
       - Score 5: awesome, incredible, amazing
    Return the result as a JSON object with "food_score" and "service_score".
    Example:
    Review: "The food was average, but the customer service was unpleasant."
    Output: {"food_score": 3, "service_score": 2}
    """
    return ConversableAgent(
        name="scoring_agent",
        system_message=scoring_agent_system_message,
        llm_config={
            "config_list": [
                {
                    "model": "gpt-4o-mini", 
                    "api_key": os.environ.get("OPENAI_API_KEY")  # Fetch API key from environment variable
                }
            ]
        }
    )

def analyze_reviews_with_agent(reviews: List[str]) -> Dict[str, List[int]]:
    """Uses an agent to analyze reviews and extract scores."""
    agent = create_scoring_agent()
    food_scores, service_scores = [], []

    for review in reviews:
        # Send the review to the agent for scoring
        user_message = f"Analyze this review: '{review}'. Extract food_score and service_score."
        print(user_message)
        **response = agent.initiate_chat([{"role": "user", "content": user_message}])**
        print(response)

        # Parse the agent's response
        try:
            result = eval(response["content"])  # Convert string to dictionary
            food_scores.append(result["food_score"])
            service_scores.append(result["service_score"])
        except Exception as e:
            print(f"Error processing review: {review}. Error: {e}")

    return {"food_scores": food_scores, "customer_service_scores": service_scores}
  • 错误:

    发生异常:AttributeError “list”对象没有属性“_raise_exception_on_async_reply_functions” 文件“/Users/shubham.gaur/Documents/Assignments/llm-agents/lab01_release/main.py”,第 113 行,位于analyze_reviews_with_agent 响应 = agent.initiate_chat([{"角色": "用户", "内容": user_message}]) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ 文件“/Users/shubham.gaur/Documents/Assignments/llm-agents/lab01_release/main.py”,第 156 行,在 main 中 分数=analyze_reviews_with_agent(reviews.get(user_query, [])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^ 文件“/Users/shubham.gaur/Documents/Assignments/llm-agents/lab01_release/main.py”,第 172 行,位于 主要(sys.argv[1]) AttributeError:“列表”对象没有属性“_raise_exception_on_async_reply_functions

我刚刚启动代理聊天,以便根据提示调用 gpt。

为了更好地了解要点,请查看屏幕截图。 analyze_reviews_with_agent 函数失败。

analyze_reviews_with_agent function

python agent gpt-4 ms-autogen
1个回答
0
投票

当 LLM Agent 开始与 AutoGen 框架中的其他代理交互时,必须有其他代理在场。根据 AutoGen 文档

initiate_chat
方法需要至少一个
recipient
,它应该是
ConversableAgent
的实例。此外,最好指定
message
max_turns
summary_method arguments

这是 AutoGen 中顺序对话模式的示例,我认为您打算使用它:

import os
from autogen import ConversableAgent

llm_config = {"config_list": [{"model": "gpt-4o-mini", "api_key": os.environ.get("OPENAI_API_KEY")}]}
# the main entrypoint/supervisor agent
entrypoint_agent = ConversableAgent("entrypoint_agent", 
                                        system_message=entrypoint_agent_system_message, 
                                        llm_config=llm_config)
# other agents
data_fetch_agent = ConversableAgent("data_fetch_agent", 
                                      system_message=data_fetch_agent_system_message, 
                                        llm_config=llm_config)
review_analysis_agent = ConversableAgent("review_analysis_agent", 
                                        system_message=review_analysis_agent_system_message, 
                                        llm_config=llm_config)
scoring_agent = ConversableAgent("scoring_agent", 
                                        system_message=scoring_agent_system_message, 
                                        llm_config=llm_config)

result = entrypoint_agent.initiate_chats(
        [
            {
                "recipient": data_fetch_agent,
                "message": f"The question is: {user_query}",
                "max_turns": 2,
                "summary_method": "last_msg",
            },
            {
                "recipient": review_analysis_agent,
                "message": "These are the reviews",
                "max_turns": 1,
                "summary_method": "reflection_with_llm",
                "summary_args": {"summary_prompt":"some prompt"},
            },
            {
                "recipient": scoring_agent,
                "message": "These are raw scores",
                "max_turns": 2,
                "summary_method": "last_msg",
            },
        ]
    )
© www.soinside.com 2019 - 2024. All rights reserved.