OpenAI函数调用提取多学生信息异常:仅返回单条数据
问题背景
需要从包含两名学生信息的文本中,通过OpenAI Function Calling提取所有学生的name、major、school、grades、club字段,生成结构化JSON,但当前代码仅返回第一位学生的数据。
目标文本
Ravi Patel is a sophomore majoring in computer science at the University of Michigan. He is South Asian Indian American and has a 3.7 GPA. Ravi is an active member of the university's Chess Club and the South Asian Student Association. He hopes to pursue a career in software engineering after graduating. David Nguyen is a sophomore majoring in computer science at Stanford University. He is Asian American and has a 3.8 GPA. David is known for his programming skills and is an active member of the university's Robotics Club. He hopes to pursue a career in artificial intelligence after graduating.
当前代码输出
{"name": "Ravi Patel", "major": "Computer Science", "school": "University of Michigan", "grades": 3.7, "club": "Chess Club, South Asian Student Association"}
问题原因
原函数参数定义为单个学生对象,未支持数组结构,导致模型默认只返回第一个匹配的学生信息。需要调整函数的参数结构,明确要求返回学生数组。
修改后的代码
%pip install --upgrade openai -q import os from openai import OpenAI import json # 设置API密钥 my_key = "API KEY" os.environ["OPENAI_API_KEY"] = my_key client = OpenAI() student_description = "Ravi Patel is a sophomore majoring in computer science at the University of Michigan. He is South Asian Indian American and has a 3.7 GPA. Ravi is an active member of the university's Chess Club and the South Asian Student Association. He hopes to pursue a career in software engineering after graduating. David Nguyen is a sophomore majoring in computer science at Stanford University. He is Asian American and has a 3.8 GPA. David is known for his programming skills and is an active member of the university's Robotics Club. He hopes to pursue a career in artificial intelligence after graduating." # 修改后的函数定义:支持返回学生数组 student_custom_functions = [ { 'name': 'extract_student_info', 'description': 'Extract all student information from the text, including name, major, school, GPA, and club memberships', 'parameters': { 'type': 'object', 'properties': { 'students': { 'type': 'array', 'description': 'List of all students mentioned in the text', 'items': { 'type': 'object', 'properties': { 'name': { 'type': 'string', 'description': 'Name of the student' }, 'major': { 'type': 'string', 'description': 'Student\'s major subject' }, 'school': { 'type': 'string', 'description': 'Name of the university' }, 'grades': { 'type': 'number', # 修正类型:GPA是小数,不是整数 'description': 'Student\'s GPA' }, 'club': { 'type': 'string', 'description': 'Extracurricular clubs the student is part of' } }, 'required': ['name', 'major', 'school', 'grades', 'club'] } } }, 'required': ['students'] } } ] # 调用API response = client.chat.completions.create( model='gpt-3.5-turbo', messages=[{'role': 'user', 'content': student_description}], functions=student_custom_functions, function_call='auto' ) # 解析响应 json_response = json.loads(response.choices[0].message.function_call.arguments) print(json.dumps(json_response, indent=2))
预期输出
{ "students": [ { "name": "Ravi Patel", "major": "computer science", "school": "University of Michigan", "grades": 3.7, "club": "Chess Club, South Asian Student Association" }, { "name": "David Nguyen", "major": "computer science", "school": "Stanford University", "grades": 3.8, "club": "Robotics Club" } ] }
关键修改说明
- 将函数参数结构改为包含
students数组,明确要求模型返回所有学生 - 修正
grades的类型为number(原定义为integer,但GPA是小数,会导致解析错误) - 添加
required字段,确保模型返回所有必填信息
内容的提问来源于stack exchange,提问作者Tim G

