基于OpenAI与DashScope的AI知识面试模拟系统实现

基于OpenAI与DashScope的AI知识面试模拟系统实现

任务介绍

实现一个基于大语言模型的AI面试模拟程序,用户可通过命令行与AI面试官进行多轮对话,测试对AI相关知识的掌握程度。系统需支持问题生成、回答评价及连续对话能力。

任务背景

在AI技术学习中,缺乏结构化反馈是常见痛点。传统面试模拟需人力参与,而基于API的自动化工具能提供低成本、可定制的练习环境。本例使用阿里云DashScope平台的QWen-Turbo模型,通过Chat Completion接口实现动态交互。

代码思路

  1. 初始化配置

    • 通过环境变量管理API密钥,增强安全性
    • 设置base_url适配阿里云兼容模式端点
  2. 对话管理

    • 采用messages列表维护对话上下文
    • 系统消息初始化面试角色设定
  3. 异常处理

    • 对API响应进行多层有效性校验
    • 网络错误时提供明确修复指引
  4. 交互循环

    • 用户输入触发模型的新问题生成
    • 退出指令quit终止会话

解决思路

  1. 上下文保持

    每次交互后更新messages列表,包含用户回答和AI问题,实现多轮对话记忆

  2. 健壮性设计

    • 检查response.choices[0].message.content存在性
    • 捕获Exception并输出可操作的错误信息
  3. 接口兼容性

    使用OpenAI SDK格式调用阿里云服务,降低迁移成本

实现代码

python 复制代码
from openai import OpenAI
import os

# 初始化客户端
api_key = os.getenv('OPENAI_API_KEY', 'TripleH')
if not api_key:
    raise ValueError("请设置环境变量 OPENAI_API_KEY")
    
client = OpenAI(
        api_key=api_key, 
        base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
        )

# 设置系统角色
prompt_for_roleplay = "我需要你面试我有关AI的知识,仅提出问题"
messages = [{'role': 'system', 'content': prompt_for_roleplay}]

# 首轮问题生成
try:
    response = client.chat.completions.create(
        model="qwen-turbo",
        messages=messages
    )
    
    if response and response.choices and len(response.choices) > 0:
        first_question = response.choices[0].message.content
        if first_question:
            messages.append({'role': 'assistant', 'content': first_question})
            print(f'面试官:{first_question}')
            print('\n')
except Exception as e:
    print(f"发生错误:{str(e)}")
    print("请检查API密钥是否正确,网络连接是否正常\n")

# 多轮对话循环
while True:
    try:
        user_input = input("请输入你的回答(输入 'quit' 退出):")
        
        if user_input.lower() == 'quit':
            print("面试结束,再见!")
            break
        
        messages.append({'role': 'user', 'content': user_input})
        
        response = client.chat.completions.create(
            model="qwen-turbo",
            messages=messages
        )
        
        if not response or not response.choices or len(response.choices) == 0:
            print("错误:模型返回了空响应")
            continue
            
        assistant_output = response.choices[0].message.content
        
        if assistant_output is None:
            print("错误:模型返回的内容为空")
            continue
        
        messages.append({'role': 'assistant', 'content': assistant_output})
        
        print(f'面试官:{assistant_output}')
        print('\n')
        
    except Exception as e:
        print(f"发生错误:{str(e)}")
        print("请检查API密钥是否正确,网络连接是否正常\n")
相关推荐
长桥夜波2 小时前
机器学习日报13
人工智能·机器学习
sensen_kiss2 小时前
INT305 Machine Learning 机器学习 Pt.8 Bagging 和 Boosting
人工智能·机器学习·boosting
艾莉丝努力练剑2 小时前
【Linux基础开发工具 (二)】详解Linux文本编辑器:Vim从入门到精通——完整教程与实战指南(上)
linux·运维·服务器·人工智能·ubuntu·centos·vim
我的世界伊若4 小时前
AI重塑IT职场:挑战与机遇并存
人工智能
lapiii3584 小时前
[智能体设计模式] 第4章:反思(Reflection)
人工智能·python·设计模式
IT_Beijing_BIT6 小时前
tensorflow 图像分类 之四
人工智能·分类·tensorflow
卡奥斯开源社区官方7 小时前
NVIDIA Blackwell架构深度解析:2080亿晶体管如何重构AI算力规则?
人工智能·重构·架构
百锦再7 小时前
第11章 泛型、trait与生命周期
android·网络·人工智能·python·golang·rust·go
数新网络10 小时前
The Life of a Read/Write Query for Apache Iceberg Tables
人工智能·apache·知识图谱