我使用 gpt 来回答有关文档的问题,它返回如下答案:
是否有自查或练习测验和/或考试的机会?不
测验和/或考试是否有多次尝试?不
写作作业有机会获得改进反馈吗?不
个人/小组项目是否有机会获得改进反馈?不
是否有频繁的知识检查来评估期中、期末考试和期末项目之外的学习情况?不
是否有任何反思性的学习机会,例如简短的写作作业或考试之外的反思?是的
然后我想使用以下代码将答案转换为布尔值
if answer.endswith("Yes") or answer.endswith("yes"):
is_true = 1
else:
is_true = 0
但是它返回的值全是 1。我不确定原因是什么,我尝试了几种不同的方式来编写这篇文章,但它要么给我全 0,要么全 1,其中预期的答案应该是 [0 , 0, 0, 0, 0, 1]。有人可以帮忙吗?我确实检查过,答案的数据类型是元组,我要求它检查字符串(是),这可能是原因吗?
完整功能在这里
def ask_questions(questions, text):
answers=[]
for question in questions:
prompt=f'''
review content of {extracted_text} and answer questions in {questions_gm} and provde boolean values (Yes and No) to each question.
Don’t justify your answers. Don’t give information not mentioned in the {extracted_text}.
'''
#get answers from gpt
response=client.chat.completions.create(
model="gpt-4-0125-preview",
messages=[{"role": "user", "content": prompt}],
temperature=0,
)
answer=response.choices[0].message.content
print(answer)
if answer.endswith("Yes") or answer.endswith("yes"):
is_true = 1
else:
is_true = 0
answers.append(is_true)
print(answers)
return answers
boolean_answers_gm=ask_questions(questions_gm, extracted_text)
如果字符串以指定值开头,则
startswith()
方法返回 True
,否则返回 False。尝试在元组上使用此方法将返回 AttributeError: 'tuple' object has no attribute 'startswith'
。
>>> example_tuple = ('Yes', 'No')
>>> example_tuple.startswith('Y')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'startswith'
但是您可以使用
join()
方法将元组中的所有项目连接到一个字符串中。
>>> ' '.join(example_tuple).startswith('Yes')
True
>>> ' '.join(example_tuple).endswith('No')
True