Skip to content

Emulate openai responses API style of RAG

0

Hi, I have a prototype with the OpenAI responses API that I am trying to port equivalently to Bedrock. I want the LLM to determine when to use the knowledge base and when not to. In openai responses api, i can simply include 'file_search' in the tools list and instruct the model in the system prompt to access the knowledge base as necessary. The model calls file_search or RAG as it sees fit. How do I do the equivalent with Bedrock. The retrieveAndGenerateCommand API seems to be for always retrieving and then generating. This used to be the old style of RAG. Whereas now RAG is done in an agentic fashion where the LLM determines when to call 'file_search', i.e. RAG. Does it make sense? What's the tool way of doing RAG in Bedrock.

asked a year ago265 views

1 Answer
0

To emulate the OpenAI responses API style of RAG in Amazon Bedrock where the model determines when to use the knowledge base, you can use the Converse API with tool definitions. This approach allows the model to decide when to access external knowledge.

Here's how to implement this in Bedrock:

  1. Define a tool for knowledge base access: Create a JSON schema that defines a tool for querying your knowledge base. This tool definition would include the name, description, and input parameters needed to search your knowledge base.

  2. Use the Converse API: When sending a message to the model via the Converse API, include your tool definition. The model will evaluate the user's query and determine if the knowledge base tool should be used.

  3. Handle tool use responses: When the model decides it needs to use the knowledge base, it will return a response with stopReason set to tool_use and include details about the tool request in the toolUse field.

  4. Make the knowledge base query: Your application should then query the knowledge base using the Retrieve API from Amazon Bedrock Knowledge Bases.

  5. Return results to the model: After getting results from the knowledge base, construct a new message that includes these results and send it back to the model in another Converse request.

  6. Get the final response: The model will generate a response based on the knowledge base results.

This approach gives the model agency to decide when to use the knowledge base, similar to how OpenAI's file_search tool works. The RetrieveAndGenerate API is more automated but doesn't provide the same level of model agency in determining when to use RAG.

For more advanced RAG patterns, you could also consider using LlamaIndex with Amazon Bedrock, which supports agentic RAG workflows where the LLM can reason about when and how to use external knowledge sources.
Sources
Call a tool with the Converse API - Amazon Bedrock
Knowledge bases for Amazon Bedrock - AWS Prescriptive Guidance
Build powerful RAG pipelines with LlamaIndex and Amazon Bedrock | AWS Machine Learning Blog
Use Amazon Bedrock tooling with Amazon SageMaker JumpStart models | AWS Machine Learning Blog

answered a year ago

  • This seems like it should technically work. I also noticed that Agents can be created and associated with knowledge base. This is probably more equivalent to the Responses API pattern I mention, coz in this case the tool search doesn't have to be explicitly implemented for one. I am curious now how the Converse API and Agents approach compare in terms of LLM behavior and effectiveness

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.