我正在使用 CrewAI 和 LangChain 开发一个用于面试模拟的脚本,其中我让代理根据用户响应生成和评估问题。但是,我在正确处理用户输入时遇到问题,并且脚本无法按预期运行。
代码片段:
import os
from crewai import Agent, Task, Crew, Process
from langchain_google_genai import ChatGoogleGenerativeAI
from dotenv import load_dotenv
load_dotenv()
gemini_llm = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0)
# Define agents
question_generator = Agent(
role='Question Generator Agent',
goal='Generate initial and follow-up questions based on user responses and behavioral analysis.',
verbose=True,
memory=True,
backstory=(
"Expert in behavioral analysis and interview techniques, always ready with probing and engaging questions."
),
llm=gemini_llm,
)
evaluator = Agent(
role='Evaluator Agent',
goal='Evaluate the user’s responses for relevance and sufficiency.',
verbose=True,
memory=True,
backstory=(
"Experienced evaluator with a keen eye for detail, ensuring responses meet the required criteria."
),
llm=gemini_llm
)
# Define tasks
initial_question_task = Task(
description="Generate an initial question for the user based on behavioral analysis.",
expected_output='A single question for the user to respond to.',
agent=question_generator,
)
evaluate_response_task = Task(
description="Evaluate the user's response for relevance and sufficiency.",
expected_output='Feedback on whether the response is satisfactory or if a follow-up question is needed.',
agent=evaluator,
)
follow_up_question_task = Task(
description="Generate a follow-up question based on the user's previous response and the evaluator's feedback.",
expected_output='A follow-up question that probes deeper into the user\'s previous answer.',
agent=question_generator,
)
# Define the crew and process
crew = Crew(
agents=[question_generator, evaluator],
tasks=[initial_question_task, evaluate_response_task, follow_up_question_task],
process=Process.sequential
)
# Function to run the interview process
def run_interview():
for question_number in range(10):
print(f"\nQuestion {question_number + 1}:")
# Generate the initial question
crew.process = Process.sequential
result = crew.kickoff(inputs={'task_name': 'initial_question_task'})
initial_question = result['output']
print(initial_question)
user_response = input("Your response: ")
# Evaluate the response
crew.process = Process.sequential
result = crew.kickoff(inputs={'task_name': 'evaluate_response_task', 'response': user_response})
evaluator_feedback = result['output']
if "satisfactory" in evaluator_feedback:
print("Response is satisfactory.")
continue
# Handle follow-up questions
for follow_up_number in range(2):
crew.process = Process.sequential
result = crew.kickoff(inputs={'task_name': 'follow_up_question_task', 'previous_response': user_response})
follow_up_question = result['output']
print(f"Follow-up question {follow_up_number + 1}: {follow_up_question}")
user_response = input("Your response: ")
crew.process = Process.sequential
result = crew.kickoff(inputs={'task_name': 'evaluate_response_task', 'response': user_response})
evaluator_feedback = result['output']
if "satisfactory" in evaluator_feedback:
print("Response is satisfactory.")
break
else:
print("Response is not satisfactory. Generating another follow-up question...")
# Start the interview process
run_interview()
遇到的问题:
在面试模拟过程中,脚本无法正确处理用户输入。具体来说,在生成初始问题并收到用户响应后,它无法正确评估响应或根据评估者的反馈生成后续问题。相反,它会抛出错误或提供不正确的输出。
预期结果:
我希望脚本能够顺利地生成问题、评估回答并根据评估者的反馈按顺序处理后续问题。
我尝试过的:
您是如何解决这个问题的?