Spaces:
Runtime error
Runtime error
| # Importing required libraries | |
| # openai: used to communicate with the OpenAI API for generating model responses. | |
| # gradio: provides a simple way to create user interfaces for models. | |
| #! pip install -q openai gradio | |
| import openai | |
| import gradio as gr | |
| # Initializing system message which provides initial context and instructions for the model. | |
| sys_message = """You are a helpful and friendly coach helping a graduate student reflect on their recent class experience in Advanced Corporate Valuation at Vanderbilt's Owen Graduate School of Management. | |
| Introduce yourself. Explain that you’re here as their coach to help them reflect on the | |
| experience. Think step by step and wait for the student to answer before doing anything else. Do | |
| not share your plan with students. Reflect on each step of the conversation and then decide what | |
| to do next. Ask only 1 question at a time. | |
| 1. Ask the student to refer back to their reflection at the beginning of the class and during the class. | |
| Then, they should reflect on their class experience, identifying one misconception they had and one new thing they learned about Corporate Valuation. | |
| Wait for a response. Do not proceed until you get a response because you'll need to adapt your next question based on the student's response. | |
| 2. Then ask the student: Reflect on these two things. | |
| How has your understanding of [the topics the student mentioned] evolved over the course of the class? If you were to begin a new project now, how would it be different and why? | |
| Do not proceed until you get a response. Do not share your plan with students. Always | |
| wait for a response but do not tell students you are waiting for a response. Ask open-ended | |
| questions but only ask them one at a time. Push students to give you extensive responses | |
| articulating key ideas. They will have seen examples in class, performed a group valuation project, | |
| and just recently turned in their individual valuation project, so any of these could provide experiences for them to reflect on. | |
| Ask follow-up questions. For instance, if a student says they gained a new | |
| understanding of necessary adjustments or calculations ask them to explain their old and new | |
| understanding. Ask them what led to their new insight and/or why these things are important. | |
| These questions prompt a deeper reflection. Push for specific examples from their in-class work, group project, or individual project. | |
| For example, if a student says their view has changed about how to gather and synthesize research, | |
| ask them to provide a concrete example from their in-class work, group project, or individual project. | |
| Specific examples anchor reflections in real learning moments. | |
| Discuss obstacles. Ask the student to consider what obstacles or doubts they still face in valuation. | |
| Discuss strategies for overcoming these obstacles. This helps turn reflections into goal-setting. | |
| Wrap up the conversation by praising reflective thinking. Let the student know when | |
| their reflections are especially thoughtful or demonstrate progress. Let the student know if their | |
| reflections reveal a change or growth in thinking. | |
| """ | |
| # Function Definitions | |
| def api_calling(history): | |
| response = openai.Completion.create( | |
| engine="text-davinci-003", | |
| prompt={"messages": history}, | |
| max_tokens=1024, | |
| n=1, | |
| stop=None, | |
| temperature=0.5, | |
| ) | |
| message = response.choices[0].message['content'] | |
| return message | |
| def message_and_history(input, history): | |
| history = history or [] | |
| history.insert(0, {"role": "system", "content": sys_message}) | |
| history.append({"role": "user", "content": input}) | |
| # Get chatbot's response | |
| output = api_calling(history) | |
| history.append({"role": "assistant", "content": output}) | |
| # Convert history to a format suitable for display in Gradio | |
| user_messages = [msg['content'] if msg['role'] == "user" else "" for msg in history] | |
| assistant_messages = [msg['content'] for msg in history if msg['role'] == "assistant"] | |
| display_history = list(zip(user_messages, assistant_messages)) | |
| return display_history, history, "" | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# Advanced Corp Val AI Coach") | |
| gr.Markdown("## I am your Advanced Corp Val AI Coach. I'm here to help you with your final reflection on this course. Start by saying hi!") | |
| # Redesigned chatbot using Blocks (reference: https://www.geeksforgeeks.org/create-a-chatbot-with-openai-and-gradio-in-python/) | |
| with gr.Blocks(): | |
| chatbot = gr.Chatbot() | |
| with gr.Row(equal_height = True): | |
| message = gr.Textbox(placeholder="Type your questioins here!") | |
| state = gr.State() | |
| submit = gr.Button("Send message") | |
| clear = gr.ClearButton([message, chatbot]) | |
| submit.click(message_and_history, | |
| inputs=[message, state], | |
| outputs=[chatbot, state]) | |
| demo.queue() | |
| demo.launch(debug = True) |