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

bug: natural language response hallucination when question returns 0 rows #53

Closed
mhordynski opened this issue Jun 17, 2024 · 0 comments · Fixed by #68
Closed

bug: natural language response hallucination when question returns 0 rows #53

mhordynski opened this issue Jun 17, 2024 · 0 comments · Fixed by #68
Assignees
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@mhordynski
Copy link
Contributor

What happened?

When a query returns zero rows, the prompt to NLResponder is formatted incorrectly. As a result, the LLM lacks information about the empty result and tends to hallucinate a response.

How can we reproduce it?

import asyncio
import dbally
import pandas as pd
from dbally import DataFrameBaseView
from dbally.audit import CLIEventHandler
from dbally.views import decorators
from dbally.llms import LiteLLM


class MyView(DataFrameBaseView):

    @decorators.view_filter()
    def filter_by_city(self, city: str):
        return self.df['city'] == city


async def main():
    df = pd.DataFrame({
        "name": ["Alice", "Bob", "Charlie", "David", "Eve"],
        "city": ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"],
    })

    collection = dbally.create_collection(
        "clients",
        llm=LiteLLM(model_name="gpt-4o"),
        event_handlers=[CLIEventHandler()]
    )
    collection.add(MyView, lambda: MyView(df))

    result = await collection.ask("What clients are from Radom?", return_natural_response=True)

    print(result)


if __name__ == '__main__':
    asyncio.run(main())

Relevant log output

Request starts...
MESSAGE: find all people from NY

=======================================
=======================================

LLM event starts...
LLM EVENT PROMPT TYPE: IQLPromptTemplate
[{'role': 'system', 'content': 'You have access to API that lets you query a database:\n\nfilter_by_city(city: str)\nPlease suggest which one(s) to call and how they
should be joined with logic operators (AND, OR, NOT).\nRemember! Don\'t give any comments, just the function calls.\nThe output will look like this:\nfilter1("arg1") AND
(NOT filter2(120) OR filter3(True))\nDO NOT INCLUDE arguments names in your response. Only the values.\nYou MUST use only these methods:\n\nfilter_by_city(city: str)\nIt
is VERY IMPORTANT not to use methods other than those listed above.If you DON\'T KNOW HOW TO ANSWER DON\'T SAY "", SAY: `UNSUPPORTED QUERY` INSTEAD! This is CRUCIAL,
otherwise the system will crash. '}, {'role': 'user', 'content': 'find all people from NY'}]

RESPONSE: filter_by_city("NY")

=======================================
=======================================

LLM event starts...
LLM EVENT PROMPT TYPE: NLResponderPromptTemplate
[{'role': 'system', 'content': "You are a helpful assistant that helps answer the user's questions based on the table provided. You MUST use the table to answer the
question. You are very intelligent and obedient.\nThe table ALWAYS contains full answer to a question.\nAnswer the question in a way that is easy to understand and
informative.\nDON'T MENTION using a table in your answer."}, {'role': 'user', 'content': 'The table below represents the answer to a question: find all people from
NY.\n\nAnswer the question: find all people from NY.'}]

RESPONSE: The people from NY are:

1. Sarah, Age 28
2.  John, Age 45

=======================================
=======================================

REQUEST OUTPUT:
Number of rows: 0
SELECT clients.id, clients.name, clients.city, clients.date_joined, clients.total_orders
FROM clients
WHERE clients.city = 'NY'
@mhordynski mhordynski added the bug Something isn't working label Jun 17, 2024
@micpst micpst added the good first issue Good for newcomers label Jun 25, 2024
@BartMiki BartMiki linked a pull request Jul 2, 2024 that will close this issue
@BartMiki BartMiki linked a pull request Jul 2, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants