Skip to main content

Quick Start for Python SDK

Introduction

The AskTable Python SDK provides a set of concise interfaces that allow you to easily integrate data analysis and question-answering capabilities into your application. Through this guide, you will learn how to use the SDK to complete the entire workflow from creating a data source to intelligent Q&A.

Other references:

Notes

  • If the AskTable Python SDK is not installed, please install it first, refer to the installation guide.

Usage Examples

AskTable supports two types of data sources: file type (Excel or CSV) and database type (such as MySQL, PostgreSQL, etc.).

The following steps are included:

  1. Initialize the SDK client
  2. Create a data source
  3. Build metadata
  4. Create an AI Data Assistant (Bot)
  5. Create a conversation (Chat)
  6. Send a question and get the answer

Complete Code Example

  1. Initialize SDK
from asktable import Asktable

# Initialize SDK
api_key = 'API_KEY'

# If using SaaS
client = Asktable(api_key=api_key)

# # If it's a private deployment
# client = Asktable(base_url='http://192.168.3.5:8000/api', api_key=api_key)

Where API_KEY is the key to access the AskTable API.

  1. Create Data Source
  • File Type

    Create a data source:

    datasource = client.datasources.create(
    engine="excel",
    name="example_excel"
    )

    Add a file to the data source:

    with open(file_path, "rb") as file_obj:
    file_id = client.datasources.add_file(
    datasource_id=datasource.id,
    file=file_obj
    )
  • Database Type

    Create a data source of database type:

    datasource = client.datasources.create(
    engine="mysql",
    name="example_mysql",
    access_config={
    "host": "your_mysql_host",
    "port": 3306,
    "user": "your_username",
    "password": "your_password",
    "db": "your_database",
    },
    )
  1. Create Metadata
meta = client.datasources.meta.create(
datasource_id=datasource.id,
name="example_meta"
)
  1. Create AI Data Assistant
bot = client.bots.create(
datasource_ids=[datasource.id],
name="example_bot"
)
  1. Create Conversation
chat = client.chats.create(
bot_id=bot.id
)
  1. Send Question and Get Answer
message = client.chats.messages.create(
chat_id=chat.id,
question="Zhang San's score"
)
print(message.content.text)