Skip to content

Commit e2cb539

Browse files
committed
Replaced RAG query with Matt's updated query
1 parent f7c0864 commit e2cb539

File tree

1 file changed

+43
-95
lines changed

1 file changed

+43
-95
lines changed

articles/search/search-get-started-rag.md

Lines changed: 43 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -186,49 +186,28 @@ This section uses Visual Studio Code and Python to call the chat completion APIs
186186
sources_to_include=5
187187
```
188188

189-
1. Set up clients, a search functions prompts, and a chat. The function retrieves selected fields from the search index.
190-
191-
```python
192-
# Set up the query for generating responses
193-
from azure.core.credentials_async import AsyncTokenCredential
194-
from azure.identity.aio import get_bearer_token_provider
195-
from azure.search.documents.aio import SearchClient
196-
from openai import AsyncAzureOpenAI
197-
from enum import Enum
198-
from typing import List, Optional
199-
200-
def create_openai_client(credential: AsyncTokenCredential) -> AsyncAzureOpenAI:
201-
token_provider = get_bearer_token_provider(credential, "https://cognitiveservices.azure.com/.default")
202-
return AsyncAzureOpenAI(
203-
api_version="2024-04-01-preview",
204-
azure_endpoint=AZURE_OPENAI_ACCOUNT,
205-
azure_ad_token_provider=token_provider
206-
)
207-
208-
def create_search_client(credential: AsyncTokenCredential) -> SearchClient:
209-
return SearchClient(
210-
endpoint=AZURE_SEARCH_SERVICE,
211-
index_name="hotels-sample-index",
212-
credential=credential
213-
)
214-
215-
# This quickstart is only using text at the moment
216-
class SearchType(Enum):
217-
TEXT = "text"
218-
VECTOR = "vector"
219-
HYBRID = "hybrid"
189+
1. Set up clients, the prompt, query, and response.
190+
191+
```python
192+
# Set up the query for generating responses
193+
from azure.identity import DefaultAzureCredential
194+
from azure.identity import get_bearer_token_provider
195+
from azure.search.documents import SearchClient
196+
from openai import AzureOpenAI
220197

221-
# This function retrieves the selected fields from the search index
222-
async def get_sources(search_client: SearchClient, query: str, search_type: SearchType, use_semantic_reranker: bool = True, sources_to_include: int = 5) -> List[str]:
223-
search_type == SearchType.TEXT,
224-
response = await search_client.search(
225-
search_text=query,
226-
query_type="semantic" if use_semantic_reranker else "simple",
227-
top=sources_to_include,
228-
select="Description,HotelName,Tags"
229-
)
198+
credential = DefaultAzureCredential()
199+
token_provider = get_bearer_token_provider(credential, "https://cognitiveservices.azure.com/.default")
200+
openai_client = AzureOpenAI(
201+
api_version="2024-06-01",
202+
azure_endpoint=AZURE_OPENAI_ACCOUNT,
203+
azure_ad_token_provider=token_provider
204+
)
230205

231-
return [ document async for document in response ]
206+
search_client = SearchClient(
207+
endpoint=AZURE_SEARCH_SERVICE,
208+
index_name="hotels-sample-index",
209+
credential=credential
210+
)
232211

233212
# This prompt provides instructions to the model
234213
GROUNDED_PROMPT="""
@@ -240,63 +219,30 @@ This section uses Visual Studio Code and Python to call the chat completion APIs
240219
Query: {query}
241220
Sources:\n{sources}
242221
"""
243-
244-
# This class instantiates the chat
245-
class ChatThread:
246-
def __init__(self):
247-
self.messages = []
248-
self.search_results = []
249-
250-
def append_message(self, role: str, message: str):
251-
self.messages.append({
252-
"role": role,
253-
"content": message
254-
})
255222

256-
async def append_grounded_message(self, search_client: SearchClient, query: str, search_type: SearchType, use_semantic_reranker: bool = True, sources_to_include: int = 5):
257-
sources = await get_sources(search_client, query, search_type, use_semantic_reranker, sources_to_include)
258-
sources_formatted = "\n".join([f'{document["HotelName"]}:{document["Description"]}:{document["Tags"]}' for document in sources])
259-
self.append_message(role="user", message=GROUNDED_PROMPT.format(query=query, sources=sources_formatted))
260-
self.search_results.append(
261-
{
262-
"message_index": len(self.messages) - 1,
263-
"query": query,
264-
"sources": sources
265-
}
266-
)
223+
# Query is the question being asked. It's sent to the search engine and the LLM.
224+
query="Can you recommend a few hotels near the ocean with beach access and good views"
267225

268-
async def get_openai_response(self, openai_client: AsyncAzureOpenAI, model: str):
269-
response = await openai_client.chat.completions.create(
270-
messages=self.messages,
271-
model=model
272-
)
273-
self.append_message(role="assistant", message=response.choices[0].message.content)
226+
# Set up the search results and the chat thread.
227+
# Retrieve the selected fields from the search index related to the question.
228+
search_results = search_client.search(
229+
search_text=query,
230+
top=5,
231+
select="Description,HotelName,Tags"
232+
)
233+
sources_formatted = "\n".join([f'{document["HotelName"]}:{document["Description"]}:{document["Tags"]}' for document in search_results])
274234

275-
def get_last_message(self) -> Optional[object]:
276-
return self.messages[-1] if len(self.messages) > 0 else None
277-
278-
def get_last_message_sources(self) -> Optional[List[object]]:
279-
return self.search_results[-1]["sources"] if len(self.search_results) > 0 else None
280-
```
281-
282-
1. Invoke the chat and call the search function, passing in a query string to search for.
283-
284-
```python
285-
import azure.identity.aio
286-
287-
chat_thread = ChatThread()
288-
chat_deployment = AZURE_DEPLOYMENT_MODEL
289-
290-
async with azure.identity.aio.DefaultAzureCredential() as credential, create_search_client(credential) as search_client, create_openai_client(credential) as openai_client:
291-
await chat_thread.append_grounded_message(
292-
search_client=search_client,
293-
query="Can you recommend a few hotels near the ocean with beach access and good views",
294-
search_type=SearchType(search_type),
295-
use_semantic_reranker=use_semantic_reranker,
296-
sources_to_include=sources_to_include)
297-
await chat_thread.get_openai_response(openai_client=openai_client, model=chat_deployment)
235+
response = openai_client.chat.completions.create(
236+
messages=[
237+
{
238+
"role": "user",
239+
"content": GROUNDED_PROMPT.format(query=query, sources=sources_formatted)
240+
}
241+
],
242+
model=AZURE_DEPLOYMENT_MODEL
243+
)
298244

299-
print(chat_thread.get_last_message()["content"])
245+
print(response.choices[0].message.content)
300246
```
301247

302248
Output is from Azure OpenAI, and it consists of recommendations for several hotels. Here's an example of what the output might look like:
@@ -312,10 +258,12 @@ This section uses Visual Studio Code and Python to call the chat completion APIs
312258

313259
If you get an authorization error message, wait a few minutes and try again. It can take several minutes for role assignments to become operational.
314260

315-
To experiment further, change the query and rerun the last step to better understand how the model works with your data.
261+
To experiment further, change the query and rerun the last step to better understand how the model works with the grounding data.
316262

317263
You can also modify the prompt to change the tone or structure of the output.
318264

265+
You might also try the query without semantic ranking by setting `use_semantic_reranker=False` in the query parameters step. Semantic ranking can noticably improve the relevance of query results and the ability of the LLM to return useful information. Experimentation can help you decide whether it makes a difference for your content.
266+
319267
## Clean up
320268

321269
When you're working in your own subscription, it's a good idea at the end of a project to identify whether you still need the resources you created. Resources left running can cost you money. You can delete resources individually or delete the resource group to delete the entire set of resources.

0 commit comments

Comments
 (0)