Skip to content

Commit 0b3b5bc

Browse files
committed
Revisions per Matt's sample update
1 parent fc28dc6 commit 0b3b5bc

File tree

2 files changed

+95
-5
lines changed

2 files changed

+95
-5
lines changed

articles/search/tutorial-rag-build-solution-maximize-relevance.md

Lines changed: 92 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,78 @@ This tutorial updates the search index created by the [indexing pipeline](tutori
4141

4242
The [sample notebook](https://github.com/Azure-Samples/azure-search-python-samples/blob/main/Tutorial-RAG/Tutorial-rag.ipynb) includes an updated index and query request.
4343

44+
## Run a baseline query for comparison
45+
46+
Let's start with a new query, "Are there any cloud formations specific to oceans and large bodies of water?".
47+
48+
To compare outcomes after adding relevance features, run the query against the existing index schema, before you add semantic ranking or a scoring profile.
49+
50+
```python
51+
from azure.search.documents import SearchClient
52+
from openai import AzureOpenAI
53+
54+
token_provider = get_bearer_token_provider(credential, "https://cognitiveservices.azure.com/.default")
55+
openai_client = AzureOpenAI(
56+
api_version="2024-06-01",
57+
azure_endpoint=AZURE_OPENAI_ACCOUNT,
58+
azure_ad_token_provider=token_provider
59+
)
60+
61+
deployment_name = "gpt-4o"
62+
63+
search_client = SearchClient(
64+
endpoint=AZURE_SEARCH_SERVICE,
65+
index_name=index_name,
66+
credential=credential
67+
)
68+
69+
GROUNDED_PROMPT="""
70+
You are an AI assistant that helps users learn from the information found in the source material.
71+
Answer the query using only the sources provided below.
72+
Use bullets if the answer has multiple points.
73+
If the answer is longer than 3 sentences, provide a summary.
74+
Answer ONLY with the facts listed in the list of sources below. Cite your source when you answer the question
75+
If there isn't enough information below, say you don't know.
76+
Do not generate answers that don't use the sources below.
77+
Query: {query}
78+
Sources:\n{sources}
79+
"""
80+
81+
# Focused query on cloud formations and bodies of water
82+
query="Are there any cloud formations specific to oceans and large bodies of water?"
83+
vector_query = VectorizableTextQuery(text=query, k_nearest_neighbors=50, fields="text_vector")
84+
85+
search_results = search_client.search(
86+
search_text=query,
87+
vector_queries= [vector_query],
88+
select=["title", "chunk", "locations"],
89+
top=5,
90+
)
91+
92+
sources_formatted = "=================\n".join([f'TITLE: {document["title"]}, CONTENT: {document["chunk"]}, LOCATIONS: {document["locations"]}' for document in search_results])
93+
94+
response = openai_client.chat.completions.create(
95+
messages=[
96+
{
97+
"role": "user",
98+
"content": GROUNDED_PROMPT.format(query=query, sources=sources_formatted)
99+
}
100+
],
101+
model=deployment_name
102+
)
103+
104+
print(response.choices[0].message.content)
105+
```
106+
107+
Output from this request might look like the following example.
108+
109+
```
110+
Yes, there are cloud formations specific to oceans and large bodies of water. A notable example is "cloud streets," which are parallel rows of clouds that form over the Bering Strait in the Arctic Ocean. These cloud streets occur when wind blows from a cold surface like sea ice over warmer, moister air near the open ocean, leading to the formation of spinning air cylinders. Clouds form along the upward cycle of these cylinders, while skies remain clear along the downward cycle (Source: page-21.pdf).
111+
```
112+
44113
## Update the index for semantic ranking and scoring profiles
45114

46-
In a previous tutorial, you [designed an index schema](tutorial-rag-build-solution-index-schema.md) for RAG workloads. We purposely omitted relevance enhancements from that schema so that you could focus on the fundamentals. Deferring relevance to a separate exercise also gives you a before-and-after comparison of the quality of search results after the updates are made.
115+
In a previous tutorial, you [designed an index schema](tutorial-rag-build-solution-index-schema.md) for RAG workloads. We purposely omitted relevance enhancements from that schema so that you could focus on the fundamentals. Deferring relevance to a separate exercise gives you a before-and-after comparison of the quality of search results after the updates are made.
47116

48117
1. Update the import statements to include classes for semantic ranking and scoring profiles.
49118

@@ -138,7 +207,7 @@ openai_client = AzureOpenAI(
138207
azure_ad_token_provider=token_provider
139208
)
140209

141-
deployment_name = "gpt-35-turbo"
210+
deployment_name = "gpt-4o"
142211

143212
search_client = SearchClient(
144213
endpoint=AZURE_SEARCH_SERVICE,
@@ -160,8 +229,8 @@ Sources:\n{sources}
160229
"""
161230

162231
# Queries are unchanged in this update
163-
query="how much of earth is covered by water"
164-
vector_query = VectorizableTextQuery(text=query, k_nearest_neighbors=1, fields="text_vector", exhaustive=True)
232+
query="Are there any cloud formations specific to oceans and large bodies of water?"
233+
vector_query = VectorizableTextQuery(text=query, k_nearest_neighbors=50, fields="text_vector")
165234

166235
# Add query_type semantic and semantic_configuration_name
167236
# Add scoring_profile and scoring_parameters
@@ -175,7 +244,7 @@ search_results = search_client.search(
175244
select="title, chunk, locations",
176245
top=5,
177246
)
178-
sources_formatted = "\n".join([f'{document["title"]}:{document["chunk"]}:{document["locations"]}' for document in search_results])
247+
sources_formatted = "=================\n".join([f'TITLE: {document["title"]}, CONTENT: {document["chunk"]}, LOCATIONS: {document["locations"]}' for document in search_results])
179248

180249
response = openai_client.chat.completions.create(
181250
messages=[
@@ -190,6 +259,24 @@ response = openai_client.chat.completions.create(
190259
print(response.choices[0].message.content)
191260
```
192261

262+
Output from a semantically ranked and boosted query might look like the following example.
263+
264+
```
265+
Yes, there are specific cloud formations influenced by oceans and large bodies of water:
266+
267+
- **Stratus Clouds Over Icebergs**: Low stratus clouds can frame holes over icebergs, such as Iceberg A-56 in the South Atlantic Ocean, likely due to thermal instability caused by the iceberg (source: page-39.pdf).
268+
269+
- **Undular Bores**: These are wave structures in the atmosphere created by the collision of cool, dry air from a continent with warm, moist air over the ocean, as seen off the coast of Mauritania (source: page-23.pdf).
270+
271+
- **Ship Tracks**: These are narrow clouds formed by water vapor condensing around tiny particles from ship exhaust. They are observed over the oceans, such as in the Pacific Ocean off the coast of California (source: page-31.pdf).
272+
273+
These specific formations are influenced by unique interactions between atmospheric conditions and the presence of large water bodies or objects within them.
274+
```
275+
276+
Adding semantic ranking and scoring profiles positively affects the response from the LLM by promoting results that meet scoring criteria and are semantically relevant.
277+
278+
Now that you have a better understanding of index and query design, let's move on to optimizing for speed and concision. We revisit the schema definition to implement quantization and storage reduction, but the rest of the pipeline and models remain intact.
279+
193280
<!-- ## Update queries for minimum thresholds ** NOT AVAILABLE IN PYTHON SDK
194281
195282
Keyword search only returns results if there's match found in the index, up to a maximum of 50 results by default. In contrast, vector search returns `k`-results every time, even if the matching vectors aren't a close match.

articles/search/tutorial-rag-build-solution-query.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,9 @@ The NASA Earth book appears to showcase various locations on Earth captured thro
234234
(Source: page-43.pdf, page-147.pdf, page-153.pdf, page-39.pdf)
235235
```
236236

237+
> [!TIP]
238+
> If you're continuing on with the tutorial, remember to restore the prompt to its previous value (`You are an AI assistant that helps users learn from the information found in the source material`).
239+
237240
Changing parameters and prompts affects the response from the LLM. As you explore on your own, keep the following tips in mind:
238241

239242
- Raising the `top` value can exhaust available quota on the model. If there's no quota, an error message is returned or the model might return "I don't know".

0 commit comments

Comments
 (0)