Implementation:PacktPublishing LLM Engineers Handbook SelfQuery Generate
Appearance
| 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
contentfield 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 queryauthor_id- The corresponding author ID looked up fromUserDocument
How It Works
- A SelfQueryTemplate generates a prompt that instructs the LLM to identify and extract the author name from the user's query
- The prompt is chained with the LLM using LangChain's pipe operator (
prompt | self._llm) - The LLM response is parsed to extract the author_full_name
- The author name is looked up against UserDocument records to obtain the corresponding author_id
- 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