Skip to content

Commit

Permalink
Update README
Browse files Browse the repository at this point in the history
Update cli quota
  • Loading branch information
wongjingping committed Mar 25, 2024
1 parent 8ce0096 commit 2d72b3c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 37 deletions.
65 changes: 41 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ Defog converts your natural language text queries into SQL and other machine rea

https://user-images.githubusercontent.com/4327467/236758074-042bc5d7-4452-46ce-bb26-e2da2a0223c6.mp4

# CLI

# Installation
## Installation
For a generic installation with Postgres or Redshift binaries, use
`pip install --upgrade defog`

Expand All @@ -23,21 +24,17 @@ For a BigQuery installation, use
For a Databricks installation, use
`pip install --upgrade 'defog[databricks]'`

# Getting your API Key
You can get your API key by going to [https://defog.ai/signup](https://defog.ai/signup) and creating an account.

# Integration

You can either use our cli, which will take you through the setup step-by-step, or pass it in explicitly in python to the `Defog` class. The CLI uses the python api's behind the hood, and is just an interactive wrapper over it that does some extra validation on your behalf.
## API Key
You can get your API key by going to [https://defog.ai/signup](https://defog.ai/signup) and creating an account. If you fail to verify your email, you can email us at support(at)defog.ai

## Connection Setup
To get started, you can run the following cli command, which will prompt you for your defog api key, database type, and the corresponding database credentials required.
You can either use our command line interface (CLI), which will take you through the setup step-by-step, or pass it in explicitly in python to the `Defog` class. The CLI uses the python api's behind the hood, and is just an interactive wrapper over it that does some extra validation on your behalf.

To get started, you can run the following CLI command, which will prompt you for your defog api key, database type, and the corresponding database credentials required.
```
defog init
```
If this is your first time running, we will write these information into a json config file, which will be stored in `~/.defog/connection.json`. If we detect a file present already, we will ask you if you intend to re-initialize the file. You can always delete the file and `defog init` all over again. Note that your credentials are _never_ sent to defog's servers.

Once you have setup the connection settings, we will ask you for the names of the tables that you would like to register (space separated), generate the schema for each of them, upload the schema to defog, and print out the filename of a CSV with your metadata. If you do not wish to provide those at this point, you can exit this prompt by hitting `ctrl+c`

## Generating your schema
Expand All @@ -63,45 +60,65 @@ defog query "<your query>"
```
Happy querying!

## Quota
## Glossary

You can check your quota usage by running:
You might notice that sometimes our model fails to take into account some prior context for your own domain, eg converting certain fields into different types, joining certain tables, how to perform string matching, etc. To give the model a standard set of instructions attached to each query, you can pass us a `glossary`, which is basically just a string blob of up to 1000 characters that gives our model more specific instructions. You can manage your glossary using the following commands:
```
defog quota
defog glossary update <path/to/glossary.txt> # Update your glossary
defog glossary get # Get your current glossary
defog glossary delete # Delete your glossary
```
Free-tier users have 1000 queries per month, while premium users have unlimited queries.

# Usage
## Golden Queries

You can use the API from within Python
In certain cases where the generated query follows a complex pattern, you can provide certain examples to our model to help it generate according to your desired patterns. You can manage your golden queries using the following commands:
```
defog golden get <json|csv> # Get your golden queries in JSON or CSV format
defog golden add <path/to/golden_queries.json> # Add golden queries from a JSON or CSV file
defog golden delete <path/to/golden_queries.json|all> # Delete specific golden queries or all of them
```
Note that when adding golden queries, the json/csv file provided needs to have the following keys/columns:
- prev_question (optional): the existing question in the database if we're replacing a golden question-query pair
- prev_sql (optional): the existing SQL in the database if we're replacing a golden question-query pair
- question: the new question
- sql: the new SQL

## Deploying
You can deploy a defog server as a cloud function using the following command:
```
defog deploy <gcp|aws> [function_name] # Deploy to GCP or AWS, optionally specifying the function name
```

## Quota

You can check your quota usage per month by running:
```
from defog import Defog
defog quota
```
Free-tier users have 1000 queries per month, while premium users have unlimited queries.

# Python Client
You can use the API from within Python as below
```py
from defog import Defog
# your credentials are never sent to our server, and always run locally
defog = Defog() # your credentials will automatically be loaded after you install defog
defog = Defog() # your credentials will automatically be loaded if you have initialized defog already
question = "question asked by a user"
# run chat version of query
results = defog.run_query(
question=question,
)
print(results)
```

# Testing

For developers who want to test or add tests for this client, you can run:
```
pytest tests
```

Note that we will transfer the existing .defog/connection.json file over to /tmp (if at all), and transfer the original file back once the tests are done to avoid messing with the original config.

If submitting a PR, please use the `black` linter to lint your code. You can add it as a git hook to your repo by running the command below:
```bash
echo -e '#!/bin/sh\n#\n# Run linter before commit\nblack $(git rev-parse --show-toplevel)' > .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
```
```
16 changes: 9 additions & 7 deletions defog/admin_methods.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from typing import Dict, Optional
import requests
import pandas as pd

Expand Down Expand Up @@ -127,14 +128,15 @@ def get_feedback(self, n_rows: int = 50, start_from: int = 0):
return df.iloc[start_from:].head(n_rows).to_markdown(index=False)


def get_quota(self) -> str:
headers = {
"Authorization": f"Bearer {self.api_key}",
}
response = requests.get(
f"{self.base_url}/quota",
headers=headers,
def get_quota(self) -> Optional[Dict]:
api_key = self.api_key
response = requests.post(
f"{self.base_url}/check_api_usage",
json={"api_key": api_key},
)
# get status code and return None if not 200
if response.status_code != 200:
return None
return response.json()


Expand Down
12 changes: 6 additions & 6 deletions defog/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,15 +728,15 @@ def quota():
"""
df = defog.Defog()
resp = df.get_quota()
if resp["ran_successfully"]:
if resp:
if resp["premium"]:
print(f"You are currently on the premium plan with unrestricted usage.")
print(f"Your current usage is {resp['queries_made']} queries.")
else:
print(
f"You are currently on the free plan with {1000-resp['queries_made']} queries remaining for the month."
)
print(f"Your current usage is {resp['queries_made']} queries.")
print(f"You are currently on the free plan with 1000 queries per month.")
print("Usage so far:")
print(f"{'Month':<10}{'API Calls':<10}")
for month, api_calls in resp["data"]:
print(f"{month}: {api_calls}")
else:
print(f"Failed to get quota")

Expand Down

0 comments on commit 2d72b3c

Please sign in to comment.