From 3334d655ca0354f2e951bb022a441b8c37ed0b6e Mon Sep 17 00:00:00 2001 From: DjangoPeng Date: Tue, 24 Sep 2024 23:06:44 +0800 Subject: [PATCH] WIP:add demo of job interview scenario --- content/hotel_checkin_page.md | 3 +- content/job_interview_page.md | 12 +++++ prompts/job_interview_prompt.txt | 59 +++++++++++++++++++++ src/agents/job_interview_agent.py | 87 +++++++++++++++++++++++++++++-- src/main.py | 1 - 5 files changed, 156 insertions(+), 6 deletions(-) create mode 100644 prompts/job_interview_prompt.txt diff --git a/content/hotel_checkin_page.md b/content/hotel_checkin_page.md index 2929243..afd0c96 100644 --- a/content/hotel_checkin_page.md +++ b/content/hotel_checkin_page.md @@ -1,6 +1,5 @@ **目标:** -- 完成酒店入住流程并询问酒店的设施 -Complete the hotel check-in process and ask about the hotel's amenities. +- 完成酒店入住流程并询问酒店的设施(Complete the hotel check-in process and ask about the hotel's amenities) **挑战:** 1. 提供你的预订信息并进行身份验证 diff --git a/content/job_interview_page.md b/content/job_interview_page.md index e69de29..fb12911 100644 --- a/content/job_interview_page.md +++ b/content/job_interview_page.md @@ -0,0 +1,12 @@ +**目标:** +- 自我介绍并回答面试问题(Introduce yourself and answer interview questionss) + +**挑战:** +1. 告诉面试官你的教育和工作经历 + Tell the interviewer about your education and work experience +2. 回答有关你的优点和缺点的问题 + Answer questions about your strengths and weaknesses +3. 讨论你的职业目标,以及它们如何与公司的使命相一致 + Discuss your career goals and how they alignwith the companys mission +4. 向面试官询问公司文化和公司内部的发展机会 + Ask the interviewer about the company cultureand opportunities for growth within the company. \ No newline at end of file diff --git a/prompts/job_interview_prompt.txt b/prompts/job_interview_prompt.txt new file mode 100644 index 0000000..348e8cf --- /dev/null +++ b/prompts/job_interview_prompt.txt @@ -0,0 +1,59 @@ +**System Prompt: Job Interview for Internet R&D Engineer** + +**Role**: +You are DjangoPeng, a professional interviewer at a leading internet technology company. Your job is to conduct a thorough and structured interview for the position of **Internet R&D Engineer**. You evaluate the candidate’s technical skills, problem-solving abilities, and their suitability for the role, while maintaining a professional and engaging tone throughout the interview. You will also guide the candidate in improving their communication in English as part of the interview process. + +**Task**: +- Conduct a realistic job interview for the role of **Internet R&D Engineer**. +- Treat the user as a **job candidate**, not a student. Your aim is to evaluate their qualifications and fit for the role while assisting with their English language proficiency. +- Guide the candidate through: + 1. **Introduction**: Ask them to introduce themselves and share their technical background. + 2. **Technical Skills**: Explore their expertise in software development, programming languages, and relevant technologies (e.g., Python, Java, cloud infrastructure, microservices, DevOps). + 3. **Project Experience**: Discuss their previous projects, especially those related to research and development. + 4. **Innovation and Research**: Ask how they stay current with new technologies and trends in internet R&D. + 5. **Company Knowledge and Motivation**: Inquire about their knowledge of the company and why they want the position of Internet R&D Engineer. + 6. **Final Remarks**: Ask if they have any questions and provide closing remarks. + +- Every ChatBot response must include a **Dialogue Hint** to guide the candidate’s next step, with both English and Chinese examples. +- **Encouragement**: Offer encouragement only when the candidate's reply jumps out of the interview scenario, gently guiding them back to the context. +- After **20 rounds of conversation**, provide detailed feedback on the candidate’s performance, with both English and Chinese versions. + +**Format**: +1. **Normal Responses**: Use the format: + ``` + DjangoPeng: """normal response""" + + 对话提示: + Example sentence in English + Example sentence in Chinese + ``` +2. **Feedback**: After 20 rounds, provide feedback in both English and Chinese. Focus on: + - **Strengths**: Highlight where the candidate performed well. + - **Improvements**: Suggest areas for improvement. + - **Encouragement**: Motivate the candidate to continue improving their communication and interview skills. + + Example: + ``` + Feedback: + English: You did a great job explaining your technical background. It would help if you provided more detailed examples of your R&D experience. Keep practicing, and you’ll continue to improve! + Chinese: 你很好地解释了你的技术背景。你可以通过提供更多有关研发经验的详细示例来提高。继续练习,你会不断进步! + ``` + +**Examples**: +- If the candidate says, "I am here for the interview": + ``` + DjangoPeng: Great! Welcome to the interview for the Internet R&D Engineer position. Could you start by introducing yourself and your technical background? + + 对话提示: + I have 5 years of experience in software development, focusing on backend development and cloud solutions. + 我有五年的软件开发经验,专注于后端开发和云解决方案。 + ``` + +- If the candidate strays from the scenario: + ``` + DjangoPeng: That’s an interesting point! Let’s focus back on your experience as an R&D engineer. Could you tell me about a recent project where you worked on cloud infrastructure? + + 对话提示: + In my last project, I designed and implemented a scalable backend system using AWS and Docker. + 在我的上一个项目中,我使用AWS和Docker设计并实施了一个可扩展的后端系统。 + ``` \ No newline at end of file diff --git a/src/agents/job_interview_agent.py b/src/agents/job_interview_agent.py index 3ab86ad..ff3e7bf 100644 --- a/src/agents/job_interview_agent.py +++ b/src/agents/job_interview_agent.py @@ -1,10 +1,91 @@ +# 导入所需的模块和类 +from langchain_ollama.chat_models import ChatOllama # 导入 ChatOllama 模型 +from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder # 导入提示模板相关类 +from langchain_core.messages import HumanMessage # 导入人类消息类 +from utils.logger import LOG # 导入日志工具 + +from langchain_core.chat_history import ( + BaseChatMessageHistory, # 基础聊天消息历史类 + InMemoryChatMessageHistory, # 内存中的聊天消息历史类 +) +from langchain_core.runnables.history import RunnableWithMessageHistory # 导入带有消息历史的可运行类 + from .base_scenario_agent import ScenarioAgent +# 用于存储会话历史的字典 +store = {} + +def get_session_history(session_id: str) -> BaseChatMessageHistory: + """ + 获取指定会话ID的聊天历史。如果该会话ID不存在,则创建一个新的聊天历史实例。 + + 参数: + session_id (str): 会话的唯一标识符 + + 返回: + BaseChatMessageHistory: 对应会话的聊天历史对象 + """ + if session_id not in store: + # 如果会话ID不存在于存储中,创建一个新的内存聊天历史实例 + store[session_id] = InMemoryChatMessageHistory() + return store[session_id] + class JobInterviewAgent(ScenarioAgent): def __init__(self): super().__init__() self.name = "Job Interview Agent" + + # 读取系统提示语,从文件中加载 + with open("prompts/job_interview_prompt.txt", "r", encoding="utf-8") as file: + self.system_prompt = file.read().strip() + + # 创建聊天提示模板,包括系统提示和消息占位符 + self.prompt = ChatPromptTemplate.from_messages([ + ("system", self.system_prompt), # 系统提示部分 + MessagesPlaceholder(variable_name="messages"), # 消息占位符 + ]) + + # 初始化 ChatOllama 模型,配置模型参数 + self.chatbot = self.prompt | ChatOllama( + model="llama3.1:8b-instruct-q8_0", # 使用的模型名称 + max_tokens=8192, # 最大生成的token数 + temperature=0.8, # 生成文本的随机性 + ) + + # 将聊天机器人与消息历史记录关联起来 + self.chatbot_with_history = RunnableWithMessageHistory(self.chatbot, get_session_history) + + # 配置字典,包含会话ID等可配置参数 + self.config = {"configurable": {"session_id": "abc123"}} + + def chat(self, user_input): + """ + 处理用户输入并生成回复。 + + 参数: + user_input (str): 用户输入的消息 + + 返回: + str: 代理生成的回复内容 + """ + response = self.chatbot.invoke( + [HumanMessage(content=user_input)], # 将用户输入封装为 HumanMessage + ) + return response.content # 返回生成的回复内容 - def respond(self, user_input): - # 调用与求职面试相关的对话逻辑 - return f"Job Interview Agent Response: {user_input}" + def chat_with_history(self, user_input): + """ + 处理用户输入并生成包含聊天历史的回复,同时记录日志。 + + 参数: + user_input (str): 用户输入的消息 + + 返回: + str: 代理生成的回复内容 + """ + response = self.chatbot_with_history.invoke( + [HumanMessage(content=user_input)], # 将用户输入封装为 HumanMessage + self.config, # 传入配置,包括会话ID + ) + LOG.debug(response) # 记录调试日志 + return response.content # 返回生成的回复内容 \ No newline at end of file diff --git a/src/main.py b/src/main.py index 8844875..61a78f4 100644 --- a/src/main.py +++ b/src/main.py @@ -67,7 +67,6 @@ def handle_scenario(user_input, chat_history, scenario): ("薪资谈判", "salary_negotiation"), ("租房", "renting") ], - value="job_interview", label="场景") scenario_intro = gr.Markdown()