Implementing Document Grounding in the Orchestration Service

Objective

After completing this lesson, you will be able to demonstrate the practical application of Document Grounding within the generative AI hub.

Implementation of Document Grounding in generative AI hub

The grounding capability is now available in the orchestration module of the generative AI hub. It provides specialized data retrieval through vector databases, grounding the retrieval process using external and context-relevant data. Grounding combines generative AI capabilities with the capacity to use real-time, precise data to improve decision-making and business operations for specific AI-driven solutions. This data supplements the natural language processing capabilities of pretrained models trained on general material. The Pipeline API is proxied through the SAP AI Core generative AI hub and incorporates vector stores, such as the managed SAP HANA database.

Screenshot of SAP AI Launchpad showing Orchestration page. The Build Orchestration Workflow progress bar highlights Grounding, displaying input and output variables, and selected sources settings.

The generative AI hub supports document grounding through several key features and processes:

  1. Access to LLMs: The generative AI hub provides instant access to a range of LLMs from different providers, such as GPT-4 by Azure OpenAI and OpenSource meta-lama. This broad access allows the orchestration of multiple models to enhance AI capabilities.
  2. Integration with SAP AI Launchpad: Users can execute prompts in the AI Launchpad, showcasing how generative AI can assist with business processes. The SAP AI Core infrastructure supports text template generation based on applications while ensuring data security.
  3. Improved Context and Accuracy: By grounding AI responses in customers' specific documents (like HR policies), the generative AI hub enhances the context and accuracy of generated content. It minimizes the chance of hallucinations and reduces ambiguity.
  4. Document Indexing: Unstructured and semi-structured data from documents are preprocessed, split into chunks, and converted into embeddings using LLMs. These embeddings are stored in the SAP HANA Vector Database for efficient querying, which aids in grounding AI responses in real, relevant data.

These features ensure that generative AI can provide reliable, transparent, and contextually accurate responses by using the customer's own document repositories.

Enhance Content Generation with Document Grounding in Generative AI Hub

In this lesson, you will learn to use the Document Grounding module in the Orchestration Service to generate content with the RAG approach. The Document Grounding module aligns input questions with relevant documents by retrieving them from a knowledge base and using them to produce high-quality responses. This knowledge base can consist of documents stored in a SharePoint folder, an Elasticsearch engine, or a data repository containing vectors.

You perform the following steps:

  • Create the knowledge base with relevant documents.
  • Configure the Document Grounding module in the Orchestration Service.
  • Generate content using the RAG approach based on the knowledge base.

Prerequisites

  • Install the Generative AI Hub SDK using the command:

    Python
    1
    %pip install generative-ai-hub-sdk

  • Set the credentials for the Generative AI Hub SDK.

Detailed Steps

Step 1: Create a Vector Knowledge Base

  • Prepare your knowledge base before using the Grounding module in the orchestration pipeline.
  • The generative AI hub provides several options for users to prepare their knowledge base data:
    • Upload documents to a supported data repository, then run the data pipeline to vectorize the documents. For more details, refer to the Pipeline API.
    • Use the Vector API to directly provide chunks of the document. For additional information, see the Vector API.

Grounding Module Options

Choose one of the following options to use grounding:

Option 1: Upload Documents to Supported Data Repository and Run Data Pipeline

  • The pipeline collects documents and segments the data into chunks.
  • It generates embeddings, which are multidimensional representations of textual information, and stores them efficiently in a vector database.

The process involves the following steps:

  1. Perform Initial One-Time Administrative Steps: Create a resource group and a generic secret for grounding. For more information, see:
  2. Prepare Vector Knowledge Base: Configure the Pipeline API to read unstructured data from data repositories and store it in a vector database. Use the Pipeline API to:
    • Read unstructured documents from various data repositories. Break the data into chunks and create embeddings.
    • Store the multidimensional representations of the textual information in the vector database.
    • Provide a repository ID to access the data.

      For more information, see Preparing Data Using the Pipeline API.

Option 2: Provide Chunks of Documents via Vector API Directly

Provide chunks of data directly and store them using the Vector API. The process involves the following steps:

  1. Perform Initial One-Time Administrative Steps:Create a Resource Group for Grounding.
  2. Prepare Vector Knowledge Base: Provide chunks of information directly and store data in the vector database using the Vector API. Use the Vector API to:
    • Create collections.
    • Create documents by directly using the chunks of data provided by users.
    • Store data in the vector database.
    • Assign repository IDs to access the data.
    • For more information, see Preparing Data Using the Vector API.
  3. Configure Grounding Module in the Orchestration: In the orchestration pipeline, you add configuration for the grounding requests:
    • Create a grounding request configuration in the orchestration pipeline using the repository IDs and filters.
    • Run the orchestration pipeline and check that the response refers to the user data. For more information, see Using the Grounding Module.

A detailed setup is also described here to prepare a knowledge base and verify it.

Step 2: Configure the Document Grounding Module

Now, you must define the configuration for the Document Grounding module, including setting up filters, and specifying the data repositories.

Python
1
orchestration_service_url = <your url code from deployment of the orchestration service>

You must have at least one orchestration-compatible deployment for a generative AI model running. For more information, see and Create a Deployment for Orchestration in SAP AI Core.

Next, you must import all relevant libraries. See the code in the code repository here.

Python
1234567891011121314151617181920212223
# Set up the Orchestration Service aicore_client = get_proxy_client().ai_core_client orchestration_service = OrchestrationService(api_url=orchestration_service_url) llm = LLM( name="gpt-4o", parameters={ 'temperature': 0.0, } ) template = Template( messages=[ SystemMessage("""Facility Solutions Company provides services to luxury residential complexes, apartments, individual homes, and commercial properties such as office buildings, retail spaces, industrial facilities, and educational institutions. Customers are encouraged to reach out with maintenance requests, service deficiencies, follow-ups, or any issues they need by email. """), UserMessage("""You are a helpful assistant for any queries for answering questions. Answer the request by providing relevant answers that fit to the request. Request: {{ ?user_query }} Context:{{ ?grounding_response }} """), ] )

This Python code sets up an orchestration service crucial for handling complex tasks. It initializes an AI core client and configures an orchestration service with a given URL. The code then sets up an LLM with specific parameters to ensure consistent responses. Lastly, it creates a message template to aid in answering customer inquiries efficiently and effectively.

Next, we set up grounding services.

Python
12345678910111213141516171819
# Set up Document Grounding filters = [DocumentGroundingFilter(id="vector", data_repositories=["<add your data repository ID>"], , search_config=GroundingFilterSearch(max_chunk_count=15), data_repository_type=DataRepositoryType.VECTOR.value ) ] grounding_config = GroundingModule( type="document_grounding_service", config=DocumentGrounding(input_params=["user_query"], output_param="grounding_response", filters=filters) ) config = OrchestrationConfig( template=template, llm=llm, grounding=grounding_config )

This Python code sets up and configures a document grounding service. It defines filters for document repositories and specifies search parameters. The grounding configuration is then integrated into an orchestration system, which uses templates and a language model to process and respond to user queries. This setup ensures efficient and accurate document retrieval based on user inputs.

Step 3: Generate Context-Relevant Answers

Run the orchestration service with the configured settings to generate answers based on user queries.

Python
123456
response = orchestration_service.run(config=config, template_values=[ TemplateValue("user_query", "List the issues that are reported by customers."), ]) print(response.orchestration_result.choices[0].message.content)

This Python code sends a request to an orchestration service to run a specific configuration. It includes a template value with a user query asking to list customer-reported issues. After running the service, it prints the response from the orchestration result, specifically the message content of the first choice. This helps automate and fetch data on customer issues efficiently.

You get the following output:

List of customer-reported issues including noise, landscaping, window cleaning, AC malfunction, janitorial, elevator, heating, electrical, plumbing, water damage, roofing, pest control, and cleaning.

You can see that the list of issues reported by customer, which is grounded in mails that customers provided.

You can see the documents that were retrieved for the context from the data repository.

Python
1
print(response.module_results.grounding.data['grounding_result'])

This code extracts and displays the value of 'grounding_result' from a nested data structure within the 'response' object. This specific piece of data could be critical for understanding the outcome of a grounding module, making the code essential for debugging or analysis.

The output lists all the relevant mails used for response earlier, providing a deep insight into the context of the grounding technique. You can see this output in the repository.

Conclusion

In this lesson, you have successfully achieved the following tasks:

  • Created a Vector Knowledge Base: Uploaded and vectorized documents to form a searchable knowledge base.
  • Configured the Document Grounding Module: Set up the module to retrieve relevant documents based on user queries.
  • Generated Context-Relevant Answers: Used the Orchestration Service to produce accurate and contextually relevant responses.

You can enhance the accuracy and relevance of AI-generated content by using document grounding in generative AI hub, addressing common challenges in AI-driven content generation.

Summarize Key Learnings

Throughout this learning journey, you have explored and applied advanced AI techniques, including document grounding, vector embeddings, data masking, content filtering, to enhance business applications using SAP's generative AI hub. These techniques collectively offer significant benefits that can transform how businesses operate and solve complex problems.

The Orchestration service streamlines AI workflows by integrating various AI components, such as templating, content filtering, and data masking. This not only improves the efficiency and accuracy of AI-driven processes but also ensures data privacy and security, enabling businesses to manage complex AI workflows with ease.

Vector embeddings enhance the ability to analyze and retrieve information by representing data in high-dimensional vectors. This allows for advanced applications like semantic search and RAG, which improve the quality and relevance of AI-generated content.

The SAP HANA Cloud vector engine stores "embeddings" of unstructured data, which are numerical representations of objects, such as text, images, or audio, in high-dimensional vectors. These embeddings are used for semantic data retrieval, enabling advanced text search and similarity queries.

Document grounding ensures that AI responses are contextually relevant and accurate by using real-time data from your own knowledge sources. This minimizes the risk of misinformation and enhances decision-making processes by providing precise and reliable insights.

Document grounding in generative AI hub leverages embedding models and the SAP HANA Cloud vector engine to enhance the contextual relevance and accuracy of AI responses.

The generative AI hub serves as an abstraction layer to access a wide range of LLMs from various providers.

By mastering these techniques, you are now equipped to implement AI solutions that drive operational efficiency, reduce costs, and enhance customer experiences. It enables you to tackle business challenges with innovative AI-driven approaches, ultimately leading to improved productivity and strategic growth.

Log in to track your progress & complete quizzes