GenerativeĀ AI has leapt from research papers to daily business realityā and SAP is surfing that wave at full speed.Ā InĀ this handsāon series, Iāll show you how to spin up a custom AI agent on SAPĀ AIĀ Core in minutes, then grow it into a productionāready assetāwithout drowning in theory.
Notice
The Japanese version is availableĀ here.
Ā
šĀ What Youāll Learn in This Series
How to spin up a custom AIāÆagent on SAPāÆAIĀ Core in minutesHandsāon with LangChain, Google Search Tool, RAG, and StreamlitExposing the agent as a RESTĀ API and rebuilding the UI in SAPUI5/Fiori
Time Commitment
Each part is designed to be completed inĀ Ā 10ā15Ā minutesĀ .
Ā
šŗĀ ļø Series Roadmap
Part 0 ProloguePart 1 Env Setup: SAP AICore & AI LaunchpadPart 2 Building a Chat Model withĀ LangChainPart 3 AgentĀ Tools: Integrating GoogleĀ SearchPart4 RAG Basicsā HANA Cloud VectorEngine & EmbeddingPart 5 RAG Basics ā”: Building Retriever Tool [current blog]Part 6 Streamlit UI PrototypePart 7 Expose as a RESTĀ APIPart 8 Rebuild theĀ UI withĀ SAPUI5
Note
Subsequent blogs will be published soon.
If you enjoyed this post, please give it aĀ Ā kudos! Your support really motivates me. Also, if thereās anything youād like to know more about, feel free to leave a comment!
RAG Basics ā”: Building Retriever Tool
1Ā | Introduction
In the previous chapter, we stored our internal documents in the SAP HANA Cloud Vector Engine and completed the similarity search between user queries (as vectors) and those documents. In this chapter, weāll turn that search logic into a tool that our AI agent can invoke (a Retriever Tool). With this in place, the AI agent will be able to build its responses using a two-stage āinternal vector store ā web searchā process.
Ā
2 | Prerequisites
BTP sub-accountSAP AI Core instanceSAP AI LaunchPad SubscriptionPython 3.13 and pipVSCode, BAS or any IDE
Note for the Trial Environment
The HANA Cloud instance in the Trial enviroment automatically shuts down every night. If your work spans past midnight, please restart the instance the following day.
Ā
3 | Build the Retriever Tool
A retriever is an object that returns text chunks whose content closely matches a given query, and itās the first component invoked in LangChainās RAG pipeline. Because our DB instance from the previous chapter already contains both vectors and metadata, we can promote it directly to a retriever.
First, letās inspect the data we embedded and stored in the last chapter via the HANA Cloud Explorer. If you open the TEST_TABLE table, youāll see that it holds both the text column and its corresponding vector column.
Ā
In LangChain, you can turn your vector store into a Retriever in one line using the .as_retriever() method. Letās reuse the db instance we created in the previous chapter:
# ā¶ Notebook Cell 11
retriever = db.as_retriever()
# Send a test query and retrieve just one result
retriever.invoke(“Tell me about SAP security”)[0]
Ā
Next, weāll package this Retriever as a tool. LangChainās create_retriever_tool helper takes three argumentsāthe Retriever itself, a tool name, and a description. Because the description is what the LLM reads to decide when to invoke the tool, be sure to state clearly what this tool searches and when it should be used. Hereās an example:
# ā¶ Notebook Cell 12
from langchain.tools.retriever import create_retriever_tool
retriever_tool = create_retriever_tool(
retriever=retriever,
name=”hana_vectorengine”,
description=(
“Use this tool to search internal SAP documents stored in HANA Cloud Vector Engine when the user asks about company-specific policies, security, or best practices.”
),
)
Ā
4 | Integrate and Running in the AI Agent
Now that we have a two-tool setupāGoogle Search and our HANA VectorEngine retrieverāwe can wire them into an AI Agent. Pass both tools into initialize_agent() to create the agent instance, then fire off a complex query:
# ā¶ Notebook Cell 13
from langchain.agents import initialize_agent, AgentType
agent = initialize_agent(
tools=[google_tool, retriever_tool],
llm=chat_llm,
agent=AgentType.OPENAI_FUNCTIONS,
verbose=True,
)
result = agent.invoke(
“Please explain the services offered by RISE with SAP and, as of 2025, the cloud-migration market share compared to other vendors.”
)
Ā
By setting verbose=True, youāll see the agentās full āThought ā Action ā Observationā trace. For example, in the screenshot below youāll spot:
Invoking: `google_search` with `cloud migration market share 2025 SAP compared to other vendors`
which shows the agent pulling the latest marketāshare data via Google Search, followed by:
Invoking: `hana_vectorengine` with `{‘query’: ‘services offered by RISE with SAP’}`
indicating that itās then querying your internal SAP documents in HANA Cloud VectorEngine. (The exact order or query phrasing may vary slightly by environment.)
āā
Finally, letās render the agentās final answer as Markdown. With this step, weāve completed our chapter goalāan AI Agent that combines a web search tool with the HANA Cloud VectorEngine Retriever!
# ā¶ Notebook Cell 14
from IPython.display import Markdown, display
display(Markdown(result[“output”]))
Ā
5 | Challenge ā Add Memory to the AI Agent
So far, the AI Agent weāve built does not retain any conversation history. Without this, it canāt handle natural dialogue features like āanswer based on the previous questionā or āremember the userās name.ā To fix that, weāll extend it to store its history using LangChainās conventional ConversationBufferMemory class.
Future migration note:
ConversationBufferMemory is deprecated in LangChain 0.3, and going forward, memory implementations based on LangGraph are recommended. In this series, weāre prioritizing āspeed to implementation,ā so weāll stick with the traditional class for now. In Part 5.5, weāll revisit this and build an AI Agent with LangGraphābased memory, covering the migration steps in detail.
# ā¶ Notebook Cell 15
from langchain.chains.conversation.memory import ConversationBufferMemory
from langchain_core.prompts import MessagesPlaceholder
from langchain.agents import initialize_agent, AgentType
agent_kwargs = {
“extra_prompt_messages”: [MessagesPlaceholder(variable_name=”memory”)],
}
memory = ConversationBufferMemory(memory_key=”memory”, return_messages=True)
agent_with_memory = initialize_agent(
tools=[google_tool, retriever_tool],
llm=chat_llm,
agent=AgentType.OPENAI_FUNCTIONS,
verbose=True,
agent_kwargs=agent_kwargs,
memory=memory,
)
In the cell above, we did two things:
Created a ConversationBufferMemory instance to hold the conversation history.
Passed that memory into initialize_agent.
By including a MessagesPlaceholder, LangChain will now automatically inject the stored history into each prompt.Ā
Next, letās verify that itās working:
result1 = agent_with_memory.invoke(“My name is Ryota”)
result2 = agent_with_memory.invoke(“Who am I”)
print(result2[“output”])
On the second call, you should see a reply like āYour name is Mr. Ito,ā which shows that the agent remembered your earlier message. Check the output to confirm that the history is being threaded through.
With memory enabled, you can now naturally ask follow-up questions in the same session, or build on results from HANA Cloud searches and Google searches. Give it a try!
Ā
7 | NextĀ Up
Part 6 Streamlit UI Prototype
In Part 6, weāll kick things off by getting hands-on with Streamlit in record time. Weāll build only the bare bones UIāchat input box, send button, and message historyāand verify that a local web app spins up successfully. Stay tuned!
Ā
Disclaimer
All the views and opinions in the blog are my own and is made in my personal capacity and that SAP shall not be responsible or liable for any of the contents published in this blog.
Ā
āĀ GenerativeĀ AI has leapt from research papers to daily business realityā and SAP is surfing that wave at full speed.Ā InĀ this handsāon series, Iāll show you how to spin up a custom AI agent on SAPĀ AIĀ Core in minutes, then grow it into a productionāready assetāwithout drowning in theory.NoticeThe Japanese version is availableĀ here.Ā šĀ What Youāll Learn in This SeriesHow to spin up a custom AIāÆagent on SAPāÆAIĀ Core in minutesHandsāon with LangChain, Google Search Tool, RAG, and StreamlitExposing the agent as a RESTĀ API and rebuilding the UI in SAPUI5/FioriTime CommitmentEach part is designed to be completed inĀ Ā 10ā15Ā minutesĀ .Ā šŗĀ ļø Series RoadmapPart 0 ProloguePart 1 Env Setup: SAP AICore & AI LaunchpadPart 2 Building a Chat Model withĀ LangChainPart 3 AgentĀ Tools: Integrating GoogleĀ SearchPart4 RAG Basicsā HANA Cloud VectorEngine & EmbeddingPart 5 RAG Basics ā”: Building Retriever Tool [current blog]Part 6 Streamlit UI PrototypePart 7 Expose as a RESTĀ APIPart 8 Rebuild theĀ UI withĀ SAPUI5NoteSubsequent blogs will be published soon.If you enjoyed this post, please give it aĀ Ā kudos! Your support really motivates me. Also, if thereās anything youād like to know more about, feel free to leave a comment!RAG Basics ā”: Building Retriever Tool1Ā | IntroductionIn the previous chapter, we stored our internal documents in the SAP HANA Cloud Vector Engine and completed the similarity search between user queries (as vectors) and those documents. In this chapter, weāll turn that search logic into a tool that our AI agent can invoke (a Retriever Tool). With this in place, the AI agent will be able to build its responses using a two-stage āinternal vector store ā web searchā process.Ā 2 | PrerequisitesBTP sub-accountSAP AI Core instanceSAP AI LaunchPad SubscriptionPython 3.13 and pipVSCode, BAS or any IDENote for the Trial EnvironmentThe HANA Cloud instance in the Trial enviroment automatically shuts down every night. If your work spans past midnight, please restart the instance the following day.Ā 3 | Build the Retriever ToolA retriever is an object that returns text chunks whose content closely matches a given query, and itās the first component invoked in LangChainās RAG pipeline. Because our DB instance from the previous chapter already contains both vectors and metadata, we can promote it directly to a retriever.First, letās inspect the data we embedded and stored in the last chapter via the HANA Cloud Explorer. If you open the TEST_TABLE table, youāll see that it holds both the text column and its corresponding vector column.Ā In LangChain, you can turn your vector store into a Retriever in one line using the .as_retriever() method. Letās reuse the db instance we created in the previous chapter:# ā¶ Notebook Cell 11
retriever = db.as_retriever()
# Send a test query and retrieve just one result
retriever.invoke(“Tell me about SAP security”)[0]Ā Next, weāll package this Retriever as a tool. LangChainās create_retriever_tool helper takes three argumentsāthe Retriever itself, a tool name, and a description. Because the description is what the LLM reads to decide when to invoke the tool, be sure to state clearly what this tool searches and when it should be used. Hereās an example:# ā¶ Notebook Cell 12
from langchain.tools.retriever import create_retriever_tool
retriever_tool = create_retriever_tool(
retriever=retriever,
name=”hana_vectorengine”,
description=(
“Use this tool to search internal SAP documents stored in HANA Cloud Vector Engine when the user asks about company-specific policies, security, or best practices.”
),
)Ā 4 | Integrate and Running in the AI AgentNow that we have a two-tool setupāGoogle Search and our HANA VectorEngine retrieverāwe can wire them into an AI Agent. Pass both tools into initialize_agent() to create the agent instance, then fire off a complex query:# ā¶ Notebook Cell 13
from langchain.agents import initialize_agent, AgentType
agent = initialize_agent(
tools=[google_tool, retriever_tool],
llm=chat_llm,
agent=AgentType.OPENAI_FUNCTIONS,
verbose=True,
)
result = agent.invoke(
“Please explain the services offered by RISE with SAP and, as of 2025, the cloud-migration market share compared to other vendors.”
)Ā By setting verbose=True, youāll see the agentās full āThought ā Action ā Observationā trace. For example, in the screenshot below youāll spot:Invoking: `google_search` with `cloud migration market share 2025 SAP compared to other vendors`which shows the agent pulling the latest marketāshare data via Google Search, followed by:Invoking: `hana_vectorengine` with `{‘query’: ‘services offered by RISE with SAP’}`indicating that itās then querying your internal SAP documents in HANA Cloud VectorEngine. (The exact order or query phrasing may vary slightly by environment.)āāFinally, letās render the agentās final answer as Markdown. With this step, weāve completed our chapter goalāan AI Agent that combines a web search tool with the HANA Cloud VectorEngine Retriever!# ā¶ Notebook Cell 14
from IPython.display import Markdown, display
display(Markdown(result[“output”]))Ā 5 | Challenge ā Add Memory to the AI AgentSo far, the AI Agent weāve built does not retain any conversation history. Without this, it canāt handle natural dialogue features like āanswer based on the previous questionā or āremember the userās name.ā To fix that, weāll extend it to store its history using LangChainās conventional ConversationBufferMemory class.Future migration note:ConversationBufferMemory is deprecated in LangChain 0.3, and going forward, memory implementations based on LangGraph are recommended. In this series, weāre prioritizing āspeed to implementation,ā so weāll stick with the traditional class for now. In Part 5.5, weāll revisit this and build an AI Agent with LangGraphābased memory, covering the migration steps in detail.# ā¶ Notebook Cell 15
from langchain.chains.conversation.memory import ConversationBufferMemory
from langchain_core.prompts import MessagesPlaceholder
from langchain.agents import initialize_agent, AgentType
agent_kwargs = {
“extra_prompt_messages”: [MessagesPlaceholder(variable_name=”memory”)],
}
memory = ConversationBufferMemory(memory_key=”memory”, return_messages=True)
agent_with_memory = initialize_agent(
tools=[google_tool, retriever_tool],
llm=chat_llm,
agent=AgentType.OPENAI_FUNCTIONS,
verbose=True,
agent_kwargs=agent_kwargs,
memory=memory,
)In the cell above, we did two things:Created a ConversationBufferMemory instance to hold the conversation history.Passed that memory into initialize_agent.By including a MessagesPlaceholder, LangChain will now automatically inject the stored history into each prompt.Ā Next, letās verify that itās working:result1 = agent_with_memory.invoke(“My name is Ryota”)
result2 = agent_with_memory.invoke(“Who am I”)
print(result2[“output”])On the second call, you should see a reply like āYour name is Mr. Ito,ā which shows that the agent remembered your earlier message. Check the output to confirm that the history is being threaded through.With memory enabled, you can now naturally ask follow-up questions in the same session, or build on results from HANA Cloud searches and Google searches. Give it a try!Ā 7 | NextĀ UpPart 6 Streamlit UI PrototypeIn Part 6, weāll kick things off by getting hands-on with Streamlit in record time. Weāll build only the bare bones UIāchat input box, send button, and message historyāand verify that a local web app spins up successfully. Stay tuned!Ā DisclaimerAll the views and opinions in the blog are my own and is made in my personal capacity and that SAP shall not be responsible or liable for any of the contents published in this blog.Ā Ā Ā Read MoreĀ Technology Blog Posts by SAP articlesĀ
#SAP
#SAPTechnologyblog