updating to single call and gpt4
Browse files
app.py
CHANGED
|
@@ -22,6 +22,7 @@ from llama_index.core import load_index_from_storage
|
|
| 22 |
storage_context = StorageContext.from_defaults(persist_dir="arv_metadata")
|
| 23 |
index = load_index_from_storage(storage_context)
|
| 24 |
query_engine = index.as_query_engine(similarity_top_k=3, llm=llm)
|
|
|
|
| 25 |
|
| 26 |
import gradio as gr
|
| 27 |
|
|
@@ -29,12 +30,24 @@ def nishauri(question: str, conversation_history: list[str]):
|
|
| 29 |
|
| 30 |
context = " ".join([item["user"] + " " + item["chatbot"] for item in conversation_history])
|
| 31 |
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
if lang_question=="sw":
|
| 35 |
question = GoogleTranslator(source='sw', target='en').translate(question)
|
| 36 |
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
background = ("The person who asked the question is a person living with HIV."
|
| 40 |
" If the person says sasa or niaje, that is swahili slang for hello."
|
|
@@ -56,8 +69,17 @@ def nishauri(question: str, conversation_history: list[str]):
|
|
| 56 |
f" Please update the response provided only if needed, based on the following background information {background}"
|
| 57 |
)
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
completion = client.chat.completions.create(
|
| 60 |
-
model="gpt-
|
| 61 |
messages=[
|
| 62 |
{"role": "user", "content": question_final}
|
| 63 |
]
|
|
|
|
| 22 |
storage_context = StorageContext.from_defaults(persist_dir="arv_metadata")
|
| 23 |
index = load_index_from_storage(storage_context)
|
| 24 |
query_engine = index.as_query_engine(similarity_top_k=3, llm=llm)
|
| 25 |
+
retriever = index.as_retriever(similarity_top_k = 3)
|
| 26 |
|
| 27 |
import gradio as gr
|
| 28 |
|
|
|
|
| 30 |
|
| 31 |
context = " ".join([item["user"] + " " + item["chatbot"] for item in conversation_history])
|
| 32 |
|
| 33 |
+
# Split the string into words
|
| 34 |
+
words = question.split()
|
| 35 |
+
|
| 36 |
+
# Count the number of words
|
| 37 |
+
num_words = len(words)
|
| 38 |
+
|
| 39 |
+
lang_question = "en"
|
| 40 |
+
|
| 41 |
+
if num_words > 4:
|
| 42 |
+
lang_question = detect(question)
|
| 43 |
|
| 44 |
if lang_question=="sw":
|
| 45 |
question = GoogleTranslator(source='sw', target='en').translate(question)
|
| 46 |
|
| 47 |
+
sources = retriever.retrieve(question)
|
| 48 |
+
source0 = sources[0].text
|
| 49 |
+
source1 = sources[1].text
|
| 50 |
+
source2 = sources[2].text
|
| 51 |
|
| 52 |
background = ("The person who asked the question is a person living with HIV."
|
| 53 |
" If the person says sasa or niaje, that is swahili slang for hello."
|
|
|
|
| 69 |
f" Please update the response provided only if needed, based on the following background information {background}"
|
| 70 |
)
|
| 71 |
|
| 72 |
+
question_final = (
|
| 73 |
+
f" The user previously asked and answered the following: {context}. "
|
| 74 |
+
f" The user just asked the following question: {question}."
|
| 75 |
+
f" Please use the following content to generate a response: {source0} {source1} {source2}."
|
| 76 |
+
f" Please update the response provided only if needed, based on the following background information {background}"
|
| 77 |
+
" Keep answers brief and limited to the question that was asked."
|
| 78 |
+
" Do not provide information the user did not ask about. If they start with a greeting, just greet them in return and don't share anything else."
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
completion = client.chat.completions.create(
|
| 82 |
+
model="gpt-4-turbo",
|
| 83 |
messages=[
|
| 84 |
{"role": "user", "content": question_final}
|
| 85 |
]
|