{"id":29098115,"url":"https://github.com/basedosdados/chatbot","last_synced_at":"2025-06-28T14:37:55.943Z","repository":{"id":296199412,"uuid":"954286659","full_name":"basedosdados/chatbot","owner":"basedosdados","description":null,"archived":false,"fork":false,"pushed_at":"2025-06-23T19:35:18.000Z","size":505,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-06-23T19:43:55.876Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/basedosdados.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-03-24T21:08:26.000Z","updated_at":"2025-06-13T17:36:41.000Z","dependencies_parsed_at":"2025-06-23T19:34:09.892Z","dependency_job_id":"b417d1b0-994e-4dca-af70-3384b8d33fb0","html_url":"https://github.com/basedosdados/chatbot","commit_stats":null,"previous_names":["basedosdados/chatbot"],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/basedosdados/chatbot","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basedosdados%2Fchatbot","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basedosdados%2Fchatbot/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basedosdados%2Fchatbot/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basedosdados%2Fchatbot/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/basedosdados","download_url":"https://codeload.github.com/basedosdados/chatbot/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basedosdados%2Fchatbot/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262447121,"owners_count":23312700,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-06-28T14:37:35.517Z","updated_at":"2025-06-28T14:37:55.936Z","avatar_url":"https://github.com/basedosdados.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Chatbot\n\n**Chatbot** is a Python library designed to make it easy for Large Language Models (LLMs) to interact with your data. It is built on top of [LangChain](https://python.langchain.com/docs/introduction/) and [LangGraph](https://langchain-ai.github.io/langgraph/concepts/why-langgraph/) and provides agents and high-level assistants for natural language querying and data visualization.\n\n\u003e [!NOTE]\n\u003e This library is **still under active development**. Expect breaking changes, incomplete features, and limited documentation.\n\n## Installation\nClone the repository and install it (you can also use [poetry](https://python-poetry.org/) or [uv](https://docs.astral.sh/uv/) instead of pip).\n```bash\ngit clone https://github.com/basedosdados/chatbot.git\ncd chatbot\npip install .\n```\n\n## Assistants\n\n### SQLAssistant\nThe [`SQLAssistant`](https://github.com/basedosdados/chatbot/blob/d5a1c275183932de52781af6346d06b1c148e675/chatbot/assistants/sql_assistant.py) allows LLMs to interact with your database so you can ask questions about it. All it needs is a LangChain [Chat Model](https://python.langchain.com/docs/integrations/chat/), a [Context Provider](https://github.com/basedosdados/chatbot/blob/d5a1c275183932de52781af6346d06b1c148e675/chatbot/contexts/context_provider.py) and a [Prompt Formatter](https://github.com/basedosdados/chatbot/blob/d5a1c275183932de52781af6346d06b1c148e675/chatbot/formatters/prompt_formatter.py). The context provider is responsible for providing context about your data to the SQL Agent and the prompt formatter is responsible for building a system prompt for **SQL generation**.\n\nWe provide a default [`BigQueryContextProvider`](https://github.com/basedosdados/chatbot/blob/d5a1c275183932de52781af6346d06b1c148e675/chatbot/contexts/bigquery_context_provider.py) for retrieving metadata directly from Google BigQuery and a default [`SQLPromptFormatter`](https://github.com/basedosdados/chatbot/blob/d5a1c275183932de52781af6346d06b1c148e675/chatbot/formatters/sql_prompt_formatter.py). You can supply your own implementation of a context provider and a prompt formatter for custom behaviour.\n```python\nfrom langchain.chat_models import init_chat_model\n\nfrom chatbot.assistants import SQLAssistant\nfrom chatbot.contexts import BigQueryContextProvider\nfrom chatbot.formatters import SQLPromptFormatter\n\nmodel = init_chat_model(\"gpt-4o\", temperature=0)\n\n# you must point the GOOGLE_APPLICATION_CREDENTIALS\n# env variable to your service account JSON file.\ncontext_provider = BigQueryContextProvider(\n    billing_project=\"your billing project\",\n    query_project=\"your query project\",\n)\n\nprompt_formatter = SQLPromptFormatter()\n\nassistant = SQLAssistant(model, context_provider, prompt_formatter)\n\nresponse = assistant.invoke(\"hello! what can you tell me about our database?\")\n```\n\nYou can optionally use a [`PostgresSaver`](https://langchain-ai.github.io/langgraph/reference/checkpoints/#langgraph.checkpoint.postgres.PostgresSaver) checkpointer to add short-term memory to your assistant and a [`VectorStore`](https://python.langchain.com/docs/integrations/vectorstores/) for few-shot prompting during **SQL queries generation**:\n```python\nfrom langchain.chat_models import init_chat_model\n\nfrom langchain_postgres import PGVector\nfrom langgraph.checkpoint.postgres import PostgresSaver\n\nfrom chatbot.assistants import SQLAssistant\nfrom chatbot.contexts import BigQueryContextProvider\nfrom chatbot.formatters import SQLPromptFormatter\n\nmodel = init_chat_model(\"gpt-4o\", temperature=0)\n\n# you must point the GOOGLE_APPLICATION_CREDENTIALS\n# env variable to your service account JSON file.\ncontext_provider = BigQueryContextProvider(\n    billing_project=\"your billing project\",\n    query_project=\"your query project\",\n)\n\n# it could be any combination of\n# a langchain vector store and an embedding model\nvector_store = PGVector(\n    connection=\"your connection string\",\n    collection_name=\"your collection name\",\n    embedding=OpenAIEmbeddings(\n      model=\"text-embedding-3-small\",\n    ),\n)\n\nprompt_formatter = SQLPromptFormatter(vector_store)\n\nDB_URI = \"postgresql://postgres:postgres@localhost:5442/postgres\n\nwith PostgresSaver.from_conn_strin(DB_URI) as checkpointer:\n    checkpointer.setup()\n\n    assistant = SQLAssistant(\n        model=model,\n        context_provider=context_provider,\n        prompt_formatter=prompt_formatter,\n        checkpointer=checkpointer,\n    )\n\n    response = assistant.invoke(\n        message=\"hello! what can you tell me about our database?\",\n        thread_id=\"some uuid\"\n    )\n```\n\nAn async version is also available: [`AsyncSQLAssistant`](https://github.com/basedosdados/chatbot/blob/d5a1c275183932de52781af6346d06b1c148e675/chatbot/assistants/async_sql_assistant.py).\n\n### SQLVizAssistant\n[`SQLVizAssistant`](https://github.com/basedosdados/chatbot/blob/d5a1c275183932de52781af6346d06b1c148e675/chatbot/assistants/sql_viz_assistant.py) extends [`SQLAssistant`](https://github.com/basedosdados/chatbot/blob/d5a1c275183932de52781af6346d06b1c148e675/chatbot/assistants/sql_assistant.py) by not only retrieving data but also **preparing it for visualization**. It determines which variables should be plotted to each axis, suggests suitable chart types, and defines metadata such as titles, labels, and legends, without actually rendering the chart. It requires a LangChain Chat Model, a Context Provider, and two separate Prompt Formatters: one for **SQL queries generation** and another for **guiding data preprocessing for visualization**.\n\nWe provide a default [`VizPromptFormatter`](https://github.com/basedosdados/chatbot/blob/d5a1c275183932de52781af6346d06b1c148e675/chatbot/formatters/viz_prompt_formatter.py), which is used internally by the Visualization Agent during **data preprocessing**.\n```python\nfrom langchain.chat_models import init_chat_model\n\nfrom chatbot.assistants import SQLAssistant\nfrom chatbot.contexts import BigQueryContextProvider\nfrom chatbot.formatters import SQLPromptFormatter, VizPromptFormatter\n\nmodel = init_chat_model(\"gpt-4o\", temperature=0)\n\n# you must point the GOOGLE_APPLICATION_CREDENTIALS\n# env variable to your service account JSON file.\ncontext_provider = BigQueryContextProvider(\n    billing_project=\"your billing project\",\n    query_project=\"your query project\",\n)\n\nsql_prompt_formatter = SQLPromptFormatter()\nviz_prompt_formatter = VizPromptFormatter()\n\nassistant = SQLVizAssistant(\n  model, context_provider, sql_prompt_formatter, viz_prompt_formatter\n)\n\nresponse = assistant.invoke(\"hello! what can you tell me about our database?\")\n```\n\nYou can also optionally use a [`PostgresSaver`](https://langchain-ai.github.io/langgraph/reference/checkpoints/#langgraph.checkpoint.postgres.PostgresSaver) checkpointer to add short-term memory to your assistant, and provide langchain vector stores for few-shot prompting during both **SQL generation** and **data preprocessing for visualization**:\n```python\nfrom langchain.chat_models import init_chat_model\n\nfrom langchain_postgres import PGVector\nfrom langgraph.checkpoint.postgres import PostgresSaver\n\nfrom chatbot.assistants import SQLAssistant\nfrom chatbot.contexts import BigQueryContextProvider\nfrom chatbot.formatters import SQLPromptFormatter, VizPromptFormatter\n\nmodel = init_chat_model(\"gpt-4o\", temperature=0)\n\n# you must point the GOOGLE_APPLICATION_CREDENTIALS\n# env variable to your service account JSON file.\ncontext_provider = BigQueryContextProvider(\n    billing_project=\"your billing project\",\n    query_project=\"your query project\",\n)\n\n# it could be any combination of\n# a langchain vector store and an embedding model\nsql_vector_store = PGVector(\n    connection=\"your connection string\",\n    collection_name=\"your sql collection name\",\n    embedding=OpenAIEmbeddings(\n      model=\"text-embedding-3-small\",\n    ),\n)\n\nviz_vector_store = PGVector(\n    connection=\"your connection string\",\n    collection_name=\"your viz collection name\",\n    embedding=OpenAIEmbeddings(\n      model=\"text-embedding-3-small\",\n    ),\n)\n\nsql_prompt_formatter = SQLPromptFormatter(sql_vector_store)\nviz_prompt_formatter = VizPromptFormatter(viz_vector_store)\n\nDB_URI = \"postgresql://postgres:postgres@localhost:5442/postgres\n\nwith PostgresSaver.from_conn_strin(DB_URI) as checkpointer:\n    checkpointer.setup()\n\n    assistant = SQLAssistant(\n        model=model,\n        context_provider=context_provider,\n        sql_prompt_formatter=sql_prompt_formatter,\n        viz_prompt_formatter=viz_prompt_formatter,\n        checkpointer=checkpointer,\n    )\n\n    response = assistant.invoke(\n        message=\"hello! what can you tell me about our database?\",\n        thread_id=\"some uuid\"\n    )\n```\nAn async version is also available: [`AsyncSQLVizAssistant`](https://github.com/basedosdados/chatbot/blob/d5a1c275183932de52781af6346d06b1c148e675/chatbot/assistants/async_sql_viz_assistant.py).\n\n\u003e [!TIP]\n\u003e To improve semantic search when using vector stores, you can enable query rewriting by setting `rewrite_query=True` when invoking the assistants or the `SQLAgent` directly.\n\n## Extensibility\nUnder the hood, both assistants rely on composable agents:\n\n- [`SQLAgent`](https://github.com/basedosdados/chatbot/blob/d5a1c275183932de52781af6346d06b1c148e675/chatbot/agents/sql_agent.py) – Handles database metadata retrieval, query generation and execution.\n- [`VizAgent`](https://github.com/basedosdados/chatbot/blob/d5a1c275183932de52781af6346d06b1c148e675/chatbot/agents/visualization_agent.py) – Handles visualization reasoning.\n- [`RouterAgent`](https://github.com/basedosdados/chatbot/blob/d5a1c275183932de52781af6346d06b1c148e675/chatbot/agents/router_agent.py) – Orchestrates SQL querying and data visualization via a multi-agent workflow..\n\nThere is also an implementation of a simple [`ReActAgent`](https://github.com/basedosdados/chatbot/blob/d5a1c275183932de52781af6346d06b1c148e675/chatbot/agents/react_agent.py) with support to custom system prompts and short-term memory, to which you can add an arbitrary set of tools.\n\nYou can directly use these agents or use them to create your own workflows.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbasedosdados%2Fchatbot","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbasedosdados%2Fchatbot","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbasedosdados%2Fchatbot/lists"}