def table_index_builder(sql_database, table_context_dict: dict[str, Literal]) -> ObjectIndex:
table_node_mapping = SQLTableNodeMapping(sql_database)
table_schema_objs: list[SQLTableSchema] = []
# Build our table schema
for table_name in table_context_dict:
# one SQLTableSchema for each table
table_schema = (SQLTableSchema(
table_name=table_name, context_str=table_context_dict[table_name]))
table_schema_objs.append(table_schema)
obj_index = ObjectIndex.from_objects(
table_schema_objs,
table_node_mapping,
VectorStoreIndex,
)
return obj_index
def build_query_engine(sql_database: SQLDatabase, service_context: ServiceContext, table_context_dict: dict[str, Literal]) -> SQLTableRetrieverQueryEngine:
# build object index
obj_index = table_index_builder(
sql_database,
table_context_dict=table_context_dict)
query_engine = SQLTableRetrieverQueryEngine(
sql_database=sql_database,
table_retriever=obj_index.as_retriever(similarity_top_k=2),
service_context=service_context
)
return query_engine
sql_database = SQLDatabase.from_uri(db_uri)
table_node_mapping = SQLTableNodeMapping(sql_database)
all_table_names = [
"responses",
"demographic",
"respondents"]
table_schema_objs = []
for table_name in all_table_names:
table_schema_objs.append(SQLTableSchema(table_name=table_name))
obj_index = ObjectIndex.from_objects(
table_schema_objs,
table_node_mapping,
VectorStoreIndex,
)
python main.py
Traceback (most recent call last):
File "C:\Users\hp\Desktop\openai\main.py", line 64, in <module>
obj_index = ObjectIndex.from_objects(
File "C:\Users\hp\Desktop\openai\jmf-env\lib\site-packages\llama_index\objects\base.py", line 50, in from_objects
nodes = object_mapping.to_nodes(objects)
File "C:\Users\hp\Desktop\openai\jmf-env\lib\site-packages\llama_index\objects\base_node_mapping.py", line 55, in to_nodes
return [self.to_node(obj) for obj in objs]
File "C:\Users\hp\Desktop\openai\jmf-env\lib\site-packages\llama_index\objects\base_node_mapping.py", line 55, in <listcomp>
return [self.to_node(obj) for obj in objs]
File "C:\Users\hp\Desktop\openai\jmf-env\lib\site-packages\llama_index\objects\table_node_mapping.py", line 46, in to_node
f"{self._sql_database.get_single_table_info(obj.table_name)}\n"
AttributeError: 'SQLDatabase' object has no attribute 'get_single_table_info'