import os os.environ["OPENAI_API_KEY"] from llama_index import ( VectorStoreIndex, SummaryIndex, SimpleKeywordTableIndex, SimpleDirectoryReader, ServiceContext, StorageContext, load_index_from_storage ) from llama_index.schema import IndexNode from llama_index.tools import QueryEngineTool, ToolMetadata from llama_index.llms import OpenAI llm = OpenAI(temperature=0, model="gpt-3.5-turbo") service_context = ServiceContext.from_defaults(llm=llm) PERSIST_DIR = "arv_metadata" storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR) index = load_index_from_storage(storage_context) query_engine = index.as_query_engine(similarity_top_k=3, llm=OpenAI(model="gpt-3.5-turbo")) preamble = (" The person asking the following prompt is a person living with HIV in Kenya." " For every response, recognize that they already have HIV and do not suggest that they have to get tested" " for HIV or take post-exposure prophylaxis, as that is not relevant, though their partners perhaps should." " Do not suggest anything that is not relevant to someone who already has HIV." " They are asking questions through a mobile application called Nishauri" " through which they can see their lab results, appointment histories, and upcoming appointments." " Here is some information that is authoritative and should guide responses, when relevant." " For questions about viral load, be sure to provide specific information" " about cutoffs for viral load categories. Under 50 copies/ml is low detectable level," " 50 - 199 copies/ml is low level viremia, 200 - 999 is high level viremia, and " " 1000 and above is suspected treatment failure." " A high viral load or non-suppressed viral load is any viral load above 200 copies/ml." " A suppressed viral load is one below 200 copies / ml." " An established client is one who is on their current ART regimen for a period greater" " than 6 months, had no active OI or in the previous 6 months, has adhered to scheduled" " clinic visits for the previous 6 months and Viral load results has been less than 200 copies/ml" " within the last 6 months." " For questions about when patients should get their viral loads taken," " if they are newly initiated on ART, the first viral load sample should be taken after 3 months of" " taking ART. Otherwise, if they are not new on ART, then if their previous result was below 50 to 199 cp/ml," " their viral load should be taken after every 12 months. If their previous result was above 200cp/ml," " then viral load sample should be taken after three months." " Please answer the prompt using the information retrieved" " and do not rely at all on your prior knowledge." " Please keep your reply to no longer than three sentences, and please use simple language. ") prompt_intro = (" Here is the prompt: ") import gradio as gr def nishauri(question: str, conversation_history: list[str]): context = " ".join([item["user"] + " " + item["chatbot"] for item in conversation_history]) response = query_engine.query(preamble + "the user previously asked and received the following: " + context + prompt_intro + question) conversation_history.append({"user": question, "chatbot": response.response}) source1 = ("File Name: " + response.source_nodes[0].metadata["file_name"] + "\nPage Number: " + response.source_nodes[0].metadata["page_label"] + "\n Source Text: " + response.source_nodes[0].text) source2 = ("File Name: " + response.source_nodes[1].metadata["file_name"] + "\nPage Number: " + response.source_nodes[1].metadata["page_label"] + "\n Source Text: " + response.source_nodes[1].text) source3 = ("File Name: " + response.source_nodes[2].metadata["file_name"] + "\nPage Number: " + response.source_nodes[2].metadata["page_label"] + "\n Source Text: " + response.source_nodes[2].text) return response, source1, source2, source3, conversation_history inputs = [gr.Textbox(lines=10, label="Question"), gr.State(value=[])] outputs = [ gr.Textbox(label="Chatbot Response", type="text"), gr.Textbox(label="Source 1", max_lines = 10, autoscroll = False, type="text"), gr.Textbox(label="Source 2", max_lines = 10, autoscroll = False, type="text"), gr.Textbox(label="Source 3", max_lines = 10, autoscroll = False, type="text"), gr.State() ] gr.Interface(fn=nishauri, inputs=inputs, outputs=outputs, title="Nishauri Chatbot", description="Enter a question and see the processed outputs in collapsible boxes.").launch()