Find answers from the community

Updated 10 months ago

Is there a way with llama_index and the

Is there a way with llama_index and the llm to, depending of my user that is logged in, to only get a part of data ? example to always add a WHERE user = '123' in the sql clause when its him logged? i tried to add to the context but the SQl query created is not respecting it
a
M
L
26 comments
Hey, have you tried modifying the prompt template?
i have some hardcoded context for each oy my sql that i pass with this :

for table_name in all_table_names :
table_schema_objs.append(SQLTableSchema(table_name=table_name, context_str=table_contexts[table_name]))

obj_index = ObjectIndex.from_objects(
table_schema_objs,
table_node_mapping,
VectorStoreIndex
)

but.. im also using the prompt from llama_index :
text2sql_prompt = DEFAULT_TEXT_TO_SQL_PROMPT.partial_format(
dialect=engine.dialect.name
)

so maybe thats the problem
yeah my suggestion would be to modify the text2sql_prompt so that "WHERE user = '<user>'" is always there.
You can do a partial_format to fill in the current user
but how would i add.. just lets say like this ? :

new_prompt = "This is some additional text. "
text2sql_prompt = DEFAULT_TEXT_TO_SQL_PROMPT.partial_format(
dialect=engine.dialect.name
)
text2sql_prompt = text2sql_prompt + new_prompt

?
Is the new_prompt meant to be a change to the template, or a new query?
i wanted to add to it
are you changing the prompt often? Sorry, I'm trying to get a better handle of your use case
Well no.. only if 3-4 specific users log in, i need to add to the prompt to do a where clause
The 4 users are salesmen. i dont want salesman A to see data of other 3
@nerdai do you think how I wanted to add is the proper way ?
@Logan M hey Logan πŸ™‚ i tried above its not a string, i cant just add to it.. do you know how I could add to the :

text2sql_prompt = DEFAULT_TEXT_TO_SQL_PROMPT.partial_format(
dialect=engine.dialect.name
)
Sorry, can you remind me what the issue is? lol
yes so, in llama_index in the last video for text-sql and querypipeline, jerry used DEFAULT_TEXT_TO_SQL_PROMPT to get a default prompt

the question is, what if I want to add a bit more text to it, how do I add text to the prompt if I use what he wrote :

text2sql_prompt = DEFAULT_TEXT_TO_SQL_PROMPT.partial_format(
dialect=engine.dialect.name
)
i cannot do : text2sql_prompt = text2sql_prompt + "test test test" it will create an error before text2sql_prompt is not a string, how do I add then,,,. ?
Ah yes. I would just copy the entire string and make a new prompt template

Plain Text
from llama_index.core.prompts import PromptTemplate

DEFAULT_TEXT_TO_SQL_TMPL = (
    "Given an input question, first create a syntactically correct {dialect} "
    "query to run, then look at the results of the query and return the answer. "
    "You can order the results by a relevant column to return the most "
    "interesting examples in the database.\n\n"
    "Never query for all the columns from a specific table, only ask for a "
    "few relevant columns given the question.\n\n"
    "Pay attention to use only the column names that you can see in the schema "
    "description. "
    "Be careful to not query for columns that do not exist. "
    "Pay attention to which column is in which table. "
    "Also, qualify column names with the table name when needed. "
    "You are required to use the following format, each taking one line:\n\n"
    "Question: Question here\n"
    "SQLQuery: SQL Query to run\n"
    "SQLResult: Result of the SQLQuery\n"
    "Answer: Final answer here\n\n"
    "Only use tables listed below.\n"
    "{schema}\n\n"
    "Question: {query_str}\n"
    "SQLQuery: "
)

DEFAULT_TEXT_TO_SQL_PROMPT = PromptTemplate(
    DEFAULT_TEXT_TO_SQL_TMPL,
    prompt_type=PromptType.TEXT_TO_SQL,
)


Then you can edit the string as needed
not ideal, but it works lol
thanks @Logan M for responding here. (Apologies @Meow Meow... just getting back to discord for the day now)
I will try tonight !
i get error on the word : PromptType
which import i need ?
Oh ignore that, my bad, should have left that out
Plain Text
DEFAULT_TEXT_TO_SQL_PROMPT = PromptTemplate(
    DEFAULT_TEXT_TO_SQL_TMPL,
)
Add a reply
Sign up and join the conversation on Discord