Replies: 2 comments 6 replies
-
Hello, @deybelman! I'm here to help you with any bugs, questions, or contributions you might have. If you need assistance, feel free to ask! To connect your existing query pipeline to a SQL query engine and wrap it in a tool for an agent to select, follow these steps:
This setup connects your query pipeline to a SQL query engine and wraps it in a tool for an agent to select [1][2]. |
Beta Was this translation helpful? Give feedback.
-
@deybelman Hope you have solved it by now; Here is how you can do it: from llama_index.core.query_engine import CustomQueryEngine
class CustomQueryEngine(CustomQueryEngine):
"""Custom Query Engine."""
query_pipeline: QueryPipeline
def custom_query(self, query_str: str):
return self.query_pipeline.run(query_str=query_str)
async def acustom_query(self, query_str: str) -> RESPONSE_TYPE:
return await self.query_pipeline.arun(query_str=query_str) Then build your query engine obj sql_query_engine = CustomQueryEngine(query_pipeline=qp) Finally the sub question query engine wrapper from llama_index.core.tools import QueryEngineTool, ToolMetadata
# # setup base query engine as tool
query_engine_tools = [
QueryEngineTool(
query_engine=sql_query_engine,
metadata=ToolMetadata(
name="sql_tool",
description="Useful for translating a natural language query into a SQL query"
),
),
]
query_engine = SubQuestionQueryEngine.from_defaults(
query_engine_tools=query_engine_tools,
use_async=True,
) To use it, you may have to use asyncio like this: import asyncio
import nest_asyncio
nest_asyncio.apply()
coroutine = query_engine.aquery(
"your question here"
)
response = asyncio.run(coroutine) I faced some issues to build this and had to add a final tool at the end of my query pipeline just to give the custom query engine the correct response as string, just like this; from llama_index.core.query_pipeline import FnComponent
from llama_index.core.llms import ChatResponse
def parse_response_to_text(response: ChatResponse) -> str:
"""Parse response as text to use in subquestion query engine."""
response = response.message.content
return response
response_parser = FnComponent(fn=parse_response_to_text) add this at the end of your query pipeline before giving it to the custom query engine |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I want to create a SQL query engine using my existing query pipeline (see code below). My query pipeline is similar to NLSQLQueryEngine but more advanced. How do I connect it to an engine? Once I do that, I will wrap it in a tool and have an agent select that tool. Please help explain exactly how to get from a query pipeline to a query engine like I'm a newbie.
Beta Was this translation helpful? Give feedback.
All reactions