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PENAI_API_KEY = "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"
|
| 7 |
+
client = OpenAI(api_key=OPENAI_API_KEY)
|
| 8 |
+
OPEN_AI_MODEL = "gpt-4-1106-preview"
|
| 9 |
+
|
| 10 |
+
thread = client.beta.threads.create()
|
| 11 |
+
thread_id = thread.id
|
| 12 |
+
|
| 13 |
+
def wait_on_run(run, thread):
|
| 14 |
+
while run.status == "queued" or run.status == "in_progress":
|
| 15 |
+
run = client.beta.threads.runs.retrieve(
|
| 16 |
+
thread_id=thread.id,
|
| 17 |
+
run_id=run.id,
|
| 18 |
+
)
|
| 19 |
+
time.sleep(0.5)
|
| 20 |
+
return run
|
| 21 |
+
|
| 22 |
+
def get_openai_assistant(assistant_name, instructions, model=OPEN_AI_MODEL):
|
| 23 |
+
assistant = client.beta.assistants.create(
|
| 24 |
+
name=assistant_name,
|
| 25 |
+
instructions=instructions,
|
| 26 |
+
model=model,
|
| 27 |
+
)
|
| 28 |
+
return assistant.id
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def show_json(obj):
|
| 32 |
+
json.loads(obj.model_dump_json())
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def abstract_assistant(pre_context_to_the_instruction, instruction, name, thread_id, thread, query):
|
| 36 |
+
assistant_id = get_openai_assistant(name, pre_context_to_the_instruction + instruction)
|
| 37 |
+
message = client.beta.threads.messages.create(
|
| 38 |
+
thread_id=thread_id, role="user", content=query)
|
| 39 |
+
run = client.beta.threads.runs.create(
|
| 40 |
+
thread_id=thread_id,
|
| 41 |
+
assistant_id=assistant_id,
|
| 42 |
+
)
|
| 43 |
+
wait_on_run(run, thread)
|
| 44 |
+
messages = client.beta.threads.messages.list(
|
| 45 |
+
thread_id=thread_id, order="asc", after=message.id
|
| 46 |
+
)
|
| 47 |
+
data = json.loads(messages.model_dump_json())
|
| 48 |
+
response = data['data'][0]['content'][0]['text']['value']
|
| 49 |
+
|
| 50 |
+
return response
|
| 51 |
+
|
| 52 |
+
def get_response_for_case_t0(thread_id, query, question_text, input, output, example):
|
| 53 |
+
|
| 54 |
+
pre_context_to_the_instruction = f"""```QUESTION:{question_text}```\n```INPUT:{input}```\n```OUTPUT:{output}```\n```EXAMPLE:{example}```"""
|
| 55 |
+
instruction = f"""Act as a Coding Mentor for a student who is currently learning Data Structures and algorithms(DSA). The student is asking a QUERY: {query} based on the
|
| 56 |
+
coding question : QUESTION which have input : INPUT output: OUTPUT and examples: EXAMPLE. The student have not implemented any code. The student might be asking
|
| 57 |
+
how to solve the question or what should be the coding approach or how to write the code/logic. Your task is to :
|
| 58 |
+
1.) Ask the student about what he/she is thinking about the problem statement or what logic the student will implement or what approach he/she will follow
|
| 59 |
+
to code the solution he/she is thinking.
|
| 60 |
+
2.) Please be in a motivational tone and never give student a solution approach just ask the students approach.
|
| 61 |
+
3.) Always answer in short and crunch way not more than 200 words. Always be to the point."""
|
| 62 |
+
|
| 63 |
+
response = abstract_assistant(pre_context_to_the_instruction,instruction, "GPT_t0", thread_id, thread, query)
|
| 64 |
+
|
| 65 |
+
return response
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def opening_statement(thread_id, question_text, input, output, example):
|
| 69 |
+
pre_context_to_the_instruction = f"""```QUESTION:{question_text}```\n```INPUT:{input}```\n```OUTPUT:{output}```\n```EXAMPLE:{example}```"""
|
| 70 |
+
instruction = f"""Act as a Coding Mentor for a student who is currently learning Data Structures and algorithms(DSA). Now the student
|
| 71 |
+
is stuck in the question for long amount of time so Ask him in a gentle motivational tone to tell or to start think where
|
| 72 |
+
he/she might be stuck or might be thinking. Output only one line not more than that be short and crunch!"""
|
| 73 |
+
|
| 74 |
+
query = ""
|
| 75 |
+
|
| 76 |
+
response = abstract_assistant(pre_context_to_the_instruction,instruction, "GPT_open", thread_id, thread, query)
|
| 77 |
+
|
| 78 |
+
return response
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def response_evaluation_for_case_tx(thread_id, query, question_text, input, output, example, user_code):
|
| 82 |
+
pre_context_to_the_instruction = f"""```QUESTION:{question_text}```\n```INPUT:{input}```\n```OUTPUT:{output}```\n```EXAMPLE:{example}```"""
|
| 83 |
+
instruction = f"""Act as a Coding Mentor for a student who is currently learning Data Structures and algorithms(DSA). The student is asking a QUERY: {query} based on the
|
| 84 |
+
coding question : QUESTION which have input : INPUT output: OUTPUT and examples: EXAMPLE. Now follow the following instrutions:
|
| 85 |
+
NEVER PROVIDE COMPLETE SOLUTION CODE AND ALL THE STEPS TO SOLVE IN ONE RESPONSE. BE SELECTIVE AND GIVE IMMEDIATE STEP ONLY ONE
|
| 86 |
+
* Always answer in short and crunch way not more than 200 words. Always be to the point
|
| 87 |
+
1.) Understand what user is thinking about to code.
|
| 88 |
+
2.) Take time to think and understand
|
| 89 |
+
3.) Analyse the Code written by the user : {user_code}
|
| 90 |
+
4.) Again take time to think and understand
|
| 91 |
+
5.) Now check if the explaination of the user is aligning with the code or not
|
| 92 |
+
6.) If not then suggest the user to align, by providing unblocking hints only!
|
| 93 |
+
7.) Never give complete solution logic or code to the student , never ! Always talk with the student let the student write code with small hints only and reach to a solution
|
| 94 |
+
8.) If user is doing wrong approach suggest correct approach slowly with step by st\ep hints only!
|
| 95 |
+
9.) At the end also suggest user about how to improve code and logic at the end"""
|
| 96 |
+
|
| 97 |
+
response = abstract_assistant(pre_context_to_the_instruction,instruction, "GPT_tx", thread_id, thread, query)
|
| 98 |
+
|
| 99 |
+
return response
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
def run_chat_in_all_cases(message, history, question_text,input, output, examples, code_written, thread_id=thread.id):
|
| 103 |
+
|
| 104 |
+
if not message and not code_written:
|
| 105 |
+
ai_message = opening_statement(thread_id, question_text, input, output, examples)
|
| 106 |
+
if not code_written:
|
| 107 |
+
ai_message = get_response_for_case_t0(thread_id, message, question_text, input, output, examples)
|
| 108 |
+
else:
|
| 109 |
+
ai_message = response_evaluation_for_case_tx(thread_id, message, question_text, input, output, examples, code_written)
|
| 110 |
+
|
| 111 |
+
return ai_message
|
| 112 |
+
|
| 113 |
+
additional_inputs=[
|
| 114 |
+
gr.Textbox(
|
| 115 |
+
label="Question Text",
|
| 116 |
+
max_lines=10,
|
| 117 |
+
interactive=True,
|
| 118 |
+
),
|
| 119 |
+
gr.Textbox(
|
| 120 |
+
label="Input",
|
| 121 |
+
max_lines=10,
|
| 122 |
+
interactive=True,
|
| 123 |
+
),
|
| 124 |
+
gr.Textbox(
|
| 125 |
+
label="Output",
|
| 126 |
+
max_lines=10,
|
| 127 |
+
interactive=True,
|
| 128 |
+
),
|
| 129 |
+
gr.Textbox(
|
| 130 |
+
label="Examples",
|
| 131 |
+
max_lines=10,
|
| 132 |
+
interactive=True,
|
| 133 |
+
),
|
| 134 |
+
gr.Code(
|
| 135 |
+
label="Code",
|
| 136 |
+
interactive=True,
|
| 137 |
+
)
|
| 138 |
+
]
|
| 139 |
+
|
| 140 |
+
gr.ChatInterface(
|
| 141 |
+
fn=run_chat_in_all_cases,
|
| 142 |
+
chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
|
| 143 |
+
additional_inputs=additional_inputs,
|
| 144 |
+
title="Mentor Mode",
|
| 145 |
+
concurrency_limit=20,
|
| 146 |
+
).launch(show_api=False)
|