from openai import OpenAI import gradio as gr import os chat_history=[] #set system prompt #messages = [ # {"role":"system", "content": "This Chatbot is a helpful and accurate assistant that will reference the MIL_Travel_Guidelines.docx, Contractor_Travel_Policy.docx, and CFR-2023-title41-vol4-subtitleF.pdf accessible in storage to respond to user questions. All questions from the user will be asked against each one of these documents independently and a response will be provided for each. If no relevant information exists in a document, you will not make anything up, but state that no applicable inforation exists in that specific document. All answers must address all 3 documents instorage. You must ensure each document is accurately cited for references. Citations should include document, page numbers, and sections cited where applicable. For example, If a document does not have sections, only cite page number and document name. This Chatbot will not answer questions not associated with the MIL_Travel_Guidelines.docx, Contractor_Travel_Policy.docx, and CFR-2023-title41-vol4-subtitleF.pdf in storage. This chatbot will assume all questions are from a Federal Contractor point of view."}, #] client = OpenAI( api_key=os.getenv('APIKEY'), organization=os.getenv('OPENAIORG'), ) assistant_id=os.getenv('ASSISTANTID1') thread = client.beta.threads.create() def validate_password(user_password): correct_password = os.getenv('MILPASSWORD') if user_password != correct_password: return False, "Incorrect password. Access denied." return True, "" #generates the ChatGPT call def generate_response(prompt, model_type): #messages.append({"role": "user", "content": prompt}) #response = client.chat.completions.create( # model=model_type, # messages=messages, # temperature=0.1 #) prompt=prompt+' End each section with a sentence that informs the user what sections from the document the answers were pulled from.' messages = client.beta.threads.messages.create( thread_id=thread.id, role="user", content=prompt ) #run = client.beta.threads.runs.create run = client.beta.threads.runs.create_and_poll( thread_id=thread.id, assistant_id=assistant_id, #instructions="Please address the user as Jane Doe. The user has a premium account." ) if run.status == 'completed': messages = client.beta.threads.messages.list( thread_id=thread.id ) message = client.beta.threads.messages.list( thread_id=thread.id ) response_text=message.data[0].content[0].text.value #response_text=message.content[0].text #messages.append({"role": "assistant", "content": response_text}) print(messages) print(prompt) #print("below is history") #print(chat_history) return (response_text) def my_chatbot(input, history, user_password): valid, message = validate_password(user_password) if not valid: return [(input, message)], [] model_type='gpt-4o-mini' history = history or [] output = generate_response(input, model_type) history.append((input, output)) return history, history #else: #history.clear() #output = generate_restart(input, model_type) #history.append((input, output)) #prompt=input #return prompt, prompt with gr.Blocks() as demo: gr.Markdown("""

MIL Custom Travel Chatbot

""") gr.Image(value="logo.PNG", width=200, height=150, interactive=False, show_share_button=False) MIL_Password = gr.Textbox(type='password', label="Enter the Password")#, width=250) #with gr.Row(equal_height=True): #create_index = gr.Radio(["Yes", "No"], label = "index creation", info="Would you like to create a new index?", value="No") #model_type = gr.Radio(["gpt-3.5-turbo", "gpt-4"], label = "Model_Type", info="Would you like to create a new index?", value="gpt-3.5-turbo") #save_index = gr.Radio(["Yes", "No"], label = "Save Index", info="Would you like to save the index for future use?", value="No") #output = gr.Textbox( # label="Output", # info="", # lines=1 # ) chatbot = gr.Chatbot() state = gr.State() text = gr.Textbox(label="Input", info="", lines=2, placeholder="Hello. Ask me a question about MIL Travel.") submit = gr.Button("SEND") submit.click(my_chatbot, inputs=[text, state, MIL_Password], outputs=[chatbot, state]) demo.launch(share = False)