annikwag commited on
Commit
035b045
·
verified ·
1 Parent(s): 7671f13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -6
app.py CHANGED
@@ -25,15 +25,18 @@ WRITE_ACCESS_TOKEN = st.secrets["Llama_3_1"]
25
  def get_rag_answer(query, top_results):
26
  """
27
  Constructs a prompt from the query and the page contexts of the top results,
28
- then sends it to the dedicated endpoint and returns the generated answer.
29
  """
30
  # Combine the context from the top results (you may adjust the separator as needed)
31
  context = "\n\n".join([res.payload["page_content"] for res in top_results])
32
 
33
- # Create a prompt: you can refine the instructions to better suit your needs.
34
  prompt = (
35
- f"Looking at the Top 15 Projects, the answer your question is.\n\n"
36
- f"Answer:"
 
 
 
37
  )
38
 
39
  headers = {"Authorization": f"Bearer {WRITE_ACCESS_TOKEN}"}
@@ -47,9 +50,11 @@ def get_rag_answer(query, top_results):
47
  response = requests.post(DEDICATED_ENDPOINT, headers=headers, json=payload)
48
  if response.status_code == 200:
49
  result = response.json()
50
- # Depending on the endpoint's response structure, adjust how you extract the generated text.
51
  answer = result[0]["generated_text"]
52
- return answer.strip()
 
 
 
53
  else:
54
  return f"Error in generating answer: {response.text}"
55
 
 
25
  def get_rag_answer(query, top_results):
26
  """
27
  Constructs a prompt from the query and the page contexts of the top results,
28
+ then sends it to the dedicated endpoint and returns only the generated answer.
29
  """
30
  # Combine the context from the top results (you may adjust the separator as needed)
31
  context = "\n\n".join([res.payload["page_content"] for res in top_results])
32
 
33
+ # Create a prompt that instructs the model to output only the answer.
34
  prompt = (
35
+ "Using the following context, answer the question concisely. "
36
+ "Only output the final answer below, without repeating the context or question.\n\n"
37
+ f"Context:\n{context}\n\n"
38
+ f"Question: {query}\n\n"
39
+ "Answer:"
40
  )
41
 
42
  headers = {"Authorization": f"Bearer {WRITE_ACCESS_TOKEN}"}
 
50
  response = requests.post(DEDICATED_ENDPOINT, headers=headers, json=payload)
51
  if response.status_code == 200:
52
  result = response.json()
 
53
  answer = result[0]["generated_text"]
54
+ # If the model returns the full prompt, split and extract only the portion after "Answer:"
55
+ if "Answer:" in answer:
56
+ answer = answer.split("Answer:")[-1].strip()
57
+ return answer
58
  else:
59
  return f"Error in generating answer: {response.text}"
60