YoniFriedman commited on
Commit
c2e27c2
·
verified ·
1 Parent(s): dfcedde

updating for single call

Browse files
Files changed (1) hide show
  1. app.py +24 -9
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
 
@@ -30,12 +31,24 @@ def nishauri(question: str, conversation_history: list[str]):
30
 
31
  context = " ".join([item["user"] + " " + item["chatbot"] for item in conversation_history])
32
 
33
- lang_question = detect(question)
 
 
 
 
 
 
 
 
 
34
 
35
  if lang_question=="sw":
36
  question = GoogleTranslator(source='sw', target='en').translate(question)
37
 
38
- response = query_engine.query(question)
 
 
 
39
 
40
  background = ("The person who asked the question is a person living with HIV."
41
  " If the person says sasa or niaje, that is swahili slang for hello."
@@ -50,15 +63,17 @@ def nishauri(question: str, conversation_history: list[str]):
50
  " A high viral load or non-suppressed viral load is any viral load above 200 copies/ml."
51
  " A suppressed viral load is one below 200 copies / ml.")
52
 
53
- question_final = (
54
- f"The user previously asked and answered the following: {context}"
55
- f" The user just asked the following question: {question}"
56
- f" The following response was generated in response: {response}"
57
- f" Please update the response provided only if needed, based on the following background information {background}"
 
 
58
  )
59
 
60
  completion = client.chat.completions.create(
61
- model="gpt-3.5-turbo",
62
  messages=[
63
  {"role": "user", "content": question_final}
64
  ]
@@ -69,7 +84,7 @@ def nishauri(question: str, conversation_history: list[str]):
69
  if lang_question=="sw":
70
  reply_to_user = GoogleTranslator(source='auto', target='sw').translate(reply_to_user)
71
 
72
- conversation_history.append({"user": question, "chatbot": response.response})
73
 
74
  source1 = ("File Name: " +
75
  response.source_nodes[0].metadata["file_name"] +
 
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
 
 
31
 
32
  context = " ".join([item["user"] + " " + item["chatbot"] for item in conversation_history])
33
 
34
+ # Split the string into words
35
+ words = question.split()
36
+
37
+ # Count the number of words
38
+ num_words = len(words)
39
+
40
+ lang_question = "en"
41
+
42
+ if num_words > 4:
43
+ lang_question = detect(question)
44
 
45
  if lang_question=="sw":
46
  question = GoogleTranslator(source='sw', target='en').translate(question)
47
 
48
+ sources = retriever.retrieve(question)
49
+ source0 = sources[0].text
50
+ source1 = sources[1].text
51
+ source2 = sources[2].text
52
 
53
  background = ("The person who asked the question is a person living with HIV."
54
  " If the person says sasa or niaje, that is swahili slang for hello."
 
63
  " A high viral load or non-suppressed viral load is any viral load above 200 copies/ml."
64
  " A suppressed viral load is one below 200 copies / ml.")
65
 
66
+ question_final = (
67
+ f" The user previously asked and answered the following: {context}. "
68
+ f" The user just asked the following question: {question}."
69
+ f" Please use the following content to generate a response: {source0} {source1} {source2}."
70
+ f" Please update the response provided only if needed, based on the following background information {background}."
71
+ " Keep answers brief and limited to the question that was asked."
72
+ " 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."
73
  )
74
 
75
  completion = client.chat.completions.create(
76
+ model="gpt-4-turbo",
77
  messages=[
78
  {"role": "user", "content": question_final}
79
  ]
 
84
  if lang_question=="sw":
85
  reply_to_user = GoogleTranslator(source='auto', target='sw').translate(reply_to_user)
86
 
87
+ conversation_history.append({"user": question, "chatbot": reply_to_user})
88
 
89
  source1 = ("File Name: " +
90
  response.source_nodes[0].metadata["file_name"] +