Skip to content

3.5 Retrieve Related Knowledge

1. Add Chat With Products Script

Let's copy over the chat_with_products.py script into our application source.

1
cp src.sample/api/chat_with_products.py src/api/.

2. Understand RAG Workflow

This script is the core orchestrator for our RAG workflow, executing the following steps:

  1. Create an Azure AI Project client (with connection string)
  2. Retrieve the inference client for chat completions model
  3. Use incoming user messages to retrieve related products
  4. Use this knowledge to populate a grounded chat template
  5. Call the chat completions client with this prompt template
Click to expand and view Chat With Products script (segment)
src/api/chat_with_products.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from azure.ai.inference.prompts import PromptTemplate


@tracer.start_as_current_span(name="chat_with_products")
def chat_with_products(messages: list, context: dict = None) -> dict:
    if context is None:
        context = {}

    documents = get_product_documents(messages, context)

    # do a grounded chat call using the search results
    grounded_chat_prompt = PromptTemplate.from_prompty(Path(ASSET_PATH) / "grounded_chat.prompty")

    system_message = grounded_chat_prompt.create_messages(documents=documents, context=context)
    response = chat.complete(
        model=os.environ["CHAT_MODEL"],
        messages=system_message + messages,
        **grounded_chat_prompt.parameters,
    )
    logger.info(f"💬 Response: {response.choices[0].message}")

    # Return a chat protocol compliant response
    return {"message": response.choices[0].message, "context": context}

In the next section, we'll look at the prompt template and run a test with a sample query.

3. Run Chat With Products Script

Before we can run this script, we need to create the Grounded Chat Prompt template for step 4. Let's do that next.