https://github.com/basedosdados/chatbot

https://github.com/basedosdados/chatbot

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (11.7%) to scientific vocabulary
Last synced: 11 months ago · JSON representation

Repository

Basic Info
  • Host: GitHub
  • Owner: basedosdados
  • License: mit
  • Language: Python
  • Default Branch: main
  • Size: 582 KB
Statistics
  • Stars: 0
  • Watchers: 4
  • Forks: 0
  • Open Issues: 12
  • Releases: 19
Created over 1 year ago · Last pushed 12 months ago
Metadata Files
Readme License

README.md

Chatbot

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 and LangGraph and provides agents and high-level assistants for natural language querying and data visualization.

[!NOTE] This library is still under active development. Expect breaking changes, incomplete features, and limited documentation.

Installation

Clone the repository and install it (you can also use poetry or uv instead of pip). bash git clone https://github.com/basedosdados/chatbot.git cd chatbot pip install .

Assistants

SQLAssistant

The SQLAssistant allows LLMs to interact with your database so you can ask questions about it. All it needs is a LangChain Chat Model, a Context Provider and a Prompt Formatter. 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.

We provide a default BigQueryContextProvider for retrieving metadata directly from Google BigQuery and a default SQLPromptFormatter. You can supply your own implementation of a context provider and a prompt formatter for custom behavior. ```python from langchain.chatmodels import initchat_model

from chatbot.assistants import SQLAssistant from chatbot.contexts import BigQueryContextProvider from chatbot.formatters import SQLPromptFormatter

model = initchatmodel("gpt-4o", temperature=0)

you must point the GOOGLEAPPLICATIONCREDENTIALS

env variable to your service account JSON file.

contextprovider = BigQueryContextProvider( billingproject="your billing project", query_project="your query project", )

prompt_formatter = SQLPromptFormatter()

assistant = SQLAssistant(model, contextprovider, promptformatter)

response = assistant.invoke("Hello! what can you tell me about our database?") ```

You can optionally use a PostgresSaver checkpointer to add short-term memory to your assistant and a VectorStore for few-shot prompting during SQL query generation: ```python from langchain.chatmodels import initchat_model

from langchain_postgres import PGVector from langgraph.checkpoint.postgres import PostgresSaver

from chatbot.assistants import SQLAssistant from chatbot.contexts import BigQueryContextProvider from chatbot.formatters import SQLPromptFormatter

model = initchatmodel("gpt-4o", temperature=0)

you must point the GOOGLEAPPLICATIONCREDENTIALS

env variable to your service account JSON file.

contextprovider = BigQueryContextProvider( billingproject="your billing project", query_project="your query project", )

it could be any combination of

a langchain vector store and an embedding model

vectorstore = PGVector( connection="your connection string", collectionname="your collection name", embedding=OpenAIEmbeddings( model="text-embedding-3-small", ), )

promptformatter = SQLPromptFormatter(vectorstore)

DB_URI = "postgresql://postgres:postgres@localhost:5442/postgres"

with PostgresSaver.fromconnstring(DB_URI) as checkpointer: checkpointer.setup()

assistant = SQLAssistant(
    model=model,
    context_provider=context_provider,
    prompt_formatter=prompt_formatter,
    checkpointer=checkpointer,
)

response = assistant.invoke(
    message="Hello! what can you tell me about our database?",
    thread_id="some uuid"
)

```

An async version is also available: AsyncSQLAssistant.

SQLVizAssistant

SQLVizAssistant extends SQLAssistant by retrieving data and preparing it for visualization. Using pandas and plotly, it generates a Python script that creates visualizations from the retrieved data. All it needs is a LangChain chat model, a context provider, and a prompt formatter for the SQL query generation step.

```python from langchain.chatmodels import initchat_model

from chatbot.assistants import SQLAssistant from chatbot.contexts import BigQueryContextProvider from chatbot.formatters import SQLPromptFormatter

model = initchatmodel("gpt-4o", temperature=0)

you must point the GOOGLEAPPLICATIONCREDENTIALS

env variable to your service account JSON file.

contextprovider = BigQueryContextProvider( billingproject="your billing project", query_project="your query project", )

prompt_formatter = SQLPromptFormatter()

assistant = SQLVizAssistant(model, contextprovider, promptformatter)

response = assistant.invoke("Hello! what can you tell me about our database?") ```

You can also optionally use a PostgresSaver checkpointer to add short-term memory to your assistant, and provide langchain vector stores for few-shot prompting during SQL query generation, just like we did with the SQLAssistant: ```python from langchain.chatmodels import initchat_model

from langchain_postgres import PGVector from langgraph.checkpoint.postgres import PostgresSaver

from chatbot.assistants import SQLAssistant from chatbot.contexts import BigQueryContextProvider from chatbot.formatters import SQLPromptFormatter

model = initchatmodel("gpt-4o", temperature=0)

you must point the GOOGLEAPPLICATIONCREDENTIALS

env variable to your service account JSON file.

contextprovider = BigQueryContextProvider( billingproject="your billing project", query_project="your query project", )

it could be any combination of

a langchain vector store and an embedding model

vectorstore = PGVector( connection="your connection string", collectionname="your sql collection name", embedding=OpenAIEmbeddings( model="text-embedding-3-small", ), )

promptformatter = SQLPromptFormatter(vectorstore)

DB_URI = "postgresql://postgres:postgres@localhost:5442/postgres"

with PostgresSaver.fromconnstring(DB_URI) as checkpointer: checkpointer.setup()

assistant = SQLVizAssistant(
    model=model,
    context_provider=context_provider,
    sql_prompt_formatter=prompt_formatter,
    checkpointer=checkpointer,
)

response = assistant.invoke(
    message="Hello! what can you tell me about our database?",
    thread_id="some uuid"
)

`` An async version is also available: [AsyncSQLVizAssistant`](https://github.com/basedosdados/chatbot/blob/d5a1c275183932de52781af6346d06b1c148e675/chatbot/assistants/asyncsqlviz_assistant.py).

[!TIP] To improve semantic search when using vector stores, you can enable query rewriting by setting rewrite_query=True when invoking the assistants.

Extensibility

Under the hood, both assistants rely on composable agents:

  • SQLAgent – Handles database metadata retrieval, query generation and execution.
  • VizAgent – Handles visualization reasoning.
  • RouterAgent – Orchestrates SQL querying and data visualization via a multi-agent workflow.

There is also an implementation of a simple ReActAgent with support to custom system prompts and short-term memory, to which you can add an arbitrary set of tools.

You can directly use these agents or use them to create your own workflows.

Owner

  • Name: Base dos Dados
  • Login: basedosdados
  • Kind: organization
  • Email: contato@basedosdados.org
  • Location: Brazil

Universalizando o acesso a dados de qualidade

GitHub Events

Total
  • Create event: 19
  • Issues event: 12
  • Release event: 7
  • Delete event: 18
  • Issue comment event: 5
  • Push event: 49
  • Public event: 1
  • Pull request event: 29
Last Year
  • Create event: 19
  • Issues event: 12
  • Release event: 7
  • Delete event: 18
  • Issue comment event: 5
  • Push event: 49
  • Public event: 1
  • Pull request event: 29

Committers

Last synced: about 1 year ago

All Time
  • Total Commits: 129
  • Total Committers: 2
  • Avg Commits per committer: 64.5
  • Development Distribution Score (DDS): 0.008
Past Year
  • Commits: 129
  • Committers: 2
  • Avg Commits per committer: 64.5
  • Development Distribution Score (DDS): 0.008
Top Committers
Name Email Commits
vrtornisiello v****o@g****m 128
Ricardo Dahis 6****s 1

Issues and Pull Requests

Last synced: about 1 year ago

All Time
  • Total issues: 9
  • Total pull requests: 22
  • Average time to close issues: 40 minutes
  • Average time to close pull requests: about 2 hours
  • Total issue authors: 2
  • Total pull request authors: 1
  • Average comments per issue: 0.22
  • Average comments per pull request: 0.0
  • Merged pull requests: 22
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 9
  • Pull requests: 22
  • Average time to close issues: 40 minutes
  • Average time to close pull requests: about 2 hours
  • Issue authors: 2
  • Pull request authors: 1
  • Average comments per issue: 0.22
  • Average comments per pull request: 0.0
  • Merged pull requests: 22
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • rdahis (13)
  • vrtornisiello (1)
Pull Request Authors
  • vrtornisiello (41)
Top Labels
Issue Labels
enhancement (8) bug (1)
Pull Request Labels

Dependencies

.github/workflows/ci.yaml actions
  • actions/checkout v4 composite
  • actions/setup-python v5 composite
poetry.lock pypi
  • 151 dependencies
pyproject.toml pypi
  • pytest ^8.3.5 develop
  • pytest-asyncio ^1.0.0 develop
  • pytest-cov ^6.1.1 develop
  • google-cloud-bigquery (>=3.33.0,<4.0.0)
  • grpcio (==1.71.0)
  • langchain (==0.3.25)
  • langchain-chroma (==0.2.2)
  • langchain-google-vertexai (==2.0.24)
  • langchain-openai (==0.3.18)
  • langgraph (==0.4.7)
  • langgraph-checkpoint-postgres (==2.0.21)
  • loguru (>=0.7.3,<0.8.0)
  • psycopg (==3.2.9)
  • psycopg-pool (==3.2.6)
  • sqlparse (>=0.5.3,<0.6.0)