Spaces:
Sleeping
Sleeping
| import os | |
| os.environ["OPENAI_API_KEY"] | |
| from llama_index.llms.openai import OpenAI | |
| from llama_index.core.schema import MetadataMode | |
| import openai | |
| from openai import OpenAI as OpenAIOG | |
| import logging | |
| import sys | |
| llm = OpenAI(temperature=0.0, model="gpt-3.5-turbo") | |
| client = OpenAIOG() | |
| # Load index | |
| from llama_index.core import VectorStoreIndex | |
| from llama_index.core import StorageContext | |
| from llama_index.core import load_index_from_storage | |
| storage_context = StorageContext.from_defaults(persist_dir="arv_metadata") | |
| index = load_index_from_storage(storage_context) | |
| query_engine = index.as_query_engine(similarity_top_k=3, llm=llm) | |
| 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(question) | |
| background = ("The person who asked the question is a person living with HIV." | |
| " 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." | |
| " Do not mention in the response that the person is living with HIV." | |
| " The following information about viral loads is authoritative and should override the initial response if appropriate:" | |
| " 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.") | |
| question_final = ( | |
| f"The user previously asked and answered the following: {context}" | |
| f" The user just asked the following question: {question}" | |
| f" The following response was generated in response: {response}" | |
| f" Please update the response provided only if needed, based on the following background information {background}" | |
| ) | |
| completion = client.chat.completions.create( | |
| model="gpt-3.5-turbo", | |
| messages=[ | |
| {"role": "user", "content": question_final} | |
| ] | |
| ) | |
| conversation_history.append({"user": question, "chatbot": response.response}) | |
| return completion.choices[0].message.content, conversation_history | |
| demo = gr.Interface( | |
| title = "Nishauri Chatbot Demo", | |
| fn=nishauri, | |
| inputs=["text", gr.State(value=[])], | |
| outputs=["text", gr.State()], | |
| ) | |
| demo.launch() | |