-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FIX] Fixed code because of bad openai version
- Loading branch information
1 parent
5d75cd3
commit 34283b7
Showing
6 changed files
with
184 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,100 @@ | ||
from langchain.chat_models import ChatOpenAI | ||
from langchain.prompts import ChatPromptTemplate | ||
from langchain.chains import LLMChain | ||
import openai | ||
from sklearn.feature_extraction.text import TfidfVectorizer | ||
from sklearn.metrics.pairwise import cosine_similarity | ||
|
||
class AIInterviewer: | ||
def __init__(self, agent): | ||
self.agent_prompt = agent.behaviour.agent_prompt | ||
self.custom_knowledge = agent.knowledge.custom_knowledge | ||
self.model_name = agent.knowledge.agent_llm | ||
|
||
def generate_next_question(self, text, last_question, n_questions, current_score): | ||
prompt = f""" | ||
{self.agent_prompt} | ||
Custom Knowledge: {self.custom_knowledge} | ||
Last Question: {last_question} | ||
Human's Answer: {text} | ||
Number of Questions Asked: {n_questions} | ||
Current Score: {current_score} | ||
Based on the information above, generate the next interview question. The question should be relevant to the previous question and answer, and appropriate for the current stage of the interview (considering the number of questions asked and the current score). | ||
DO NOT INCLUDE ANY GREETING TO THE USER, be professional. | ||
Next Question: | ||
""" | ||
|
||
response = openai.chat.completions.create( | ||
model=self.model_name, | ||
messages=[ | ||
{"role": "system", "content": "You are a helpful assistant that generates interview questions."}, | ||
{"role": "user", "content": prompt} | ||
], | ||
max_tokens=100, | ||
temperature=0.6, | ||
) | ||
|
||
return response.choices[0].message.content.strip() | ||
|
||
|
||
def generate_ideal_answer(self, question, user_skills): | ||
prompt = f""" | ||
Custom Knowledge: {self.custom_knowledge} | ||
User Skills: {user_skills} | ||
Question: {question} | ||
Based on the custom knowledge and the user's skills, generate an ideal answer to the given question. The answer should be comprehensive and demonstrate the expected knowledge for someone with the specified skills. | ||
Ideal Answer: | ||
""" | ||
|
||
response = openai.chat.completions.create( | ||
model=self.model_name, | ||
messages=[ | ||
{"role": "system", "content": "You are a helpful assistant that generates ideal answers based on user skills."}, | ||
{"role": "user", "content": prompt} | ||
], | ||
max_tokens=200, | ||
temperature=0.5, | ||
) | ||
|
||
return response.choices[0].message.content.strip() | ||
|
||
|
||
@staticmethod | ||
def calculate_answer_similarity(ideal_answer, human_answer): | ||
vectorizer = TfidfVectorizer().fit_transform([ideal_answer, human_answer]) | ||
cosine_sim = cosine_similarity(vectorizer[0:1], vectorizer[1:2]) | ||
return cosine_sim[0][0] | ||
|
||
def ai_interviewer(text, session): | ||
interviewer = AIInterviewer(session.order.agent) | ||
|
||
ideal_answer = interviewer.generate_ideal_answer(session.last_question, session.applicant.skills) | ||
answer_similarity = AIInterviewer.calculate_answer_similarity(ideal_answer, text) | ||
|
||
agent = session.order.agent | ||
question_score = answer_similarity * 100 | ||
|
||
agent_greeting = agent['behaviour']['agent_greeting'] | ||
agent_prompt = agent['behaviour']['agent_prompt'] | ||
custom_knowledge = agent['knowledge']['custom_knowledge'] | ||
model_name = agent['knowledge']['agent_llm'] | ||
if session.n_questions == 0: | ||
session.score = question_score + 10 | ||
if session.score > 100: | ||
session.score = 100 | ||
else: | ||
session.score = (session.score * session.n_questions + question_score + 10) / (session.n_questions + 1) | ||
if session.score > 100: | ||
session.score = 100 | ||
|
||
# Create ChatOpenAI instance | ||
llm = ChatOpenAI(model_name=model_name, temperature=0.4) | ||
session.n_questions += 1 | ||
|
||
interview_template = ChatPromptTemplate.from_template( | ||
f"{agent_prompt}\n\n" | ||
f"Custom Knowledge: {custom_knowledge}\n\n" | ||
"Interview progress: {progress}\n" | ||
"User's previous message: {user_message}\n\n" | ||
"Based on the interview progress, provide the appropriate response or question. " | ||
"If this is the final response, include an evaluation of the candidate." | ||
) | ||
if session.n_questions >= 10 or (session.n_questions >= 5 and session.score < 50): | ||
session.final = 1 | ||
next_question = None | ||
else: | ||
next_question = interviewer.generate_next_question( | ||
text=text, | ||
last_question=session.last_question, | ||
n_questions=session.n_questions, | ||
current_score=session.score | ||
) | ||
|
||
session.last_answer = text | ||
session.last_question = next_question | ||
|
||
session.save() | ||
|
||
return "AI default ", 0, 1, "100%" | ||
return session.score, next_question |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.