nat.plugins.vanna.vanna_utils#
Attributes#
Classes#
LangChain LLM integration for Vanna framework. |
|
Extended Milvus vector store for Vanna. |
|
Combined Vanna implementation with Milvus and LangChain LLM. |
|
Singleton manager for Vanna instances. |
Functions#
|
Extract JSON from a string that may contain additional content. |
|
Remove think tags from reasoning model output based on model type. |
|
Convert message dicts to LangChain message objects. |
|
Train Vanna with DDL, documentation, and question-SQL examples. |
Module Contents#
- logger#
- extract_json_from_string(content: str) dict#
Extract JSON from a string that may contain additional content.
- Args:
content: String containing JSON data
- Returns:
Parsed JSON as dictionary
- Raises:
ValueError: If no valid JSON found
- remove_think_tags( ) str#
Remove think tags from reasoning model output based on model type.
- Args:
text: Text potentially containing think tags model_name: Name of the model reasoning_models: Set of model names that require think tag removal
- Returns:
Text with think tags removed if applicable
- to_langchain_msgs(msgs)#
Convert message dicts to LangChain message objects.
- class VannaLangChainLLM(client=None, config=None)#
Bases:
vanna.legacy.base.VannaBaseLangChain LLM integration for Vanna framework.
- client = None#
- config#
- dialect#
- model = 'unknown'#
- milvus_search_limit#
- reasoning_models#
- chat_models#
- get_training_sql_prompt(ddl_list: list, doc_list: list) list#
Generate prompt for synthetic question-SQL pairs.
- class MilvusVectorStore(config=None)#
Bases:
vanna.legacy.milvus.Milvus_VectorStoreExtended Milvus vector store for Vanna.
- async _ensure_collections_created()#
Ensure all necessary Milvus collections are created (async).
- async add_question_sql(question: str, sql: str, \*\*kwargs) str#
Add question-SQL pair to collection using async client.
- async add_documentation(documentation: str, \*\*kwargs) str#
Add documentation to collection using async client.
Retrieve all related records using async client.
- async get_similar_question_sql(question: str, \*\*kwargs) list#
Get similar question-SQL pairs using async client.
- async get_training_data(\*\*kwargs)#
Get all training data using async client.
- async close()#
Close async Milvus client connection.
- class VannaLangChain(client, config=None)#
Bases:
MilvusVectorStore,VannaLangChainLLMCombined Vanna implementation with Milvus and LangChain LLM.
Initialize VannaLangChain.
- Args:
client: LangChain LLM client config: Configuration dict for Milvus vector store and LLM settings
- db_engine = None#
- async generate_sql(
- question: str,
- allow_llm_to_see_data: bool = False,
- error_message: dict | None = None,
- \*\*kwargs,
Generate SQL using the LLM.
- Args:
question: Natural language question to convert to SQL allow_llm_to_see_data: Whether to allow LLM to see actual data error_message: Optional error message from previous SQL execution kwargs: Additional keyword arguments
- Returns:
Dictionary with ‘sql’ and optional ‘explanation’ keys
- class VannaSingleton#
Singleton manager for Vanna instances.
- _instance: VannaLangChain | None = None#
- _lock: asyncio.Lock | None = None#
- classmethod _get_lock() asyncio.Lock#
Get or create the lock in the current event loop.
- classmethod instance() VannaLangChain | None#
Get current instance without creating one.
- Returns:
Current Vanna instance or None if not initialized
- classmethod get_instance(
- llm_client,
- embedder_client,
- async_milvus_client,
- dialect: str = 'SQLite',
- initial_prompt: str | None = None,
- n_results: int = 5,
- sql_collection: str = 'vanna_sql',
- ddl_collection: str = 'vanna_ddl',
- doc_collection: str = 'vanna_documentation',
- milvus_search_limit: int = 1000,
- reasoning_models: set[str] | None = None,
- chat_models: set[str] | None = None,
- create_collections: bool = True,
- Async:
Get or create a singleton Vanna instance.
- Args:
llm_client: LangChain LLM client for SQL generation embedder_client: LangChain embedder for vector operations async_milvus_client: Async Milvus client dialect: SQL dialect (e.g., ‘databricks’, ‘postgres’, ‘mysql’) initial_prompt: Optional custom system prompt n_results: Number of similar examples to retrieve sql_collection: Collection name for SQL examples ddl_collection: Collection name for DDL doc_collection: Collection name for documentation milvus_search_limit: Maximum limit size for vector search operations reasoning_models: Models requiring special handling for think tags chat_models: Models using standard response handling create_collections: Whether to create Milvus collections if they don’t exist (default True)
- Returns:
Initialized Vanna instance
- classmethod reset()#
- Async:
Reset the singleton Vanna instance.
Useful for testing or when configuration changes. Properly disposes of database engine if present.
- async train_vanna(vn: VannaLangChain, auto_train: bool = False)#
Train Vanna with DDL, documentation, and question-SQL examples.
- Args:
vn: Vanna instance auto_train: Whether to automatically train Vanna (auto-extract DDL and generate training data from database)