Skip to content

Commit 705538c

Browse files
committed
Include web result used in answer in the response.
1 parent 05754e2 commit 705538c

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

app/backend/approaches/approach.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
from approaches.promptmanager import PromptManager
2828
from core.authentication import AuthenticationHelper
29+
from bing_client import WebPage
2930

3031

3132
@dataclass
@@ -236,6 +237,9 @@ def get_citation(self, sourcepage: str, use_image_citation: bool) -> str:
236237

237238
return sourcepage
238239

240+
def get_links(self, webpages: list[WebPage]) -> list[str]:
241+
return [f"{page.id}: {page.snippet}" for page in webpages]
242+
239243
async def compute_text_embedding(self, q: str):
240244
SUPPORTED_DIMENSIONS_MODEL = {
241245
"text-embedding-ada-002": False,

app/backend/approaches/chatreadretrieveread.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from approaches.approach import Document, ThoughtStep
1515
from approaches.chatapproach import ChatApproach
1616
from approaches.promptmanager import PromptManager
17-
from bing_client import AsyncBingClient
17+
from bing_client import AsyncBingClient, WebPage
1818
from core.authentication import AuthenticationHelper
1919

2020

@@ -167,9 +167,10 @@ async def keyword_rewrite(rendered_prompt, tools):
167167

168168
# STEP 3: Generate a contextual and content specific answer using the search results and chat history
169169
text_sources = self.get_sources_content(results, use_semantic_captions, use_image_citation=False)
170-
170+
web_sources: list[WebPage] = []
171171
if use_bing_search and bing_results.totalEstimatedMatches > 0:
172-
web_sources = [hit.snippet for hit in bing_results.value[:2]]
172+
web_sources = bing_results.value[:2]
173+
web_sources_text = self.get_links(web_sources)
173174

174175
rendered_answer_prompt = self.prompt_manager.render_prompt(
175176
self.bing_answer_prompt,
@@ -179,7 +180,7 @@ async def keyword_rewrite(rendered_prompt, tools):
179180
"past_messages": messages[:-1],
180181
"user_query": original_user_query,
181182
"text_sources": text_sources,
182-
"web_search_snippets": web_sources,
183+
"web_search_snippets": web_sources_text,
183184
},
184185
)
185186
else:
@@ -205,7 +206,7 @@ async def keyword_rewrite(rendered_prompt, tools):
205206
)
206207

207208
extra_info = {
208-
"data_points": {"text": text_sources},
209+
"data_points": {"text": text_sources, "web_search": [hit.model_dump() for hit in web_sources]},
209210
"thoughts": [
210211
ThoughtStep(
211212
"Prompt to generate search query",

app/backend/approaches/prompts/chat_bing_answer_question.prompty

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ Assistant helps the company employees with their healthcare plan questions, and
2424
Answer ONLY with the facts listed in the list of sources below. If there isn't enough information below, say you don't know. Do not generate answers that don't use the sources below. If asking a clarifying question to the user would help, ask the question.
2525
If the question is not in English, answer in the language used in the question.
2626
Each source has a name followed by colon and the actual information, always include the source name for each fact you use in the response. Use square brackets to reference the source, for example [info1.txt]. Don't combine sources, list each source separately, for example [info1.txt][info2.pdf].
27-
Additional web search snippets are included. If the sources do not answer the question, you can use the web search snippets to find the answer. The original sources take precedence over the web search snippets.
27+
Additional "web search snippets" are included, each with a unique URI then a colon and the snippet text in HTML. If the sources do not answer the question, you can use the web search snippets to find the answer. The original sources take precedence over the web search snippets.
28+
If you use a web search snippet in your response, always include the URI of the snippet in square brackets, for example [https://api.bing.microsoft.com/api/v7/#WebPages.0].
2829
{{ injected_prompt }}
2930
{% endif %}
3031

app/frontend/src/components/SupportingContent/SupportingContent.tsx

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,26 @@ import { parseSupportingContentItem } from "./SupportingContentParser";
22

33
import styles from "./SupportingContent.module.css";
44

5+
interface WebPage {
6+
id: string;
7+
name: string;
8+
url: string;
9+
displayUrl: string;
10+
dateLastCrawled: string;
11+
language: string;
12+
snippet?: string;
13+
isFamilyFriendly?: boolean;
14+
siteName?: string;
15+
}
16+
517
interface Props {
6-
supportingContent: string[] | { text: string[]; images?: string[] };
18+
supportingContent: string[] | { text: string[]; images?: string[]; web_search?: WebPage[] };
719
}
820

921
export const SupportingContent = ({ supportingContent }: Props) => {
1022
const textItems = Array.isArray(supportingContent) ? supportingContent : supportingContent.text;
1123
const imageItems = !Array.isArray(supportingContent) ? supportingContent?.images : [];
24+
const webSearchItems = !Array.isArray(supportingContent) ? supportingContent?.web_search : [];
1225
return (
1326
<ul className={styles.supportingContentNavList}>
1427
{textItems.map((c, ind) => {
@@ -27,6 +40,17 @@ export const SupportingContent = ({ supportingContent }: Props) => {
2740
</li>
2841
);
2942
})}
43+
{webSearchItems?.map((webPage, ind) => {
44+
return (
45+
<li className={styles.supportingContentItem} key={`supporting-content-web-search-${ind}`}>
46+
<h4 className={styles.supportingContentItemHeader}>{webPage.name}</h4>
47+
<a className={styles.supportingContentItemText} href={webPage.url} target="_blank" rel="noreferrer">
48+
{webPage.url}
49+
</a>
50+
<p className={styles.supportingContentItemText}>{webPage.snippet}</p>
51+
</li>
52+
);
53+
})}
3054
</ul>
3155
);
3256
};

0 commit comments

Comments
 (0)