run-llama / llama_index

LlamaIndex is a data framework for your LLM applications
https://docs.llamaindex.ai
MIT License
33.32k stars 4.67k forks source link

[Question]: How do I pass a static parameter when I defined FnComponent #14281

Open yukiman76 opened 2 weeks ago

yukiman76 commented 2 weeks ago

Question Validation

Question

I would like to pass in a connection to a Function Component like the following

def parse_sql_guard_component(response: ChatResponse, test_engine: Engine) -> str:
        """Parse response to SQL."""
        # simple clean SQL
        sSQL = response.text.split('\n')[0].strip().strip("```").strip().replace('"', "'")
        # connect to a Read Only test DB that cannot be harmed
        with test_engine.connect() as connection:
            try:
                connection.execute(text(sSQL))
                connection.commit()  # commits "some statement"
            except Exception as e:
                raise SQLGuardError("Unsafe SQL attempt", [e.message])
        return sSQL
    logging.info("Initiating sql_guard_component")

# i was thinking something like this 
sql_guard_component = FnComponent(fn=parse_sql_guard_component, test_engine=my_test_engine)
logan-markewich commented 2 weeks ago

@yukiman76 hmm, I think you could use a partial, but I also think a partial would break the schema parsing for the function

Probably, the fn_component needs a dedicated partials dict 🤔

yukiman76 commented 2 weeks ago

I was thinking the same, and FnComponent derives from QueryComponent which does have an internal partial support I can do the following temp

def parse_sql_guard_component(response: ChatResponse, test_engine: Engine) -> str:
        """Parse response to SQL."""
        # simple clean SQL
        sSQL = response.text.split('\n')[0].strip().strip("```").strip().replace('"', "'")
        # connect to a Read Only test DB that cannot be harmed
        with test_engine.connect() as connection:
            try:
                connection.execute(text(sSQL))
                connection.commit()  # commits "some statement"
            except Exception as e:
                raise SQLGuardError("Unsafe SQL attempt", [e.message])
        return sSQL
    logging.info("Initiating sql_guard_component")

sql_guard_component = FnComponent(fn=parse_sql_guard_component,
                                  req_params={"response", "test_engine"})
sql_guard_component.partial(test_engine=test_engine)

testing now

yukiman76 commented 2 weeks ago

Looks like that's working, we should think of a better solution tho, add it to the FnComponent class and make it a first class