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
Объект «список» не имеет атрибута «_raise_Exception_on_async_reply_functions»
Файл «/Users/shubham.gaur/Documents/Assignments/llm-agents/lab01_release/main.py», строка 113, в анализе_reviews_with_agent
response = Agent.initiate_chat([{"role": "user", "content": user_message}])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^
Файл "/Users/shubham.gaur/Documents/Assignments/llm-agents/lab01_release/main.py", строка 156, в main
scores = Analysis_reviews_with_agent(reviews.get(user_query, []))
^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Файл "/Users/shubham.gaur/Documents/Assignments/llm-agents/lab01_release/main.py", строка 172, в
main(sys.argv[1]) AttributeError: ' Объект list» не имеет атрибута «_raise_Exception_on_async_reply_functions»
Я просто инициировал чат с агентом, чтобы он вызывал gpt по запросу.
Чтобы лучше понять суть, взгляните на скриншот. происходит сбой в функции Analysis_reviews_with_agent.
Это функция Conversable Agent: [code]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} [/code] [list] [*][b]Ошибка:[/b] Произошло исключение: AttributeError Объект «список» не имеет атрибута «_raise_Exception_on_async_reply_functions» Файл «/Users/shubham.gaur/Documents/Assignments/llm-agents/lab01_release/main.py», строка 113, в анализе_reviews_with_agent response = Agent.initiate_chat([{"role": "user", "content": user_message}]) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^ Файл "/Users/shubham.gaur/Documents/Assignments/llm-agents/lab01_release/main.py", строка 156, в main scores = Analysis_reviews_with_agent(reviews.get(user_query, [])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Файл "/Users/shubham.gaur/Documents/Assignments/llm-agents/lab01_release/main.py", строка 172, в main(sys.argv[1]) [b]AttributeError: ' Объект list» не имеет атрибута «_raise_Exception_on_async_reply_functions[/b]»
[/list] Я просто инициировал чат с агентом, чтобы он вызывал gpt по запросу. Чтобы лучше понять суть, взгляните на скриншот. происходит сбой в функции Analysis_reviews_with_agent. [img]https://i.sstatic.net/TMDFrToJ.png[/img]