Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to run nested queries in python #189

Closed
banacer opened this issue Aug 21, 2023 · 3 comments
Closed

How to run nested queries in python #189

banacer opened this issue Aug 21, 2023 · 3 comments
Labels
question Questions about using LMQL.

Comments

@banacer
Copy link

banacer commented Aug 21, 2023

@lmql.query
def hello():
    @lmql.query
    def chain_of_thought():
        '''lmql
            "A: Let's think step by step.\n [REASONING]"
            "Therefore the answer is[ANSWER]" where STOPS_AT(ANSWER, ".")
            return ANSWER.strip()
        '''
    
    '''lmql
    "Q: It is August 12th, 2020. What date was it 100 days ago? [ANSWER]" where [ANSWER: chain_of_thought]
    '''
x = hello()[0]
print(x)

Output I get:

@lmql.query
def hello():
    @lmql.query
    def chain_of_thought():
        '''lmql
            "A: Let's think step by step.\n [REASONING]"
            "Therefore the answer is[ANSWER]" where STOPS_AT(ANSWER, ".")
            return ANSWER.strip()
        '''
    
    '''lmql
    "Q: It is August 12th, 2020. What date was it 100 days ago? [ANSWER]" where [ANSWER: chain_of_thought()]
    '''

RuntimeError: Failed to parse docstring of query function as LMQL code

@lbeurerkellner lbeurerkellner added the question Questions about using LMQL. label Aug 21, 2023
@lbeurerkellner
Copy link
Collaborator

Hi there, you almost got it right, there are only two small syntactic issues in your code.

The nested query chain_of_thought should be declared on the same level as hello itself. I just added an example of this to the docs, see here: https://docs.lmql.ai/en/latest/language/functions.html#nested-queries-in-python

The second one is on how you call the nested function. Your main query code should just be:

"Q: It is August 12th, 2020. What date was it 100 days ago? [ANSWER: chain_of_thought()]"

You don't need to specify a separate where clause, but instead specify the nested query in-line, i.e. [ANSWER: chain_of_thought()]

So overall, you get:

import lmql

@lmql.query
def chain_of_thought():
    '''lmql
        "A: Let's think step by step.\n [REASONING]"
        "Therefore the answer is[ANSWER]" where STOPS_AT(ANSWER, ".")
        return ANSWER.strip()
    '''

@lmql.query
def hello():
    '''lmql
    "Q: It is August 12th, 2020. What date was it 100 days ago? [ANSWER: chain_of_thought]"
    '''

r = hello()
print(r) # [LMQLResult(prompt='Q: It is August 12th, 2020. What date was it 100 days ago? May 4th, 2020.', variables={'ANSWER': 'May 4th, 2020.'}, distribution_variable=None, distribution_values=None)]

@banacer
Copy link
Author

banacer commented Aug 21, 2023

thanks a lot for your response!
is it supported in the last beta release or we need to point to main branch?

@banacer
Copy link
Author

banacer commented Aug 22, 2023

I pulled the master and it works! Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Questions about using LMQL.
Projects
None yet
Development

No branches or pull requests

2 participants