CoolFace
Apppublic

dukemsec/coding-interviewer

sourceHugging Facemitupdated 3y agoView on Hugging Face
0likes
lib.py81 linesDownload Raw Back to root
1import openai2 3from key import APIKEY4 5 6openai.api_key = APIKEY7 8def generatePrompt(questionLink, userCode):9    10    preset = "You now act as an coding interviewer. You will be given a leetcode link and a code snippet. \11              You ned to analyze the code and ask questions to the candidate. \n"12 13    required = "You need to return following information as a dictionary: \14                {'time complexity': 'you add', \15                'space complexity': 'you add', \16                'why time complexity': 'you add', \17                'why space complexity': 'you add', \18                'follow-up questions' : 'you add', \19                'rate users code': 'you add'}, please make sure it's complete dictionary and what you add is string type. \n"20 21    prompt = preset + f"Leetcode Link: {questionLink} \n" + f"Code Snippet: {userCode} \n" + required22 23    return prompt24 25def generate_response(prompt, model="text-davinci-003", max_tokens=2000):26    response = openai.Completion.create(27        engine=model,28        prompt=prompt,29        max_tokens=max_tokens,30        n=1,31        stop=None,32        temperature=0.5,33    )34 35    message = response.choices[0].text.strip()36    return message37 38 39def identifyKeyInfo(phase, userInput, model="text-davinci-003", max_tokens=2000):40    prompt = f"You now act as an coding interviewer. Now you need substract the key information from the prompt. \41              For example, if your are in the phase of asking for leetcode link, you need to identify the link from the prompt and return the link string. \42              If you are in the phase of asking for code snippet, you need to identify the code snippet from the prompt and return the code snippet string. \43              If you are in the phase of asking for time and complexity, you need to identify the complexity formatted as O(N) from the prompt and return the complexity string. \44              If you think user input can not match current phase, pls return '400' as a string. Thank you.  \45              Now you are in phare of {phase}, please substract the key info from user input {userInput}!"46    47    response = openai.Completion.create(48        engine=model,49        prompt=prompt,50        max_tokens=max_tokens,51        n=1,52        stop=None,53        temperature=0.5,54    )55 56    message = response.choices[0].text.strip()57    return message58 59 60def GiveOverallScore(history, model="text-davinci-003", max_tokens=2000):61    prompt = f"You now act as an coding interviewer. Now you need to give overall score to the user. \62              You will be given the history of the conversation \n \63             '{history}'. Please analyze user's code for the leetcode problem (efficient, readibility), \64              user's answers for time complexity and space complexity, user's follow-up questions. \n \65              Your need to return a dictionary with following keys: \66              'overall score out of 100': 'you add', 'feedbacks': 'you add'. \67              Please use double quotes for the keys and values when you output the dictionary. \68              Or you need to aviod use quotes in your values strings. \69              Also, if your score is not generous, please include the improvement points for users. Thank you."70    71    response = openai.Completion.create(72        engine=model,73        prompt=prompt,74        max_tokens=max_tokens,75        n=1,76        stop=None,77        temperature=0.5,78    )79 80    message = response.choices[0].text.strip()81    return message