jarolsb's picture
Upload 8 files
9c73114
Raw History Blame Contribute Delete
17.1 kB
# Experiment to create a student dialog initiatied and led by an AI agent (GPT).
# Convert to objects: prompt_transcript, user_dialog_transcript, AI_agent
import os
import gradio as gr
import openai as gpt
gpt.api_key = os.environ.get('OPENAI_API_KEY')
system_role = "high school english teacher"
subject = "chapter 1 of William Golding's Lord of the Flies"
follow_up_subject = "chapter 1 of Lord of the Flies"
AI_entity = "Learning Assistant"
human_entity = "Student"
#SCRIPT PROTOTYPE
# The dialog script should be loaded from the contents of a file, such as csv format.
# The following embedded script is an example.
script = []
script_item = []
file_index = 1
with open('dialog script.csv', 'r') as f:
for imported_script_line in f:
script_item = []
script_item_index = 0
script_item_from_file = imported_script_line.split(",")
for line_item in script_item_from_file:
script_item.append(line_item.strip())
script_item_index += 1
script.append(script_item)
print(script_item)
print("\n\nFinal Script: ")
print(script)
'''
script = [
[0, "prompt", "role_definition", "You are a high school english teacher."],
[1, "prompt", "subject_definition", "William Golding's 'Lord of the Flies', Chapter 1"],
[2, "prompt", "evaluation_criteria", "The user response accurately reflects the themes and actions depicted in the novel, and expresses them clearly, concisely, and with good english grammar and spelling."],
[3, "prompt", "question_specific", "Where are the boys and how did they get there?"],
[4, "prompt", "answer_feedback"],
[5, "if", "answer_sufficient", 8],
[6, "prompt", "question_followup", "Do they feel fearful or excited about their situation?"],
[7, "prompt", "answer_feedback"],
[8, "prompt", "question_topical", "Ask the user a question about actions the boys take once they understand their situation."],
[9, "prompt", "answer_feedback"],
[10, "prompt", "final_feedback", "Provide the user with an evaluation of their overall performance against the evaluation criteria."],
[11, "prompt", "final_evaluation", "Provide the " + AI_entity + " with an assessment of the user's performance with grading recommendations."],
[12, "prompt", "completion", "Thank the user for their participation and end discussion."]
]
'''
def submitPrompt(prompt):
completion = gpt.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=prompt
)
AI_message = completion["choices"][0]["message"]["content"] + "\n\n"
return(AI_message)
# print(os.environ.get('OPENAI_API_KEY'))
# with gr.Blocks(theme=gr.themes.Soft()) as demo:
with gr.Blocks() as demo:
def discuss(user_input, chat_transcript, step_pos, internal_dialog_text, dialog):
# dialog = dialog or []
# chat_transcript = chat_transcript or ""
# step_pos = step_pos or 0
while step_pos < len(script):
print(step_pos)
if user_input != "":
print("User Input: " + user_input + "\n")
# print("DIALOG BEFORE APPEND: ")
# print(dialog)
dialog.append({"role": "user", "content": user_input})
# print("DIALOG: ")
# print(dialog)
internal_dialog_text += user_input + "\n"
chat_transcript += human_entity + ": " + user_input + "\n\n"
# print("CHAT: " + chat_transcript)
AI_response = submitPrompt(dialog)
user_input = ""
action = script[step_pos]
step_pos += 1
print("NEXT ACTION: " + action[2] + "\n")
match action[1]:
case 'prompt':
match action[2]:
case 'role_definition':
dialog.append({"role": "system", "content": action[3]})
internal_dialog_text += "Role Definition: " + action[3] + "\n\n"
AI_response = submitPrompt(dialog)
dialog.append({"role": "assistant", "content": AI_response})
internal_dialog_text += "Role Response: " + AI_response + "\n"
case 'subject_definition':
dialog.append({"role": "system", "content": "Subject: " + action[3]})
internal_dialog_text += "Subject Definition: " + action[3] + "\n\n"
AI_response = submitPrompt(dialog)
dialog.append({"role": "assistant", "content": AI_response})
internal_dialog_text += "Subject Definition Response: " +AI_response + "\n"
case 'get_external_prompt_text':
# Open a text file, such as a video transcript, and read it into the prompt.
# The prompt should be a single string.
# The following code is an example.
prompt_transcript = "" # This should be the transcript of the video.
prompt_text = ""
with open('prompt transcript.txt', 'r') as f:
for line in f:
prompt_transcript += line
prompt_text = prompt_transcript
dialog.append({"role": "system", "content": prompt_text})
internal_dialog_text += "Prompt Text: " + prompt_text + "\n\n"
AI_response = submitPrompt(dialog)
dialog.append({"role": "assistant", "content": AI_response})
internal_dialog_text += "Prompt Text Response: " + AI_response + "\n"
case 'evaluation_criteria':
dialog.append({"role": "system", "content": "Evaluation Criteria: " + action[3]})
internal_dialog_text += "Evaluation Criteria Definition: " + action[3] + "\n\n"
AI_response = submitPrompt(dialog)
dialog.append({"role": "assistant", "content": AI_response})
internal_dialog_text += "Evaluation Criteria Definition Response: " + AI_response + ""
# with open('AI Dialog Transcript.txt', 'w') as f:
# f.write(internal_dialog_text)
# f.close()
case 'question_specific':
dialog.append({"role": "assistant", "content": "Ask user the question: " + action[3]})
internal_dialog_text += "Ask the user the question: " + action[3] + "\n\n"
AI_response = submitPrompt(dialog)
# dialog.append({"role": "assistant", "content": AI_response})
chat_transcript += (AI_entity + ": " + action[3] + "\n\n")
internal_dialog_text += AI_entity + ": " + AI_response + "\n"
break
case 'answer_feedback':
# Add process to check for and AI-generated answer.
prompt_text = "Answer yes or no to the whether the user's answer is likely to be AI generated?"
internal_dialog_text += ("AI Detected Prompt: " + prompt_text + "\n\n")
dialog.append({"role": "assistant", "content": prompt_text})
AI_response = submitPrompt(dialog)
# print("AI ANSWER DETECTED? " + AI_response)
internal_dialog_text += "AI Detected Response: " + AI_response + "\n"
if "yes" in AI_response.lower():
print("AI ANSWER DETECTED\n")
prompt_text = "Say to the user: Please answer in your own words."
internal_dialog_text += AI_entity + ": " + prompt_text + "\n\n"
dialog.append({"role": "assistant", "content": prompt_text})
AI_response = submitPrompt(dialog)
print("AI_RESPONSE FROM REQUEST TO ANSWER IN OWN WORDS: " + AI_response)
chat_transcript += (AI_entity + ": " + AI_response + "\n\n")
internal_dialog_text += (AI_entity + ": " + AI_response + "\n")
step_pos -= 1
break
else:
print("ANSWER_FEEDBACK\n")
dialog.append({"role": "assistant", "content": "Provide feedback as if speaking to a high school student on the quality of the most recent response based on the subject and evaluation criteria."})
internal_dialog_text += "Answer Feedback Definition: " + "Provide feedback as if speaking to a high school student on the quality of the most recent response based on the subject and evaluation criteria.\n\n"
AI_response = submitPrompt(dialog)
# print("AI_response in Feedback: " + AI_response + "\n")
dialog.append({"role": "assistant", "content": AI_response})
chat_transcript += (AI_entity + ": " + AI_response + "\n")
internal_dialog_text += ("learning Assistant: " + AI_response + "\n")
case 'question_followup':
print("ASKING FOLLOWUP QUESTION\n")
prompt_text = "Ask the user a relevant followup question \
based on the previous question, answer, subject definition, and evaluation criteria."
dialog.append({"role": "assistant", "content": prompt_text})
internal_dialog_text += "Followup Question Guidance: " + prompt_text + "\n\n"
AI_response = submitPrompt(dialog)
print("Followup Question: " + AI_response + "\n")
dialog.append({"role": "assistant", "content": AI_response})
chat_transcript += (AI_entity + ": " + AI_response + "\n")
internal_dialog_text += ("learning Assistant: " + AI_response + "\n")
break
case 'question_topical':
print("QUESTION_TOPICAL\n")
dialog.append({"role": "assistant", "content": action[3]})
internal_dialog_text += "Topical Question Definition: " + action[3] + "\n\n"
AI_response = submitPrompt(dialog)
# dialog.append({"role": "assistant", "content": AI_response})
print("CHAT BEFORE: " + chat_transcript + "\n\n")
chat_transcript += (AI_entity + ": " + AI_response + "\n")
internal_dialog_text += ("Learning Assistant: " + AI_response + "\n")
break
case 'final_feedback':
# Do stuff here
dialog.append({"role": "assistant", "content": action[3]})
internal_dialog_text += "Final Feedback Instructions: " + action[3] + "\n\n"
AI_response = submitPrompt(dialog)
dialog.append({"role": "assistant", "content": AI_response})
print("CHAT BEFORE: " + chat_transcript + "\n\n")
chat_transcript += (AI_entity + ": " + AI_response + "\n")
internal_dialog_text += ("Learning Assistant: " + AI_response + "\n")
case 'final_evaluation':
# Do stuff here
dialog.append({"role": "assistant", "content": action[3]})
internal_dialog_text += "Final Evaluation Instructions: " + action[3] + "\n\n"
AI_response = submitPrompt(dialog)
dialog.append({"role": "assistant", "content": AI_response})
print("CHAT BEFORE: " + chat_transcript + "\n\n")
# chat_transcript += (AI_entity + ": " + AI_response + "\n")
internal_dialog_text += ("Learning Assistant: " + AI_response + "\n")
case 'completion':
chat_transcript += "Thank you for participating. You may close the browser.\n"
with open('AI Dialog Transcript.txt', 'w') as f:
f.write(internal_dialog_text)
f.close()
# break should be unnecessary.
case 'if':
match action[2]:
case 'answer_sufficient':
dialog.append({"role": "assistant", "content": "State as yes or no whether the last answer provided by the user demonstrates a strong understanding of the previous question."})
internal_dialog_text += "Answer Evaluation Instructions: " + "State as yes or no whether the last answer provided by the user demonstrates a strong understanding of the previous question.\n\n"
AI_response = submitPrompt(dialog)
print("IF EVALUATION: " + AI_response)
dialog.append({"role": "assistant", "content": AI_response})
# chat_transcript += (AI_entity + ": " + AI_response + "\n\n")
internal_dialog_text += ("learning Assistant: " + AI_response + "\n")
if "yes" in AI_response.lower():
step_pos = int(action[3])
# print("POSITION UPDATED: " + str(step_pos))
# print("\n" + script[step_pos][2])
# print("Internal Text: " + internal_dialog_text)
# print(chat_transcript + "\n")
# print(dialog)
return "", chat_transcript, chat_transcript, dialog, internal_dialog_text, step_pos
# Start processing the script before establishing the session state. This may be
# susceptible to concurrency collisions, though, so it would be better to figure out
# how to always use Gradio session state variables.
initial_chat_recording = ""
initial_prompt_history = []
initial_step_num = 0
initial_internal_transcript = ""
placeholder_str = ""
placeholder_str, initial_chat_recording, initial_chat_recording, initial_prompt_history, initial_internal_transcript, initial_step_num = discuss("", initial_chat_recording, 0, initial_internal_transcript, initial_prompt_history)
print("INITIAL TRANSCRIPT: " + initial_internal_transcript + "\n")
prompt_history = gr.State(initial_prompt_history)
chat_recording = gr.State(initial_chat_recording)
internal_transcript = gr.State(initial_internal_transcript)
step_num = gr.State(initial_step_num)
# gr.Image(shape=(200, 200)
with gr.Row():
with gr.Column(scale=1, min_width=200):
# gr.Image("https://svhs.co/wp-content/uploads/2018/04/SVHS.png", shape=(200, 50))
logomark = gr.Image("SVHSLogo.png", shape=(200, 50))
logomark.label = ""
with gr.Column(scale=4, min_width=400):
title = gr.Label("Welcome to the SVHS Learning Assistant.")
title.css = "font-size: 20px;"
title.label = ""
# Empty space
with gr.Row():
with gr.Column(scale=1, min_width=300):
output = gr.Textbox(label="Dialog", value=chat_recording.value)
student_answer = gr.Textbox(label="Answer")
student_answer_btn = gr.Button("Answer")
with gr.Column(scale=1, min_width=300):
running_transcript = gr.Textbox(label="AI Transcript (Hidden From Student)", value=internal_transcript.value)
student_answer_btn.click(fn=discuss, inputs=[student_answer, chat_recording, step_num, running_transcript, prompt_history], outputs= [student_answer, output, chat_recording, prompt_history, running_transcript, step_num])
# chat_recording, chat_recording, prompt_history, step_num = discuss("", chat_recording, 0, prompt_history)
# examples = gr.Examples(examples=[["", chat_recording, 0, prompt_history]], inputs=[student_answer, chat_recording, step_num, prompt_history])
demo.launch(share=False)