David Chu commited on
Commit
ab2c1b8
·
unverified ·
1 Parent(s): 5bb1986

refactor: improve footnote formatting

Browse files
Files changed (1) hide show
  1. main.py +14 -9
main.py CHANGED
@@ -9,13 +9,11 @@ from google.genai import types
9
  from tools import dailymed, semantic_scholar
10
 
11
  INSTRUCTION = """\
12
- You are a medical research expert.
13
 
14
- Please answer the user's query clearly and concisely, using no more than 250 words.
15
 
16
- Base every claim or statement strictly on the provided sources. For each claim, include a citation referencing the source's ID (do not include the citation in the `text` field). A claim may be supported by one or multiple sources, but only cite sources that directly support the claim. Do not add unnecessary citations.
17
-
18
- You can use markdown format, such as **, to highlight the key part of the answer. But do not return the response in a markdown code block.
19
 
20
  If none of the sources contain relevant information to answer the query, politely inform the user that an answer cannot be provided, and do not use any citations.
21
 
@@ -68,14 +66,21 @@ def format_output(response: str) -> tuple[str, str]:
68
  answer = ""
69
  citations = {}
70
  for statement in statements:
71
- answer += statement["text"]
 
 
 
 
 
 
72
  for source in statement.get("sources", []):
73
  source_str = f"[{source['title']}]({source['url']})"
74
  if not (citation_id := citations.get(source_str)):
75
  citation_id = len(citations) + 1
76
  citations[source_str] = citation_id
77
- answer += f"[^{citation_id}]"
78
- answer += " "
 
79
  except KeyError as err:
80
  print(err)
81
  return response, ""
@@ -87,7 +92,7 @@ def format_output(response: str) -> tuple[str, str]:
87
  def main():
88
  gemini_client = genai.Client(api_key=os.environ["GOOGLE_API_KEY"])
89
 
90
- st.title("Ask ~~Jeeves~~ Elna")
91
  with st.form("search", border=False):
92
  query = st.text_input("Your medical question")
93
  submit = st.form_submit_button("Ask")
 
9
  from tools import dailymed, semantic_scholar
10
 
11
  INSTRUCTION = """\
12
+ You are a medical research expert. Provide a concise answer to the query below, using no more than 250 words.
13
 
14
+ Base every claim or statement strictly on the sources returned from the tool calls. For each claim, include a citation referencing the source's ID (do not include the citation in the `text` field). A claim may be supported by one or multiple sources, but only cite sources that directly support the claim. Do not add unnecessary citations.
15
 
16
+ You may use markdown formatting, such as **, to highlight key parts of the text. Do not return the response in a markdown code block.
 
 
17
 
18
  If none of the sources contain relevant information to answer the query, politely inform the user that an answer cannot be provided, and do not use any citations.
19
 
 
66
  answer = ""
67
  citations = {}
68
  for statement in statements:
69
+ text = statement["text"].strip()
70
+ answer = (
71
+ answer + f"\n{text}"
72
+ if text.startswith("*") or text.startswith("-")
73
+ else answer + f" {text}"
74
+ )
75
+ citation_ids = []
76
  for source in statement.get("sources", []):
77
  source_str = f"[{source['title']}]({source['url']})"
78
  if not (citation_id := citations.get(source_str)):
79
  citation_id = len(citations) + 1
80
  citations[source_str] = citation_id
81
+ citation_ids.append(citation_id)
82
+ if citation_ids:
83
+ answer += " ".join(f"[^{i}]" for i in sorted(citation_ids))
84
  except KeyError as err:
85
  print(err)
86
  return response, ""
 
92
  def main():
93
  gemini_client = genai.Client(api_key=os.environ["GOOGLE_API_KEY"])
94
 
95
+ st.title("Elna")
96
  with st.form("search", border=False):
97
  query = st.text_input("Your medical question")
98
  submit = st.form_submit_button("Ask")