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

DOC: No example of usage implementation is provided for the langchain.chains.query_constructor.base.load_query_constructor_runnable function #21478

Open
2 tasks done
moneebullah25 opened this issue May 9, 2024 · 1 comment
Labels
🤖:docs Changes to documentation and examples, like .md, .rst, .ipynb files. Changes to the docs/ folder 🔌: openai Primarily related to OpenAI integrations Ɑ: Runnables Related to Runnables

Comments

@moneebullah25
Copy link

Checklist

  • I added a very descriptive title to this issue.
  • I included a link to the documentation page I am referring to (if applicable).

Issue with current documentation:

Description:

Currently, the load_query_constructor_runnable function documentation lacks doesn't have usage examples or scenarios, making it challenging for developers to understand.

URL to the documentation: https://api.python.langchain.com/en/latest/chains/langchain.chains.query_constructor.base.load_query_constructor_runnable.html#langchain.chains.query_constructor.base.load_query_constructor_runnable

Idea or request for content:

I tried running the function and below is the complete code and output:

from langchain.chains.query_constructor.base import load_query_constructor_runnable
from langchain.chains.query_constructor.schema import AttributeInfo
from langchain_openai import ChatOpenAI
from langchain.chains.query_constructor.ir import (
    Comparator,
    Comparison,
    Operation,
    Operator,
    StructuredQuery,
)

# Define your document contents and attribute information
document_contents = """
product_name: Widget, price: $20
product_name: Gadget, price: $35
product_name: Gizmo, price: $50
"""

attribute_info: AttributeInfo = [
    {"name": "product_name", "type": "string"},
    {"name": "price", "type": "number"},
]

model = ChatOpenAI(model="gpt-3.5-turbo", temperature=0.5)

# Create a runnable for constructing queries
runnable = load_query_constructor_runnable(
    llm=model,
    document_contents=document_contents,
    attribute_info=attribute_info,
    allowed_comparators=[Comparator.EQ, Comparator.LT, Comparator.GT],
    allowed_operators=[Operator.AND, Operator.NOT, Operator.OR],
    enable_limit=True,
    schema_prompt="Describe the query schema using allowed comparators and operators.",
    fix_invalid=True,
)

# Now you can use the runnable to construct queries based on user input
user_input = "Show me products with price less than 30"
query = runnable.middle[0].invoke(user_input).content
print(f"Constructed query: {query}")

Output:

Constructed query: 1. Wireless Bluetooth Earbuds - $29.99
2. Portable Phone Charger - $24.99
3. Travel Makeup Bag - $19.99
4. Insulated Water Bottle - $15.99
5. LED Desk Lamp - $27.99
6. Resistance Bands Set - $12.99
7. Stainless Steel Mixing Bowls - $19.99
8. Yoga Mat - $24.99
9. Essential Oil Diffuser - $28.99
10. Electric Handheld Milk Frother - $14.99

However the output is wrong and is not providing the references to the original documents provided. Needed usage implementation.

@dosubot dosubot bot added Ɑ: Runnables Related to Runnables 🔌: openai Primarily related to OpenAI integrations 🤖:docs Changes to documentation and examples, like .md, .rst, .ipynb files. Changes to the docs/ folder labels May 9, 2024
@wood001
Copy link

wood001 commented Jun 7, 2024

run you code, and client will send prompt as following:

Your goal is to structure the user\'s query to match the request schema provided below.

Describe the query schema using allowed comparators and operators.

<< Example 1. >>
Data Source:
'''json
{
    "content": "Lyrics of a song",
    "attributes": {
        "artist": {
            "type": "string",
            "description": "Name of the song artist"
        },
        "length": {

        "type": "integer",
            "description": "Length of the song in seconds"
        },
        "genre": {
            "type": "string",
            "description": "The song genre, one of "pop", "rock" or "rap""
        }
    }
}
'''

User Query:
What are songs by Taylor Swift or Katy Perry about teenage romance under 3 minutes long in the dance pop genre

Structured Request:
'''json
{
    "query": "teenager love",
    "filter": "and(or(eq(\\"artist\\", \\"Taylor Swift\\"), eq(\\"artist\\", \\"Katy Perry\\")), lt(\\"length\\", 180), eq(\\"genre\\", \\"pop\\"))"
}
'''


<< Example 2. >>
Data Source:
'''json
{
    "content": "Lyrics of a song",
    "attributes": {
        "artist": {
            "type": "string",
            "description": "Name of the song artist"
        },
        "length": {
            "type": "integer",
            "description": "Length of the song in seconds"
        },
        "genre": {
            "type": "string",
            "description": "The song genre, one of "pop", "rock" or "rap""
        }
    }
}
'''

User Query:
What are songs that were not published on Spotify

Structured Request:
'''json
{
    "query": "",
    "filter": "NO_FILTER"
}
'''


<< Example 3. >>
Data Source:
'''json
{
    "content": "Lyrics of a song",
    "attributes": {
        "artist": {
            "type": "string",
            "description": "Name of the song artist"
        },
        "length": {
            "type": "integer",
            "description": "Length of the song in seconds"
        },
        "genre": {
            "type": "string",
            "description": "The song genre, one of "pop", "rock" or "rap""
        }
    }
}
'''

User Query:
What are three songs about love

Structured Request:
'''json
{
    "query": "love",
    "filter": "NO_FILTER",
    "limit": 2
}
'''


<< Example 4. >>
Data Source:
'''json
{
    "content": "Hardware Products Price List",
    "attributes": {
    "product_name": {
        "type": "string"
    },
    "price": {
        "type": "number"
    }
}
}
'''

User Query:
Show me products with price less than 30

Structured Request:

so I change the document_contents content, and get the correct answer.

# Define your document contents and attribute information
document_contents = "Hardware Products Price List"

attribute_info: AttributeInfo = [
    {"name": "product_name", "type": "string"},
    {"name": "price", "type": "number"},
]

# Create a runnable for constructing queries
runnable = load_query_constructor_runnable(
    llm=llm,
    document_contents=document_contents,
    attribute_info=attribute_info,
    allowed_comparators=[Comparator.EQ, Comparator.LT, Comparator.GT],
    allowed_operators=[Operator.AND, Operator.NOT, Operator.OR],
    enable_limit=True,
    schema_prompt="Describe the query schema using allowed comparators and operators.",
    fix_invalid=True,
)

# Now you can use the runnable to construct queries based on user input
user_input = "What are products that price less than 30"
query = runnable.invoke(user_input)
print(f"Constructed query: {query}")

you can try, query will been one StructuredQuery object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖:docs Changes to documentation and examples, like .md, .rst, .ipynb files. Changes to the docs/ folder 🔌: openai Primarily related to OpenAI integrations Ɑ: Runnables Related to Runnables
Projects
None yet
Development

No branches or pull requests

2 participants