Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:PacktPublishing LLM Engineers Handbook SelfQuery Generate

From Leeroopedia


Field Value
Type API Doc
Workflow RAG_Inference
Repository PacktPublishing/LLM-Engineers-Handbook
Source self_query.py:L15-37
Implements Principle:PacktPublishing_LLM_Engineers_Handbook_Self_Query_Metadata_Extraction

API Signature

SelfQuery.generate(self, query: Query) -> Query

Import

from llm_engineering.application.rag.self_query import SelfQuery

Key Code

class SelfQuery(RAGStep):
    @opik.track(name="SelfQuery.generate")
    def generate(self, query: Query) -> Query:
        prompt = SelfQueryTemplate().create_template()
        chain = prompt | self._llm
        response = chain.invoke({"question": query.content})
        # Extracts author_full_name from LLM response
        # Looks up author_id from UserDocument
        query.author_full_name = author_full_name
        query.author_id = author_id
        return query

Parameters

Parameter Type Description
query Query The raw user query object containing the natural language question

Inputs and Outputs

Inputs:

  • query: Query - The raw user query with the content field containing the natural language question

Outputs:

  • Query - The same query object enriched with extracted metadata:
    • author_full_name - The full name of the author extracted from the query
    • author_id - The corresponding author ID looked up from UserDocument

How It Works

  1. A SelfQueryTemplate generates a prompt that instructs the LLM to identify and extract the author name from the user's query
  2. The prompt is chained with the LLM using LangChain's pipe operator (prompt | self._llm)
  3. The LLM response is parsed to extract the author_full_name
  4. The author name is looked up against UserDocument records to obtain the corresponding author_id
  5. Both fields are set on the query object, which is returned for use in downstream filtering

External Dependencies

  • langchain_openai (ChatOpenAI) - LLM used for metadata extraction
  • opik - Observability and tracing decorator for monitoring
  • loguru - Structured logging

Source File

  • llm_engineering/application/rag/self_query.py (lines 15-37)

See Also

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment