Tomáš Kubica

Your own data (RAG) for your AI agent, part three - AI chooses the strategy for getting to data itself

AI plans reformulations and SQL strategies, fixes errors, and chooses the best path to the data.

In the first part, I put together the basic techniques for working with the knowledge base in AI agents and we solved semantic, full-text and hybrid searches. To better address more complex problems and aggregation queries, in the second part we used AI to extract critical concepts from the data, create their summaries and connect everything in a knowledge graph. Subsequently, we used several techniques for entering the graph and moving around the graph, but these were predefined procedures that I imprinted in the code. Today it will be different - the AI ​​will directly plan the strategy, execute it, create the necessary queries and correct itself if it fails. We remain with the issue of films today.

In this article I will try to implement the following procedure:

Agentický RAG postup

  1. 1
    Final answer

    we pass the assembled information to the AI ​​to formulate the final answer.

Switch to Azure SQL and prepare data

After publishing the second part of the series, a colleague showed me how different forms of searching can be done in Microsoft SQL, specifically in Azure SQL Serverless. It became clear that the functions of traditional search, fulltext search, vector search and movement along the graph are tightly integrated into the language. This makes it a very versatile solution, which is perfect for today's part, because the SQL queries will be generated by AI today and I want to make it as simple as possible for him. So for today's part, I switched to Azure SQL and defined Node and Edge tables, fulltext index (using CONTAINS query), vector column (using vector_distance function in query) and movement along the graph (using MATCH in query).

You can find the specific procedure in my notebook

Reference query for comparison

Take a look at this notebook

In today's episode, we will be sending significantly more movies to the model, so it will be fair to make a comparison with a simple RAG approach, i.e. semantic search, but this time send the 200 closest movies to LLM. The results are very good for me. However, there is a sense that although the list of films itself is relevant and quite complete, the answer lacks context, insight. It doesn't talk about the sagas, archetypes of the main characters, it doesn't explain as well how specifically this movie fits into the user's question.

Also note that the dataset is very small (8500 movies), so I'm currently sending about 2% of all data to LLM. If the knowledge base were several orders of magnitude larger, this brute-force approach will perform dramatically worse. So today we will try a significantly more intelligent search and see that it offers better, more mature and more comprehensive answers to a number of questions, however, as for the films themselves, it will be quite similar. But let's keep this in mind - for a large data set, a smarter search will have a greater effect.

Když AI sama řídí hledání

In notebook I start by having the user query reformulated into more variants or sub-queries. The point is that sometimes the user makes typos in the question or expresses himself vaguely, and the search can then suffer. In addition, perhaps a different wording of the question can significantly improve the chances of a correct search. It's excellent to see with the Star Wars question, which I think is exactly the one where we suffered the most in the previous installments, and the intelligent RAG proves itself here.

Question: I have seen all Star Wars movies and would like tips for something similar I can watch next.
Generating sub-questions.
  -> Sub-question: What are some science fiction movies similar to Star Wars?
  -> Sub-question: Can you suggest movies or shows with epic space battles and adventures?
  -> Sub-question: What are some recommendations for movies with themes of rebellion against tyranny?
  -> Sub-question: Are there any film series with iconic characters and expansive universes like Star Wars?
  -> Sub-question: What are other popular franchises that mix science fiction and fantasy elements?

Note that quite a few variants finally do not contain the word Star Wars, which caused us so much trouble in the search, because it mainly returned Star Wars. The prompt is relatively simple:

Your task is to rephrase the user's question to improve retrieval accuracy in a Retrieval Augmented Generation system, following a ReSP pattern (retrieve, summarize, plan).

Keep the following in mind:
- Preserve all essential details from the original question.
- Do not introduce new information or context.
- Improve phrasing to be concise and clear.

The main magic takes place in the preparation of the query strategy directly in LLM. The whole prompt can be found here, but I'll "reprint" part of it here:

Your task is to create a query strategy for a Retrieval Augmented Generation system. The strategy should:

# Instructions
- Analyze the user query and decide whether to use semantic search, full_text search or graph search.
- Identify one or more traits (e.g., Movie, Genre, Character, Setting, Theme, Series) to target.
- Rewrite the user query: 
    - For keyword search, extract ONLY the essential identifying terms (names, entities, specific descriptors) from the user's question. DO NOT include any additional words or reformulate the query. Extract only the raw keywords themselves.
      - GOOD EXAMPLES:
         - For "Who directed Inception?" → "Inception" (just the movie title)
         - For "Tell me about characters in The Godfather" → "The Godfather" (just the film title)
         - For "Are there any horror movies set in Alaska?" → "Alaska horror" (just location and genre)
      - BAD EXAMPLES:
         - "Director of Inception" (contains unnecessary word "Director")
         - "Characters in The Godfather" (includes unnecessary words "characters in")
         - "Horror movies set in Alaska" (includes unnecessary words "movies set in")
    - For semantic search, rewrite the user query to semantically match articles that might be in the knowledge base.
- Provide one, preferably two or more strategies. Make sure each strategy is distinct.
- All SQL searches must return **no more than 20 results**. Use various techniques to select best ones if query matches more, eg. reorder results using semantic search (vector similarity).
- For graph search, use the `MATCH` clause to find related nodes and edges. Use `WITH` clause to create temporary tables for intermediate results.
- For keyword search, use `CONTAINS` clause to find related nodes and edges. 
- Use `TOP 20` in the final result to limit the number of results returned.

# Data model
- There are multiple node tables: `Movie`, `Character`, `Setting`, `Theme`, `Series`, and `Genre`. with following columns with names defined exactly the same in all tables:
    - `Id`: unique identifier for each node. 
    - `Name`: name of the node (e.g., movie title, character name, etc.).
    - `Content`: text content of the node. This must be included in all outputs.
    - `Embedding`: vector representation of the node for semantic search.
- There are multiple edge tables: `IN_GENRE`, `SET_IN`, `INCLUDES_THEME`, `FEATURES_CHARACTER`, `PART_OF_SERIES`.

## SQL schema of used tables
...

# Example queries:
- Similarity search on Movie:

SELECT TOP 10 Id, Content, vector_distance('cosine', Embedding, @q) AS Similarity
FROM [dbo].[Movie]
WHERE Embedding IS NOT NULL AND Content IS NOT NULL
ORDER BY Similarity ASC

- Fulltext search on Movie together with semantic search on results to get top 10 results:

SELECT TOP 10 Id, Content, vector_distance('cosine', Embedding, @q) AS Similarity
FROM [dbo].[Movie]
WHERE CONTAINS (Content, '"Indiana Jones" AND "Temple"')
ORDER BY Similarity ASC

- Semantic search on Character, graph search to find Movie related to that Character ans lastly get top 10 results ordered by amount of shared relations (edges) with the first one:

WITH topcharacters AS (
    SELECT TOP 50 Id, vector_distance('cosine', Embedding, @q) AS Similarity
    FROM [dbo].[Character]
    WHERE Embedding IS NOT NULL AND Content IS NOT NULL
    ORDER BY Similarity ASC
),
MovieMatches AS (
    SELECT TOP 10 m.Content as Content, COUNT(c.Id) AS MatchedCharacterCount
    FROM [dbo].[Movie] m, [dbo].[FEATURES_CHARACTER] fc, [dbo].[Character] c
    WHERE MATCH (m-(fc)->c)
    AND c.Id IN (SELECT Id FROM topcharacters)
    GROUP BY m.Content
)
SELECT TOP 10 Content
FROM MovieMatches
ORDER BY MatchedCharacterCount DESC;

After the model, we want it to figure out how best to formulate SQL queries in order to obtain data relevant to the user's query. In order to be able to process it well, I use Structured Outputs, that is, I prescribe the structure of the answer to the model, including the description of the fields. This can be done the Pydantic way and here is the output structure:

class QueryStrategy(BaseModel):
    id: str = Field(
        description="Unique random identifier for this strategy"
    )
    message_to_user: str = Field(
        description="Message to be displayed to the user, explaining the purpose of this query. Include type of each query step and its relation to other steps. Wording should be about what you are doing, example: 'Searching Movie using keyword search for Star Wars followed by graph query to get related genres and semantic movie search among results."
    )
    query: str = Field(
        description="Rewrite query to better match knowledge base using semantic search. This field is used to create embedding and store as @q variable for command to use."
    )
    command: str = Field(
        description="Command to be executed in the database. This should be a SQL query that can be executed directly against the database. All text-based queries should be hardcoded, just vector will be referenced as %q."
    )

class QueryStrategies(BaseModel):
    strategies: List[QueryStrategy] = Field(
        description="List of strategies, each containing a series of steps to be executed sequentially. Each strategy is a sequence of steps, where each step depends on the results of previous steps."
    )

It works surprisingly well and LLM serves up some pretty good ideas on how to access the data.

In the next step, I send individual queries to the SQL database, but sometimes it happens that there is a syntax error. I deal with this by taking it and calling LLM again for a fix and giving it five tries if it doesn't work. Most of the time, if there was a mistake there, it will be fixed during the first repair. You can see the whole fix prompt, it again has the SQL schema and examples, but the beginning of it looks like this:

Your task is to fix a Microsoft SQL query that is failing with syntax errors.

# Instructions
- Analyze the query and errors
- All SQL searches must return **no more than 20 results**. Use various techniques to select best ones if query matches more, eg. reorder results using semantic search (vector similarity).
- For graph search, use the `MATCH` clause to find related nodes and edges. Use `WITH` clause to create temporary tables for intermediate results.
- Use `TOP 20` in the final result to limit the number of results returned.

# Original query
{{query}}

# Error
{{error}}

Let's stay with the Star Wars question - this is how the agent tried it:

Generating query strategies.
 -> Using semantic search to find science fiction movies similar to Star Wars.
    -> Found 20 records
 -> Performing keyword search for science fiction movies and Star Wars, followed by semantic search within results.
    -> Trying to fix error in query
    -> Fixed query successful on attempt 1.
    -> Found 20 records
 -> Searching for Movies and Series using semantic search to match the theme of epic space battles and adventures.
    -> Found 20 records
 -> Searching for Movies and Series using semantic search to match the theme of epic space battles and adventures.
    -> Found 20 records
 -> Searching for themes related to rebellion against tyranny using semantic search, followed by a graph query to retrieve associated movies.
    -> Trying to fix error in query
    -> Fixed query successful on attempt 4.
    -> Found 20 records
 -> Performing a semantic search directly on movies to find content matching the theme of rebellion against tyranny.
    -> Found 20 records
 -> Performing semantic search for expansive film series with iconic characters and universes.
    -> Found 20 records
 -> Using keyword search for iconic series and characters, followed by graph search to explore related movies.
    -> Found 18 records
 -> Performing semantic search on the Series table to identify franchises blending science fiction and fantasy elements.
    -> Found 20 records
 -> Using a keyword-based search on the Theme table to find themes combining science fiction and fantasy, followed by graph search to identify related franchises.
    -> Trying to fix error in query
    -> Fixed query successful on attempt 1.
    -> Found 20 records

After we fix some queries, I keep a list of executions, i.e. prepared and fixed strategies and the results it returned. In the next step, we ask the AI ​​to evaluate the strategies and identify the one that was the best and produced the most useful results. We then scale up this strategy, i.e. let the model pick it up and reformulate the SQL query so that it returns more outputs, because it goes in a promising direction. The prompt is here:

# Role
Your task is to review JSON with executions of different query strategies each identified by unique ID, assess results of each strategy and select the one that is most useful for answering the user question. Based on the selected strategy, you will tak winning SQL query and modify it capture larger set of results by increasing TOP value to 60.

# Instructions
- Review the JSON with query strategy executions and their results. JSON will be provided to you in the next user message.
- User question will be provided to you in the last user message as a string.
- Assess quality of results for each strategy. Consider the following:
  - Are the results relevant to the user question?
  - Are there any missing or incomplete results?
  - Is the number of results appropriate?
- Based on your assessment, select the best strategy.
- Modify the winning SQL query to increase the TOP value to 60.
- Provide the modified SQL query as output.)

The model actually did it.

Selecting the best strategy based on the results.
Expanding results of the best strategy.
 -> This query is designed to find film series that are similar to Star Wars in their blending of science fiction, fantasy, and epic storytelling. It aims to identify options for viewers seeking engaging narratives and expansive universes.
    -> Found 60 records

Now it's the same as in all the other examples. We will serve LLM the question and all collected documents. Here is the result for the Star Wars question:

If you've watched all the "Star Wars" movies and are seeking something similar, here are recommendations based on themes, world-building, and narrative structures akin to the "Star Wars" saga:

### **1. "Star Trek" Movie Series**

- **Why Similar**: Like "Star Wars," the "Star Trek" series explores interstellar conflicts, diplomacy, and the moral complexities of leadership and humanity's place in the universe. It features compelling ensemble casts, advanced technology, and philosophical undertones.
- **Highlights**: Iconic characters like Captain Kirk and Spock navigate moral dilemmas and thrilling adventures across the galaxy. Key films include *Star Trek II: The Wrath of Khan* and the 2009 reboot *Star Trek*, which reintroduce classic elements with modern storytelling.

### **2. "Battlestar Galactica" Series**

- **Why Similar**: This series delves into the survival of humanity against an AI-driven race (the Cylons), blending space-based action with moral struggles and complex interpersonal dynamics. It mirrors "Star Wars" themes of rebellion and redemption.
- **Highlights**: The movies *Battlestar Galactica: Razor* and *Battlestar Galactica: The Plan* provide deeper insights into character backstories and philosophical questions about existence and war.

### **3. "Guardians of the Galaxy" Series**

- **Why Similar**: With its mix of humor, heartfelt character dynamics, and intergalactic adventures, "Guardians of the Galaxy" mirrors the camaraderie and action-packed escapades of "Star Wars."
- **Highlights**: The films (*Guardians of the Galaxy* and *Guardians of the Galaxy Vol. 2*) focus on a ragtag group of misfits who band together to confront cosmic threats, offering both emotional depth and humor.

### **4. "Pacific Rim" Series**

- **Why Similar**: Lush visuals, teamwork, and humanity's battle against towering alien threats resonate with the larger-than-life battles in "Star Wars."
- **Highlights**: Pilots join forces to control giant robots in *Pacific Rim* and *Pacific Rim: Uprising*, blending epic battles with themes of sacrifice and resilience.

### **5. "Dune Saga"**

- **Why Similar**: "Dune" shares the epic scale and political intrigue of "Star Wars," with a focus on power and rebellion in a world driven by mystical elements (similar to the Force).
- **Highlights**: The latest adaptation of *Dune* explores complex characters and breathtaking visuals on the desert planet Arrakis, which is akin to Tatooine's aesthetic.

### **6. "Space Pirate Captain Harlock"**

- **Why Similar**: A rebellious space pirate fights against oppressive forces, echoing Han Solo's roguish charm and the broader rebellion themes in "Star Wars."
- **Highlights**: This animated series features thrilling space battles, rich visuals, and a deep meditation on freedom and heroism.

### **7. "Iron Sky" Series**

- **Why Similar**: Its blend of sci-fi action and satire offers a unique take on galactic conflict, with absurd yet entertaining depictions of war and ideology.
- **Highlights**: *Iron Sky* and *Iron Sky: The Coming Race* deliver political commentary with retro-futuristic aesthetics.

### **8. "The Matrix" Series**

- **Why Similar**: Themes of prophecy, rebellion, and the fight for freedom against oppressive systems parallel the Jedi's battle against the Sith.
- **Highlights**: The philosophical depth and action sequences in *The Matrix* and its sequels provide thought-provoking entertainment akin to "Star Wars."

### **9. "2001: A Space Odyssey" Series**

- **Why Similar**: If you enjoy the deeper philosophical aspects of "Star Wars," this series explores humanity's relationship with technology and the unknown.
- **Highlights**: *2001: A Space Odyssey* and *2010* offer stunning visuals and existential questions about evolution and intelligence.

### **10. "Starship Troopers" Series**

- **Why Similar**: Like "Star Wars," this series features interstellar battles against alien species and critiques themes of militarism and heroism.
- **Highlights**: The original *Starship Troopers* combines thrilling combat sequences with satirical social commentary.

Each of these series offers compelling narratives and expansive worlds that align with the adventure, intrigue, and moral dilemmas found in "Star Wars." Whether you're drawn to space battles, intricate politics, or the hero's journey, these films and franchises provide exciting options for your next viewing experience.

I really like this answer and here are a few reasons why: - Contains links to entire series, sagas, and at the same time also lists representatives of the given series. He speaks to Star Trek, Guardians of the Galaxy, Dune, The Matrix and more. - At each point, he points out some things worth noting, such as a specific film in the series or key elements of the film. - It focuses on what the movie is like, why I might like it.

Compared to the basic RAG of 200 films, this is a completely different quality of response, since it was more of a list of films without any reference to series, typical elements and so on. I could almost consider the results I got with a smart search to be something close to the Deep Research phenomenon.

But let me not praise - I do not notice an improvement in some questions. I notice a wider context and deeper answers in all of them, but the differences somewhere do not compensate for the significantly greater difficulty of obtaining such answers and preparing the data for the chart. However, with a larger knowledge base, I think the benefits of smart search will be even greater. From my amateur example, where we did everything ourselves on our knees, it may not fully convince you, but I think the results of various professional researches in this area confirm it.

Conclusion

This concludes the trilogy on adding your own data to your AI agent. From simple principles of search, through advanced extraction of concepts from data, search for relationships and connections, charting and preparation by summarizing superior concepts, today we have reached a state where AI actively intervenes in the process of searching for relevant data. AI reformulates questions, creates a plan, search strategy and even writes SQL queries themselves combining full text, semantic search and graph traversal. In code, we execute his plan and report back to the AI ​​if we find errors. We evaluate the progress and in my simplified example we simply find the most successful strategy and scale it up, but in practice we could re-plan things here and change strategies as we go. I think something like this, but done professionally, happens when you use Deep Research in one of the tools. This is a concept that I really liked. I don't just want a better search engine that comments on individual sources and summarizes them at the end. I give the AI ​​time and want it to research the problem on its own and give me an answer based on the larger context.

I don't just want a better search engine; I want to give the AI ​​time to research the problem on its own and return an answer from the wider context.