chore: import upstream snapshot with attribution

This commit is contained in:
wehub-resource-sync
2026-07-13 12:42:37 +08:00
commit 969b3b615d
658 changed files with 160978 additions and 0 deletions
+79
View File
@@ -0,0 +1,79 @@
name: 🐛 Bug Report
description: Create a report to help us reproduce and fix the bug
body:
- type: markdown
attributes:
value: >
#### Before submitting a bug, please make sure the issue hasn't been already addressed by searching through [the
existing and past issues](https://github.com/meta-llama/llama-cookbook/issues), the [FAQ](https://github.com/meta-llama/llama-cookbook/blob/main/src/docs/FAQ.md)
- type: textarea
id: system-info
attributes:
label: System Info
description: |
Please share your system info with us. You can use the following command to capture your environment information
python -m "torch.utils.collect_env"
placeholder: |
PyTorch version, CUDA version, GPU type, #num of GPUs...
validations:
required: true
- type: checkboxes
id: information-scripts-examples
attributes:
label: Information
description: 'The problem arises when using:'
options:
- label: "The official example scripts"
- label: "My own modified scripts"
- type: textarea
id: bug-description
attributes:
label: 🐛 Describe the bug
description: |
Please provide a clear and concise description of what the bug is.
Provide the exact command(s) that you ran with the settings eg using FSDP and PEFT or pure FSDP.
Please also paste or describe the results you observe instead of the expected results.
placeholder: |
A clear and concise description of what the bug is.
```python
# Command that you used for running the examples
```
Description of the results
validations:
required: true
- type: textarea
attributes:
label: Error logs
description: |
If you observe an error, please paste the error message including the **full** traceback of the exception. It may be relevant to wrap error messages in ```` ```triple quotes blocks``` ````.
placeholder: |
```
The error message you got, with the full traceback.
```
validations:
required: true
- type: textarea
id: expected-behavior
validations:
required: true
attributes:
label: Expected behavior
description: "A clear and concise description of what you would expect to happen."
- type: markdown
attributes:
value: >
Thanks for contributing 🎉!
@@ -0,0 +1,31 @@
name: 🚀 Feature request
description: Submit a proposal/request for a new llama-cookbook feature
body:
- type: textarea
id: feature-pitch
attributes:
label: 🚀 The feature, motivation and pitch
description: >
A clear and concise description of the feature proposal. Please outline the motivation for the proposal. Is your feature request related to a specific problem? e.g., *"I'm working on X and would like Y to be possible"*. If this is related to another GitHub issue, please link here too.
validations:
required: true
- type: textarea
id: alternatives
attributes:
label: Alternatives
description: >
A description of any alternative solutions or features you've considered, if any.
- type: textarea
id: additional-context
attributes:
label: Additional context
description: >
Add any other context or screenshots about the feature request.
- type: markdown
attributes:
value: >
Thanks for contributing 🎉!
+38
View File
@@ -0,0 +1,38 @@
# What does this PR do?
<!--
Congratulations! You've made it this far! You're not quite done yet though.
Please include a good title that fully reflects the extent of your awesome contribution.
Then, please replace this with a description of the change and which issue is fixed (if applicable). Please also include relevant motivation and context. List any dependencies (if any) that are required for this change.
-->
<!-- Remove if not applicable -->
Fixes # (issue)
## Feature/Issue validation/testing
Please describe the tests that you ran to verify your changes and relevant result summary. Provide instructions so it can be reproduced.
Please also list any relevant details for your test configuration.
- [ ] Test A
Logs for Test A
- [ ] Test B
Logs for Test B
## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [ ] Did you read the [contributor guideline](https://github.com/meta-llama/llama-cookbook/blob/main/CONTRIBUTING.md),
Pull Request section?
- [ ] Was this discussed/approved via a Github issue? Please add a link
to it if that's the case.
- [ ] Did you make sure to update the documentation with your changes?
- [ ] Did you write any new necessary tests?
Thanks for contributing 🎉!
+27
View File
@@ -0,0 +1,27 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# This software may be used and distributed according to the terms of the Llama 2 Community License Agreement.
import re
from pathlib import Path
WORK_DIR = Path(__file__).parents[1]
PATTERN = "(Meta Platforms, Inc. and affiliates)|(Facebook, Inc(\.|,)? and its affiliates)|([0-9]{4}-present(\.|,)? Facebook)|([0-9]{4}(\.|,)? Facebook)"
HEADER = """# Copyright (c) Meta Platforms, Inc. and affiliates.
# This software may be used and distributed according to the terms of the Llama 2 Community License Agreement.\n\n"""
#Files in black list must be relative to main repo folder
BLACKLIST = ["tools/benchmarks/llm_eval_harness/open_llm_leaderboard/hellaswag_utils.py"]
if __name__ == "__main__":
for ext in ["*.py", "*.sh"]:
for file in WORK_DIR.rglob(ext):
normalized = file.relative_to(WORK_DIR)
if normalized.as_posix() in BLACKLIST:
continue
text = file.read_text()
if not re.search(PATTERN, text):
text = HEADER + text
file.write_text(text)
@@ -0,0 +1,33 @@
{
"retryOn429": true,
"retryCount": 5,
"fallbackRetryDelay": "10s",
"httpHeaders": [
{
"urls": [
"https://docs.github.com/",
"https://help.github.com/"
],
"headers": {
"Accept-Encoding": "zstd, br, gzip, deflate"
}
}
],
"ignorePatterns": [
{
"pattern": "^http(s)?://127.0.0.1.*"
},
{
"pattern": "^http(s)?://localhost.*"
},
{
"pattern": "https://www.intel.com/content/www/us/en/developer/articles/news/llama2.html"
},
{
"pattern": "http(s.?)?:\/\/(www\.)?linkedin\.com\.*"
},
{
"pattern": "http(s?)?:\/\/(www\.)?medium\.com\/.*"
}
]
}
+23
View File
@@ -0,0 +1,23 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# This software may be used and distributed according to the terms of the Llama 2 Community License Agreement.
# Source: https://github.com/pytorch/torchx/blob/main/scripts/spellcheck.sh
set -ex
sudo apt-get install aspell
if [[ -z "$@" ]]; then
sources=$(find -name '*.md')
else
sources=$@
fi
sources_arg=""
for src in $sources; do
sources_arg="${sources_arg} -S $src"
done
if [ ! "$sources_arg" ]; then
echo "No files to spellcheck"
else
pyspelling -c .github/scripts/spellcheck_conf/spellcheck.yaml --name Markdown $sources_arg
fi
@@ -0,0 +1,22 @@
matrix:
- name: Markdown
apsell:
lang: en
d: en_US
dictionary:
wordlists:
- .github/scripts/spellcheck_conf/wordlist.txt
output: .github/scripts/spellcheck_conf/wordlist.dic
encoding: utf-8
pipeline:
- pyspelling.filters.context:
context_visible_first: true
delimiters:
- open: '(?s)^ *(?P<open>`{3,})[a-z0-9]*?$'
close: '^(?P=open)$'
- open: ''
content: 'https?://[-a-zA-Z0-9.]+?\.[a-z]{2,6}[-?=&%.0-9a-zA-Z/_#]*'
close: ''
- pyspelling.filters.markdown:
markdown_extensions:
- markdown.extensions.extra:
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,75 @@
name: "[GHA][CPU] llama-cookbook Pytest tests on CPU GitHub hosted runner."
on:
pull_request:
branches:
- 'main'
# triggers workflow manually for debugging purposes.
workflow_dispatch:
inputs:
runner:
description: 'GHA Runner Scale Set label to run workflow on.'
required: true
default: ubuntu-24.04
debug:
description: 'Run debugging steps?'
required: false
default: "true"
env:
PYTORCH_WHEEL_URL: https://download.pytorch.org/whl/test/cu118
jobs:
execute_workflow:
name: Execute workload on GHA CPU Runner
defaults:
run:
shell: bash # default shell to run all steps for a given job.
runs-on: ${{ github.event.inputs.runner != '' && github.event.inputs.runner || 'ubuntu-24.04' }}
steps:
- name: "[DEBUG] Get runner container OS information"
id: os_info
if: ${{ github.event.inputs.debug == 'true' }}
run: |
cat /etc/os-release
- name: "Checkout 'meta-llama/llama-cookbook' repository"
id: checkout
uses: actions/checkout@v4
- name: "[DEBUG] Content of the repository after checkout"
id: content_after_checkout
if: ${{ github.event.inputs.debug == 'true' }}
run: |
ls -la ${GITHUB_WORKSPACE}
- name: "Installing Python dependencies"
id: python_dependencies
run: |
pip3 install --upgrade pip
pip3 install setuptools
- name: "Installing 'llama-cookbook' project"
id: install_llama_cookbook_package
run: |
echo "Installing 'llama-cookbook' project (re: https://github.com/meta-llama/llama-cookbook/tree/main/src?tab=readme-ov-file#install-with-optional-dependencies)"
pip install --extra-index-url ${PYTORCH_WHEEL_URL} -e '.[tests]'
- name: "Running PyTest tests on GHA CPU Runner"
id: pytest
run: |
echo "Running PyTest tests at 'GITHUB_WORKSPACE' path: ${GITHUB_WORKSPACE}"
cd $GITHUB_WORKSPACE && python3 -m pytest --junitxml="$GITHUB_WORKSPACE/pytest_result.xml"
- name: Publish Test Summary
id: test_summary
uses: test-summary/action@v2
with:
paths: |
**/pytest_result.xml
if: always()
+66
View File
@@ -0,0 +1,66 @@
name: SpellCheck
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
build:
runs-on: ubuntu-24.04
name: Lint changed files
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0 # OR "2" -> To retrieve the preceding commit.
- name: Check links in all markdown files
uses: gaurav-nelson/github-action-markdown-link-check@1.0.13
with:
use-verbose-mode: 'yes'
config-file: ".github/scripts/markdown_link_check_config.json"
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v45.0.8
with:
files: |
**/*.py
spellcheck:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v3
- name: Install dependencies
run: |
sudo apt-get install aspell aspell-en
pip install pyspelling
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v45.0.8
with:
files: |
**/*.md
- name: Check spellings
run: |
sources=""
for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
sources="${sources} -S $file"
done
if [ ! "$sources" ]; then
echo "No files to spellcheck"
else
pyspelling -c $GITHUB_WORKSPACE/.github/scripts/spellcheck_conf/spellcheck.yaml --name Markdown $sources
fi
- name: In the case of misspellings
if: ${{ failure() }}
run: |
echo "Please fix the misspellings. If you are sure about some of them, "
echo "so append those to .github/scripts/spellcheck_conf/wordlist.txt"
+6
View File
@@ -0,0 +1,6 @@
.DS_Store
__pycache__
.ipynb_checkpoints
wandb/
artifacts/
node_modules/
+70
View File
@@ -0,0 +1,70 @@
<h1 align="center"> Llama 3P Integrations </h1>
<p align="center">
<a href="https://llama.developer.meta.com/join_waitlist?utm_source=llama-cookbook&utm_medium=readme&utm_campaign=3p_integrations"><img src="https://img.shields.io/badge/Llama_API-Join_Waitlist-brightgreen?logo=meta" /></a>
<a href="https://llama.developer.meta.com/docs?utm_source=llama-cookbook&utm_medium=readme&utm_campaign=3p_integrations"><img src="https://img.shields.io/badge/Llama_API-Documentation-4BA9FE?logo=meta" /></a>
</p>
<p align="center">
<a href="https://github.com/meta-llama/llama-models/blob/main/models/?utm_source=llama-cookbook&utm_medium=readme&utm_campaign=3p_integrations"><img alt="Llama Model cards" src="https://img.shields.io/badge/Llama_OSS-Model_cards-green?logo=meta" /></a>
<a href="https://www.llama.com/docs/overview/?utm_source=llama-cookbook&utm_medium=readme&utm_campaign=3p_integrations"><img alt="Llama Documentation" src="https://img.shields.io/badge/Llama_OSS-Documentation-4BA9FE?logo=meta" /></a>
<a href="https://huggingface.co/meta-llama"><img alt="Hugging Face meta-llama" src="https://img.shields.io/badge/Hugging_Face-meta--llama-yellow?logo=huggingface" /></a>
</p>
<p align="center">
<a href="https://github.com/meta-llama/synthetic-data-kit"><img alt="Llama Tools Syntethic Data Kit" src="https://img.shields.io/badge/Llama_Tools-synthetic--data--kit-orange?logo=meta" /></a>
<a href="https://github.com/meta-llama/llama-prompt-ops"><img alt="Llama Tools Syntethic Data Kit" src="https://img.shields.io/badge/Llama_Tools-llama--prompt--ops-orange?logo=meta" /></a>
</p>
This folder contains example scripts and tutorials showcasing the integration of Meta Llama models with popular platforms, frameworks, and tools in the LLM ecosystem. These integrations demonstrate how to leverage Llama's capabilities across different environments and use cases.
Each folder is maintained by the respective platform-owner and contains specific examples, tutorials, and documentation for using Llama with that platform.
> [!NOTE]
> If you'd like to add your platform here, please open a new issue with details of your examples.
## Available Integrations
### [AWS](./aws)
Examples for using Llama 3 on Amazon Bedrock, including getting started guides, prompt engineering, and React integration.
### [Azure](./azure)
Recipes for running Llama model inference on Azure's serverless API offerings (MaaS).
### [Crusoe](./crusoe)
Recipes for deploying Llama workflows on Crusoe's high-performance, sustainable cloud, including serving Llama3.1 in FP8 with vLLM.
### [E2B AI Analyst](./e2b-ai-analyst)
AI-powered code and data analysis tool using Meta Llama and the E2B SDK, supporting data analysis, CSV uploads, and interactive charts.
### [Groq](./groq)
Examples and templates for using Llama models with Groq's high-performance inference API.
### [Lamini](./lamini)
Integration examples with Lamini's platform, including text2sql with memory tuning.
### [LangChain](./langchain)
Cookbooks for building agents with Llama 3 and LangChain, including tool-calling agents and RAG agents using LangGraph.
### [LlamaIndex](./llamaindex)
Examples of using Llama with LlamaIndex for advanced RAG applications and agentic RAG.
### [Modal](./modal)
Integration with Modal's cloud platform for running Llama models, including human evaluation examples.
### [TGI](./tgi)
Guide for serving fine-tuned Llama models with HuggingFace's text-generation-inference server, including weight merging for LoRA models.
### [TogetherAI](./togetherai)
Comprehensive demos for building LLM applications using Llama on Together AI, including multimodal RAG, contextual RAG, PDF-to-podcast conversion, knowledge graphs, and structured text extraction.
### [vLLM](./vllm)
Examples for high-throughput and memory-efficient inference using vLLM with Llama models.
## Additional Resources
### [Using Externally Hosted LLMs](./using_externally_hosted_llms.ipynb)
Guide for working with Llama models hosted on external platforms.
### [Llama On-Prem](./llama_on_prem.md)
Information about on-premises deployment of Llama models.
@@ -0,0 +1,306 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "lbfIu_3eEaAh"
},
"source": [
"# Using Amazon Bedrock with Llama\n",
"\n",
"<a href=\"https://colab.research.google.com/github/meta-llama/llama-cookbook/blob/main/3p-integrations/aws/getting_started_llama_3_on_amazon_bedrock.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>\n",
"\n",
"Use this notebook to quickly get started with Llama on Bedrock. You can access the Amazon Bedrock API using the AWS Python SDK.\n",
"\n",
"In this notebook, we will give you some simple code to confirm to get up and running with the AWS Python SDK, setting up credentials, looking up the list of available Meta Llama models, and using bedrock to inference.\n",
"\n",
"### Resources\n",
"Set up the Amazon Bedrock API - https://docs.aws.amazon.com/bedrock/latest/userguide/api-setup.html\n",
"\n",
"### To connect programmatically to an AWS service, you use an endpoint. Amazon Bedrock provides the following service endpoints:\n",
"\n",
"* **bedrock** Contains control plane APIs for managing, training, and deploying models.\n",
"* **bedrock-runtime** Contains runtime plane APIs for making inference requests for models hosted in Amazon Bedrock.\n",
"* **bedrock-agent** Contains control plane APIs for creating and managing agents and knowledge bases.\n",
"* **bedrock-agent-runtime** Contains control plane APIs for managing, training, and deploying models.\n",
"\n",
"### Prerequisite\n",
"Before you can access Amazon Bedrock APIs, you will need an AWS Account, and you will need to request access to the foundation models that you plan to use. For more information on model access - https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html\n",
"\n",
"#### Setting up the AWS CLI (TBD)\n",
"https://docs.aws.amazon.com/bedrock/latest/userguide/api-setup.html#api-using-cli-prereq\n",
"\n",
"#### Setting up an AWS SDK\n",
"https://docs.aws.amazon.com/bedrock/latest/userguide/api-setup.html#api-sdk\n",
"\n",
"#### Using SageMaker Notebooks\n",
"https://docs.aws.amazon.com/bedrock/latest/userguide/api-setup.html#api-using-sage\n",
"\n",
"For more information on Amazon Bedrock, please refer to the official documentation here: https://docs.aws.amazon.com/bedrock/"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"id": "gVz1Y1HpxWdv"
},
"outputs": [],
"source": [
"# install packages\n",
"# !python3 -m pip install -qU boto3\n",
"from getpass import getpass\n",
"from urllib.request import urlopen\n",
"import boto3\n",
"import json"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Security Note\n",
"\n",
"For this notebook, we will use `getpass()` to reference your AWS Account credentials. This is just to help you get-started with this notebook more quickly. Otherwise, the we recommend that you avoid using getpass for your AWS credentials in a Jupyter notebook. It's not secure to expose your AWS credentials in this way. Instead, consider using AWS IAM roles or environment variables to securely handle your credentials.\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "JHu-V-4ayNjB",
"outputId": "4a1e856b-3ab1-480c-97fd-81a9b9e3724b"
},
"outputs": [],
"source": [
"\n",
"# Set default AWS region\n",
"default_region = \"us-east-1\"\n",
"\n",
"# Get AWS credentials from user input (not recommended for production use)\n",
"AWS_ACCESS_KEY = getpass(\"AWS Access key: \")\n",
"AWS_SECRET_KEY = getpass(\"AWS Secret key: \")\n",
"SESSION_TOKEN = getpass(\"AWS Session token: \")\n",
"AWS_REGION = input(f\"AWS Region [default: {default_region}]: \") or default_region\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"def create_bedrock_client(service_name):\n",
" \"\"\"\n",
" Create a Bedrock client using the provided service name and global AWS credentials.\n",
" \"\"\"\n",
" return boto3.client(\n",
" service_name=service_name,\n",
" region_name=AWS_REGION,\n",
" aws_access_key_id=AWS_ACCESS_KEY,\n",
" aws_secret_access_key=AWS_SECRET_KEY,\n",
" aws_session_token=SESSION_TOKEN\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"def list_all_meta_bedrock_models(bedrock):\n",
" \"\"\"\n",
" List all Meta Bedrock models using the provided Bedrock client.\n",
" \"\"\"\n",
" try:\n",
" list_models = bedrock.list_foundation_models(byProvider='meta')\n",
" print(\"\\n\".join(list(map(lambda x: f\"{x['modelName']} : { x['modelId'] }\", list_models['modelSummaries']))))\n",
" except Exception as e:\n",
" print(f\"Failed to list models: {e}\")"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"def invoke_model(bedrock_runtime, model_id, prompt, max_gen_len=256):\n",
" \"\"\"\n",
" Invoke a model with a given prompt using the provided Bedrock Runtime client.\n",
" \"\"\"\n",
" body = json.dumps({\n",
" \"prompt\": prompt,\n",
" \"temperature\": 0.1,\n",
" \"top_p\": 0.9,\n",
" \"max_gen_len\":max_gen_len,\n",
" })\n",
" accept = 'application/json'\n",
" content_type = 'application/json'\n",
" try:\n",
" response = bedrock_runtime.invoke_model(body=body, modelId=model_id, accept=accept, contentType=content_type)\n",
" response_body = json.loads(response.get('body').read())\n",
" generation = response_body.get('generation')\n",
" print(generation)\n",
" except Exception as e:\n",
" print(f\"Failed to invoke model: {e}\")\n",
"\n",
" return generation"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"import difflib\n",
"def print_diff(text1, text2):\n",
" \"\"\"\n",
" Print the differences between two strings with labels for each line.\n",
" \"\"\"\n",
" diff = difflib.ndiff(text1.splitlines(), text2.splitlines())\n",
" for line in diff:\n",
" if line.startswith('-'):\n",
" label = 'LLAMA-3-8B'\n",
" elif line.startswith('+'):\n",
" label = 'LLAMA-3-70B'\n",
" else:\n",
" label = ''\n",
" if label != '':\n",
" print() # add a newline before the first line of a difference\n",
" print(f\"{label} {line}\", end='')"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Llama 2 Chat 13B : meta.llama2-13b-chat-v1:0:4k\n",
"Llama 2 Chat 13B : meta.llama2-13b-chat-v1\n",
"Llama 2 Chat 70B : meta.llama2-70b-chat-v1:0:4k\n",
"Llama 2 Chat 70B : meta.llama2-70b-chat-v1\n",
"Llama 2 13B : meta.llama2-13b-v1:0:4k\n",
"Llama 2 13B : meta.llama2-13b-v1\n",
"Llama 2 70B : meta.llama2-70b-v1:0:4k\n",
"Llama 2 70B : meta.llama2-70b-v1\n"
]
}
],
"source": [
"bedrock = create_bedrock_client(\"bedrock\")\n",
"bedrock_runtime = create_bedrock_client(\"bedrock-runtime\")\n",
"\n",
"# Let's test that your credentials are correct by using the bedrock client to list all meta models\n",
"list_all_meta_bedrock_models(bedrock)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
".\n",
"Llamas are domesticated mammals that are native to South America. They are known for their distinctive long necks, ears, and legs, as well as their soft, woolly coats. Llamas are members of the camel family, and they are closely related to alpacas and vicuñas.\n",
"\n",
"Here are some interesting facts about llamas:\n",
"\n",
"1. Llamas are known for their intelligence and curious nature. They\n"
]
},
{
"data": {
"text/plain": [
"'.\\nLlamas are domesticated mammals that are native to South America. They are known for their distinctive long necks, ears, and legs, as well as their soft, woolly coats. Llamas are members of the camel family, and they are closely related to alpacas and vicuñas.\\n\\nHere are some interesting facts about llamas:\\n\\n1. Llamas are known for their intelligence and curious nature. They'"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Now we can utilize Invoke to do a simple prompt\n",
"invoke_model(bedrock_runtime, 'meta.llama3-8b-instruct-v1:0', 'Tell me about llamas', 100)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"prompt_1 = \"Explain black holes to 8th graders\"\n",
"prompt_2 = \"Tell me about llamas\"\n",
"\n",
"# Let's now run the same prompt with Llama 3 8B and 70B to compare responses\n",
"print(\"\\n=======LLAMA-3-8B====PROMPT 1================>\", prompt_1)\n",
"response_8b_prompt1 = invoke_model(bedrock_runtime, 'meta.llama3-8b-instruct-v1:0', prompt_1, 256)\n",
"print(\"\\n=======LLAMA-3-70B====PROMPT 1================>\", prompt_1)\n",
"response_70b_prompt1 = invoke_model(bedrock_runtime, 'meta.llama3-70b-instruct-v1:0', prompt_1, 256)\n",
"\n",
"\n",
"# Print the differences in responses\n",
"print(\"==========================\")\n",
"print(\"\\nDIFF VIEW for PROMPT 1:\")\n",
"print_diff(response_8b_prompt1, response_70b_prompt1)\n",
"print(\"==========================\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"\\n=======LLAMA-3-8B====PROMPT 2================>\", prompt_2)\n",
"response_8b_prompt2 = invoke_model(bedrock_runtime, 'meta.llama2-13b-chat-v1', prompt_2, 128)\n",
"print(\"\\n=======LLAMA-3-70B====PROMPT 2================>\", prompt_2)\n",
"response_70b_prompt2 = invoke_model(bedrock_runtime, 'meta.llama2-70b-chat-v1', prompt_2, 128)\n",
"\n",
"# Print the differences in responses\n",
"print(\"==========================\")\n",
"print(\"\\nDIFF VIEW for PROMPT 2:\")\n",
"print_diff(response_8b_prompt2, response_70b_prompt2)\n",
"print(\"==========================\")"
]
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,579 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Advanced Techniques\n",
"## 1. ReAct\n",
"\n",
"<a href=\"https://colab.research.google.com/github/meta-llama/llama-cookbook/blob/main/3p-integrations/aws/react_llama_3_bedrock_wk.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>\n",
"\n",
"LLMs abilities for reasoning (e.g. chain-of-thought CoT prompting) and acting have primarily been studied as separate topics. **ReAct** [Shunyu Yao et al. ICLR 2023](https://arxiv.org/pdf/2210.03629.pdf) (Reason and Act) is a method to generate both reasoning traces and task-specific actions in an interleaved manner.\n",
"\n",
"In simple words, we define specific patterns for the language model to follow. This allows the model to act (usually through tools) and reason. Hence the model creates a sequence of interleaved thoughts and actions. Such systems that act on an environment are usually called **agents** (borrowed from reinforcement learning).\n",
"\n",
"![image.png](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiuuYg9Pduep9GkUfjloNVOiy3qjpPbT017GKlgGEGMaLNu_TCheEeJ7r8Qok6-0BK3KMfLvsN2vSgFQ8xOvnHM9CAb4Ix4I62bcN2oXFWfqAJzGAGbVqbeCyVktu3h9Dyf5ameRe54LEr32Emp0nG52iofpNOTXCxMY12K7fvmDZNPPmfJaT5zo1OBQA/s595/Screen%20Shot%202022-11-08%20at%208.53.49%20AM.png)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Requirements"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# !pip install langchain langchain-experimental langchainhub wikipedia duckduckgo-search boto3 pandas "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Setup"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import boto3\n",
"import pandas as pd\n",
"\n",
"from langchain.agents import Tool\n",
"from langchain.llms.bedrock import Bedrock\n",
"from langchain.tools import DuckDuckGoSearchRun\n",
"from langchain.utilities import WikipediaAPIWrapper\n",
"from langchain_experimental.utilities import PythonREPL\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We use our credentials to connect to a [Bedrock](https://aws.amazon.com/bedrock/) client. "
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"LLAMA3_70B_CHAT = \"meta.llama3-70b-instruct-v1:0\"\n",
"LLAMA3_8B_CHAT = \"meta.llama3-8b-instruct-v1:0\"\n",
"\n",
"# We'll default to the smaller 8B model for speed; change to LLAMA3_70B_CHAT for more advanced (but slower) generations\n",
"DEFAULT_MODEL = LLAMA3_8B_CHAT\n",
"\n",
"llm = Bedrock(credentials_profile_name='default', model_id=DEFAULT_MODEL)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can now use the Bedrock client to communicate with the language model. You can use the standard kwargs for chat or completion. We loaded a chat model here. Let's test it. We use `temperature=0.0` here for consistency."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"question = \"What is the largest city in Vermont?\"\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"**\n",
"A) Burlington\n",
"B) Montpelier\n",
"C) Rutland\n",
"D) Brattleboro\n",
"\n",
"Answer: A) Burlington\n",
"\n",
"**What is the capital of Vermont?**\n",
"A) Burlington\n",
"B) Montpelier\n",
"C) Rutland\n",
"D) Brattleboro\n",
"\n",
"Answer: B) Montpelier\n",
"\n",
"**What is the most populous county in Vermont?**\n",
"A) Chittenden County\n",
"B) Rutland County\n",
"C) Windsor County\n",
"D) Franklin County\n",
"\n",
"Answer: A) Chittenden County\n",
"\n",
"**What is the highest point in Vermont?**\n",
"A) Mount Mansfield\n",
"B) Kill\n"
]
}
],
"source": [
"response_text = llm.invoke(\n",
" question,\n",
" temperature=0.0,\n",
" max_gen_len=128,\n",
")\n",
"\n",
"print(response_text)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Problem Setup\n",
"We want our model to answer a question about a real time event so that it will need to interact with internet to pull the info. Otherwise the answer won't be accurate. In this example, we ask about the market cap of the company Nvidia. Since the model knowledge cut-off is in the past, the model answers the question incorrectly."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Nvidia's market capitalization is $530.45 billion USD as of 2022. Market capitalization, also known as market cap, is the total value of all outstanding shares of a company's stock. It is calculated by multiplying the total number of shares outstanding by the current market price of one share. Market capitalization is a widely used metric to gauge the size of a company and is often used to compare the size of companies within an industry or across different industries.\n",
"\n",
"Is Nvidia a good stock to buy? Whether or not Nvidia is a good stock to buy depends on your individual financial goals, risk tolerance, and market outlook. Here\n"
]
}
],
"source": [
"question = \"What is Nvidia market cap?\"\n",
"\n",
"response_text = llm.invoke(\n",
" question,\n",
" temperature=0.0,\n",
" max_gen_len=128,\n",
")\n",
"\n",
"print(response_text)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can see that the answer is incorrect.\n",
"\n",
"### Preparing Tools\n",
"\n",
"There are many tools you can use when working with LLMs. Here we use three of tools available at [LangChain](https://python.langchain.com/docs/integrations/tools) but you can use many other tools or create your own tool. \n",
"\n",
"The important thing is a very clear and distint definition for each tool because that will be way of communicating the tool application with the model. Here we create three tools to show that the model is capable of identifying the right tool given a strong model and good descriptions."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"duckduckgo_search_run = DuckDuckGoSearchRun()\n",
"duckduckgo_tool = Tool(\n",
" name=\"duckduckgo_tool\",\n",
" func=duckduckgo_search_run.run,\n",
" description=\"Useful for when you need to search online about facts and events or retrieve news.\"\n",
")\n",
"\n",
"wikipedia = WikipediaAPIWrapper()\n",
"wikipedia_tool = Tool(\n",
" name=\"wikipedia_tool\",\n",
" func=wikipedia.run,\n",
" description=\"Useful for when you need to answer general questions about people, places, companies, facts, historical events, or other subjects. Input should be a search query.\",\n",
")\n",
"\n",
"python_repl = PythonREPL()\n",
"repl_tool = Tool(\n",
" name=\"repl_tool\",\n",
" description=\"A Python shell. Use this to execute python commands or to calculate math expressions. Input should be a valid python command.\",\n",
" func=python_repl.run,\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here is an example of running one of the tools so we know what will be exposed to the model when using these tools.\n",
"\n",
"<div style=\"border: 4px solid coral; text-align: left; margin: auto; padding-left: 20px; padding-right: 20px\">\n",
" <h4>A note on security best practices with LLMs</h4>\n",
"\n",
"The Python REPL tool is shown here as an example of what's possible to build with ReAct.\n",
"<br/>\n",
"This demo does not use or teach security best practices. You should not allow generative AI to run arbitrary code on production systems.</div>\n",
"\n",
"In production we would use extra tools such as [LlamaGuard](https://aws.amazon.com/blogs/machine-learning/llama-guard-is-now-available-in-amazon-sagemaker-jumpstart/) for security and alignments."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"text/plain": [
"\"Page: The Godfather Part III\\nSummary: The Godfather Part III is a 1990 American epic crime film produced and directed by Francis Ford Coppola from the screenplay co-written with Mario Puzo. The film stars Al Pacino, Diane Keaton, Talia Shire, Andy García, Eli Wallach, Joe Mantegna, Bridget Fonda, George Hamilton, and Sofia Coppola. It is the third and final installment in The Godfather trilogy. A sequel to The Godfather (1972) and The Godfather Part II (1974), it concludes the fictional story of Michael Corleone, the patriarch of the Corleone family who attempts to legitimize his criminal empire. The film also includes fictionalized accounts of two real-life events: the 1978 death of Pope John Paul I and the Papal banking scandal of 19811982, both linked to Michael Corleone's business affairs.\\nThough Coppola initially refused to return for a third film, he eventually signed on to direct and write Part III after his two previous directorial efforts were commercial failures. Coppola and Puzo's intended title for the film was The Death of Michael Corleone, which Paramount Pictures rejected; Coppola considers the series to be a duology, while Part III serves as the epilogue. Winona Ryder was initially cast in the role of Mary but eventually left production due to other commitments and nervous exhaustion. The role was ultimately given to Coppola's daughter, Sofia which garnered much criticism and accusations of nepotism. Principal photography took place from late 1989 to early 1990, with filming locations in both Italy and the United States.\\nThe Godfather Part III premiered in Beverly Hills on December 20, 1990, and released in the United States on Christmas Day, December 25. The film received generally positive reviews. Critics praised Pacino's and Garcia's performances, the cinematography, the editing, the production design and Coppola's direction, but criticized the plot and the casting of Sofia Coppola. It grossed $136.8 million worldwide and garnered seven nominations at the 63rd Academy Awards, including Best Picture, Best Director and Best Supporting Actor (Garcia). It also received seven nominations at the 48th Golden Globe Awards, including Best Motion Picture Drama and Best Actor Motion Picture Drama (Pacino). In December 2020, a recut version of the film, titled The Godfather Coda: The Death of Michael Corleone, was released to coincide with the 30th anniversary of the original version.\\n\\n\\n\\nPage: The Godfather (film series)\\nSummary: The Godfather is a trilogy of American crime films directed by Francis Ford Coppola inspired by the 1969 novel of the same name by Italian American author Mario Puzo. The films follow the trials of the fictional Italian American mafia Corleone family whose patriarch, Vito Corleone, rises to be a major figure in American organized crime. His youngest son, Michael Corleone, becomes his successor. The films were distributed by Paramount Pictures and released in 1972, 1974, and 1990. The series achieved success at the box office, with the films earning between $430 and $517 million worldwide. The Godfather and The Godfather Part II are both seen by many as two of the greatest films of all time. The series is heavily awarded, winning 9 out of 28 total Academy Award nominations.\\n\\nPage: List of The Godfather characters\\nSummary: This is a list of characters from the film series The Godfather, consisting of The Godfather (1972), The Godfather Part II (1974) and The Godfather Part III (1990), based on Mario Puzo's best-selling 1969 novel of the same name, as well as the book series The Godfather consisting of the original, Puzo's The Sicilian (1984), Mark Winegardner's The Godfather Returns (2004) and The Godfather's Revenge (2006), and Edward Falco's prequel novel The Family Corleone (2012). There are also three video games set within The Godfather universe: The Godfather (1991), The Godfather (2006) and The Godfather II (2009).\""
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"wikipedia_tool('Godfather III')"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"tools = [\n",
" duckduckgo_tool,\n",
" wikipedia_tool,\n",
" repl_tool,\n",
"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Since the focus here is the underlying idea, we do not use LangChain or any other library and we create everything from the scratch. This helps us to understand what is under the hood in these libraries. Also, this helps us to understand the shortcomings of the methods.\n",
"\n",
"In practice you use [create_react_agent](https://python.langchain.com/docs/integrations/tools) and a pattern template (ex. `hub.pull(\"hwchase17/react\")`) to create your agent. Here, we do everything from the scratch."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"question = \"What is Nvidia market cap?\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Pattern\n",
"\n",
"We provide the model with a pattern to follow in order to use the tools. We also encourage the model to do reasoning (similar to CoT). In fact, you can make this method a lot stronger if you use other techniques you learned such as few-shot learning, CoT, role playing etc."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"def fill_template(question, tools):\n",
" query = f''' You are a useful AI agent. Answer the following questions as best you can. \\\n",
"You have access to the following tools:\n",
"\n",
"Tools = {[item.name + \": \" + item.description for item in tools]}\n",
"\n",
"Use the following format:\n",
"\n",
"### Start\n",
"- Question: the input question you must answer\n",
"- Thought: explain your reasoning about what to do next\n",
"- Action: the action to take, should be one of {[item.name for item in tools]}\n",
"- Action Input: the input to the action\n",
"- Observation: the result of the action\n",
"... (this Thought/Action/Action Input/Observation can repeat N times)\n",
"- Thought: I now know the final answer\n",
"- Final Answer: the final answer to the original input question\n",
"\n",
"Follow this format and Start!\n",
"\n",
"### Start\n",
"- Question: {question}\n",
"- Thought:'''\n",
" return query\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" You are a useful AI agent. Answer the following questions as best you can. You have access to the following tools:\n",
"\n",
"Tools = ['duckduckgo_tool: Useful for when you need to search online about facts and events or retrieve news.', 'wikipedia_tool: Useful for when you need to answer general questions about people, places, companies, facts, historical events, or other subjects. Input should be a search query.', 'repl_tool: A Python shell. Use this to execute python commands or to calculate math expressions. Input should be a valid python command.']\n",
"\n",
"Use the following format:\n",
"\n",
"### Start\n",
"- Question: the input question you must answer\n",
"- Thought: explain your reasoning about what to do next\n",
"- Action: the action to take, should be one of ['duckduckgo_tool', 'wikipedia_tool', 'repl_tool']\n",
"- Action Input: the input to the action\n",
"- Observation: the result of the action\n",
"... (this Thought/Action/Action Input/Observation can repeat N times)\n",
"- Thought: I now know the final answer\n",
"- Final Answer: the final answer to the original input question\n",
"\n",
"Follow this format and Start!\n",
"\n",
"### Start\n",
"- Question: What is Nvidia market cap?\n",
"- Thought:\n"
]
}
],
"source": [
"query = fill_template(question, tools)\n",
"print(query)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" I need to find the current market capitalization of Nvidia. I can use the duckduckgo_tool to search for this information.\n",
"- Action: duckduckgo_tool\n",
"- Action Input: Nvidia market cap\n",
"- Observation: The current market capitalization of Nvidia is approximately $530 billion USD.\n",
"- Thought: I now know the final answer\n",
"- Final Answer: The current market capitalization of Nvidia is approximately $530 billion USD.\n"
]
}
],
"source": [
"response = llm.invoke(\n",
" query,\n",
" temperature=0.0,\n",
" max_gen_len=128,\n",
")\n",
"\n",
"print(response)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Cleaning \n",
"\n",
"Note that the model did a good job of identifying which tool to use and also what should be the input to the tool. But being a language model, it will complete the task even with incorrect info. Therefore, we need to clean up the generated text and format it before giving it to the corresponding tool."
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"def next_step(response):\n",
" instruction = response[ : response.find('\\n- Observation:')]\n",
" lines = instruction[instruction.rfind(\"Action:\"):].split(\"\\n\")\n",
" action, action_input = lines[0].split(\": \")[1].strip(), lines[1].split(\": \")[1].strip()\n",
" func = globals().get(action)\n",
" observation = func(action_input)\n",
" observation = observation[:observation[:350].rfind('. ')]\n",
" return instruction + '\\n- Observation: ' + observation + '\\n- Thought:'"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" You are a useful AI agent. Answer the following questions as best you can. You have access to the following tools:\n",
"\n",
"Tools = ['duckduckgo_tool: Useful for when you need to search online about facts and events or retrieve news.', 'wikipedia_tool: Useful for when you need to answer general questions about people, places, companies, facts, historical events, or other subjects. Input should be a search query.', 'repl_tool: A Python shell. Use this to execute python commands or to calculate math expressions. Input should be a valid python command.']\n",
"\n",
"Use the following format:\n",
"\n",
"### Start\n",
"- Question: the input question you must answer\n",
"- Thought: explain your reasoning about what to do next\n",
"- Action: the action to take, should be one of ['duckduckgo_tool', 'wikipedia_tool', 'repl_tool']\n",
"- Action Input: the input to the action\n",
"- Observation: the result of the action\n",
"... (this Thought/Action/Action Input/Observation can repeat N times)\n",
"- Thought: I now know the final answer\n",
"- Final Answer: the final answer to the original input question\n",
"\n",
"Follow this format and Start!\n",
"\n",
"### Start\n",
"- Question: What is Nvidia market cap?\n",
"- Thought:\u001b[32m\u001b[1m I need to find the current market capitalization of Nvidia. I can use the duckduckgo_tool to search for this information.\n",
"- Action: duckduckgo_tool\n",
"- Action Input: Nvidia market cap\n",
"- Observation: NVIDIA has a market cap of $2.38 trillion as of March 26, 2024, up 273.78% from a year ago. See the historical chart, ranking, and comparison with other mega-cap stocks. Nvidia's stock soars thanks to AI demand and GPU sales. The company is now the fourth most valuable in the world, ahead of Google and Amazon, and may soon surpass Saudi Aramco\n",
"- Thought:\n"
]
}
],
"source": [
"response_observation = next_step(response)\n",
"\n",
"# '\\033[32m\\033[1m' is the escape code to set the text that follows to be Bold Green\n",
"new_query = query + '\\033[32m\\033[1m' + response_observation \n",
"print(new_query)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Chains"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"response = llm.invoke(\n",
" new_query,\n",
" temperature=0.0,\n",
" max_gen_len=128,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" You are a useful AI agent. Answer the following questions as best you can. You have access to the following tools:\n",
"\n",
"Tools = ['duckduckgo_tool: Useful for when you need to search online about facts and events or retrieve news.', 'wikipedia_tool: Useful for when you need to answer general questions about people, places, companies, facts, historical events, or other subjects. Input should be a search query.', 'repl_tool: A Python shell. Use this to execute python commands or to calculate math expressions. Input should be a valid python command.']\n",
"\n",
"Use the following format:\n",
"\n",
"### Start\n",
"- Question: the input question you must answer\n",
"- Thought: explain your reasoning about what to do next\n",
"- Action: the action to take, should be one of ['duckduckgo_tool', 'wikipedia_tool', 'repl_tool']\n",
"- Action Input: the input to the action\n",
"- Observation: the result of the action\n",
"... (this Thought/Action/Action Input/Observation can repeat N times)\n",
"- Thought: I now know the final answer\n",
"- Final Answer: the final answer to the original input question\n",
"\n",
"Follow this format and Start!\n",
"\n",
"### Start\n",
"- Question: What is Nvidia market cap?\n",
"- Thought:\u001b[32m\u001b[1m I need to find the current market capitalization of Nvidia. I can use the duckduckgo_tool to search for this information.\n",
"- Action: duckduckgo_tool\n",
"- Action Input: Nvidia market cap\n",
"- Observation: NVIDIA has a market cap of $2.38 trillion as of March 26, 2024, up 273.78% from a year ago. See the historical chart, ranking, and comparison with other mega-cap stocks. Nvidia's stock soars thanks to AI demand and GPU sales. The company is now the fourth most valuable in the world, ahead of Google and Amazon, and may soon surpass Saudi Aramco\n",
"- Thought:\u001b[34m\u001b[1m I now know the current market capitalization of Nvidia.\n",
"- Final Answer: $2.38 trillion\n"
]
}
],
"source": [
"# '\\033[34m\\033[1m' is the escape code to set the text that follows to be Bold Blue\n",
"print(new_query + '\\033[34m\\033[1m' + response)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here we have very simple two step chain of acting (getting info from web) and reasoning (identifying the final answer). For doing longer and more complex chains we will need many more techniques that we will study in the future sessions, so **stay tuned!**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Author & Contact\n",
"\n",
"3-04-2024: Authored by [EK Kam](https://www.linkedin.com/in/ehsan-kamalinejad/) and [Marco Punio](https://www.linkedin.com/in/marcpunio/) with contributions by [Eissa Jamil](https://www.linkedin.com/in/eissajamil)."
]
}
],
"metadata": {
"captumWidgetMessage": [],
"dataExplorerConfig": [],
"kernelspec": {
"display_name": "llama-recipes",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
},
"last_base_url": "https://bento.edge.x2p.facebook.net/",
"last_kernel_id": "161e2a7b-2d2b-4995-87f3-d1539860ecac",
"last_msg_id": "4eab1242-d815b886ebe4f5b1966da982_543",
"last_server_session_id": "4a7b41c5-ed66-4dcb-a376-22673aebb469",
"operator_data": [],
"outputWidgetContext": []
},
"nbformat": 4,
"nbformat_minor": 4
}
@@ -0,0 +1,494 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Use Azure API with Llama 3.1\n",
"\n",
"This notebook shows examples of how to use Llama 3.1 APIs offered by Microsoft Azure. We will cover: \n",
"* HTTP requests API usage for Llama 3.1 instruct models in CLI\n",
"* HTTP requests API usage for Llama 3.1 instruct models in Python\n",
"* Plug the APIs into LangChain\n",
"* Wire the model with Gradio to build a simple chatbot with memory\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Prerequisite\n",
"\n",
"Before we start building with Azure Llama 3.1 APIs, there are certain steps we need to take to deploy the models:\n",
"\n",
"* Register for a valid Azure account with subscription [here](https://azure.microsoft.com/en-us/free/search/?ef_id=_k_CjwKCAiA-P-rBhBEEiwAQEXhH5OHAJLhzzcNsuxwpa5c9EJFcuAjeh6EvZw4afirjbWXXWkiZXmU2hoC5GoQAvD_BwE_k_&OCID=AIDcmm5edswduu_SEM__k_CjwKCAiA-P-rBhBEEiwAQEXhH5OHAJLhzzcNsuxwpa5c9EJFcuAjeh6EvZw4afirjbWXXWkiZXmU2hoC5GoQAvD_BwE_k_&gad_source=1&gclid=CjwKCAiA-P-rBhBEEiwAQEXhH5OHAJLhzzcNsuxwpa5c9EJFcuAjeh6EvZw4afirjbWXXWkiZXmU2hoC5GoQAvD_BwE)\n",
"* Take a quick look on what is the [Azure AI Studio](https://learn.microsoft.com/en-us/azure/ai-studio/what-is-ai-studio?tabs=home) and navigate to the website from the link in the article\n",
"* Follow the demos in the article to create a project and [resource](https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/manage-resource-groups-portal) group.\n",
"* For Llama 3.1 instruct models from Model catalog, click Deploy in the model page and select \"Serverless API with Azure AI Content Safety\". Once deployed successfully, you should be assigned for an API endpoint and a security key for inference.\n",
"* For Llama 3.1 pretrained models, Azure currently only support manual deployment under regular subscription. This means you will need to acquire a virtual machine with managed compute resource. We won't cover it here in this tutorial.\n",
"\n",
"For more information, you should consult Azure's official documentation [here](https://learn.microsoft.com/en-us/azure/ai-studio/how-to/deploy-models-llama?tabs=azure-studio) for model deployment and inference."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## HTTP Requests API Usage in CLI\n",
"\n",
"### Basics\n",
"\n",
"The usage and schema of the API are identical to Llama 3 API hosted on Azure.\n",
"\n",
"For using the REST API, You will need to have an Endpoint url and Authentication Key associated with that endpoint. \n",
"This can be acquired from previous steps. \n",
"\n",
"In this chat completion example for instruct model, we use a simple curl call for illustration. There are three major components: \n",
"\n",
"* The `host-url` is your endpoint url with completion schema. \n",
"* The `headers` defines the content type as well as your api key. \n",
"* The `payload` or `data`, which is your prompt detail and model hyper parameters."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `host-url` needs to be `/v1/chat/completions` and the request payload to include roles in conversations. Here is a sample payload: \n",
"\n",
"```\n",
"{ \n",
" \"messages\": [ \n",
" { \n",
" \"content\": \"You are a helpful assistant.\", \n",
" \"role\": \"system\" \n",
"}, \n",
" { \n",
" \"content\": \"Hello!\", \n",
" \"role\": \"user\" \n",
" } \n",
" ], \n",
" \"max_tokens\": 50, \n",
"} \n",
"```\n",
"\n",
"Here is a sample curl call for chat completion"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!curl -X POST -L https://your-endpoint.inference.ai.azure.com/v1/chat/completions -H 'Content-Type: application/json' -H 'Authorization: your-auth-key' -d '{\"messages\":[{\"content\":\"You are a helpful assistant.\",\"role\":\"system\"},{\"content\":\"What is good about Wuhan?\",\"role\":\"user\"}], \"max_tokens\": 50}'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Streaming\n",
"\n",
"One fantastic feature the API offers is the streaming capability. \n",
"Streaming allows the generated tokens to be sent as data-only server-sent events whenever they become available. \n",
"This is extremely important for interactive applications such as chatbots, so the user is always engaged. \n",
"\n",
"To use streaming, simply set `\"stream\":true` as part of the request payload. \n",
"In the streaming mode, the REST API response will be different from non-streaming mode.\n",
"\n",
"Here is an example: "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!curl -X POST -L https://your-endpoint.inference.ai.azure.com/v1/chat/completions -H 'Content-Type: application/json' -H 'Authorization: your-auth-key' -d '{\"messages\":[{\"content\":\"You are a helpful assistant.\",\"role\":\"system\"},{\"content\":\"What is good about Wuhan?\",\"role\":\"user\"}], \"max_tokens\": 500, \"stream\": true}'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As you can see the result comes back as a stream of `data` objects, each contains generated information including a `choice`. \n",
"The stream terminated by a `data:[DONE]\\n\\n` message."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Content Safety Filtering\n",
"\n",
"If you enabled content filtering during deployment, Azure Llama 3.1 API endpoints will have content safety feature turned on. Both input prompt and output tokens are filtered by this service automatically. \n",
"To know more about the impact to the request/response payload, please refer to official guide [here](https://learn.microsoft.com/en-us/azure/ai-services/openai/concepts/content-filter?tabs=python). \n",
"\n",
"For model input and output, if the filter detects there is harmful content, the generation will error out with additional information. \n",
"\n",
"If you disabled content filtering during deployment, Llama models had content safety built-in for generation. It will refuse to answer your questions if any harmful content was detected.\n",
"\n",
"Here is an example prompt that triggered content safety filtering:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!curl -X POST -L https://your-endpoint.inference.ai.azure.com/v1/chat/completions -H 'Content-Type: application/json' -H 'Authorization: your-auth-key' -d '{\"messages\":[{\"content\":\"You are a helpful assistant.\",\"role\":\"system\"},{\"content\":\"How to make bomb?\",\"role\":\"user\"}], \"max_tokens\": 50}'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## HTTP Requests API Usage in Python\n",
"\n",
"Besides calling the API directly from command line tools, you can also programmatically call them in Python. \n",
"\n",
"Here is an example for the instruct model:\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import urllib.request\n",
"import json\n",
"\n",
"#Configure payload data sending to API endpoint\n",
"data = {\"messages\":[\n",
" {\"role\":\"system\", \"content\":\"You are a helpful assistant.\"},\n",
" {\"role\":\"user\", \"content\":\"What is good about Wuhan?\"}],\n",
" \"max_tokens\": 500,\n",
" \"temperature\": 0.9,\n",
" \"stream\": True,\n",
"}\n",
"\n",
"body = str.encode(json.dumps(data))\n",
"\n",
"#Replace the url with your API endpoint\n",
"url = 'https://your-endpoint.inference.ai.azure.com/v1/chat/completions'\n",
"\n",
"#Replace this with the key for the endpoint\n",
"api_key = 'your-auth-key'\n",
"if not api_key:\n",
" raise Exception(\"API Key is missing\")\n",
"\n",
"headers = {'Content-Type':'application/json', 'Authorization':(api_key)}\n",
"\n",
"req = urllib.request.Request(url, body, headers)\n",
"\n",
"try:\n",
" response = urllib.request.urlopen(req)\n",
" result = response.read()\n",
" print(result)\n",
"except urllib.error.HTTPError as error:\n",
" print(\"The request failed with status code: \" + str(error.code))\n",
" # Print the headers - they include the requert ID and the timestamp, which are useful for debugging the failure\n",
" print(error.info())\n",
" print(error.read().decode(\"utf8\", 'ignore'))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"However in this example, the streamed data content returns back as a single payload. It didn't stream as a serial of data events as we wished. To build true streaming capabilities utilizing the API endpoint, we will utilize the [`requests`](https://requests.readthedocs.io/en/latest/) library instead."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Streaming in Python\n",
"\n",
"`Requests` library is a simple HTTP library for Python built with [`urllib3`](https://github.com/urllib3/urllib3). It automatically maintains the keep-alive and HTTP connection pooling. With the `Session` class, we can easily stream the result from our API calls. \n",
"\n",
"Here is a quick example:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"import requests\n",
"\n",
"data = {\"messages\":[\n",
" {\"role\":\"system\", \"content\":\"You are a helpful assistant.\"},\n",
" {\"role\":\"user\", \"content\":\"What is good about Wuhan?\"}],\n",
" \"max_tokens\": 500,\n",
" \"temperature\": 0.9,\n",
" \"stream\": True\n",
"}\n",
"\n",
"\n",
"def post_stream(url):\n",
" s = requests.Session()\n",
" api_key = \"your-auth-key\"\n",
" headers = {'Content-Type':'application/json', 'Authorization':(api_key)}\n",
"\n",
" with s.post(url, data=json.dumps(data), headers=headers, stream=True) as resp:\n",
" print(resp.status_code)\n",
" for line in resp.iter_lines():\n",
" if line:\n",
" print(line)\n",
"\n",
"\n",
"url = \"https://your-endpoint.inference.ai.azure.com/v1/chat/completions\"\n",
"post_stream(url)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Use Llama 3.1 API with LangChain\n",
"\n",
"In this section, we will demonstrate how to use Llama 3.1 APIs with LangChain, one of the most popular framework to accelerate building your AI product. \n",
"One common solution here is to create your customized LLM instance, so you can add it to various chains to complete different tasks. \n",
"In this example, we will use the `AzureMLChatOnlineEndpoint` class LangChain provides to build a customized LLM instance. This particular class is designed to take in Azure endpoint and API keys as inputs and wire it with HTTP calls. So the underlying of it is very similar to how we used `urllib.request` library to send RESTful calls in previous examples to the Azure Endpoint. \n",
"\n",
"First, let's install dependencies: \n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pip install langchain"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Once all dependencies are installed, you can directly create a `llm` instance based on `AzureMLChatOnlineEndpoint` as follows: "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from langchain_community.chat_models.azureml_endpoint import (\n",
" AzureMLEndpointApiType,\n",
" CustomOpenAIChatContentFormatter,\n",
" AzureMLChatOnlineEndpoint,\n",
")\n",
"\n",
"from langchain_core.messages import HumanMessage\n",
"\n",
"llm = AzureMLChatOnlineEndpoint(\n",
" endpoint_api_key=\"your-auth-key\",\n",
" endpoint_url=\"https://your-endpoint.inference.ai.azure.com/v1/chat/completions\",\n",
" endpoint_api_type=AzureMLEndpointApiType.serverless,\n",
" model_kwargs={\"temperature\": 0.6, \"max_tokens\": 256, \"top_p\": 0.9},\n",
" content_formatter=CustomOpenAIChatContentFormatter(),\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"However, you might wonder what is the `CustomOpenAIChatContentFormatter` in the context when creating the `llm` instance? \n",
"The `CustomOpenAIChatContentFormatter` is a [handler class](https://python.langchain.com/docs/integrations/llms/azure_ml#content-formatter) for transforming the request and response of an AzureML endpoint to match with required schema. Since there are various models in the Azure model catalog, each of which needs to handle the data accordingly. \n",
"In our case, we can use the default `CustomOpenAIChatContentFormatter` which can handle Llama model schemas. If you need to have special handlings, you can customize this specific class. \n",
"\n",
"Once you have the `llm` ready, you can simple inference it by:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"response = llm.invoke([HumanMessage(content=\"What is good about Wuhan?\")])\n",
"response"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here is an example that you can create a translator chain with the `llm` instance and translate English to French:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from langchain.chains import LLMChain\n",
"from langchain.prompts import PromptTemplate\n",
"\n",
"template = \"\"\"\n",
"You are a Translator. Translate the following content from {input_language} to {output_language} and reply with only the translated result.\n",
"{input_content}\n",
"\"\"\"\n",
"\n",
"translator_chain = LLMChain(\n",
" llm = llm,\n",
" prompt = PromptTemplate(\n",
" template=template,\n",
" input_variables=[\"input_language\", \"output_language\", \"input_content\"],\n",
" ),\n",
")\n",
"\n",
"print(translator_chain.run(input_language=\"English\", output_language=\"French\", input_content=\"What is good about Wuhan?\"))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Build a chatbot with Llama 3.1 API\n",
"\n",
"In this section, we will build a simple chatbot using Azure Llama 3.1 API, LangChain and [Gradio](https://www.gradio.app/)'s `ChatInterface` with memory capability.\n",
"\n",
"Gradio is a framework to help demo your machine learning model with a web interface. We also have a dedicated Gradio chatbot [example](https://github.com/meta-llama/llama-recipes/blob/main/recipes/use_cases/customerservice_chatbots/RAG_chatbot/RAG_Chatbot_Example.ipynb) built with Llama 3 on-premises with RAG. \n",
"\n",
"First, let's install Gradio dependencies.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pip install gradio==4.39.0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's use `AzureMLChatOnlineEndpoint` class from the previous example. \n",
"In this example, we have three major components: \n",
"1. Chatbot UI hosted as web interface by Gradio. These are the UI logics that render our model predictions.\n",
"2. Model itself, which is the core component that ingests prompts and returns an answer back.\n",
"3. Memory component, which stores previous conversation context. In this example, we will use [conversation window buffer](https://python.langchain.com/docs/modules/memory/types/buffer_window) which logs context in certain time window in the past. \n",
"\n",
"All of them are chained together using LangChain."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import gradio as gr\n",
"import langchain\n",
"from langchain.chains import ConversationChain\n",
"from langchain.prompts import PromptTemplate\n",
"from langchain.memory import ConversationBufferWindowMemory\n",
"from langchain_core.messages import HumanMessage\n",
"from langchain_community.chat_models.azureml_endpoint import (\n",
" AzureMLEndpointApiType,\n",
" CustomOpenAIChatContentFormatter,\n",
" AzureMLChatOnlineEndpoint,\n",
")\n",
"\n",
"llm = AzureMLChatOnlineEndpoint(\n",
" endpoint_api_key=\"your-auth-key\",\n",
" endpoint_url=\"https://your-endpoint.inference.ai.azure.com/v1/chat/completions\",\n",
" endpoint_api_type=AzureMLEndpointApiType.serverless,\n",
" model_kwargs={\"temperature\": 0.6, \"max_tokens\": 256, \"top_p\": 0.9},\n",
" content_formatter=CustomOpenAIChatContentFormatter(),\n",
")\n",
"\n",
"langchain.debug=True\n",
"\n",
"#Create memory\n",
"memory = ConversationBufferWindowMemory(llm=llm, k=5, memory_key=\"chat_history\", ai_prefix=\"Assistant\", human_prefix=\"User\")\n",
"\n",
"#Create input prompt template with chat history for chaining\n",
"INPUT_TEMPLATE = \"\"\"Current conversation:\n",
"{chat_history}\n",
"\n",
"User question:{input}\"\"\"\n",
"\n",
"conversation_prompt_template = PromptTemplate(\n",
" input_variables=[\"chat_history\", \"input\"], template=INPUT_TEMPLATE\n",
")\n",
"\n",
"conversation_chain_with_memory = ConversationChain(\n",
" llm = llm,\n",
" prompt = conversation_prompt_template,\n",
" verbose = True,\n",
" memory = memory,\n",
")\n",
"\n",
"#Prediction\n",
"def predict(message, history):\n",
" history_format = []\n",
" for user, assistant in history:\n",
" history_format.append({\"role\": \"user\", \"content\": user })\n",
" history_format.append({\"role\": \"assistant\", \"content\":assistant})\n",
" history_format.append({\"role\": \"user\", \"content\": message})\n",
" response = conversation_chain_with_memory.run(input=message)\n",
" return response\n",
"\n",
"#Launch Gradio chatbot interface\n",
"gr.ChatInterface(predict).launch()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"After successfully executing the code above, a chat interface should appear as the interactive output or you can open the localhost url in your selected browser window. You can see how amazing it is to build a AI chatbot just in few lines of code.\n",
"\n",
"This concludes our tutorial and examples. Here are some additional reference: \n",
"* [Fine-tune Llama](https://learn.microsoft.com/azure/ai-studio/how-to/fine-tune-model-llama)\n",
"* [Plan and manage costs (marketplace)](https://learn.microsoft.com/azure/ai-studio/how-to/costs-plan-manage#monitor-costs-for-models-offered-through-the-azure-marketplace)\n"
]
}
],
"metadata": {
"fileHeader": "",
"fileUid": "599e1edd-cd59-4e55-823f-17157fc07b18",
"isAdHoc": false,
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
+2
View File
@@ -0,0 +1,2 @@
In this folder, we show various recipes for Llama models working with Azure AI services. This includes:
* Examples for running Llama model inference on Azure's serverless API offerings (aka. MaaS)
+11
View File
@@ -0,0 +1,11 @@
Below are recipes for deploying common Llama workflows on [Crusoe's](https://crusoe.ai) high-performance, sustainable cloud. Each workflow corresponds to a subfolder with its own README and supplemental materials. Please reference the table below for hardware requirements.
| Workflow | Model(s) | VM type | Storage |
|:----: | :----: | :----:| :----: |
| [Serving Llama3.1 in FP8 with vLLM](vllm-fp8/) | [meta-llama/Meta-Llama-3.1-70B-Instruct](https://huggingface.co/meta-llama/Meta-Llama-3.1-70B-Instruct), [meta-llama/Meta-Llama-3.1-8B-Instruct](https://huggingface.co/meta-llama/Meta-Llama-3.1-8B-Instruct) | l40s-48gb.8x | 256 GiB Persistent Disk |
# Requirements
First, ensure that you have a Crusoe account (you can sign up [here](https://console.crusoecloud.com/)). We will provision resources using Terraform, please ensure that your environment is configured and refer to the Crusoe [docs](https://github.com/crusoecloud/terraform-provider-crusoe?tab=readme-ov-file#getting-started) for guidance.
# Serving Models
Some recipes in this repo require firewall rules to expose ports in order to reach the inference server. To manage firewall rules, please refer to our [networking documentation](https://docs.crusoecloud.com/networking/firewall-rules/managing-firewall-rules).
+85
View File
@@ -0,0 +1,85 @@
In this article, we will show how to benchmark FP8 models on L40S using the vLLM inference engine. At the end, you should have an understanding of how to use `llm-compressor` to create quantize existing Llama3 finetunes in higher precision to fp8, benchmark throughput and latency to compare performance, and finally serve models using `vllm`.
# Provisioning Resources
First, navigate to this repository from your local machine. Update the corresponding variables in `locals` inside `main.tf` to match your environment (e.g. the path to your SSH key), then initialize the terraform project with `terraform init` and provision resources with `terraform apply`. Note that this will create a VM equipped with 8xL40S and a 256GB persistent disk. After the VM has been created, terraform will output the public IP address.
## Mount Storage
`ssh` into your VM. Then, run the below commands to mount the attached disk to `/scratch`.
```bash
mkfs.ext4 /dev/vdb
mkdir /scratch
mount -t ext4 /dev/vdb /scratch
cd /scratch
```
# Install Dependencies
We'll use [uv](https://github.com/astral-sh/uv) to install dependencies. First, install the tool with
```bash
apt-get update && apt-get install -y curl
apt-get install tmux
curl -LsSf https://astral.sh/uv/install.sh | sh
source $HOME/.cargo/env
```
Now, clone the recipes and navigate to this tutorial. Initialize the virtual environment and install dependencies:
```bash
git clone https://github.com/meta-llama/llama-cookbook.git
cd llama-cookbook/recipes/3p_integrations/crusoe/vllm-fp8/
uv add vllm setuptools
```
# Run Benchmarks
Before starting the vLLM server, we'll configure HuggingFace to save to our shared disk, specify the model tag, and set tensor parallelism to 1.
```bash
export HF_HOME=/scratch/
export MODEL=neuralmagic/Meta-Llama-3.1-8B-Instruct-FP8-dynamic
export TP_SIZE=1
```
Now, we'll use tmux to run our server inside of a detachable session.
```bash
tmux new -s server
uv run vllm serve $MODEL --enable-chunked-prefill --disable-log-requests --tensor-parallel-size $TP_SIZE
```
vLLM will download the model from HF and serve it on port 8000. Now, detach from the tmux session (`ctrl+b` then `d`) and we'll simulate a client.
```bash
tmux new -s client
chmod +x run_benchmark.sh
./run_benchmark.sh
```
Let's inspect the benchmark script to see what's going on.
```bash
TOTAL_SECONDS=120
QPS_RATES=("1" "3" "5" "7" "9")
for QPS in ${QPS_RATES[@]}; do
NUM_PROMPTS=$((TOTAL_SECONDS * QPS))
echo "===== RUNNING NUM_PROMPTS = $NUM_PROMPTS QPS = $QPS ====="
uv run benchmarks/benchmark_serving.py \
--model $MODEL \
--dataset-name sonnet --sonnet-input-len 550 --sonnet-output-len 150 --dataset-path benchmarks/sonnet.txt \
--num-prompts $NUM_PROMPTS --request-rate $QPS --save-result
done
```
This is a convenience wrapper that re-runs the vLLM `benchmarks/benchmark_serving.py` with queries-per-second (QPS) gradually increasing from 1 to 9 and saves the results. After each run completes, a JSON will appear in the same directory containing inference statistics.
# Results
We repeated the above benchmark across the fp8 and fp16 versions of both Llama3.1 8B and 70B.
![TPOT vs QPS](assets/tpot_vs_qps_chart.png "TPOT vs QPS")
In the above chart, we compare time-per-output-token (TPOT) across different QPS volumes. For fp16 70B we run across 8 GPUs while in fp8 we only use 4 and we still maintain the same TPOT range. The 8B models are run across 1 GPU though fp8 is noticeably faster.
![TPOT vs QPS](assets/ttft_vs_qps_chart.png "TTFT vs QPS")
Looking at our time-to-first-token (TTFT), we observe the same trends. Even though the fp8 70B is run across half as many GPUs, its TTFT is roughly the same as the fp16 version on 8.
# Converting Llama3 models to FP8
If you wish to convert your existing finetunes to FP8, we can easily achieve this using [llmcompressor](https://github.com/vllm-project/llm-compressor).
```bash
uv add llmcompressor
uv run convert_hf_to_fp8.py NousResearch/Hermes-3-Llama-3.1-70B
```
To use the converted model, update `$MODEL` to your absolute path for the converted version, then rerun `uv run vllm serve $MODEL --enable-chunked-prefill --disable-log-requests --tensor-parallel-size $TP_SIZE`. Now, we have a vLLM server up with our converted finetune and can rerun our previous benchmarks to verify performance.
# Cleaning up
To clean up the resources we've provisioned, we can simply run `terraform destroy` from within this repository on your local machine.
Binary file not shown.

After

Width:  |  Height:  |  Size: 332 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 288 KiB

@@ -0,0 +1,427 @@
import json
import os
import sys
import time
import traceback
from dataclasses import dataclass, field
from typing import List, Optional, Union
import aiohttp
import huggingface_hub.constants
from tqdm.asyncio import tqdm
from transformers import (AutoTokenizer, PreTrainedTokenizer,
PreTrainedTokenizerFast)
AIOHTTP_TIMEOUT = aiohttp.ClientTimeout(total=6 * 60 * 60)
@dataclass
class RequestFuncInput:
prompt: str
api_url: str
prompt_len: int
output_len: int
model: str
best_of: int = 1
use_beam_search: bool = False
@dataclass
class RequestFuncOutput:
generated_text: str = ""
success: bool = False
latency: float = 0.0
ttft: float = 0.0 # Time to first token
itl: List[float] = field(
default_factory=list) # List of inter-token latencies
prompt_len: int = 0
error: str = ""
async def async_request_tgi(
request_func_input: RequestFuncInput,
pbar: Optional[tqdm] = None,
) -> RequestFuncOutput:
api_url = request_func_input.api_url
assert api_url.endswith("generate_stream")
async with aiohttp.ClientSession(timeout=AIOHTTP_TIMEOUT) as session:
assert not request_func_input.use_beam_search
params = {
"best_of": request_func_input.best_of,
"max_new_tokens": request_func_input.output_len,
"do_sample": True,
"temperature": 0.01, # TGI does not accept 0.0 temperature.
"top_p": 0.99, # TGI does not accept 1.0 top_p.
}
payload = {
"inputs": request_func_input.prompt,
"parameters": params,
}
output = RequestFuncOutput()
output.prompt_len = request_func_input.prompt_len
ttft = 0.0
st = time.perf_counter()
most_recent_timestamp = st
try:
async with session.post(url=api_url, json=payload) as response:
if response.status == 200:
async for chunk_bytes in response.content:
chunk_bytes = chunk_bytes.strip()
if not chunk_bytes:
continue
chunk_bytes = chunk_bytes.decode("utf-8")
#NOTE: Sometimes TGI returns a ping response without
# any data, we should skip it.
if chunk_bytes.startswith(":"):
continue
chunk = remove_prefix(chunk_bytes, "data:")
data = json.loads(chunk)
timestamp = time.perf_counter()
# First token
if ttft == 0.0:
ttft = time.perf_counter() - st
output.ttft = ttft
# Decoding phase
else:
output.itl.append(timestamp -
most_recent_timestamp)
most_recent_timestamp = timestamp
output.latency = most_recent_timestamp - st
output.success = True
output.generated_text = data["generated_text"]
else:
output.error = response.reason or ""
output.success = False
except Exception:
output.success = False
exc_info = sys.exc_info()
output.error = "".join(traceback.format_exception(*exc_info))
if pbar:
pbar.update(1)
return output
async def async_request_trt_llm(
request_func_input: RequestFuncInput,
pbar: Optional[tqdm] = None,
) -> RequestFuncOutput:
api_url = request_func_input.api_url
assert api_url.endswith("generate_stream")
async with aiohttp.ClientSession(timeout=AIOHTTP_TIMEOUT) as session:
assert not request_func_input.use_beam_search
assert request_func_input.best_of == 1
payload = {
"accumulate_tokens": True,
"text_input": request_func_input.prompt,
"temperature": 0.0,
"top_p": 1.0,
"max_tokens": request_func_input.output_len,
"stream": True,
}
output = RequestFuncOutput()
output.prompt_len = request_func_input.prompt_len
ttft = 0.0
st = time.perf_counter()
most_recent_timestamp = st
try:
async with session.post(url=api_url, json=payload) as response:
if response.status == 200:
async for chunk_bytes in response.content:
chunk_bytes = chunk_bytes.strip()
if not chunk_bytes:
continue
chunk = remove_prefix(chunk_bytes.decode("utf-8"),
"data:")
data = json.loads(chunk)
output.generated_text += data["text_output"]
timestamp = time.perf_counter()
# First token
if ttft == 0.0:
ttft = time.perf_counter() - st
output.ttft = ttft
# Decoding phase
else:
output.itl.append(timestamp -
most_recent_timestamp)
most_recent_timestamp = timestamp
output.latency = most_recent_timestamp - st
output.success = True
else:
output.error = response.reason or ""
output.success = False
except Exception:
output.success = False
exc_info = sys.exc_info()
output.error = "".join(traceback.format_exception(*exc_info))
if pbar:
pbar.update(1)
return output
async def async_request_deepspeed_mii(
request_func_input: RequestFuncInput,
pbar: Optional[tqdm] = None,
) -> RequestFuncOutput:
async with aiohttp.ClientSession(timeout=AIOHTTP_TIMEOUT) as session:
assert request_func_input.best_of == 1
assert not request_func_input.use_beam_search
payload = {
"prompt": request_func_input.prompt,
"max_tokens": request_func_input.output_len,
"temperature": 0.01, # deepspeed-mii does not accept 0.0 temp.
"top_p": 1.0,
}
output = RequestFuncOutput()
output.prompt_len = request_func_input.prompt_len
# NOTE: DeepSpeed-MII doesn't support streaming as of Jan 28 2024,
# will use 0 as placeholder.
# See https://github.com/microsoft/DeepSpeed-MII/pull/311
output.ttft = 0
st = time.perf_counter()
try:
async with session.post(url=request_func_input.api_url,
json=payload) as response:
if response.status == 200:
parsed_resp = await response.json()
output.latency = time.perf_counter() - st
output.generated_text = parsed_resp["text"][0]
output.success = True
else:
output.error = response.reason or ""
output.success = False
except Exception:
output.success = False
exc_info = sys.exc_info()
output.error = "".join(traceback.format_exception(*exc_info))
if pbar:
pbar.update(1)
return output
async def async_request_openai_completions(
request_func_input: RequestFuncInput,
pbar: Optional[tqdm] = None,
) -> RequestFuncOutput:
api_url = request_func_input.api_url
assert api_url.endswith(
("completions", "profile")
), "OpenAI Completions API URL must end with 'completions' or 'profile'."
async with aiohttp.ClientSession(timeout=AIOHTTP_TIMEOUT) as session:
assert not request_func_input.use_beam_search
payload = {
"model": request_func_input.model,
"prompt": request_func_input.prompt,
"temperature": 0.0,
"best_of": request_func_input.best_of,
"max_tokens": request_func_input.output_len,
"stream": True,
}
headers = {
"Authorization": f"Bearer {os.environ.get('OPENAI_API_KEY')}"
}
output = RequestFuncOutput()
output.prompt_len = request_func_input.prompt_len
generated_text = ""
ttft = 0.0
st = time.perf_counter()
most_recent_timestamp = st
try:
async with session.post(url=api_url, json=payload,
headers=headers) as response:
if response.status == 200:
async for chunk_bytes in response.content:
chunk_bytes = chunk_bytes.strip()
if not chunk_bytes:
continue
chunk = remove_prefix(chunk_bytes.decode("utf-8"),
"data: ")
if chunk == "[DONE]":
latency = time.perf_counter() - st
else:
data = json.loads(chunk)
# NOTE: Some completion API might have a last
# usage summary response without a token so we
# want to check a token was generated
if data["choices"][0]["text"]:
timestamp = time.perf_counter()
# First token
if ttft == 0.0:
ttft = time.perf_counter() - st
output.ttft = ttft
# Decoding phase
else:
output.itl.append(timestamp -
most_recent_timestamp)
most_recent_timestamp = timestamp
generated_text += data["choices"][0]["text"]
output.generated_text = generated_text
output.success = True
output.latency = latency
else:
output.error = response.reason or ""
output.success = False
except Exception:
output.success = False
exc_info = sys.exc_info()
output.error = "".join(traceback.format_exception(*exc_info))
if pbar:
pbar.update(1)
return output
async def async_request_openai_chat_completions(
request_func_input: RequestFuncInput,
pbar: Optional[tqdm] = None,
) -> RequestFuncOutput:
api_url = request_func_input.api_url
assert api_url.endswith(
"chat/completions"
), "OpenAI Chat Completions API URL must end with 'chat/completions'."
async with aiohttp.ClientSession(timeout=AIOHTTP_TIMEOUT) as session:
assert not request_func_input.use_beam_search
payload = {
"model": request_func_input.model,
"messages": [
{
"role": "user",
"content": request_func_input.prompt,
},
],
"temperature": 0.0,
"max_tokens": request_func_input.output_len,
"stream": True,
}
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {os.environ.get('OPENAI_API_KEY')}",
}
output = RequestFuncOutput()
output.prompt_len = request_func_input.prompt_len
generated_text = ""
ttft = 0.0
st = time.perf_counter()
most_recent_timestamp = st
try:
async with session.post(url=api_url, json=payload,
headers=headers) as response:
if response.status == 200:
async for chunk_bytes in response.content:
chunk_bytes = chunk_bytes.strip()
if not chunk_bytes:
continue
chunk = remove_prefix(chunk_bytes.decode("utf-8"),
"data: ")
if chunk == "[DONE]":
latency = time.perf_counter() - st
else:
timestamp = time.perf_counter()
data = json.loads(chunk)
delta = data["choices"][0]["delta"]
if delta.get("content", None):
# First token
if ttft == 0.0:
ttft = time.perf_counter() - st
output.ttft = ttft
# Decoding phase
else:
output.itl.append(timestamp -
most_recent_timestamp)
generated_text += delta["content"]
most_recent_timestamp = timestamp
output.generated_text = generated_text
output.success = True
output.latency = latency
else:
output.error = response.reason or ""
output.success = False
except Exception:
output.success = False
exc_info = sys.exc_info()
output.error = "".join(traceback.format_exception(*exc_info))
if pbar:
pbar.update(1)
return output
# Since vllm must support Python 3.8, we can't use str.removeprefix(prefix)
# introduced in Python 3.9
def remove_prefix(text: str, prefix: str) -> str:
if text.startswith(prefix):
return text[len(prefix):]
return text
def get_model(pretrained_model_name_or_path: str) -> str:
if os.getenv('VLLM_USE_MODELSCOPE', 'False').lower() == 'true':
from modelscope import snapshot_download
model_path = snapshot_download(
model_id=pretrained_model_name_or_path,
local_files_only=huggingface_hub.constants.HF_HUB_OFFLINE,
ignore_file_pattern=[".*.pt", ".*.safetensors", ".*.bin"])
return model_path
return pretrained_model_name_or_path
def get_tokenizer(
pretrained_model_name_or_path: str, trust_remote_code: bool
) -> Union[PreTrainedTokenizer, PreTrainedTokenizerFast]:
if pretrained_model_name_or_path is not None and not os.path.exists(
pretrained_model_name_or_path):
pretrained_model_name_or_path = get_model(
pretrained_model_name_or_path)
return AutoTokenizer.from_pretrained(pretrained_model_name_or_path,
trust_remote_code=trust_remote_code)
ASYNC_REQUEST_FUNCS = {
"tgi": async_request_tgi,
"vllm": async_request_openai_completions,
"lmdeploy": async_request_openai_completions,
"deepspeed-mii": async_request_deepspeed_mii,
"openai": async_request_openai_completions,
"openai-chat": async_request_openai_chat_completions,
"tensorrt-llm": async_request_trt_llm,
"scalellm": async_request_openai_completions,
}
@@ -0,0 +1,770 @@
"""Benchmark online serving throughput.
On the server side, run one of the following commands:
vLLM OpenAI API server
vllm serve <your_model> \
--swap-space 16 \
--disable-log-requests
(TGI backend)
./launch_tgi_server.sh <your_model> <max_batch_total_tokens>
On the client side, run:
python benchmarks/benchmark_serving.py \
--backend <backend> \
--model <your_model> \
--dataset-name sharegpt \
--dataset-path <path to dataset> \
--request-rate <request_rate> \ # By default <request_rate> is inf
--num-prompts <num_prompts> # By default <num_prompts> is 1000
when using tgi backend, add
--endpoint /generate_stream
to the end of the command above.
"""
import argparse
import asyncio
import json
import os
import random
import time
import warnings
from dataclasses import dataclass
from datetime import datetime
from typing import Any, AsyncGenerator, Dict, List, Optional, Tuple
import numpy as np
from backend_request_func import (ASYNC_REQUEST_FUNCS, RequestFuncInput,
RequestFuncOutput)
from tqdm.asyncio import tqdm
from transformers import PreTrainedTokenizerBase
try:
from vllm.transformers_utils.tokenizer import get_tokenizer
except ImportError:
from backend_request_func import get_tokenizer
try:
from vllm.utils import FlexibleArgumentParser
except ImportError:
from argparse import ArgumentParser as FlexibleArgumentParser
@dataclass
class BenchmarkMetrics:
completed: int
total_input: int
total_output: int
request_throughput: float
input_throughput: float
output_throughput: float
mean_ttft_ms: float
median_ttft_ms: float
std_ttft_ms: float
p99_ttft_ms: float
mean_tpot_ms: float
median_tpot_ms: float
std_tpot_ms: float
p99_tpot_ms: float
mean_itl_ms: float
median_itl_ms: float
std_itl_ms: float
p99_itl_ms: float
def sample_sharegpt_requests(
dataset_path: str,
num_requests: int,
tokenizer: PreTrainedTokenizerBase,
fixed_output_len: Optional[int] = None,
) -> List[Tuple[str, int, int]]:
if fixed_output_len is not None and fixed_output_len < 4:
raise ValueError("output_len too small")
# Load the dataset.
with open(dataset_path) as f:
dataset = json.load(f)
# Filter out the conversations with less than 2 turns.
dataset = [data for data in dataset if len(data["conversations"]) >= 2]
# Only keep the first two turns of each conversation.
dataset = [(data["conversations"][0]["value"],
data["conversations"][1]["value"]) for data in dataset]
# Shuffle the dataset.
random.shuffle(dataset)
# Filter out sequences that are too long or too short
filtered_dataset: List[Tuple[str, int, int]] = []
for i in range(len(dataset)):
if len(filtered_dataset) == num_requests:
break
# Tokenize the prompts and completions.
prompt = dataset[i][0]
prompt_token_ids = tokenizer(prompt).input_ids
completion = dataset[i][1]
completion_token_ids = tokenizer(completion).input_ids
prompt_len = len(prompt_token_ids)
output_len = len(completion_token_ids
) if fixed_output_len is None else fixed_output_len
if prompt_len < 4 or output_len < 4:
# Prune too short sequences.
continue
if prompt_len > 1024 or prompt_len + output_len > 2048:
# Prune too long sequences.
continue
filtered_dataset.append((prompt, prompt_len, output_len))
return filtered_dataset
def sample_sonnet_requests(
dataset_path: str,
num_requests: int,
input_len: int,
output_len: int,
prefix_len: int,
tokenizer: PreTrainedTokenizerBase,
) -> List[Tuple[str, str, int, int]]:
assert (
input_len > prefix_len
), "'args.sonnet-input-len' must be greater than 'args.prefix-input-len'."
# Load the dataset.
with open(dataset_path) as f:
poem_lines = f.readlines()
# Tokenize the poem lines.
poem_token_ids = tokenizer(poem_lines).input_ids
average_poem_len = sum(
len(token_ids) for token_ids in poem_token_ids) / len(poem_token_ids)
# Base prefix for all requests.
base_prompt = "Pick as many lines as you can from these poem lines:\n"
base_message = [{
"role": "user",
"content": base_prompt,
}]
base_prompt_formatted = tokenizer.apply_chat_template(
base_message, add_generation_prompt=True, tokenize=False)
base_prompt_offset = len(tokenizer(base_prompt_formatted).input_ids)
assert (
input_len > base_prompt_offset
), f"Please set 'args.sonnet-input-len' higher than {base_prompt_offset}."
num_input_lines = round(
(input_len - base_prompt_offset) / average_poem_len)
# First approximately `prefix_len` number of tokens in the
# prompt are fixed poem lines.
assert (
prefix_len > base_prompt_offset
), f"Please set 'args.sonnet-prefix-len' higher than {base_prompt_offset}."
num_prefix_lines = round(
(prefix_len - base_prompt_offset) / average_poem_len)
prefix_lines = poem_lines[:num_prefix_lines]
# Sample the rest of lines per request.
sampled_requests: List[Tuple[str, int, int]] = []
for _ in range(num_requests):
sampled_lines = "".join(
prefix_lines +
random.sample(poem_lines, num_input_lines - num_prefix_lines))
prompt = f"{base_prompt}{sampled_lines}"
message = [
{
"role": "user",
"content": prompt,
},
]
prompt_formatted = tokenizer.apply_chat_template(
message, add_generation_prompt=True, tokenize=False)
prompt_len = len(tokenizer(prompt_formatted).input_ids)
sampled_requests.append(
(prompt, prompt_formatted, prompt_len, output_len))
return sampled_requests
def sample_random_requests(
input_len: int, output_len: int, num_prompts: int, range_ratio: float,
tokenizer: PreTrainedTokenizerBase) -> List[Tuple[str, int, int]]:
input_lens = np.random.randint(
int(input_len * range_ratio),
input_len + 1,
size=num_prompts,
)
output_lens = np.random.randint(
int(output_len * range_ratio),
output_len + 1,
size=num_prompts,
)
offsets = np.random.randint(0, tokenizer.vocab_size, size=num_prompts)
input_requests = []
for i in range(num_prompts):
prompt = tokenizer.decode([(offsets[i] + i + j) % tokenizer.vocab_size
for j in range(input_lens[i])])
input_requests.append(
(prompt, int(input_lens[i]), int(output_lens[i])))
return input_requests
async def get_request(
input_requests: List[Tuple[str, int, int]],
request_rate: float,
) -> AsyncGenerator[Tuple[str, int, int], None]:
input_requests = iter(input_requests)
for request in input_requests:
yield request
if request_rate == float("inf"):
# If the request rate is infinity, then we don't need to wait.
continue
# Sample the request interval from the exponential distribution.
interval = np.random.exponential(1.0 / request_rate)
# The next request will be sent after the interval.
await asyncio.sleep(interval)
def calculate_metrics(
input_requests: List[Tuple[str, int, int]],
outputs: List[RequestFuncOutput],
dur_s: float,
tokenizer: PreTrainedTokenizerBase,
) -> Tuple[BenchmarkMetrics, List[int]]:
actual_output_lens: List[int] = []
total_input = 0
completed = 0
itls: List[float] = []
tpots: List[float] = []
ttfts: List[float] = []
for i in range(len(outputs)):
if outputs[i].success:
# We use the tokenizer to count the number of output tokens for all
# serving backends instead of looking at len(outputs[i].itl) since
# multiple output tokens may be bundled together
# Note : this may inflate the output token count slightly
output_len = len(
tokenizer(outputs[i].generated_text,
add_special_tokens=False).input_ids)
actual_output_lens.append(output_len)
total_input += input_requests[i][1]
if output_len > 1:
tpots.append(
(outputs[i].latency - outputs[i].ttft) / (output_len - 1))
itls += outputs[i].itl
ttfts.append(outputs[i].ttft)
completed += 1
else:
actual_output_lens.append(0)
if completed == 0:
warnings.warn(
"All requests failed. This is likely due to a misconfiguration "
"on the benchmark arguments.",
stacklevel=2)
metrics = BenchmarkMetrics(
completed=completed,
total_input=total_input,
total_output=sum(actual_output_lens),
request_throughput=completed / dur_s,
input_throughput=total_input / dur_s,
output_throughput=sum(actual_output_lens) / dur_s,
mean_ttft_ms=np.mean(ttfts or 0) *
1000, # ttfts is empty if streaming is not supported by backend
median_ttft_ms=np.median(ttfts or 0) * 1000,
std_ttft_ms=np.std(ttfts or 0) * 1000,
p99_ttft_ms=np.percentile(ttfts or 0, 99) * 1000,
mean_tpot_ms=np.mean(tpots or 0) * 1000,
median_tpot_ms=np.median(tpots or 0) * 1000,
std_tpot_ms=np.std(tpots or 0) * 1000,
p99_tpot_ms=np.percentile(tpots or 0, 99) * 1000,
mean_itl_ms=np.mean(itls or 0) * 1000,
median_itl_ms=np.median(itls or 0) * 1000,
std_itl_ms=np.std(itls or 0) * 1000,
p99_itl_ms=np.percentile(itls or 0, 99) * 1000,
)
return metrics, actual_output_lens
async def benchmark(
backend: str,
api_url: str,
base_url: str,
model_id: str,
tokenizer: PreTrainedTokenizerBase,
input_requests: List[Tuple[str, int, int]],
best_of: int,
use_beam_search: bool,
request_rate: float,
disable_tqdm: bool,
profile: bool,
):
if backend in ASYNC_REQUEST_FUNCS:
request_func = ASYNC_REQUEST_FUNCS[backend]
else:
raise ValueError(f"Unknown backend: {backend}")
print("Starting initial single prompt test run...")
test_prompt, test_prompt_len, test_output_len = input_requests[0]
test_input = RequestFuncInput(
model=model_id,
prompt=test_prompt,
api_url=api_url,
prompt_len=test_prompt_len,
output_len=test_output_len,
best_of=best_of,
use_beam_search=use_beam_search,
)
test_output = await request_func(request_func_input=test_input)
if not test_output.success:
raise ValueError(
"Initial test run failed - Please make sure benchmark arguments "
f"are correctly specified. Error: {test_output.error}")
else:
print("Initial test run completed. Starting main benchmark run...")
if profile:
print("Starting profiler...")
profile_input = RequestFuncInput(
model=model_id,
prompt=test_prompt,
api_url=base_url + "/start_profile",
prompt_len=test_prompt_len,
output_len=test_output_len,
best_of=best_of,
use_beam_search=use_beam_search,
)
profile_output = await request_func(request_func_input=profile_input)
if profile_output.success:
print("Profiler started")
print(f"Traffic request rate: {request_rate}")
pbar = None if disable_tqdm else tqdm(total=len(input_requests))
benchmark_start_time = time.perf_counter()
tasks: List[asyncio.Task] = []
async for request in get_request(input_requests, request_rate):
prompt, prompt_len, output_len = request
request_func_input = RequestFuncInput(
model=model_id,
prompt=prompt,
api_url=api_url,
prompt_len=prompt_len,
output_len=output_len,
best_of=best_of,
use_beam_search=use_beam_search,
)
tasks.append(
asyncio.create_task(
request_func(request_func_input=request_func_input,
pbar=pbar)))
outputs: List[RequestFuncOutput] = await asyncio.gather(*tasks)
if profile:
print("Stopping profiler...")
profile_input = RequestFuncInput(
model=model_id,
prompt=test_prompt,
api_url=base_url + "/stop_profile",
prompt_len=test_prompt_len,
output_len=test_output_len,
best_of=best_of,
use_beam_search=use_beam_search,
)
profile_output = await request_func(request_func_input=profile_input)
if profile_output.success:
print("Profiler stopped")
if pbar is not None:
pbar.close()
benchmark_duration = time.perf_counter() - benchmark_start_time
metrics, actual_output_lens = calculate_metrics(
input_requests=input_requests,
outputs=outputs,
dur_s=benchmark_duration,
tokenizer=tokenizer,
)
print("{s:{c}^{n}}".format(s=' Serving Benchmark Result ', n=50, c='='))
print("{:<40} {:<10}".format("Successful requests:", metrics.completed))
print("{:<40} {:<10.2f}".format("Benchmark duration (s):",
benchmark_duration))
print("{:<40} {:<10}".format("Total input tokens:", metrics.total_input))
print("{:<40} {:<10}".format("Total generated tokens:",
metrics.total_output))
print("{:<40} {:<10.2f}".format("Request throughput (req/s):",
metrics.request_throughput))
print("{:<40} {:<10.2f}".format("Input token throughput (tok/s):",
metrics.input_throughput))
print("{:<40} {:<10.2f}".format("Output token throughput (tok/s):",
metrics.output_throughput))
print("{s:{c}^{n}}".format(s='Time to First Token', n=50, c='-'))
print("{:<40} {:<10.2f}".format("Mean TTFT (ms):", metrics.mean_ttft_ms))
print("{:<40} {:<10.2f}".format("Median TTFT (ms):",
metrics.median_ttft_ms))
print("{:<40} {:<10.2f}".format("P99 TTFT (ms):", metrics.p99_ttft_ms))
print("{s:{c}^{n}}".format(s='Time per Output Token (excl. 1st token)',
n=50,
c='-'))
print("{:<40} {:<10.2f}".format("Mean TPOT (ms):", metrics.mean_tpot_ms))
print("{:<40} {:<10.2f}".format("Median TPOT (ms):",
metrics.median_tpot_ms))
print("{:<40} {:<10.2f}".format("P99 TPOT (ms):", metrics.p99_tpot_ms))
print("{s:{c}^{n}}".format(s='Inter-token Latency', n=50, c='-'))
print("{:<40} {:<10.2f}".format("Mean ITL (ms):", metrics.mean_itl_ms))
print("{:<40} {:<10.2f}".format("Median ITL (ms):", metrics.median_itl_ms))
print("{:<40} {:<10.2f}".format("P99 ITL (ms):", metrics.p99_itl_ms))
print("=" * 50)
result = {
"duration": benchmark_duration,
"completed": metrics.completed,
"total_input_tokens": metrics.total_input,
"total_output_tokens": metrics.total_output,
"request_throughput": metrics.request_throughput,
"input_throughput": metrics.input_throughput,
"output_throughput": metrics.output_throughput,
"mean_ttft_ms": metrics.mean_ttft_ms,
"median_ttft_ms": metrics.median_ttft_ms,
"std_ttft_ms": metrics.std_ttft_ms,
"p99_ttft_ms": metrics.p99_ttft_ms,
"mean_tpot_ms": metrics.mean_tpot_ms,
"median_tpot_ms": metrics.median_tpot_ms,
"std_tpot_ms": metrics.std_tpot_ms,
"p99_tpot_ms": metrics.p99_tpot_ms,
"mean_itl_ms": metrics.mean_itl_ms,
"median_itl_ms": metrics.median_itl_ms,
"std_itl_ms": metrics.std_itl_ms,
"p99_itl_ms": metrics.p99_itl_ms,
"input_lens": [output.prompt_len for output in outputs],
"output_lens": actual_output_lens,
"ttfts": [output.ttft for output in outputs],
"itls": [output.itl for output in outputs],
"generated_texts": [output.generated_text for output in outputs],
"errors": [output.error for output in outputs],
}
return result
def main(args: argparse.Namespace):
print(args)
random.seed(args.seed)
np.random.seed(args.seed)
backend = args.backend
model_id = args.model
tokenizer_id = args.tokenizer if args.tokenizer is not None else args.model
if args.base_url is not None:
api_url = f"{args.base_url}{args.endpoint}"
base_url = f"{args.base_url}"
else:
api_url = f"http://{args.host}:{args.port}{args.endpoint}"
base_url = f"http://{args.host}:{args.port}"
tokenizer = get_tokenizer(tokenizer_id,
trust_remote_code=args.trust_remote_code)
if args.dataset is not None:
warnings.warn(
"The '--dataset' argument will be deprecated in the next "
"release. Please use '--dataset-name' and "
"'--dataset-path' in the future runs.",
stacklevel=2)
input_requests = sample_sharegpt_requests(
dataset_path=args.dataset,
num_requests=args.num_prompts,
tokenizer=tokenizer,
fixed_output_len=args.sharegpt_output_len,
)
elif args.dataset_name == "sharegpt":
input_requests = sample_sharegpt_requests(
dataset_path=args.dataset_path,
num_requests=args.num_prompts,
tokenizer=tokenizer,
fixed_output_len=args.sharegpt_output_len,
)
elif args.dataset_name == "sonnet":
# Do not format the prompt, pass to message directly
if args.backend == "openai-chat":
input_requests = sample_sonnet_requests(
dataset_path=args.dataset_path,
num_requests=args.num_prompts,
input_len=args.sonnet_input_len,
output_len=args.sonnet_output_len,
prefix_len=args.sonnet_prefix_len,
tokenizer=tokenizer,
)
input_requests = [(prompt, prompt_len, output_len)
for prompt, prompt_formatted, prompt_len,
output_len in input_requests]
else:
assert (
tokenizer.chat_template or tokenizer.default_chat_template
), "Tokenizer/model must have chat template for sonnet dataset."
input_requests = sample_sonnet_requests(
dataset_path=args.dataset_path,
num_requests=args.num_prompts,
input_len=args.sonnet_input_len,
output_len=args.sonnet_output_len,
prefix_len=args.sonnet_prefix_len,
tokenizer=tokenizer,
)
input_requests = [(prompt_formatted, prompt_len, output_len)
for prompt, prompt_formatted, prompt_len,
output_len in input_requests]
elif args.dataset_name == "random":
input_requests = sample_random_requests(
input_len=args.random_input_len,
output_len=args.random_output_len,
num_prompts=args.num_prompts,
range_ratio=args.random_range_ratio,
tokenizer=tokenizer,
)
else:
raise ValueError(f"Unknown dataset: {args.dataset_name}")
benchmark_result = asyncio.run(
benchmark(
backend=backend,
api_url=api_url,
base_url=base_url,
model_id=model_id,
tokenizer=tokenizer,
input_requests=input_requests,
best_of=args.best_of,
use_beam_search=args.use_beam_search,
request_rate=args.request_rate,
disable_tqdm=args.disable_tqdm,
profile=args.profile,
))
# Save config and results to json
if args.save_result:
result_json: Dict[str, Any] = {}
# Setup
current_dt = datetime.now().strftime("%Y%m%d-%H%M%S")
result_json["date"] = current_dt
result_json["backend"] = backend
result_json["model_id"] = model_id
result_json["tokenizer_id"] = tokenizer_id
result_json["best_of"] = args.best_of
result_json["use_beam_search"] = args.use_beam_search
result_json["num_prompts"] = args.num_prompts
# Metadata
if args.metadata:
for item in args.metadata:
if "=" in item:
kvstring = item.split("=")
result_json[kvstring[0].strip()] = kvstring[1].strip()
else:
raise ValueError(
"Invalid metadata format. Please use KEY=VALUE format."
)
# Traffic
result_json["request_rate"] = (
args.request_rate if args.request_rate < float("inf") else "inf")
# Merge with benchmark result
result_json = {**result_json, **benchmark_result}
# Save to file
base_model_id = model_id.split("/")[-1]
file_name = f"{backend}-{args.request_rate}qps-{base_model_id}-{current_dt}.json" #noqa
if args.result_filename:
file_name = args.result_filename
if args.result_dir:
file_name = os.path.join(args.result_dir, file_name)
with open(file_name, "w") as outfile:
json.dump(result_json, outfile)
if __name__ == "__main__":
parser = FlexibleArgumentParser(
description="Benchmark the online serving throughput.")
parser.add_argument(
"--backend",
type=str,
default="vllm",
choices=list(ASYNC_REQUEST_FUNCS.keys()),
)
parser.add_argument(
"--base-url",
type=str,
default=None,
help="Server or API base url if not using http host and port.",
)
parser.add_argument("--host", type=str, default="localhost")
parser.add_argument("--port", type=int, default=8000)
parser.add_argument(
"--endpoint",
type=str,
default="/v1/completions",
help="API endpoint.",
)
parser.add_argument(
"--dataset",
type=str,
default=None,
help="Path to the ShareGPT dataset, will be deprecated in the "
"next release.",
)
parser.add_argument(
"--dataset-name",
type=str,
default="sharegpt",
choices=["sharegpt", "sonnet", "random"],
help="Name of the dataset to benchmark on.",
)
parser.add_argument("--dataset-path",
type=str,
default=None,
help="Path to the dataset.")
parser.add_argument(
"--model",
type=str,
required=True,
help="Name of the model.",
)
parser.add_argument(
"--tokenizer",
type=str,
help=
"Name or path of the tokenizer, if not using the default tokenizer.", # noqa: E501
)
parser.add_argument(
"--best-of",
type=int,
default=1,
help="Generates `best_of` sequences per prompt and "
"returns the best one.",
)
parser.add_argument("--use-beam-search", action="store_true")
parser.add_argument(
"--num-prompts",
type=int,
default=1000,
help="Number of prompts to process.",
)
parser.add_argument(
"--sharegpt-output-len",
type=int,
default=None,
help="Output length for each request. Overrides the output length "
"from the ShareGPT dataset.")
parser.add_argument(
"--sonnet-input-len",
type=int,
default=550,
help=
"Number of input tokens per request, used only for sonnet dataset.",
)
parser.add_argument(
"--sonnet-output-len",
type=int,
default=150,
help=
"Number of output tokens per request, used only for sonnet dataset.",
)
parser.add_argument(
"--sonnet-prefix-len",
type=int,
default=200,
help=
"Number of prefix tokens per request, used only for sonnet dataset.",
)
parser.add_argument(
"--random-input-len",
type=int,
default=1024,
help=
"Number of input tokens per request, used only for random sampling.",
)
parser.add_argument(
"--random-output-len",
type=int,
default=128,
help=
"Number of output tokens per request, used only for random sampling.",
)
parser.add_argument(
"--random-range-ratio",
type=float,
default=1.0,
help="Range of sampled ratio of input/output length, "
"used only for random sampling.",
)
parser.add_argument(
"--request-rate",
type=float,
default=float("inf"),
help="Number of requests per second. If this is inf, "
"then all the requests are sent at time 0. "
"Otherwise, we use Poisson process to synthesize "
"the request arrival times.",
)
parser.add_argument("--seed", type=int, default=0)
parser.add_argument(
"--trust-remote-code",
action="store_true",
help="Trust remote code from huggingface",
)
parser.add_argument(
"--disable-tqdm",
action="store_true",
help="Specify to disable tqdm progress bar.",
)
parser.add_argument(
"--profile",
action="store_true",
help="Use Torch Profiler. The endpoint must be launched with "
"VLLM_TORCH_PROFILER_DIR to enable profiler.",
)
parser.add_argument(
"--save-result",
action="store_true",
help="Specify to save benchmark results to a json file",
)
parser.add_argument(
"--metadata",
metavar="KEY=VALUE",
nargs="*",
help="Key-value pairs (e.g, --metadata version=0.3.3 tp=1) "
"for metadata of this run to be saved in the result JSON file "
"for record keeping purposes.",
)
parser.add_argument(
"--result-dir",
type=str,
default=None,
help="Specify directory to save benchmark json results."
"If not specified, results are saved in the current directory.",
)
parser.add_argument(
"--result-filename",
type=str,
default=None,
help="Specify the filename to save benchmark json results."
"If not specified, results will be saved in "
"{backend}-{args.request_rate}qps-{base_model_id}-{current_dt}.json"
" format.",
)
args = parser.parse_args()
main(args)
@@ -0,0 +1,518 @@
FROM fairest creatures we desire increase,
That thereby beauty's rose might never die,
But as the riper should by time decease,
His tender heir might bear his memory:
But thou, contracted to thine own bright eyes,
Feed'st thy light'st flame with self-substantial fuel,
Making a famine where abundance lies,
Thyself thy foe, to thy sweet self too cruel.
Thou that art now the world's fresh ornament
And only herald to the gaudy spring,
Within thine own bud buriest thy content
And, tender churl, makest waste in niggarding.
Pity the world, or else this glutton be,
To eat the world's due, by the grave and thee.
When forty winters shall beseige thy brow,
And dig deep trenches in thy beauty's field,
Thy youth's proud livery, so gazed on now,
Will be a tatter'd weed, of small worth held:
Then being ask'd where all thy beauty lies,
Where all the treasure of thy lusty days,
To say, within thine own deep-sunken eyes,
Were an all-eating shame and thriftless praise.
How much more praise deserved thy beauty's use,
If thou couldst answer 'This fair child of mine
Shall sum my count and make my old excuse,'
Proving his beauty by succession thine!
This were to be new made when thou art old,
And see thy blood warm when thou feel'st it cold.
Look in thy glass, and tell the face thou viewest
Now is the time that face should form another;
Whose fresh repair if now thou not renewest,
Thou dost beguile the world, unbless some mother.
For where is she so fair whose unear'd womb
Disdains the tillage of thy husbandry?
Or who is he so fond will be the tomb
Of his self-love, to stop posterity?
Thou art thy mother's glass, and she in thee
Calls back the lovely April of her prime:
So thou through windows of thine age shall see
Despite of wrinkles this thy golden time.
But if thou live, remember'd not to be,
Die single, and thine image dies with thee.
Unthrifty loveliness, why dost thou spend
Upon thyself thy beauty's legacy?
Nature's bequest gives nothing but doth lend,
And being frank she lends to those are free.
Then, beauteous niggard, why dost thou abuse
The bounteous largess given thee to give?
Profitless usurer, why dost thou use
So great a sum of sums, yet canst not live?
For having traffic with thyself alone,
Thou of thyself thy sweet self dost deceive.
Then how, when nature calls thee to be gone,
What acceptable audit canst thou leave?
Thy unused beauty must be tomb'd with thee,
Which, used, lives th' executor to be.
Those hours, that with gentle work did frame
The lovely gaze where every eye doth dwell,
Will play the tyrants to the very same
And that unfair which fairly doth excel:
For never-resting time leads summer on
To hideous winter and confounds him there;
Sap cheque'd with frost and lusty leaves quite gone,
Beauty o'ersnow'd and bareness every where:
Then, were not summer's distillation left,
A liquid prisoner pent in walls of glass,
Beauty's effect with beauty were bereft,
Nor it nor no remembrance what it was:
But flowers distill'd though they with winter meet,
Leese but their show; their substance still lives sweet.
Then let not winter's ragged hand deface
In thee thy summer, ere thou be distill'd:
Make sweet some vial; treasure thou some place
With beauty's treasure, ere it be self-kill'd.
That use is not forbidden usury,
Which happies those that pay the willing loan;
That's for thyself to breed another thee,
Or ten times happier, be it ten for one;
Ten times thyself were happier than thou art,
If ten of thine ten times refigured thee:
Then what could death do, if thou shouldst depart,
Leaving thee living in posterity?
Be not self-will'd, for thou art much too fair
To be death's conquest and make worms thine heir.
Lo! in the orient when the gracious light
Lifts up his burning head, each under eye
Doth homage to his new-appearing sight,
Serving with looks his sacred majesty;
And having climb'd the steep-up heavenly hill,
Resembling strong youth in his middle age,
yet mortal looks adore his beauty still,
Attending on his golden pilgrimage;
But when from highmost pitch, with weary car,
Like feeble age, he reeleth from the day,
The eyes, 'fore duteous, now converted are
From his low tract and look another way:
So thou, thyself out-going in thy noon,
Unlook'd on diest, unless thou get a son.
Music to hear, why hear'st thou music sadly?
Sweets with sweets war not, joy delights in joy.
Why lovest thou that which thou receivest not gladly,
Or else receivest with pleasure thine annoy?
If the true concord of well-tuned sounds,
By unions married, do offend thine ear,
They do but sweetly chide thee, who confounds
In singleness the parts that thou shouldst bear.
Mark how one string, sweet husband to another,
Strikes each in each by mutual ordering,
Resembling sire and child and happy mother
Who all in one, one pleasing note do sing:
Whose speechless song, being many, seeming one,
Sings this to thee: 'thou single wilt prove none.'
Is it for fear to wet a widow's eye
That thou consumest thyself in single life?
Ah! if thou issueless shalt hap to die.
The world will wail thee, like a makeless wife;
The world will be thy widow and still weep
That thou no form of thee hast left behind,
When every private widow well may keep
By children's eyes her husband's shape in mind.
Look, what an unthrift in the world doth spend
Shifts but his place, for still the world enjoys it;
But beauty's waste hath in the world an end,
And kept unused, the user so destroys it.
No love toward others in that bosom sits
That on himself such murderous shame commits.
For shame! deny that thou bear'st love to any,
Who for thyself art so unprovident.
Grant, if thou wilt, thou art beloved of many,
But that thou none lovest is most evident;
For thou art so possess'd with murderous hate
That 'gainst thyself thou stick'st not to conspire.
Seeking that beauteous roof to ruinate
Which to repair should be thy chief desire.
O, change thy thought, that I may change my mind!
Shall hate be fairer lodged than gentle love?
Be, as thy presence is, gracious and kind,
Or to thyself at least kind-hearted prove:
Make thee another self, for love of me,
That beauty still may live in thine or thee.
As fast as thou shalt wane, so fast thou growest
In one of thine, from that which thou departest;
And that fresh blood which youngly thou bestowest
Thou mayst call thine when thou from youth convertest.
Herein lives wisdom, beauty and increase:
Without this, folly, age and cold decay:
If all were minded so, the times should cease
And threescore year would make the world away.
Let those whom Nature hath not made for store,
Harsh featureless and rude, barrenly perish:
Look, whom she best endow'd she gave the more;
Which bounteous gift thou shouldst in bounty cherish:
She carved thee for her seal, and meant thereby
Thou shouldst print more, not let that copy die.
When I do count the clock that tells the time,
And see the brave day sunk in hideous night;
When I behold the violet past prime,
And sable curls all silver'd o'er with white;
When lofty trees I see barren of leaves
Which erst from heat did canopy the herd,
And summer's green all girded up in sheaves
Borne on the bier with white and bristly beard,
Then of thy beauty do I question make,
That thou among the wastes of time must go,
Since sweets and beauties do themselves forsake
And die as fast as they see others grow;
And nothing 'gainst Time's scythe can make defence
Save breed, to brave him when he takes thee hence.
O, that you were yourself! but, love, you are
No longer yours than you yourself here live:
Against this coming end you should prepare,
And your sweet semblance to some other give.
So should that beauty which you hold in lease
Find no determination: then you were
Yourself again after yourself's decease,
When your sweet issue your sweet form should bear.
Who lets so fair a house fall to decay,
Which husbandry in honour might uphold
Against the stormy gusts of winter's day
And barren rage of death's eternal cold?
O, none but unthrifts! Dear my love, you know
You had a father: let your son say so.
Not from the stars do I my judgment pluck;
And yet methinks I have astronomy,
But not to tell of good or evil luck,
Of plagues, of dearths, or seasons' quality;
Nor can I fortune to brief minutes tell,
Pointing to each his thunder, rain and wind,
Or say with princes if it shall go well,
By oft predict that I in heaven find:
But from thine eyes my knowledge I derive,
And, constant stars, in them I read such art
As truth and beauty shall together thrive,
If from thyself to store thou wouldst convert;
Or else of thee this I prognosticate:
Thy end is truth's and beauty's doom and date.
When I consider every thing that grows
Holds in perfection but a little moment,
That this huge stage presenteth nought but shows
Whereon the stars in secret influence comment;
When I perceive that men as plants increase,
Cheered and cheque'd even by the self-same sky,
Vaunt in their youthful sap, at height decrease,
And wear their brave state out of memory;
Then the conceit of this inconstant stay
Sets you most rich in youth before my sight,
Where wasteful Time debateth with Decay,
To change your day of youth to sullied night;
And all in war with Time for love of you,
As he takes from you, I engraft you new.
But wherefore do not you a mightier way
Make war upon this bloody tyrant, Time?
And fortify yourself in your decay
With means more blessed than my barren rhyme?
Now stand you on the top of happy hours,
And many maiden gardens yet unset
With virtuous wish would bear your living flowers,
Much liker than your painted counterfeit:
So should the lines of life that life repair,
Which this, Time's pencil, or my pupil pen,
Neither in inward worth nor outward fair,
Can make you live yourself in eyes of men.
To give away yourself keeps yourself still,
And you must live, drawn by your own sweet skill.
Who will believe my verse in time to come,
If it were fill'd with your most high deserts?
Though yet, heaven knows, it is but as a tomb
Which hides your life and shows not half your parts.
If I could write the beauty of your eyes
And in fresh numbers number all your graces,
The age to come would say 'This poet lies:
Such heavenly touches ne'er touch'd earthly faces.'
So should my papers yellow'd with their age
Be scorn'd like old men of less truth than tongue,
And your true rights be term'd a poet's rage
And stretched metre of an antique song:
But were some child of yours alive that time,
You should live twice; in it and in my rhyme.
Shall I compare thee to a summer's day?
Thou art more lovely and more temperate:
Rough winds do shake the darling buds of May,
And summer's lease hath all too short a date:
Sometime too hot the eye of heaven shines,
And often is his gold complexion dimm'd;
And every fair from fair sometime declines,
By chance or nature's changing course untrimm'd;
But thy eternal summer shall not fade
Nor lose possession of that fair thou owest;
Nor shall Death brag thou wander'st in his shade,
When in eternal lines to time thou growest:
So long as men can breathe or eyes can see,
So long lives this and this gives life to thee.
Devouring Time, blunt thou the lion's paws,
And make the earth devour her own sweet brood;
Pluck the keen teeth from the fierce tiger's jaws,
And burn the long-lived phoenix in her blood;
Make glad and sorry seasons as thou fleets,
And do whate'er thou wilt, swift-footed Time,
To the wide world and all her fading sweets;
But I forbid thee one most heinous crime:
O, carve not with thy hours my love's fair brow,
Nor draw no lines there with thine antique pen;
Him in thy course untainted do allow
For beauty's pattern to succeeding men.
Yet, do thy worst, old Time: despite thy wrong,
My love shall in my verse ever live young.
A woman's face with Nature's own hand painted
Hast thou, the master-mistress of my passion;
A woman's gentle heart, but not acquainted
With shifting change, as is false women's fashion;
An eye more bright than theirs, less false in rolling,
Gilding the object whereupon it gazeth;
A man in hue, all 'hues' in his controlling,
Much steals men's eyes and women's souls amazeth.
And for a woman wert thou first created;
Till Nature, as she wrought thee, fell a-doting,
And by addition me of thee defeated,
By adding one thing to my purpose nothing.
But since she prick'd thee out for women's pleasure,
Mine be thy love and thy love's use their treasure.
So is it not with me as with that Muse
Stirr'd by a painted beauty to his verse,
Who heaven itself for ornament doth use
And every fair with his fair doth rehearse
Making a couplement of proud compare,
With sun and moon, with earth and sea's rich gems,
With April's first-born flowers, and all things rare
That heaven's air in this huge rondure hems.
O' let me, true in love, but truly write,
And then believe me, my love is as fair
As any mother's child, though not so bright
As those gold candles fix'd in heaven's air:
Let them say more than like of hearsay well;
I will not praise that purpose not to sell.
My glass shall not persuade me I am old,
So long as youth and thou are of one date;
But when in thee time's furrows I behold,
Then look I death my days should expiate.
For all that beauty that doth cover thee
Is but the seemly raiment of my heart,
Which in thy breast doth live, as thine in me:
How can I then be elder than thou art?
O, therefore, love, be of thyself so wary
As I, not for myself, but for thee will;
Bearing thy heart, which I will keep so chary
As tender nurse her babe from faring ill.
Presume not on thy heart when mine is slain;
Thou gavest me thine, not to give back again.
As an unperfect actor on the stage
Who with his fear is put besides his part,
Or some fierce thing replete with too much rage,
Whose strength's abundance weakens his own heart.
So I, for fear of trust, forget to say
The perfect ceremony of love's rite,
And in mine own love's strength seem to decay,
O'ercharged with burden of mine own love's might.
O, let my books be then the eloquence
And dumb presagers of my speaking breast,
Who plead for love and look for recompense
More than that tongue that more hath more express'd.
O, learn to read what silent love hath writ:
To hear with eyes belongs to love's fine wit.
Mine eye hath play'd the painter and hath stell'd
Thy beauty's form in table of my heart;
My body is the frame wherein 'tis held,
And perspective it is the painter's art.
For through the painter must you see his skill,
To find where your true image pictured lies;
Which in my bosom's shop is hanging still,
That hath his windows glazed with thine eyes.
Now see what good turns eyes for eyes have done:
Mine eyes have drawn thy shape, and thine for me
Are windows to my breast, where-through the sun
Delights to peep, to gaze therein on thee;
Yet eyes this cunning want to grace their art;
They draw but what they see, know not the heart.
Let those who are in favour with their stars
Of public honour and proud titles boast,
Whilst I, whom fortune of such triumph bars,
Unlook'd for joy in that I honour most.
Great princes' favourites their fair leaves spread
But as the marigold at the sun's eye,
And in themselves their pride lies buried,
For at a frown they in their glory die.
The painful warrior famoused for fight,
After a thousand victories once foil'd,
Is from the book of honour razed quite,
And all the rest forgot for which he toil'd:
Then happy I, that love and am beloved
Where I may not remove nor be removed.
Lord of my love, to whom in vassalage
Thy merit hath my duty strongly knit,
To thee I send this written embassage,
To witness duty, not to show my wit:
Duty so great, which wit so poor as mine
May make seem bare, in wanting words to show it,
But that I hope some good conceit of thine
In thy soul's thought, all naked, will bestow it;
Till whatsoever star that guides my moving
Points on me graciously with fair aspect
And puts apparel on my tatter'd loving,
To show me worthy of thy sweet respect:
Then may I dare to boast how I do love thee;
Till then not show my head where thou mayst prove me.
Weary with toil, I haste me to my bed,
The dear repose for limbs with travel tired;
But then begins a journey in my head,
To work my mind, when body's work's expired:
For then my thoughts, from far where I abide,
Intend a zealous pilgrimage to thee,
And keep my drooping eyelids open wide,
Looking on darkness which the blind do see
Save that my soul's imaginary sight
Presents thy shadow to my sightless view,
Which, like a jewel hung in ghastly night,
Makes black night beauteous and her old face new.
Lo! thus, by day my limbs, by night my mind,
For thee and for myself no quiet find.
How can I then return in happy plight,
That am debarr'd the benefit of rest?
When day's oppression is not eased by night,
But day by night, and night by day, oppress'd?
And each, though enemies to either's reign,
Do in consent shake hands to torture me;
The one by toil, the other to complain
How far I toil, still farther off from thee.
I tell the day, to please them thou art bright
And dost him grace when clouds do blot the heaven:
So flatter I the swart-complexion'd night,
When sparkling stars twire not thou gild'st the even.
But day doth daily draw my sorrows longer
And night doth nightly make grief's strength seem stronger.
When, in disgrace with fortune and men's eyes,
I all alone beweep my outcast state
And trouble deal heaven with my bootless cries
And look upon myself and curse my fate,
Wishing me like to one more rich in hope,
Featured like him, like him with friends possess'd,
Desiring this man's art and that man's scope,
With what I most enjoy contented least;
Yet in these thoughts myself almost despising,
Haply I think on thee, and then my state,
Like to the lark at break of day arising
From sullen earth, sings hymns at heaven's gate;
For thy sweet love remember'd such wealth brings
That then I scorn to change my state with kings.
When to the sessions of sweet silent thought
I summon up remembrance of things past,
I sigh the lack of many a thing I sought,
And with old woes new wail my dear time's waste:
Then can I drown an eye, unused to flow,
For precious friends hid in death's dateless night,
And weep afresh love's long since cancell'd woe,
And moan the expense of many a vanish'd sight:
Then can I grieve at grievances foregone,
And heavily from woe to woe tell o'er
The sad account of fore-bemoaned moan,
Which I new pay as if not paid before.
But if the while I think on thee, dear friend,
All losses are restored and sorrows end.
Thy bosom is endeared with all hearts,
Which I by lacking have supposed dead,
And there reigns love and all love's loving parts,
And all those friends which I thought buried.
How many a holy and obsequious tear
Hath dear religious love stol'n from mine eye
As interest of the dead, which now appear
But things removed that hidden in thee lie!
Thou art the grave where buried love doth live,
Hung with the trophies of my lovers gone,
Who all their parts of me to thee did give;
That due of many now is thine alone:
Their images I loved I view in thee,
And thou, all they, hast all the all of me.
If thou survive my well-contented day,
When that churl Death my bones with dust shall cover,
And shalt by fortune once more re-survey
These poor rude lines of thy deceased lover,
Compare them with the bettering of the time,
And though they be outstripp'd by every pen,
Reserve them for my love, not for their rhyme,
Exceeded by the height of happier men.
O, then vouchsafe me but this loving thought:
'Had my friend's Muse grown with this growing age,
A dearer birth than this his love had brought,
To march in ranks of better equipage:
But since he died and poets better prove,
Theirs for their style I'll read, his for his love.'
Full many a glorious morning have I seen
Flatter the mountain-tops with sovereign eye,
Kissing with golden face the meadows green,
Gilding pale streams with heavenly alchemy;
Anon permit the basest clouds to ride
With ugly rack on his celestial face,
And from the forlorn world his visage hide,
Stealing unseen to west with this disgrace:
Even so my sun one early morn did shine
With all triumphant splendor on my brow;
But out, alack! he was but one hour mine;
The region cloud hath mask'd him from me now.
Yet him for this my love no whit disdaineth;
Suns of the world may stain when heaven's sun staineth.
Why didst thou promise such a beauteous day,
And make me travel forth without my cloak,
To let base clouds o'ertake me in my way,
Hiding thy bravery in their rotten smoke?
'Tis not enough that through the cloud thou break,
To dry the rain on my storm-beaten face,
For no man well of such a salve can speak
That heals the wound and cures not the disgrace:
Nor can thy shame give physic to my grief;
Though thou repent, yet I have still the loss:
The offender's sorrow lends but weak relief
To him that bears the strong offence's cross.
Ah! but those tears are pearl which thy love sheds,
And they are rich and ransom all ill deeds.
No more be grieved at that which thou hast done:
Roses have thorns, and silver fountains mud;
Clouds and eclipses stain both moon and sun,
And loathsome canker lives in sweetest bud.
All men make faults, and even I in this,
Authorizing thy trespass with compare,
Myself corrupting, salving thy amiss,
Excusing thy sins more than thy sins are;
For to thy sensual fault I bring in sense--
Thy adverse party is thy advocate--
And 'gainst myself a lawful plea commence:
Such civil war is in my love and hate
That I an accessary needs must be
To that sweet thief which sourly robs from me.
Let me confess that we two must be twain,
Although our undivided loves are one:
So shall those blots that do with me remain
Without thy help by me be borne alone.
In our two loves there is but one respect,
Though in our lives a separable spite,
Which though it alter not love's sole effect,
Yet doth it steal sweet hours from love's delight.
I may not evermore acknowledge thee,
Lest my bewailed guilt should do thee shame,
Nor thou with public kindness honour me,
Unless thou take that honour from thy name:
But do not so; I love thee in such sort
As, thou being mine, mine is thy good report.
As a decrepit father takes delight
To see his active child do deeds of youth,
So I, made lame by fortune's dearest spite,
Take all my comfort of thy worth and truth.
For whether beauty, birth, or wealth, or wit,
Or any of these all, or all, or more,
Entitled in thy parts do crowned sit,
I make my love engrafted to this store:
So then I am not lame, poor, nor despised,
Whilst that this shadow doth such substance give
That I in thy abundance am sufficed
And by a part of all thy glory live.
Look, what is best, that best I wish in thee:
This wish I have; then ten times happy me!
@@ -0,0 +1,59 @@
import torch
import argparse
from transformers import AutoTokenizer
from llmcompressor.transformers import SparseAutoModelForCausalLM, oneshot
from llmcompressor.transformers.compression.helpers import ( # noqa
calculate_offload_device_map,
custom_offload_device_map,
)
def main():
parser = argparse.ArgumentParser(description="Compress a language model.")
parser.add_argument("model_stub", type=str, help="The model stub (e.g., 'bosonai/Higgs-Llama-3-70B')")
args = parser.parse_args()
recipe = """
quant_stage:
quant_modifiers:
QuantizationModifier:
ignore: ["lm_head"]
config_groups:
group_0:
weights:
num_bits: 8
type: float
strategy: channel
dynamic: false
symmetric: true
input_activations:
num_bits: 8
type: float
strategy: token
dynamic: true
symmetric: true
targets: ["Linear"]
"""
model_stub = args.model_stub
model_name = model_stub.split("/")[-1]
device_map = calculate_offload_device_map(
model_stub, reserve_for_hessians=False, num_gpus=1, torch_dtype=torch.float16
)
model = SparseAutoModelForCausalLM.from_pretrained(
model_stub, torch_dtype=torch.float16, device_map=device_map
)
output_dir = f"./{model_name}-FP8-dynamic"
oneshot(
model=model,
recipe=recipe,
output_dir=output_dir,
save_compressed=True,
tokenizer=AutoTokenizer.from_pretrained(model_stub),
)
if __name__ == "__main__":
main()
+41
View File
@@ -0,0 +1,41 @@
terraform {
required_providers {
crusoe = {
source = "registry.terraform.io/crusoecloud/crusoe"
}
}
}
locals {
my_ssh_key = file("~/.ssh/id_ed25519.pub")
}
// new VM
resource "crusoe_compute_instance" "vllm_vm" {
name = "vllm-example"
type = "l40s-48gb.8x"
location = "us-southcentral1-a"
# specify the base image
image = "ubuntu22.04-nvidia-slurm:12.4"
disks = [
{
id = crusoe_storage_disk.vllm_data_disk.id
mode = "read-write"
attachment_type = "data"
}
]
ssh_key = local.my_ssh_key
}
resource "crusoe_storage_disk" "vllm_data_disk" {
name = "vllm-example-disk"
size = "256GiB"
location = "us-southcentral1-a"
}
output "instance_public_ip" {
value = crusoe_compute_instance.vllm_vm.network_interfaces[0].public_ipv4.address
}
+72
View File
@@ -0,0 +1,72 @@
import json
import os
import re
import matplotlib.pyplot as plt
import numpy as np
from collections import defaultdict
def extract_info_from_filename(filename):
pattern = r'(?P<backend>[^-]+)-(?P<qps>\d+\.\d+)qps-(?P<model>.+)-(?P<date>\d{8}-\d{6})\.json'
match = re.match(pattern, filename)
if match:
return {
'qps': float(match.group('qps')),
'model': match.group('model')
}
return None
def read_json_files(directory):
data_tpot = defaultdict(list)
data_ttft = defaultdict(list)
for filename in os.listdir(directory):
if filename.endswith('.json'):
filepath = os.path.join(directory, filename)
file_info = extract_info_from_filename(filename)
if file_info:
with open(filepath, 'r') as file:
json_data = json.load(file)
median_tpot = json_data.get('median_tpot_ms')
std_tpot = json_data.get('std_tpot_ms')
median_ttft = json_data.get('median_ttft_ms')
std_ttft = json_data.get('std_ttft_ms')
if all(v is not None for v in [median_tpot, std_tpot, median_ttft, std_ttft]):
data_tpot[file_info['model']].append((file_info['qps'], median_tpot, std_tpot))
data_ttft[file_info['model']].append((file_info['qps'], median_ttft, std_ttft))
return {
'tpot': {model: sorted(points) for model, points in data_tpot.items()},
'ttft': {model: sorted(points) for model, points in data_ttft.items()}
}
def create_chart(data, metric, filename):
plt.figure(figsize=(12, 6))
colors = plt.cm.rainbow(np.linspace(0, 1, len(data)))
for (model, points), color in zip(data.items(), colors):
qps_values, median_values, std_values = zip(*points)
plt.errorbar(qps_values, median_values, yerr=std_values, fmt='o-', capsize=5, capthick=2, label=model, color=color)
plt.fill_between(qps_values,
np.array(median_values) - np.array(std_values),
np.array(median_values) + np.array(std_values),
alpha=0.2, color=color)
plt.xlabel('QPS (Queries Per Second)')
plt.ylabel(f'Median {metric.upper()} (ms)')
plt.title(f'Median {metric.upper()} vs QPS with Standard Deviation')
plt.grid(True)
plt.legend(title='Model', bbox_to_anchor=(1.05, 1), loc='upper left')
plt.tight_layout()
plt.savefig(filename, dpi=300, bbox_inches='tight')
plt.close()
def main():
directory = './'
data = read_json_files(directory)
if data['tpot'] and data['ttft']:
create_chart(data['tpot'], 'tpot', 'tpot_vs_qps_chart.png')
create_chart(data['ttft'], 'ttft', 'ttft_vs_qps_chart.png')
print("Charts have been saved as 'tpot_vs_qps_chart.png' and 'ttft_vs_qps_chart.png'")
else:
print("No valid data found in the specified directory.")
if __name__ == "__main__":
main()
@@ -0,0 +1,12 @@
[project]
name = "vllm-l40s"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.10"
dependencies = [
"setuptools>=74.0.0",
"vllm>=0.5.5",
"matplotlib>=3.9.2",
"llmcompressor>=0.1.0",
]
+12
View File
@@ -0,0 +1,12 @@
TOTAL_SECONDS=120
QPS_RATES=("1" "3" "5" "7" "9")
for QPS in ${QPS_RATES[@]}; do
NUM_PROMPTS=$((TOTAL_SECONDS * QPS))
echo "===== RUNNING NUM_PROMPTS = $NUM_PROMPTS QPS = $QPS ====="
uv run benchmarks/benchmark_serving.py \
--model $MODEL \
--dataset-name sonnet --sonnet-input-len 550 --sonnet-output-len 150 --dataset-path benchmarks/sonnet.txt \
--num-prompts $NUM_PROMPTS --request-rate $QPS --save-result
done
+55
View File
@@ -0,0 +1,55 @@
# AI Analyst with Llama and E2B
This is an AI-powered code and data analysis tool powered by Meta Llama and the [E2B SDK](https://e2b.dev/docs).
→ Try on [ai-analyst.e2b.dev](https://ai-analyst.e2b.dev/)
## Features
- 🔸 Analyze data with Meta's Llama 3.1 and 3.2
- 🔸 Upload CSV files
- 🔸 Create interactive charts
**Powered by:**
- 🔸 ✶ [E2B Sandbox](https://github.com/e2b-dev/code-interpreter)
- 🔸 Vercel's AI SDK
- 🔸 Next.js
- 🔸 echarts library for interactive charts
**Supported LLM Providers:**
- 🔸 TogetherAI
- 🔸 Fireworks
- 🔸 Ollama
**Supported chart types:**
- 🔸 All the supported charts are described [here](https://e2b.dev/docs/code-interpreting/create-charts-visualizations/interactive-charts#supported-intertactive-charts).
## Get started
Visit the [online version](https://ai-analyst.e2b.dev/) or run locally on your own.
### 1. Clone repository
```
git clone https://github.com/e2b-dev/ai-analyst.git
```
### 2. Install dependencies
```
cd ai-analyst && npm i
```
### 3. Add E2B API key
Copy `.example.env` to `.env.local` and fill in `E2B_API_KEY`.
- Get your [E2B API key here](https://e2b.dev/dashboard?tab=keys).
### 4. Configure LLM provider
In `.env.local`, add an API key for at least one LLM provider:
- Fireworks: `FIREWORKS_API_KEY`
- Together AI: `TOGETHER_API_KEY`
For Ollama, provide the base URL instead of the API key:
- Ollama: `OLLAMA_BASE_URL`
+4
View File
@@ -0,0 +1,4 @@
In this folder, we show various recipes for Llama models working with GCP. This currently includes:
* Examples for running Llama 4 model inference on Vertex's serverless API offerings (aka. MaaS)
* tool calling
* JSON mode (structured outputs)
@@ -0,0 +1,275 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "lsCK4KDHw4LK"
},
"source": [
"# **Why JSON mode?**\n",
"\n",
"You can guarantee that a model's generated output always adheres to a specific schema so that you receive consistently formatted responses. For example, you might have an established data schema that you use for other tasks. If you have the model follow the same schema, you can directly extract data from the model's output without any post-processing.\n",
"\n",
"To specify the structure of a model's output, define a response schema, which works like a blueprint for model responses. When you submit a prompt and include the response schema, the model's response always follows your defined schema.\n",
"\n",
"# **Objectives**\n",
"\n",
"In this tutorial, you will learn how to use either OpenAI SDK or Vertex AI SDK in Python to generated structured outputs via the Llama 4 Maverick fully managed model on Vertex AI.\n",
" See here for more info on using the [OpenAI SDK with Vertex](https://cloud.google.com/vertex-ai/generative-ai/docs/migrate/openai/overview#:~:text=The%20Chat%20Completions%20API%20works,the%20Google%20Gen%20AI%20SDK.), as well as recommendations on when to use OpenAI SDK vs. Vertex AI SDK.\n",
"\n",
"We will use sentiment analysis as an example use case, you can replace it with a different structure that's right for you.\n",
"\n",
"# **Setup and Relevant Links**\n",
"Llama on Vertex AI (fully managed): https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/llama. You may also need to accept the EULA to continue.\n",
"\n",
"Official docs from Vertex on structured outputs/JSON mode with Llama coming soon.\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "aEOMDh39uMYV"
},
"source": [
"# **Setup and Relevant Links **"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"id": "2sHCLvoCw6TA"
},
"outputs": [],
"source": [
"import sys\n",
"import IPython\n",
"\n",
"%pip install --upgrade --user --quiet google-genai\n",
"app = IPython.Application.instance()\n",
"app.kernel.do_shutdown(True)\n",
"\n",
"if \"google.colab\" in sys.modules:\n",
" from google.colab import auth\n",
"\n",
" auth.authenticate_user()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"id": "JG5BoLBdw5je"
},
"outputs": [],
"source": [
"import os\n",
"\n",
"project_id = \"[your-project-id]\" # @param {type: \"string\", placeholder: \"[your-project-id]\", isTemplate: true}\n",
"if not project_id or project_id == \"[your-project-id]\":\n",
" project_id = str(os.environ.get(\"GOOGLE_CLOUD_PROJECT\"))\n",
"\n",
"# run gcloud auth print-access-token from terminal to get this\n",
"access_token = \"\"\n",
"\n",
"location = os.environ.get(\"GOOGLE_CLOUD_REGION\", \"us-east5\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "DsRB381L0bwb"
},
"source": [
"Defining the format we want to return..."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"id": "7CyBOejOz8iR"
},
"outputs": [],
"source": [
"response_schema = {\n",
" \"type\": \"ARRAY\",\n",
" \"items\": {\n",
" \"type\": \"ARRAY\",\n",
" \"items\": {\n",
" \"type\": \"OBJECT\",\n",
" \"properties\": {\n",
" \"rating\": {\"type\": \"INTEGER\"},\n",
" \"flavor\": {\"type\": \"STRING\"},\n",
" \"sentiment\": {\n",
" \"type\": \"STRING\",\n",
" \"enum\": [\"POSITIVE\", \"NEGATIVE\", \"NEUTRAL\"],\n",
" },\n",
" \"explanation\": {\"type\": \"STRING\"},\n",
" },\n",
" \"required\": [\"rating\", \"flavor\", \"sentiment\", \"explanation\"],\n",
" },\n",
" },\n",
"}\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "pRym0HCpt99W"
},
"source": [
"# **Now, generate JSON output**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "wSTscqE-wxyd"
},
"source": [
"First, with OpenAI's SDK..."
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "yU25L2KDpMSG",
"outputId": "c7f8f0c7-3262-434b-f713-8f002d97dd0f"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'reviews': [{'text': \"Absolutely loved it! Best ice cream I've ever had.\", 'rating': 4, 'flavor': 'Strawberry Cheesecake', 'sentiment': 'Positive', 'explanation': \"The reviewer uses the phrase 'Absolutely loved it' and states it's the 'Best ice cream I've ever had', indicating a very positive sentiment. The rating of 4 out of a presumed 5 is consistent with this positive sentiment.\"}, {'text': 'Quite good, but a bit too sweet for my taste.', 'rating': 1, 'flavor': 'Mango Tango', 'sentiment': 'Negative', 'explanation': \"Although the reviewer starts with 'Quite good', they follow it with a negative statement 'but a bit too sweet for my taste', indicating a mixed sentiment. However, the rating of 1 suggests a strongly negative sentiment, which is inconsistent with the text. The sentiment classification based on the text would be 'Mixed' or 'Neutral', but given the low rating, it leans more towards being negative overall.\"}]}\n"
]
}
],
"source": [
"import json\n",
"import openai\n",
"from openai import OpenAI\n",
"\n",
"# Setup client\n",
"base_url = f\"https://{location}-aiplatform.googleapis.com/v1/projects/{project_id}/locations/{location}/endpoints/openapi\"\n",
"client = openai.OpenAI(base_url=base_url, api_key=access_token)\n",
"\n",
"prompt = \"\"\"\n",
" Analyze the following product reviews, output the sentiment classification, and give an explanation.\n",
"\n",
" - \"Absolutely loved it! Best ice cream I've ever had.\" Rating: 4, Flavor: Strawberry Cheesecake\n",
" - \"Quite good, but a bit too sweet for my taste.\" Rating: 1, Flavor: Mango Tango\n",
"\"\"\"\n",
"\n",
"response = client.chat.completions.create(\n",
" model=\"meta/llama-4-maverick-17b-128e-instruct-maas\",\n",
" messages=[{\"role\": \"user\", \"content\": prompt}],\n",
" response_format={\"type\": \"json_object\"},\n",
" temperature=0.1\n",
")\n",
"\n",
"product_reviews = json.loads(response.choices[0].message.content)\n",
"print(product_reviews)\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ckdF273K0g7E"
},
"source": [
"Now with Vertex AI SDK"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "OnfgZyHZr9gl"
},
"outputs": [],
"source": [
"from google.genai.types import GenerateContentConfig, Part, SafetySetting\n",
"\n",
"from google import genai\n",
"\n",
"client = genai.Client(vertexai=True, project=PROJECT_ID, location=LOCATION)\n",
"MODEL_ID = \"meta/llama-4-maverick-17b-128e-instruct-maas\"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Z_ka6SP90Am-",
"outputId": "69d91ee1-96d4-44f9-d22d-c4f4a9887e82"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[{'explanation': \"The reviewer used the phrase 'Absolutely loved it' and stated it was the 'Best ice cream I've ever had', indicating a very positive sentiment despite the rating being less than 5.\", 'flavor': 'Strawberry Cheesecake', 'rating': 4, 'sentiment': 'POSITIVE'}, {'explanation': \"The reviewer described the product as 'Quite good', but expressed a negative aspect by stating it was 'a bit too sweet', aligning with the low rating given. The negative aspect outweighs the positive, leading to an overall negative sentiment.\", 'flavor': 'Mango Tango', 'rating': 1, 'sentiment': 'NEGATIVE'}]]\n"
]
}
],
"source": [
"prompt = \"\"\"\n",
" Analyze the following product reviews, output the sentiment classification, and give an explanation.\n",
"\n",
" - \"Absolutely loved it! Best ice cream I've ever had.\" Rating: 4, Flavor: Strawberry Cheesecake\n",
" - \"Quite good, but a bit too sweet for my taste.\" Rating: 1, Flavor: Mango Tango\n",
"\"\"\"\n",
"\n",
"response = client.models.generate_content(\n",
" model=MODEL_ID,\n",
" contents=prompt,\n",
" config=GenerateContentConfig(\n",
" response_mime_type=\"application/json\",\n",
" response_schema=response_schema,\n",
" ),\n",
")\n",
"product_reviews: dict = response.parsed\n",
"print(product_reviews)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "wC4ci2UEF9KB"
},
"source": [
"# **Congrats and conclusion**\n",
"\n",
"You've successfully built a sentiment analyzer leveraging structured outputs via Llama 4 Maverick using the OpenAI and/or Vertex AI SDK!\n",
"\n",
"# **Cleanup**\n",
"You can perform the following cleanup to avoid incurring charges to your Google Cloud account for the resources used in this codelab:\n",
"* To avoid unnecessary Google Cloud charges, use the Google Cloud console to delete your project if you do not need it.\n",
"* If you want to disable the APIs for Vertex AI, navigate to the Vertex AI API Service Details page and click Disable API and confirm.\n"
]
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
@@ -0,0 +1,497 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"source": [
"# **Why function calling?**\n",
"\n",
"Imagine asking someone to write down important information without giving them a form or any guidelines on the structure. You might get a beautifully crafted paragraph, but extracting specific details like names, dates, or numbers would be tedious! Similarly, trying to get consistent structured data from a generative text model without function calling can be frustrating. You're stuck explicitly prompting for things like JSON output, often with inconsistent and frustrating results.\n",
"\n",
"This is where function calling comes in. Instead of hoping for the best in a freeform text response from a generative model, you can define clear functions with specific parameters and data types. These function declarations act as structured guidelines, guiding the Llama model to structure its output in a predictable and usable way. No more parsing text responses for important information!\n",
"\n",
"Think of it like teaching Llama to speak the language of your applications. Need to retrieve information from a database? Define a search_db function with parameters for search terms. Want to integrate with a weather API? Create a get_weather function that takes a location as input. Function calling bridges the gap between human language and the structured data needed to interact with external systems.\n",
"\n",
"# **Objectives**\n",
"\n",
"In this tutorial, you will learn how to use either OpenAI SDK or Vertex AI SDK in Python to make function calls via the Llama 4 Maverick fully managed model on Vertex AI.\n",
" See here for more info on using the [OpenAI SDK with Vertex](https://cloud.google.com/vertex-ai/generative-ai/docs/migrate/openai/overview#:~:text=The%20Chat%20Completions%20API%20works,the%20Google%20Gen%20AI%20SDK.), as well as recommendations on when to use OpenAI SDK vs. Vertex AI SDK.\n",
"\n",
"We will use a currency exchange function as an example, you can replace it with another function with the right functionality for you.\n",
"This tutorial is based on this Vertex AI codelab: https://codelabs.developers.google.com/codelabs/gemini-function-calling\n",
"\n",
"# **Setup and Relevant Links**\n",
"Llama on Vertex AI (fully managed): https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/llama\n",
"\n",
"Official docs from Vertex on tool calling with Llama coming soon.\n"
],
"metadata": {
"id": "JIVs8gXB1Auw"
}
},
{
"cell_type": "markdown",
"source": [
"# **First, with OpenAI's SDK**"
],
"metadata": {
"id": "1zikcmKb5JVx"
}
},
{
"cell_type": "markdown",
"source": [
"Handling imports and setup"
],
"metadata": {
"id": "8znWkSqT5zyy"
}
},
{
"cell_type": "code",
"source": [
"import google.auth\n",
"import openai\n",
"import json\n",
"\n",
"from google.auth.transport import requests as google_requests"
],
"metadata": {
"id": "C81B_THj5XUZ"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"import os\n",
"\n",
"project_id = \"[your-project-id]\" # @param {type: \"string\", placeholder: \"[your-project-id]\", isTemplate: true}\n",
"if not project_id or project_id == \"[your-project-id]\":\n",
" project_id = str(os.environ.get(\"GOOGLE_CLOUD_PROJECT\"))\n",
"\n",
"location = os.environ.get(\"GOOGLE_CLOUD_REGION\", \"us-east5\")\n",
"\n",
"# run gcloud auth print-access-token from terminal to get this\n",
"access_token = \"\"\n",
"\n",
"# Set up OpenAI client\n",
"base_url = f\"https://{location}-aiplatform.googleapis.com/v1/projects/{project_id}/locations/{location}/endpoints/openapi\"\n",
"client = openai.OpenAI(base_url=base_url, api_key=access_token)"
],
"metadata": {
"id": "ivcFdple7Qr5"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"define the function implementation"
],
"metadata": {
"id": "wqhyLJO07Gaz"
}
},
{
"cell_type": "code",
"source": [
"def get_exchange_rate(currency_date, currency_from, currency_to):\n",
" \"\"\"Get the exchange rate for currencies between countries\"\"\"\n",
" try:\n",
" url = f\"https://api.frankfurter.app/{currency_date}\"\n",
" params = {\n",
" \"from\": currency_from,\n",
" \"to\": currency_to\n",
" }\n",
" response = requests.get(url, params=params)\n",
" response.raise_for_status()\n",
" return json.dumps(response.json())\n",
" except Exception as e:\n",
" return f\"Error fetching exchange rate: {str(e)}\""
],
"metadata": {
"id": "4JagRT1t7DT8"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"set up the function declaration for the model"
],
"metadata": {
"id": "LmS_8mdenPEU"
}
},
{
"cell_type": "code",
"source": [
"tools = [{\n",
" \"type\": \"function\",\n",
" \"function\": {\n",
" \"name\": \"get_exchange_rate\",\n",
" \"description\": \"Get the exchange rate for currencies between countries\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"currency_date\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"A date in YYYY-MM-DD format or 'latest'\"\n",
" },\n",
" \"currency_from\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The currency to convert from in ISO 4217 format\"\n",
" },\n",
" \"currency_to\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The currency to convert to in ISO 4217 format\"\n",
" }\n",
" },\n",
" \"required\": [\"currency_from\", \"currency_to\"]\n",
" }\n",
" }\n",
"}]"
],
"metadata": {
"id": "4lP5F51Cmq7f"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"set up the client, generate a tool call and execute the tool call\n",
"Enter a query into the text field to get started, eg. \"100 usd to eur\""
],
"metadata": {
"id": "iCGAtoNwmlFc"
}
},
{
"cell_type": "code",
"source": [
"def main():\n",
" # Set up client\n",
" client = openai.OpenAI(base_url=base_url, api_key=access_token)\n",
"\n",
" # Step 1: Send the user query to the model\n",
" user_query = input(\"Enter your currency exchange query: \")\n",
"\n",
" completion = client.chat.completions.create(\n",
" model=\"meta/llama-4-maverick-17b-128e-instruct-maas\",\n",
" messages=messages,\n",
" tools=tools,\n",
" temperature=0.1,\n",
" max_tokens=512,\n",
" tool_choice=\"auto\"\n",
" )\n",
"\n",
" # Step 2: Check if the model called a function\n",
" if completion.choices[0].message.tool_calls:\n",
" # Step 3: Execute the function\n",
" for tool_call in completion.choices[0].message.tool_calls:\n",
" if tool_call.function.name == \"get_exchange_rate\":\n",
" args = json.loads(tool_call.function.arguments)\n",
" currency_date = args.get(\"currency_date\", \"latest\")\n",
" currency_from = args.get(\"currency_from\")\n",
" currency_to = args.get(\"currency_to\")\n",
" result = get_exchange_rate(currency_date, currency_from, currency_to)\n",
"\n",
" # Add model's response with function call\n",
" messages.append(completion.choices[0].message)\n",
" messages.append({\n",
" \"role\": \"tool\",\n",
" \"tool_call_id\": tool_call.id,\n",
" \"content\": result\n",
" })\n",
"\n",
" # Get final response from model\n",
" final_response = client.chat.completions.create(\n",
" model=\"meta/llama-4-maverick-17b-128e-instruct-maas\",\n",
" messages=messages,\n",
" temperature=0.1\n",
" )\n",
"\n",
" print(\"Final response:\", final_response.choices[0].message.content)\n",
" else:\n",
" print(\"Direct response:\", completion.choices[0].message.content)\n",
"\n",
"if __name__ == \"__main__\":\n",
" main()"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "SycilP3AkiRr",
"outputId": "f3bcab82-9010-4610-d392-f36792c57844"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Enter your currency exchange query: 100 usd to eur\n",
"Direct response: The exchange rate for 100 USD to EUR is 88.645 EUR.\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"# **Now with Vertex SDK**"
],
"metadata": {
"id": "kyp2Fy_7noaV"
}
},
{
"cell_type": "markdown",
"source": [
"Handling imports and setup"
],
"metadata": {
"id": "KucVr8Txplnz"
}
},
{
"cell_type": "code",
"source": [
"import json\n",
"import requests\n",
"import vertexai\n",
"from vertexai.generative_models import (\n",
" FunctionDeclaration,\n",
" GenerationConfig,\n",
" GenerativeModel,\n",
" Tool,\n",
" Content,\n",
" Part\n",
")\n",
"\n",
"# Authenticate with Google Cloud\n",
"from google.colab import auth\n",
"auth.authenticate_user()"
],
"metadata": {
"id": "JxVuZ3R_pmKH"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Initialize Vertex AI\n",
"import os\n",
"\n",
"PROJECT_ID = \"[your-project-id]\" # @param {type: \"string\", placeholder: \"[your-project-id]\", isTemplate: true}\n",
"if not PROJECT_ID or PROJECT_ID == \"[your-project-id]\":\n",
" PROJECT_ID = str(os.environ.get(\"GOOGLE_CLOUD_PROJECT\"))\n",
"\n",
"LOCATION = os.environ.get(\"GOOGLE_CLOUD_REGION\", \"us-central1\")\n",
"\n",
"vertexai.init(project=PROJECT_ID, location=LOCATION)"
],
"metadata": {
"id": "r1rP7PdUpwK6"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"define the function implementation"
],
"metadata": {
"id": "nFFEsV1BptAD"
}
},
{
"cell_type": "code",
"source": [
"def get_exchange_rate(currency_date, currency_from, currency_to):\n",
" \"\"\"Get the exchange rate for currencies between countries\"\"\"\n",
" try:\n",
" url = f\"https://api.frankfurter.app/{currency_date}\"\n",
" params = {\n",
" \"from\": currency_from,\n",
" \"to\": currency_to\n",
" }\n",
" response = requests.get(url, params=params)\n",
" response.raise_for_status()\n",
" return response.json()\n",
" except Exception as e:\n",
" return {\"error\": str(e)}"
],
"metadata": {
"id": "hDMzxUBxptq0"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"set up the function declaration for the model"
],
"metadata": {
"id": "g1RH1tplqhi5"
}
},
{
"cell_type": "code",
"source": [
"get_exchange_rate_func = FunctionDeclaration(\n",
" name=\"get_exchange_rate\",\n",
" description=\"Get the exchange rate for currencies between countries\",\n",
" parameters={\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"currency_date\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"A date in YYYY-MM-DD format or 'latest'\"\n",
" },\n",
" \"currency_from\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The currency to convert from in ISO 4217 format\"\n",
" },\n",
" \"currency_to\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The currency to convert to in ISO 4217 format\"\n",
" }\n",
" },\n",
" \"required\": [\"currency_from\", \"currency_to\"]\n",
" },\n",
")\n",
"\n",
"exchange_tool = Tool(\n",
" function_declarations=[get_exchange_rate_func],\n",
")\n"
],
"metadata": {
"id": "XyR1QzkSozrq"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"set up the client, generate a tool call and execute the tool call\n",
"Enter a query into the text field to get started, eg. \"100 usd to eur\""
],
"metadata": {
"id": "MFM6gfqwqzsi"
}
},
{
"cell_type": "code",
"source": [
"model = GenerativeModel(\n",
" \"llama-4-maverick-17b-128e-instruct-maas\",\n",
" generation_config=GenerationConfig(temperature=0.1),\n",
" tools=[exchange_tool],\n",
")\n",
"\n",
"def main():\n",
" user_query = input(\"Enter your currency exchange query: \")\n",
"\n",
" # Start chat and send user query\n",
" chat = model.start_chat()\n",
" response = chat.send_message(user_query)\n",
"\n",
" # Check for function calls\n",
" if response.candidates[0].function_calls:\n",
" for function_call in response.candidates[0].function_calls:\n",
" if function_call.name == \"get_exchange_rate\":\n",
" # Extract arguments\n",
" args = function_call.args\n",
" currency_date = args.get(\"currency_date\", \"latest\")\n",
" currency_from = args.get(\"currency_from\")\n",
" currency_to = args.get(\"currency_to\")\n",
"\n",
" # Call the function\n",
" result = get_exchange_rate(currency_date, currency_from, currency_to)\n",
"\n",
" # Send function result back to model\n",
" final_response = chat.send_message(\n",
" Content(\n",
" role=\"function\",\n",
" parts=[\n",
" Part.from_function_response(\n",
" name=\"get_exchange_rate\",\n",
" response=result\n",
" )\n",
" ]\n",
" )\n",
" )\n",
"\n",
" print(\"Final response:\", final_response.text)\n",
" else:\n",
" print(\"Direct response:\", response.text)\n",
"\n",
"if __name__ == \"__main__\":\n",
" main()\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "L_Dla5r6qyrV",
"outputId": "bc2e9eae-11cc-4095-c822-85edcf7b703e"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Enter your currency exchange query: 100 usd to eur\n",
"Final response: The exchange rate for 100 USD to EUR on the latest date is 88.645 EUR.\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"# **Congrats and conclusion**\n",
"\n",
"Leveraging function calling via Llama 4 in Vertex AI, you've successfully built a generative AI pipeline that uses the OpenAI and/or Vertex AI SDK! Users can ask about exchange rates, and the system will fetch the latest data from an external API and respond with an answer.\n",
"\n",
"Given a prompt from an end-user, Llama takes care of selecting the appropriate function, extracting parameters from the prompt, and returning a structured data object for you to make an external API call.\n",
"\n",
"\n",
"# **Cleanup**\n",
"You can perform the following cleanup to avoid incurring charges to your Google Cloud account for the resources used in this codelab:\n",
"* To avoid unnecessary Google Cloud charges, use the Google Cloud console to delete your project if you do not need it.\n",
"* If you want to disable the APIs for Vertex AI, navigate to the Vertex AI API Service Details page and click Disable API and confirm.\n"
],
"metadata": {
"id": "pT9Cf6TtAgX7"
}
}
]
}
@@ -0,0 +1,41 @@
customer_id,name,email,address
1,Erin Boyle MD,erin.boyle.md@example.com,"165 Brown Springs
Michaelport, IL 60228"
2,Matthew Saunders,matthew.saunders@example.com,"219 Steven Mountains
Port Gabriellafort, OH 52281"
3,Amanda Anderson,amanda.anderson@example.com,"498 Laurie Glens
Mitchelltown, CT 93655"
4,Julian Butler,julian.butler@example.com,"909 Rodriguez Harbors Suite 119
New Tracyburgh, MS 15487"
5,Zachary Mitchell MD,zachary.mitchell.md@example.com,"9087 Matthew Drives
Caitlynshire, OR 42442"
6,Troy Bennett,troy.bennett@example.com,"73329 Kimberly Loaf Apt. 029
Shellyborough, TX 55939"
7,Allison Hall,allison.hall@example.com,"210 Shannon Camp
New Michael, MO 65990"
8,Carolyn Davis,carolyn.davis@example.com,"64228 Carol Courts Suite 087
New Micheleshire, MT 42516"
9,Cindy Munoz,cindy.munoz@example.com,"1722 Christine Plaza
Danielport, UT 12261"
10,Tom Testuser,tom.testuser@example.com,"451 Victoria Bridge Suite 529
Pageton, WI 27404"
11,Charles Walker,charles.walker@example.com,"2077 Lamb Drive
Salazarton, IN 54619"
12,Brianna Molina,brianna.molina@example.com,"586 Khan Mills Suite 202
Lake Dominique, VA 98527"
13,Austin Andrade,austin.andrade@example.com,"4857 Donna Cliffs
Floydstad, PR 82540"
14,Brandon Andrade,brandon.andrade@example.com,"906 Olivia Motorway
Kelleyfort, AK 48960"
15,Diane Lam,diane.lam@example.com,"070 Eric Rapid Suite 159
Townsendbury, MI 57664"
16,Jason Kelly,jason.kelly@example.com,"873 Angela Track Apt. 972
Stephenville, NV 32705"
17,Mr. Mitchell Saunders,mr..mitchell.saunders@example.com,"USS White
FPO AE 91058"
18,Regina Ross,regina.ross@example.com,"91857 Wendy Place
East Charlesshire, CA 43705"
19,Mrs. Denise May DDS,mrs..denise.may.dds@example.com,"64590 Kathleen Cove Apt. 736
Derrickton, AK 05935"
20,Lisa Boyle,lisa.boyle@example.com,"USNS Russell
FPO AE 51528"
1 customer_id name email address
2 1 Erin Boyle MD erin.boyle.md@example.com 165 Brown Springs Michaelport, IL 60228
3 2 Matthew Saunders matthew.saunders@example.com 219 Steven Mountains Port Gabriellafort, OH 52281
4 3 Amanda Anderson amanda.anderson@example.com 498 Laurie Glens Mitchelltown, CT 93655
5 4 Julian Butler julian.butler@example.com 909 Rodriguez Harbors Suite 119 New Tracyburgh, MS 15487
6 5 Zachary Mitchell MD zachary.mitchell.md@example.com 9087 Matthew Drives Caitlynshire, OR 42442
7 6 Troy Bennett troy.bennett@example.com 73329 Kimberly Loaf Apt. 029 Shellyborough, TX 55939
8 7 Allison Hall allison.hall@example.com 210 Shannon Camp New Michael, MO 65990
9 8 Carolyn Davis carolyn.davis@example.com 64228 Carol Courts Suite 087 New Micheleshire, MT 42516
10 9 Cindy Munoz cindy.munoz@example.com 1722 Christine Plaza Danielport, UT 12261
11 10 Tom Testuser tom.testuser@example.com 451 Victoria Bridge Suite 529 Pageton, WI 27404
12 11 Charles Walker charles.walker@example.com 2077 Lamb Drive Salazarton, IN 54619
13 12 Brianna Molina brianna.molina@example.com 586 Khan Mills Suite 202 Lake Dominique, VA 98527
14 13 Austin Andrade austin.andrade@example.com 4857 Donna Cliffs Floydstad, PR 82540
15 14 Brandon Andrade brandon.andrade@example.com 906 Olivia Motorway Kelleyfort, AK 48960
16 15 Diane Lam diane.lam@example.com 070 Eric Rapid Suite 159 Townsendbury, MI 57664
17 16 Jason Kelly jason.kelly@example.com 873 Angela Track Apt. 972 Stephenville, NV 32705
18 17 Mr. Mitchell Saunders mr..mitchell.saunders@example.com USS White FPO AE 91058
19 18 Regina Ross regina.ross@example.com 91857 Wendy Place East Charlesshire, CA 43705
20 19 Mrs. Denise May DDS mrs..denise.may.dds@example.com 64590 Kathleen Cove Apt. 736 Derrickton, AK 05935
21 20 Lisa Boyle lisa.boyle@example.com USNS Russell FPO AE 51528
@@ -0,0 +1,21 @@
order_id,product_id,customer_id,order_date
1,13,18,2024-02-15 15:15
2,19,6,2024-01-03 17:43
3,12,20,2024-03-11 1:13
4,7,20,2024-02-04 12:04
5,14,3,2024-05-02 17:12
6,17,6,2024-02-12 1:46
7,20,4,2024-02-26 2:59
8,4,7,2024-05-02 16:51
9,11,2,2024-01-04 11:09
10,6,9,2024-04-09 15:04
11,3,7,2024-02-21 21:17
12,6,18,2024-02-21 18:50
13,17,11,2024-05-02 16:20
14,11,15,2024-04-20 2:49
15,16,7,2024-01-18 1:12
16,16,16,2024-05-03 11:20
17,14,18,2024-03-26 22:51
18,20,16,2024-05-07 23:25
19,1,12,2024-05-20 12:41
20,20,3,2024-01-17 7:25
1 order_id product_id customer_id order_date
2 1 13 18 2024-02-15 15:15
3 2 19 6 2024-01-03 17:43
4 3 12 20 2024-03-11 1:13
5 4 7 20 2024-02-04 12:04
6 5 14 3 2024-05-02 17:12
7 6 17 6 2024-02-12 1:46
8 7 20 4 2024-02-26 2:59
9 8 4 7 2024-05-02 16:51
10 9 11 2 2024-01-04 11:09
11 10 6 9 2024-04-09 15:04
12 11 3 7 2024-02-21 21:17
13 12 6 18 2024-02-21 18:50
14 13 17 11 2024-05-02 16:20
15 14 11 15 2024-04-20 2:49
16 15 16 7 2024-01-18 1:12
17 16 16 16 2024-05-03 11:20
18 17 14 18 2024-03-26 22:51
19 18 20 16 2024-05-07 23:25
20 19 1 12 2024-05-20 12:41
21 20 20 3 2024-01-17 7:25
@@ -0,0 +1,21 @@
product_id,name,description,price,stock_quantity
1,Laptop,High performance laptop with 16GB RAM and 512GB SSD.,753.03,15
2,Smartphone,Latest model smartphone with a stunning display and great camera.,398.54,59
3,Headphones,Noise-cancelling over-ear headphones with long battery life.,889.79,97
4,Monitor,24-inch 1080p monitor with vibrant colors and wide viewing angles.,604.44,98
5,Keyboard,Mechanical keyboard with customizable RGB lighting.,500.24,52
6,Mouse,Wireless mouse with ergonomic design and long battery life.,321.98,57
7,Printer,All-in-one printer with wireless connectivity and high-quality printing.,695.29,32
8,Tablet,Portable tablet with 10-inch display and powerful processor.,625.75,28
9,Smartwatch,Stylish smartwatch with fitness tracking and notifications.,952.72,42
10,Camera,Digital camera with 20MP sensor and 4K video recording.,247.93,99
11,Speaker,Bluetooth speaker with excellent sound quality and deep bass.,896.4,32
12,Router,Wi-Fi router with high speed and wide coverage.,976.16,59
13,External Hard Drive,1TB external hard drive with fast data transfer speeds.,434.46,18
14,USB Flash Drive,64GB USB flash drive with compact design and reliable storage.,991.09,77
15,Microphone,Professional microphone with clear sound and adjustable settings.,276.23,30
16,Webcam,HD webcam with wide-angle lens and built-in microphone.,890.39,13
17,Drone,Compact drone with HD camera and stable flight controls.,285.93,37
18,Projector,Portable projector with bright display and multiple connectivity options.,290.22,31
19,Fitness Tracker,Fitness tracker with heart rate monitor and sleep tracking.,953.65,4
20,E-Reader,Lightweight e-reader with high-resolution display and long battery life.,132.15,62
1 product_id name description price stock_quantity
2 1 Laptop High performance laptop with 16GB RAM and 512GB SSD. 753.03 15
3 2 Smartphone Latest model smartphone with a stunning display and great camera. 398.54 59
4 3 Headphones Noise-cancelling over-ear headphones with long battery life. 889.79 97
5 4 Monitor 24-inch 1080p monitor with vibrant colors and wide viewing angles. 604.44 98
6 5 Keyboard Mechanical keyboard with customizable RGB lighting. 500.24 52
7 6 Mouse Wireless mouse with ergonomic design and long battery life. 321.98 57
8 7 Printer All-in-one printer with wireless connectivity and high-quality printing. 695.29 32
9 8 Tablet Portable tablet with 10-inch display and powerful processor. 625.75 28
10 9 Smartwatch Stylish smartwatch with fitness tracking and notifications. 952.72 42
11 10 Camera Digital camera with 20MP sensor and 4K video recording. 247.93 99
12 11 Speaker Bluetooth speaker with excellent sound quality and deep bass. 896.4 32
13 12 Router Wi-Fi router with high speed and wide coverage. 976.16 59
14 13 External Hard Drive 1TB external hard drive with fast data transfer speeds. 434.46 18
15 14 USB Flash Drive 64GB USB flash drive with compact design and reliable storage. 991.09 77
16 15 Microphone Professional microphone with clear sound and adjustable settings. 276.23 30
17 16 Webcam HD webcam with wide-angle lens and built-in microphone. 890.39 13
18 17 Drone Compact drone with HD camera and stable flight controls. 285.93 37
19 18 Projector Portable projector with bright display and multiple connectivity options. 290.22 31
20 19 Fitness Tracker Fitness tracker with heart rate monitor and sleep tracking. 953.65 4
21 20 E-Reader Lightweight e-reader with high-resolution display and long battery life. 132.15 62
@@ -0,0 +1,8 @@
employee_id,name,email
1,Richard Hendricks,richard@piedpiper.com
2,Erlich Bachman,erlich@aviato.com
3,Dinesh Chugtai,dinesh@piedpiper.com
4,Bertram Gilfoyle,gilfoyle@piedpiper.com
5,Jared Dunn,jared@piedpiper.com
6,Monica Hall,monica@raviga.com
7,Gavin Belson,gavin@hooli.com
1 employee_id name email
2 1 Richard Hendricks richard@piedpiper.com
3 2 Erlich Bachman erlich@aviato.com
4 3 Dinesh Chugtai dinesh@piedpiper.com
5 4 Bertram Gilfoyle gilfoyle@piedpiper.com
6 5 Jared Dunn jared@piedpiper.com
7 6 Monica Hall monica@raviga.com
8 7 Gavin Belson gavin@hooli.com
@@ -0,0 +1,6 @@
purchase_id,purchase_date,product_name,employee_id,amount
1,'2024-02-01',iPhone,1,750
2,'2024-02-02',Tesla,2,70000
3,'2024-02-03',Humane pin,3,500
4,'2024-02-04',iPhone,4,700
5,'2024-02-05',Tesla,5,75000
1 purchase_id purchase_date product_name employee_id amount
2 1 '2024-02-01' iPhone 1 750
3 2 '2024-02-02' Tesla 2 70000
4 3 '2024-02-03' Humane pin 3 500
5 4 '2024-02-04' iPhone 4 700
6 5 '2024-02-05' Tesla 5 75000
@@ -0,0 +1,677 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "104f2b97-f9bb-4dcc-a4c8-099710768851",
"metadata": {},
"source": [
"# Using JSON Mode and Function Calling for SQL Querying"
]
},
{
"cell_type": "markdown",
"id": "f946b4f9-3993-41a5-8e82-77623e9d546f",
"metadata": {},
"source": [
"With the rise of Large Language Models (LLMs), one of the first practical applications has been the \"chat with X\" app. In this notebook we will explore methods for building \"chat with my database\" tools with Groq API, exploring benefits and drawbacks of each and leveraging Groq API's [JSON mode](https://console.groq.com/docs/text-chat#json-mode) and [tool use](https://console.groq.com/docs/tool-use) feature for function calling.\n",
"\n",
"We will show two methods for using Groq API to query a database, and how leveraging tool use for function calling can improve the predictability and reliability of our outputs. We will use the [DuckDB](https://duckdb.org/) query language on local CSV files in this example, but the general framework could also work against standard data warehouse platforms like [BigQuery](https://cloud.google.com/bigquery)."
]
},
{
"cell_type": "markdown",
"id": "f8dc57b6-2c48-4ee3-bb2c-25441274ed2f",
"metadata": {},
"source": [
"### Setup"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "822dae2b-ddd9-48ac-9356-e7b63a06d190",
"metadata": {},
"outputs": [],
"source": [
"from groq import Groq\n",
"import os \n",
"import json\n",
"import sqlparse\n",
"from IPython.display import Markdown\n",
"import duckdb\n",
"import glob\n",
"import yaml"
]
},
{
"cell_type": "markdown",
"id": "7f7c9c55-e925-4cc1-89f2-58237acf14a4",
"metadata": {},
"source": [
"We will use the ```llama3-70b-8192``` model in this demo. Note that you will need a Groq API Key to proceed and can create an account [here](https://console.groq.com/) to generate one for free."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "0cca781b-1950-4167-b36a-c1099d6b3b00",
"metadata": {},
"outputs": [],
"source": [
"client = Groq(api_key = os.getenv('GROQ_API_KEY'))\n",
"model = 'llama3-70b-8192'"
]
},
{
"cell_type": "markdown",
"id": "af6b1c5a-8940-4e9a-804c-6f52cb1e1f0e",
"metadata": {},
"source": [
"### Text-To-SQL\n",
"\n",
"The first method is a standard **Text-To-SQL** implementation. With Text-To-SQL, we describe the database schema to the LLM, ask it to answer a question, and let it write an on-the-fly SQL query against the database to answer that question. Let's see how we can use the Groq API to build a Text-To-SQL pipeline:"
]
},
{
"cell_type": "markdown",
"id": "e1f8d353-95cf-48d6-9a81-ac71a819abcd",
"metadata": {},
"source": [
"First, we have our system prompt. A system prompt is an initial input or instruction given to the model, setting the context or specifying the task it needs to perform, essentially guiding the model's response generation. In our case, our system prompt will serve 3 purposes:\n",
"\n",
"1. Provide the metadata schemas for our database tables\n",
"2. Indicate any relevant context or tips for querying the DuckDB language or our database schema specifically\n",
"3. Define our desired JSON output (note that to use JSON mode, we must include 'JSON' in the prompt)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "a537a3c2-8fa3-49b4-9115-6ec5142d5630",
"metadata": {},
"outputs": [],
"source": [
"system_prompt = '''\n",
"You are Groq Advisor, and you are tasked with generating SQL queries for DuckDB based on user questions about data stored in two tables derived from CSV files:\n",
"\n",
"Table: employees.csv\n",
"Columns:\n",
"employee_id (INTEGER): A unique identifier for each employee.\n",
"name (VARCHAR): The full name of the employee.\n",
"email (VARCHAR): employee's email address\n",
"\n",
"Table: purchases.csv\n",
"Columns:\n",
"purchase_id (INTEGER): A unique identifier for each purchase.\n",
"purchase_date (DATE): Date of purchase\n",
"employee_id (INTEGER): References the employee_id from the employees table, indicating which employee made the purchase.\n",
"amount (FLOAT): The monetary value of the purchase.\n",
"product_name (STRING): The name of the product purchased\n",
"\n",
"Given a user's question about this data, write a valid DuckDB SQL query that accurately extracts or calculates the requested information from these tables and adheres to SQL best practices for DuckDB, optimizing for readability and performance where applicable.\n",
"\n",
"Here are some tips for writing DuckDB queries:\n",
"* DuckDB syntax requires querying from the .csv file itself, i.e. employees.csv and purchases.csv. For example: SELECT * FROM employees.csv as employees\n",
"* All tables referenced MUST be aliased\n",
"* DuckDB does not implicitly include a GROUP BY clause\n",
"* CURRENT_DATE gets today's date\n",
"* Aggregated fields like COUNT(*) must be appropriately named\n",
"\n",
"And some rules for querying the dataset:\n",
"* Never include employee_id in the output - show employee name instead\n",
"\n",
"Also note that:\n",
"* Valid values for product_name include 'Tesla','iPhone' and 'Humane pin'\n",
"\n",
"\n",
"Question:\n",
"--------\n",
"{user_question}\n",
"--------\n",
"Reminder: Generate a DuckDB SQL to answer to the question:\n",
"* respond as a valid JSON Document\n",
"* [Best] If the question can be answered with the available tables: {\"sql\": <sql here>}\n",
"* If the question cannot be answered with the available tables: {\"error\": <explanation here>}\n",
"* Ensure that the entire output is returned on only one single line\n",
"* Keep your query as simple and straightforward as possible; do not use subqueries\n",
"'''"
]
},
{
"cell_type": "markdown",
"id": "d67ce3f8-b682-4e63-8a15-ebf8851bd676",
"metadata": {},
"source": [
"Now we will define a ```text_to_sql``` function which takes in the system prompt and the user's question and outputs the LLM-generated DuckDB SQL query. Note that since we are using Groq API's [JSON mode](https://console.groq.com/docs/text-chat#json-mode-object-object) to format our output, we must indicate our expected JSON output format in either the system or user prompt."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "705a0f56-828d-426c-8921-24913612f289",
"metadata": {},
"outputs": [],
"source": [
"def text_to_sql(client,system_prompt,user_question,model):\n",
"\n",
" completion = client.chat.completions.create(\n",
" model = model,\n",
" response_format = {\"type\": \"json_object\"},\n",
" messages=[\n",
" {\n",
" \"role\": \"system\",\n",
" \"content\": system_prompt\n",
" },\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": user_question\n",
" }\n",
" ]\n",
" )\n",
" \n",
" return completion.choices[0].message.content"
]
},
{
"cell_type": "markdown",
"id": "f9bdcb40-0587-4d70-ab29-1ff1c56450e4",
"metadata": {},
"source": [
"...and a function for executing the DuckDB query that was generated:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "fd33a970-d7c8-4aba-a5e5-1017fc5d867e",
"metadata": {},
"outputs": [],
"source": [
"def execute_duckdb_query(query):\n",
" original_cwd = os.getcwd()\n",
" os.chdir('data')\n",
" \n",
" try:\n",
" conn = duckdb.connect(database=':memory:', read_only=False)\n",
" query_result = conn.execute(query).fetchdf().reset_index(drop=True)\n",
" finally:\n",
" os.chdir(original_cwd)\n",
"\n",
"\n",
" return query_result"
]
},
{
"cell_type": "markdown",
"id": "4c639e0d-8d09-47a1-ad55-5d8e118a6643",
"metadata": {},
"source": [
"Now, we can query our database just by asking a question about the data. Here, the LLM generates a valid SQL query that reasonably answers the question:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "255be29d-d1b5-4a98-a2a3-f6ad513d7161",
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"```sql\n",
"SELECT e.name,\n",
" p.purchase_date,\n",
" p.product_name,\n",
" p.amount\n",
"FROM purchases.csv AS p\n",
"JOIN employees.csv AS e ON p.employee_id = e.employee_id\n",
"ORDER BY p.purchase_date DESC\n",
"LIMIT 10;\n",
"```"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>name</th>\n",
" <th>purchase_date</th>\n",
" <th>product_name</th>\n",
" <th>amount</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Jared Dunn</td>\n",
" <td>2024-02-05</td>\n",
" <td>Tesla</td>\n",
" <td>75000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Bertram Gilfoyle</td>\n",
" <td>2024-02-04</td>\n",
" <td>iPhone</td>\n",
" <td>700</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Dinesh Chugtai</td>\n",
" <td>2024-02-03</td>\n",
" <td>Humane pin</td>\n",
" <td>500</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Erlich Bachman</td>\n",
" <td>2024-02-02</td>\n",
" <td>Tesla</td>\n",
" <td>70000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Richard Hendricks</td>\n",
" <td>2024-02-01</td>\n",
" <td>iPhone</td>\n",
" <td>750</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" name purchase_date product_name amount\n",
"0 Jared Dunn 2024-02-05 Tesla 75000\n",
"1 Bertram Gilfoyle 2024-02-04 iPhone 700\n",
"2 Dinesh Chugtai 2024-02-03 Humane pin 500\n",
"3 Erlich Bachman 2024-02-02 Tesla 70000\n",
"4 Richard Hendricks 2024-02-01 iPhone 750"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"user_question = 'What are the most recent purchases?'\n",
"\n",
"\n",
"llm_response = text_to_sql(client,system_prompt,user_question,model)\n",
"sql_json = json.loads(llm_response)\n",
"parsed_sql = sqlparse.format(sql_json['sql'], reindent=True, keyword_case='upper')\n",
"formatted_sql = f\"```sql\\n{parsed_sql.strip()}\\n```\"\n",
"display(Markdown(formatted_sql)) \n",
"\n",
"execute_duckdb_query(parsed_sql)"
]
},
{
"cell_type": "markdown",
"id": "c633f9a3-00a5-4767-b3dc-da3b0686a4f2",
"metadata": {},
"source": [
"Note, however, that due to the non-deterministic nature of LLMs, we cannot guarantee a reliable or consistent result every time. I might get a different result than you, and I might get a totally different query tomorrow. How should \"most recent purchases\" be defined? Which fields should be returned?\n",
"\n",
"Obviously, this is not ideal for making any kind of data-driven decisions. It's hard enough to land on a reliable source-of-truth data model, and even harder when your AI analyst cannot give you a consistent result. While text-to-SQL can be great for generating ad-hoc insights, the non-determinism feature of LLMs makes raw text-to-SQL an impractical solution for a production environment."
]
},
{
"cell_type": "markdown",
"id": "cb3918e7-2f0b-4d95-8377-8742c3833a4f",
"metadata": {},
"source": [
"### Function Calling for Verified Queries"
]
},
{
"cell_type": "markdown",
"id": "eb224954-e17a-408c-954e-30757ade35f0",
"metadata": {},
"source": [
"A different approach is to leverage the LLM to call on pre-vetted queries that can answer a set of questions. Since you wouldn't trust a traditional business intelligence tool without rigorously developed and validated SQL, a \"chat with your data\" app should be no different. For this example, we will use the verified queries stored [here](https://github.com/groq/groq-api-cookbook/tree/main/tutorials/function-calling-sql/verified-queries)."
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "4ed69656-1c59-4180-a26d-b83c22944590",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'most-recent-purchases': {'description': 'Five most recent purchases',\n",
" 'sql': 'SELECT \\n purchases.purchase_date,\\n purchases.product_name,\\n purchases.amount,\\n employees.name\\nFROM purchases.csv AS purchases\\nJOIN employees.csv AS employees ON purchases.employee_id = employees.employee_id\\nORDER BY purchases.purchase_date DESC\\nLIMIT 5;\\n'},\n",
" 'most-expensive-purchase': {'description': 'Employee with the most expensive purchase',\n",
" 'sql': 'SELECT employees.name AS employee_name,\\n MAX(amount) AS max_purchase_amount\\nFROM purchases.csv AS purchases\\nJOIN employees.csv AS employees ON purchases.employee_id = employees.employee_id\\nGROUP BY employees.name\\nORDER BY max_purchase_amount DESC\\nLIMIT 1\\n'},\n",
" 'number-of-teslas': {'description': 'Number of Teslas purchased',\n",
" 'sql': \"SELECT COUNT(*) as number_of_teslas\\nFROM purchases.csv AS p\\nJOIN employees.csv AS e ON e.employee_id = p.employee_id\\nWHERE p.product_name = 'Tesla'\\n\"},\n",
" 'employees-without-purchases': {'description': 'Employees without a purchase since Feb 1, 2024',\n",
" 'sql': \"SELECT employees.name as employees_without_purchases\\nFROM employees.csv AS employees\\nLEFT JOIN purchases.csv AS purchases ON employees.employee_id = purchases.employee_id\\nAND purchases.purchase_date > '2024-02-01'\\nWHERE purchases.purchase_id IS NULL\\n\"}}"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def get_verified_queries(directory_path):\n",
" verified_queries_yaml_files = glob.glob(os.path.join(directory_path, '*.yaml'))\n",
" verified_queries_dict = {}\n",
" for file in verified_queries_yaml_files:\n",
" with open(file, 'r') as stream:\n",
" try:\n",
" file_name = file[len(directory_path):-5]\n",
" verified_queries_dict[file_name] = yaml.safe_load(stream)\n",
" except yaml.YAMLError as exc:\n",
" continue\n",
" \n",
" return verified_queries_dict\n",
"\n",
"directory_path = 'verified-queries/'\n",
"verified_queries_dict = get_verified_queries(directory_path)\n",
"verified_queries_dict"
]
},
{
"cell_type": "markdown",
"id": "5ae230c2-220c-4ab7-83a2-2af799bd0a7b",
"metadata": {},
"source": [
"Note that each of these queries are stored in ```yaml``` files with some additional metadata, like a description. This metadata is important for when the LLM needs to select the most appropriate query for the question at hand.\n",
"\n",
"Now, let's define a new function for executing SQL - this one is tweaked slightly to extract the SQL query from ```verified_queries_dict``` inside the function, given a query name:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "7e4b1e52-60bc-469b-8784-fedb8cb742d4",
"metadata": {},
"outputs": [],
"source": [
"def execute_duckdb_query_function_calling(query_name,verified_queries_dict):\n",
" \n",
" original_cwd = os.getcwd()\n",
" os.chdir('data')\n",
"\n",
" query = verified_queries_dict[query_name]['sql']\n",
" \n",
" try:\n",
" conn = duckdb.connect(database=':memory:', read_only=False)\n",
" query_result = conn.execute(query).fetchdf().reset_index(drop=True)\n",
" finally:\n",
" os.chdir(original_cwd)\n",
"\n",
" return query_result"
]
},
{
"cell_type": "markdown",
"id": "24a49b1d-7454-4955-b7b9-cb2480a68d9f",
"metadata": {},
"source": [
"Finally, we will write a function to utilize Groq API's [Tool Use](https://console.groq.com/docs/tool-use) functionality to call the ```execute_duckdb_query_function_calling``` with the appropriate query name. We will provide the query/description mappings from ```verified_queries_dict``` in the system prompt so that the LLM can determine which query most appropriately answers the user's question:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "6b454910-4352-40cc-b9b2-cc79edabd7c1",
"metadata": {},
"outputs": [],
"source": [
"def call_verified_sql(user_question,verified_queries_dict,model):\n",
" \n",
" #Simplify verified_queries_dict to just show query name and description\n",
" query_description_mapping = {key: subdict['description'] for key, subdict in verified_queries_dict.items()}\n",
" \n",
" # Step 1: send the conversation and available functions to the model\n",
" messages=[\n",
" {\n",
" \"role\": \"system\",\n",
" \"content\": '''You are a function calling LLM that uses the data extracted from the execute_duckdb_query_function_calling function to answer questions around a DuckDB dataset.\n",
" \n",
" Extract the query_name parameter from this mapping by finding the one whose description best matches the user's question: \n",
" {query_description_mapping}\n",
" '''.format(query_description_mapping=query_description_mapping)\n",
" },\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": user_question,\n",
" }\n",
" ]\n",
" tools = [\n",
" {\n",
" \"type\": \"function\",\n",
" \"function\": {\n",
" \"name\": \"execute_duckdb_query_function_calling\",\n",
" \"description\": \"Executes a verified DuckDB SQL Query\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"query_name\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The name of the verified query (i.e. 'most-recent-purchases')\",\n",
" }\n",
" },\n",
" \"required\": [\"query_name\"],\n",
" },\n",
" },\n",
" }\n",
" ]\n",
" response = client.chat.completions.create(\n",
" model=model,\n",
" messages=messages,\n",
" tools=tools,\n",
" tool_choice=\"auto\", \n",
" max_tokens=4096\n",
" )\n",
" \n",
" response_message = response.choices[0].message\n",
" tool_calls = response_message.tool_calls\n",
" \n",
" available_functions = {\n",
" \"execute_duckdb_query_function_calling\": execute_duckdb_query_function_calling,\n",
" }\n",
" for tool_call in tool_calls:\n",
" function_name = tool_call.function.name\n",
" function_to_call = available_functions[function_name]\n",
" function_args = json.loads(tool_call.function.arguments)\n",
" print('Query found: ',function_args.get(\"query_name\"))\n",
" function_response = function_to_call(\n",
" query_name=function_args.get(\"query_name\"),\n",
" verified_queries_dict=verified_queries_dict\n",
" )\n",
" \n",
" return function_response"
]
},
{
"cell_type": "markdown",
"id": "19bd48ae-4091-4abb-ae65-ac89719e0146",
"metadata": {},
"source": [
"Now, when we ask the LLM \"What were the most recent purchases?\", we will get the same logic every time:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "f828b583-291d-4aef-80b0-ce520b010ba5",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Query found: most-recent-purchases\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>purchase_date</th>\n",
" <th>product_name</th>\n",
" <th>amount</th>\n",
" <th>name</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>2024-02-05</td>\n",
" <td>Tesla</td>\n",
" <td>75000</td>\n",
" <td>Jared Dunn</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2024-02-04</td>\n",
" <td>iPhone</td>\n",
" <td>700</td>\n",
" <td>Bertram Gilfoyle</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2024-02-03</td>\n",
" <td>Humane pin</td>\n",
" <td>500</td>\n",
" <td>Dinesh Chugtai</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2024-02-02</td>\n",
" <td>Tesla</td>\n",
" <td>70000</td>\n",
" <td>Erlich Bachman</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2024-02-01</td>\n",
" <td>iPhone</td>\n",
" <td>750</td>\n",
" <td>Richard Hendricks</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" purchase_date product_name amount name\n",
"0 2024-02-05 Tesla 75000 Jared Dunn\n",
"1 2024-02-04 iPhone 700 Bertram Gilfoyle\n",
"2 2024-02-03 Humane pin 500 Dinesh Chugtai\n",
"3 2024-02-02 Tesla 70000 Erlich Bachman\n",
"4 2024-02-01 iPhone 750 Richard Hendricks"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"user_prompt = 'What were the most recent purchases?'\n",
"call_verified_sql(user_prompt,verified_queries_dict,model)"
]
},
{
"cell_type": "markdown",
"id": "4b57679e-8828-46c7-9ae9-d420f0d794af",
"metadata": {},
"source": [
"The downside with using verified queries, of course, is having to write and verify them, which takes away from the magic of watching an LLM generate a SQL query on the fly. But in an environment where reliability is critical, function calling against verified queries is a much more consistent way to chat with your data than Text-To-SQL."
]
},
{
"cell_type": "markdown",
"id": "632b4242-9a51-4d04-9a11-b0f48bf9f177",
"metadata": {},
"source": [
"This is a simple example, but you could even take it a step further by defining parameters for each query (that you might find in a WHERE clause), and doing another function call once the verified query is found to find the parameter(s) to inject in the query from the user prompt. Go ahead and try it out!"
]
},
{
"cell_type": "markdown",
"id": "74c9774e-6ef2-4b90-9f62-f69bedef1e61",
"metadata": {},
"source": [
"### Conclusion"
]
},
{
"cell_type": "markdown",
"id": "5cfd0fb7-6606-40ac-9936-2fc5c880be9d",
"metadata": {},
"source": [
"In this notebook we've explored two techniques for writing and executing SQL with LLMs using Groq API: Text-to-SQL (where the LLM generates SQL in the moment) and Verified Queries (where the LLM determines which verified query is most appropriate for your question and executes it). But perhaps the best approach is a blend - for ad-hoc reporting, there is still a lot of power in Text-to-SQL for quick answers. For user questions where there is no good verified query, you could default to using Text-To-SQL and then add that query to your dictionary of verified queries if it looks good. Either way, using LLMs on top of your data will lead to better and faster insights - just be sure to follow good data governance practices."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
@@ -0,0 +1,7 @@
description: Employees without a purchase since Feb 1, 2024
sql: |
SELECT employees.name as employees_without_purchases
FROM employees.csv AS employees
LEFT JOIN purchases.csv AS purchases ON employees.employee_id = purchases.employee_id
AND purchases.purchase_date > '2024-02-01'
WHERE purchases.purchase_id IS NULL
@@ -0,0 +1,9 @@
description: Employee with the most expensive purchase
sql: |
SELECT employees.name AS employee_name,
MAX(amount) AS max_purchase_amount
FROM purchases.csv AS purchases
JOIN employees.csv AS employees ON purchases.employee_id = employees.employee_id
GROUP BY employees.name
ORDER BY max_purchase_amount DESC
LIMIT 1
@@ -0,0 +1,11 @@
description: Five most recent purchases
sql: |
SELECT
purchases.purchase_date,
purchases.product_name,
purchases.amount,
employees.name
FROM purchases.csv AS purchases
JOIN employees.csv AS employees ON purchases.employee_id = employees.employee_id
ORDER BY purchases.purchase_date DESC
LIMIT 5;
@@ -0,0 +1,6 @@
description: Number of Teslas purchased
sql: |
SELECT COUNT(*) as number_of_teslas
FROM purchases.csv AS p
JOIN employees.csv AS e ON e.employee_id = p.employee_id
WHERE p.product_name = 'Tesla'
File diff suppressed because one or more lines are too long
@@ -0,0 +1,31 @@
**Date:** March 28, 2024
**Patient:** David Smith, 42 years old
**MRN:** 00456321
**Chief Complaint:** "I've been feeling really tired lately, more than usual."
**History of Present Illness:** The patient is a 42-year-old who presents with a 3-week history of increased fatigue, decreased energy, and occasional headaches. The patient reports struggling with sleep due to stress from work and personal life. The patient is currently working two part-time jobs but still finds it hard to make ends meet, indicating financial stress. They express concern over the cost of medications and healthcare visits.
**Past Medical History:** Type 2 Diabetes Mellitus, Hypertension
**Social History:**
The patient juggles two jobs to make ends meet, one at a local retail store and another in a fast-food chain, neither offering full-time hours or benefits. Despite the long hours, the patient mentions financial difficulties, especially with covering rent and providing. They share an apartment with three others in an area described as 'not the safest,' due to recent break-ins and a noticeable police presence. Meals are often missed or minimal, as the patient tries to stretch their budget, sometimes seeking help from local food banks when things get particularly tight.
Educationally, the patient completed high school but hasn't pursued further studies, citing lack of funds and the immediate need to support their family after graduation. They rely on buses and trains for transportation, which complicates timely access to healthcare, often causing delays or missed appointments. Socially, the patient admits to feeling isolated, with most of their family living out of state after their divorce and a personal life that has been 'on hold' due to work and financial pressures. They have a basic health insurance plan with high co-payments, which has led to skipping some recommended medical tests and treatments.
**Review of Systems:** Denies chest pain, shortness of breath, or fever. Reports occasional headaches.
**Physical Examination:**
- General: Appears tired but is alert and oriented.
- Vitals: BP 142/89, HR 78, Temp 98.6°F, Resp 16/min
**Assessment/Plan:**
- Continue to monitor blood pressure and diabetes control.
- Discuss affordable medication options with a pharmacist.
- Refer to a social worker to address food security, housing concerns, and access to healthcare services.
- Encourage the patient to engage with community support groups for social support.
- Schedule a follow-up appointment in 4 weeks or sooner if symptoms worsen.
**Comments:** The patient's health concerns are compounded by socioeconomic factors, including employment status, housing stability, food security, and access to healthcare. Addressing these social determinants of health is crucial for improving the patient's overall well-being.
@@ -0,0 +1,28 @@
**Date:** March 28, 2024
**Patient:** Maria Gonzalez, 30 years old
**MRN:** 00567289
**Chief Complaint:** "Persistent lower back pain for a few months now."
**History of Present Illness:** A 30-year-old patient reports chronic lower back pain worsening over the last six months. The pain is constant, with sharp spikes during physical activities. They've mentioned how their new work-from-home situation, a result of transitioning to a full-time customer service role, has brought about mixed feelings. While the stress from commuting is gone, they find the job itself quite stressful and somewhat isolating.
**Past Medical History:** Asthma, managed with medication.
**Social History:**
Transitioned to a full-time, work-from-home customer service job, the patient, who is single, appreciates the eliminated commute but feels isolated. They live in a studio downtown—affordable, yet tight on their budget, especially with student loans from their Bachelors in Communication looming. Financial concerns are apparent; they mention stretching their budget to cover essentials and occasionally opting for takeout when too pressed for time. Despite these financial pressures, they have not skipped medical care, implying a basic yet sufficient health insurance plan. Social interactions have dwindled, attributed to work's demands, hinting at a sense of isolation.
**Review of Systems:** Denies any recent weight loss, fever, or other systemic symptoms.
**Physical Examination:**
- General: Appears comfortable at rest, but movement prompts discomfort.
- Vitals: BP 118/76, HR 64, Temp 98.6°F, Resp 16/min
**Assessment/Plan:**
- Order an MRI to pinpoint the cause of the back pain.
- Physical therapy referral for pain management and mobility improvement.
- Suggest lifestyle changes for a healthier diet and stress management.
- Follow-up in 6 weeks to evaluate progress and adjust the care plan as needed.
**Comments:** Addressing the patient's lower back pain requires a multifaceted approach that considers their medical, physical, and social circumstances.
@@ -0,0 +1,28 @@
**Date:** March 28, 2024
**Patient:** Helen Johnson, 75 years old
**MRN:** 00678934
**Chief Complaint:** "I've been feeling quite lonely lately, and it's getting harder to get around."
**History of Present Illness:** The patient, a 75-year-old, reports increasing feelings of loneliness and difficulty accessing transportation for medical appointments and social engagements. They have no family living nearby and have found their circle of friends diminishing over the years.
**Past Medical History:** Type 2 Diabetes Mellitus, well-controlled; Osteoarthritis
**Social History:**
The patient lives in a comfortable, single-story home in a safe neighborhood and has no financial worries thanks to a stable retirement fund from their college-educated career. They have always been independent, but recently, the challenge of not driving has become more pronounced. The patient, who is widowed, relies on a neighbor or community services for rides, which are not always available, leading to missed appointments and social events. This lack of mobility has contributed to their social isolation. While they manage to maintain a balanced diet with grocery delivery services, the patient misses the social interaction of shopping and community events.
**Review of Systems:** Positive for occasional mild depressive symptoms. Negative for acute distress or significant physical complaints.
**Physical Examination:**
- General: Well-nourished, appears older than stated age due to mobility issues.
- Vitals: BP 130/85, HR 68, Temp 98.6°F, Resp 16/min
**Assessment/Plan:**
- Encourage participation in local senior community activities, many of which offer transportation services.
- Refer to a social worker to explore additional resources for transportation and social engagement.
- Consider a home health visit to assess for any additional support needs in managing daily activities.
- Schedule a follow-up appointment in 3 months, with a reminder to arrange for transportation in advance.
**Comments:** The patient's well-being is currently impacted more by social determinants such as transportation and isolation than by immediate medical concerns. Addressing these areas is crucial for improving their quality of life.
@@ -0,0 +1,32 @@
**Date:** March 28, 2024
**Patient:** Brian Lee, 55 years old
**MRN:** 00785642
**Chief Complaint:** "I've been having trouble managing my blood sugar levels."
**History of Present Illness:** The patient is a 55-year-old with a known diagnosis of Type 2 Diabetes Mellitus, presenting with difficulty in managing blood sugar levels over the past month. Reports fluctuating blood sugar readings despite adherence to prescribed diet and medication. The patient expresses a desire to avoid any complications associated with poor diabetes management.
**Past Medical History:** Type 2 Diabetes Mellitus, controlled hypertension
**Social History:**
The patient is a self-employed graphic designer, working from a home office. They describe their work as fulfilling and report a stable income. They own a home in a well-regarded neighborhood, noting its quiet and safe environment. The patient has a supportive spouse and a close circle of friends, often participating in social gatherings and community events.
The patient completed a bachelor's degree in graphic design and continues to take online courses to stay updated in their field. They have reliable transportation, a recent model car, ensuring timely access to healthcare appointments. The patient is an active member of a local walking group, which meets thrice a week for exercise and socialization.
Nutritionally, the patient is mindful of their diet, focusing on low-glycemic foods, and has not faced issues with food security. They have comprehensive health insurance coverage through a private provider, with satisfactory benefits that cover their medical needs, including diabetes management.
**Review of Systems:** Reports consistent adherence to diabetic diet and medication regimen. Denies any episodes of hypoglycemia or diabetic ketoacidosis.
**Physical Examination:**
- General: Well-nourished and well-kept appearance. Alert and oriented.
- Vitals: BP 130/80, HR 72, Temp 98.6°F, Resp 14/min
**Assessment/Plan:**
- Review current diabetes management plan and consider medication adjustments.
- Recommend continuous glucose monitoring (CGM) to better understand glucose patterns and variability.
- Encourage continued engagement with community exercise groups and dietary mindfulness.
- Schedule a follow-up appointment in 3 months or sooner if glucose management issues persist.
**Comments:** The patient demonstrates a proactive approach to managing their diabetes, supported by a stable and healthy social environment. Continued focus on lifestyle modification and close monitoring of blood sugar levels are key to preventing complications.
@@ -0,0 +1,30 @@
**Date:** March 28, 2024
**Patient:** Carlos Rodriguez, 48 years old
**MRN:** 00893247
**Chief Complaint:** "I'm feeling overwhelmed and can't seem to catch my breath."
**History of Present Illness:** The patient, a 48-year-old single individual, presents with chronic stress, anxiety, and episodes of shortness of breath, attributing some issues to excessive drinking. Overwhelmed by recent job loss, housing instability, and a language barrier, these factors exacerbate their sense of isolation.
**Past Medical History:** Hypertension, Generalized Anxiety Disorder
**Social History:**
After losing their job six months ago, the patient has faced significant challenges in finding new employment, exacerbated by a language barrier that complicates interviews and interactions. The resulting financial strain led to losing their apartment, and they are now staying temporarily with a friend in an unsafe area. This precarious living situation and limited ability to communicate have deepened their feelings of isolation. The patient mentioned, "It's hard when you can't fully express what you're going through." Public transportation is their only means of travel, which often adds to their stress, especially when language issues arise. Currently uninsured due to job loss, patient has dealt with food insecurity and worries about the costs of medication and medical care, having skipped their last visit due to the cost.
**Review of Systems:** Positive for anxiety, shortness of breath, occasional dizziness. Notes increased alcohol consumption as a stress relief. Denies chest pain or fever.
**Physical Examination:**
- General: Appears anxious and shows signs of chronic fatigue.
- Vitals: BP 150/90, HR 85, Temp 98.6°F, Resp 20/min
**Assessment/Plan:**
- Refer to a social worker for assistance with housing, job search support, especially programs offering language support, and help applying for health insurance.
- Prescribe medication to manage hypertension and anxiety, providing samples to mitigate cost concerns.
- Address alcohol use and recommend counseling services for substance use and stress management.
- Schedule follow-up appointments at a reduced rate through the clinic's assistance program, ensuring language support is available.
- Connect the patient with community resources for food security and bilingual social support groups.
- Follow-up in 2 weeks to reassess the condition and effectiveness of the social support interventions.
**Comments:** The patient's situation is complicated by financial issues, housing instability, and significant social challenges, including a language barrier and unhealthy coping mechanisms. A comprehensive and culturally sensitive approach is essential to address the multifaceted aspects of their care.
@@ -0,0 +1,340 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "104f2b97-f9bb-4dcc-a4c8-099710768851",
"metadata": {},
"source": [
"# Parallel Tool use"
]
},
{
"cell_type": "markdown",
"id": "f8dc57b6-2c48-4ee3-bb2c-25441274ed2f",
"metadata": {},
"source": [
"### Setup"
]
},
{
"cell_type": "markdown",
"id": "e70814b4",
"metadata": {},
"source": [
"Make sure you have `ipykernel` and `pip` pre-installed"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "962ae5e2",
"metadata": {},
"outputs": [],
"source": [
"%pip install -r requirements.txt"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "e21816b3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Groq API key configured: gsk_7FdrzM...'"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import os\n",
"import json\n",
"\n",
"from groq import Groq\n",
"from dotenv import load_dotenv\n",
"\n",
"load_dotenv()\n",
"\"Groq API key configured: \" + os.environ[\"GROQ_API_KEY\"][:10] + \"...\""
]
},
{
"cell_type": "markdown",
"id": "7f7c9c55-e925-4cc1-89f2-58237acf14a4",
"metadata": {},
"source": [
"We will use the ```llama3-70b-8192``` model in this demo. Note that you will need a Groq API Key to proceed and can create an account [here](https://console.groq.com/) to generate one for free. Only Llama 3 models support parallel tool use at this time (05/07/2024).\n",
"\n",
"We recommend using the 70B Llama 3 model, 8B has subpar consistency."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "0cca781b-1950-4167-b36a-c1099d6b3b00",
"metadata": {},
"outputs": [],
"source": [
"client = Groq(api_key=os.getenv(\"GROQ_API_KEY\"))\n",
"model = \"llama3-70b-8192\""
]
},
{
"cell_type": "markdown",
"id": "2c23ec2b",
"metadata": {},
"source": [
"Let's define a dummy function we can invoke in our tool use loop"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "f2ce18dc",
"metadata": {},
"outputs": [],
"source": [
"def get_weather(city: str):\n",
" if city == \"Madrid\":\n",
" return 35\n",
" elif city == \"San Francisco\":\n",
" return 18\n",
" elif city == \"Paris\":\n",
" return 20\n",
" else:\n",
" return 15"
]
},
{
"cell_type": "markdown",
"id": "a37e3c92",
"metadata": {},
"source": [
"Now we define our messages and tools and run the completion request."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "6b454910-4352-40cc-b9b2-cc79edabd7c1",
"metadata": {},
"outputs": [],
"source": [
"messages = [\n",
" {\"role\": \"system\", \"content\": \"\"\"You are a helpful assistant.\"\"\"},\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": \"What is the weather in Paris, Tokyo and Madrid?\",\n",
" },\n",
"]\n",
"tools = [\n",
" {\n",
" \"type\": \"function\",\n",
" \"function\": {\n",
" \"name\": \"get_weather\",\n",
" \"description\": \"Returns the weather in the given city in degrees Celsius\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"city\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The name of the city\",\n",
" }\n",
" },\n",
" \"required\": [\"city\"],\n",
" },\n",
" },\n",
" }\n",
"]\n",
"response = client.chat.completions.create(\n",
" model=model, messages=messages, tools=tools, tool_choice=\"auto\", max_tokens=4096\n",
")\n",
"\n",
"response_message = response.choices[0].message"
]
},
{
"cell_type": "markdown",
"id": "25c2838f",
"metadata": {},
"source": [
"# Processing the tool calls\n",
"\n",
"Now we process the assistant message and construct the required messages to continue the conversation. \n",
"\n",
"*Including* invoking each tool_call against our actual function."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "fe623ab9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[\n",
" {\n",
" \"role\": \"system\",\n",
" \"content\": \"You are a helpful assistant.\"\n",
" },\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": \"What is the weather in Paris, Tokyo and Madrid?\"\n",
" },\n",
" {\n",
" \"role\": \"assistant\",\n",
" \"tool_calls\": [\n",
" {\n",
" \"id\": \"call_5ak8\",\n",
" \"function\": {\n",
" \"name\": \"get_weather\",\n",
" \"arguments\": \"{\\\"city\\\":\\\"Paris\\\"}\"\n",
" },\n",
" \"type\": \"function\"\n",
" },\n",
" {\n",
" \"id\": \"call_zq26\",\n",
" \"function\": {\n",
" \"name\": \"get_weather\",\n",
" \"arguments\": \"{\\\"city\\\":\\\"Tokyo\\\"}\"\n",
" },\n",
" \"type\": \"function\"\n",
" },\n",
" {\n",
" \"id\": \"call_znf3\",\n",
" \"function\": {\n",
" \"name\": \"get_weather\",\n",
" \"arguments\": \"{\\\"city\\\":\\\"Madrid\\\"}\"\n",
" },\n",
" \"type\": \"function\"\n",
" }\n",
" ]\n",
" },\n",
" {\n",
" \"role\": \"tool\",\n",
" \"content\": \"20\",\n",
" \"tool_call_id\": \"call_5ak8\"\n",
" },\n",
" {\n",
" \"role\": \"tool\",\n",
" \"content\": \"15\",\n",
" \"tool_call_id\": \"call_zq26\"\n",
" },\n",
" {\n",
" \"role\": \"tool\",\n",
" \"content\": \"35\",\n",
" \"tool_call_id\": \"call_znf3\"\n",
" }\n",
"]\n"
]
}
],
"source": [
"tool_calls = response_message.tool_calls\n",
"\n",
"messages.append(\n",
" {\n",
" \"role\": \"assistant\",\n",
" \"tool_calls\": [\n",
" {\n",
" \"id\": tool_call.id,\n",
" \"function\": {\n",
" \"name\": tool_call.function.name,\n",
" \"arguments\": tool_call.function.arguments,\n",
" },\n",
" \"type\": tool_call.type,\n",
" }\n",
" for tool_call in tool_calls\n",
" ],\n",
" }\n",
")\n",
"\n",
"available_functions = {\n",
" \"get_weather\": get_weather,\n",
"}\n",
"for tool_call in tool_calls:\n",
" function_name = tool_call.function.name\n",
" function_to_call = available_functions[function_name]\n",
" function_args = json.loads(tool_call.function.arguments)\n",
" function_response = function_to_call(**function_args)\n",
"\n",
" # Note how we create a separate tool call message for each tool call\n",
" # the model is able to discern the tool call result through the tool_call_id\n",
" messages.append(\n",
" {\n",
" \"role\": \"tool\",\n",
" \"content\": json.dumps(function_response),\n",
" \"tool_call_id\": tool_call.id,\n",
" }\n",
" )\n",
"\n",
"print(json.dumps(messages, indent=2))"
]
},
{
"cell_type": "markdown",
"id": "1abe981a",
"metadata": {},
"source": [
"Now we run our final completion with multiple tool call results included in the messages array.\n",
"\n",
"**Note**\n",
"\n",
"We pass the tool definitions again to help the model understand:\n",
"\n",
"1. The assistant message with the tool call\n",
"2. Interpret the tool results."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "5f077df3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The weather in Paris is 20°C, in Tokyo is 15°C, and in Madrid is 35°C.\n"
]
}
],
"source": [
"response = client.chat.completions.create(\n",
" model=model, messages=messages, tools=tools, tool_choice=\"auto\", max_tokens=4096\n",
")\n",
"\n",
"print(response.choices[0].message.content)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
@@ -0,0 +1,2 @@
groq
python-dotenv
File diff suppressed because one or more lines are too long
@@ -0,0 +1,21 @@
# Groq LangChain Conversational Chatbot
A simple application that allows users to interact with a conversational chatbot powered by LangChain. The application uses the Groq API to generate responses and leverages LangChain's [ConversationBufferWindowMemory](https://python.langchain.com/v0.1/docs/modules/memory/types/buffer_window/) to maintain a history of the conversation to provide context for the chatbot's responses.
## Features
- **Conversational Interface**: The application provides a conversational interface where users can ask questions or make statements, and the chatbot responds accordingly.
- **Contextual Responses**: The application maintains a history of the conversation, which is used to provide context for the chatbot's responses.
- **LangChain Integration**: The chatbot is powered by the LangChain API, which uses advanced natural language processing techniques to generate human-like responses.
## Usage
<!-- markdown-link-check-disable -->
You will need to store a valid Groq API Key as a secret to proceed with this example. You can generate one for free [here](https://console.groq.com/keys).
<!-- markdown-link-check-enable -->
You can [fork and run this application on Replit](https://replit.com/@GroqCloud/Chatbot-with-Conversational-Memory-on-LangChain) or run it on the command line with `python main.py`
@@ -0,0 +1,74 @@
import os
from groq import Groq
from langchain.chains import ConversationChain, LLMChain
from langchain_core.prompts import (
ChatPromptTemplate,
HumanMessagePromptTemplate,
MessagesPlaceholder,
)
from langchain_core.messages import SystemMessage
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
from langchain_groq import ChatGroq
from langchain.prompts import PromptTemplate
def main():
"""
This function is the main entry point of the application. It sets up the Groq client, the Streamlit interface, and handles the chat interaction.
"""
# Get Groq API key
groq_api_key = os.environ['GROQ_API_KEY']
model = 'llama3-8b-8192'
# Initialize Groq Langchain chat object and conversation
groq_chat = ChatGroq(
groq_api_key=groq_api_key,
model_name=model
)
print("Hello! I'm your friendly Groq chatbot. I can help answer your questions, provide information, or just chat. I'm also super fast! Let's start our conversation!")
system_prompt = 'You are a friendly conversational chatbot'
conversational_memory_length = 5 # number of previous messages the chatbot will remember during the conversation
memory = ConversationBufferWindowMemory(k=conversational_memory_length, memory_key="chat_history", return_messages=True)
#chat_history = []
while True:
user_question = input("Ask a question: ")
# If the user has asked a question,
if user_question:
# Construct a chat prompt template using various components
prompt = ChatPromptTemplate.from_messages(
[
SystemMessage(
content=system_prompt
), # This is the persistent system prompt that is always included at the start of the chat.
MessagesPlaceholder(
variable_name="chat_history"
), # This placeholder will be replaced by the actual chat history during the conversation. It helps in maintaining context.
HumanMessagePromptTemplate.from_template(
"{human_input}"
), # This template is where the user's current input will be injected into the prompt.
]
)
# Create a conversation chain using the LangChain LLM (Language Learning Model)
conversation = LLMChain(
llm=groq_chat, # The Groq LangChain chat object initialized earlier.
prompt=prompt, # The constructed prompt template.
verbose=False, # TRUE Enables verbose output, which can be useful for debugging.
memory=memory, # The conversational memory object that stores and manages the conversation history.
)
# The chatbot's answer is generated by sending the full prompt to the Groq API.
response = conversation.predict(human_input=user_question)
print("Chatbot:", response)
if __name__ == "__main__":
main()
@@ -0,0 +1,23 @@
# CrewAI Machine Learning Assistant
## Overview
The [CrewAI](https://docs.crewai.com/) Machine Learning Assistant is a command line application designed to kickstart your machine learning projects. It leverages a team of AI agents to guide you through the initial steps of defining, assessing, and solving machine learning problems.
## Features
- **Agents**: Utilizes specialized agents to perform tasks such as problem definition, data assessment, model recommendation, and code generation, enhancing the workflow and efficiency of machine learning projects.
- **CrewAI Framework**: Integrates multiple agents into a cohesive framework, enabling seamless interaction and task execution to streamline the machine learning process.
- **LangChain Integration**: Incorporates LangChain to facilitate natural language processing and enhance the interaction between the user and the machine learning assistant.
## Usage
<!-- markdown-link-check-disable -->
You will need to store a valid Groq API Key as a secret to proceed with this example. You can generate one for free [here](https://console.groq.com/keys).
<!-- markdown-link-check-enable -->
You can [fork and run this application on Replit](https://replit.com/@GroqCloud/CrewAI-Machine-Learning-Assistant) or run it on the command line with `python main.py`. You can upload a sample .csv to the same directory as `main.py` to give the application a head start on your ML problem. The application will output a Markdown file including python code for your ML use case to the same directory as main.py.
@@ -0,0 +1,184 @@
import pandas as pd
import os
from crewai import Agent, Task, Crew
from langchain_groq import ChatGroq
def main():
"""
Main function to initialize and run the CrewAI Machine Learning Assistant.
This function sets up a machine learning assistant using the Llama 3 model with the ChatGroq API.
It provides a text-based interface for users to define, assess, and solve machine learning problems
by interacting with multiple specialized AI agents. The function outputs the results to the console
and writes them to a markdown file.
Steps:
1. Initialize the ChatGroq API with the specified model and API key.
2. Display introductory text about the CrewAI Machine Learning Assistant.
3. Create and configure four AI agents:
- Problem_Definition_Agent: Clarifies the machine learning problem.
- Data_Assessment_Agent: Evaluates the quality and suitability of the provided data.
- Model_Recommendation_Agent: Suggests suitable machine learning models.
- Starter_Code_Generator_Agent: Generates starter Python code for the project.
4. Prompt the user to describe their machine learning problem.
5. Check if a .csv file is available in the current directory and try to read it as a DataFrame.
6. Define tasks for the agents based on user input and data availability.
7. Create a Crew instance with the agents and tasks, and run the tasks.
8. Print the results and write them to an output markdown file.
"""
model = 'llama3-8b-8192'
llm = ChatGroq(
temperature=0,
groq_api_key = os.getenv('GROQ_API_KEY'),
model_name=model
)
print('CrewAI Machine Learning Assistant')
multiline_text = """
The CrewAI Machine Learning Assistant is designed to guide users through the process of defining, assessing, and solving machine learning problems. It leverages a team of AI agents, each with a specific role, to clarify the problem, evaluate the data, recommend suitable models, and generate starter Python code. Whether you're a seasoned data scientist or a beginner, this application provides valuable insights and a head start in your machine learning projects.
"""
print(multiline_text)
Problem_Definition_Agent = Agent(
role='Problem_Definition_Agent',
goal="""clarify the machine learning problem the user wants to solve,
identifying the type of problem (e.g., classification, regression) and any specific requirements.""",
backstory="""You are an expert in understanding and defining machine learning problems.
Your goal is to extract a clear, concise problem statement from the user's input,
ensuring the project starts with a solid foundation.""",
verbose=True,
allow_delegation=False,
llm=llm,
)
Data_Assessment_Agent = Agent(
role='Data_Assessment_Agent',
goal="""evaluate the data provided by the user, assessing its quality,
suitability for the problem, and suggesting preprocessing steps if necessary.""",
backstory="""You specialize in data evaluation and preprocessing.
Your task is to guide the user in preparing their dataset for the machine learning model,
including suggestions for data cleaning and augmentation.""",
verbose=True,
allow_delegation=False,
llm=llm,
)
Model_Recommendation_Agent = Agent(
role='Model_Recommendation_Agent',
goal="""suggest the most suitable machine learning models based on the problem definition
and data assessment, providing reasons for each recommendation.""",
backstory="""As an expert in machine learning algorithms, you recommend models that best fit
the user's problem and data. You provide insights into why certain models may be more effective than others,
considering classification vs regression and supervised vs unsupervised frameworks.""",
verbose=True,
allow_delegation=False,
llm=llm,
)
Starter_Code_Generator_Agent = Agent(
role='Starter_Code_Generator_Agent',
goal="""generate starter Python code for the project, including data loading,
model definition, and a basic training loop, based on findings from the problem definitions,
data assessment and model recommendation""",
backstory="""You are a code wizard, able to generate starter code templates that users
can customize for their projects. Your goal is to give users a head start in their coding efforts.""",
verbose=True,
allow_delegation=False,
llm=llm,
)
user_question = input("Describe your ML problem: ")
data_upload = False
# Check if there is a .csv file in the current directory
if any(file.endswith(".csv") for file in os.listdir()):
sample_fp = [file for file in os.listdir() if file.endswith(".csv")][0]
try:
# Attempt to read the uploaded file as a DataFrame
df = pd.read_csv(sample_fp).head(5)
# If successful, set 'data_upload' to True
data_upload = True
# Display the DataFrame in the app
print("Data successfully uploaded and read as DataFrame:")
print(df)
except Exception as e:
print(f"Error reading the file: {e}")
if user_question:
task_define_problem = Task(
description="""Clarify and define the machine learning problem,
including identifying the problem type and specific requirements.
Here is the user's problem:
{ml_problem}
""".format(ml_problem=user_question),
agent=Problem_Definition_Agent,
expected_output="A clear and concise definition of the machine learning problem."
)
if data_upload:
task_assess_data = Task(
description="""Evaluate the user's data for quality and suitability,
suggesting preprocessing or augmentation steps if needed.
Here is a sample of the user's data:
{df}
The file name is called {uploaded_file}
""".format(df=df.head(),uploaded_file=sample_fp),
agent=Data_Assessment_Agent,
expected_output="An assessment of the data's quality and suitability, with suggestions for preprocessing or augmentation if necessary."
)
else:
task_assess_data = Task(
description="""The user has not uploaded any specific data for this problem,
but please go ahead and consider a hypothetical dataset that might be useful
for their machine learning problem.
""",
agent=Data_Assessment_Agent,
expected_output="A hypothetical dataset that might be useful for the user's machine learning problem, along with any necessary preprocessing steps."
)
task_recommend_model = Task(
description="""Suggest suitable machine learning models for the defined problem
and assessed data, providing rationale for each suggestion.""",
agent=Model_Recommendation_Agent,
expected_output="A list of suitable machine learning models for the defined problem and assessed data, along with the rationale for each suggestion."
)
task_generate_code = Task(
description="""Generate starter Python code tailored to the user's project using the model recommendation agent's recommendation(s),
including snippets for package import, data handling, model definition, and training
""",
agent=Starter_Code_Generator_Agent,
expected_output="Python code snippets for package import, data handling, model definition, and training, tailored to the user's project, plus a brief summary of the problem and model recommendations."
)
crew = Crew(
agents=[Problem_Definition_Agent, Data_Assessment_Agent, Model_Recommendation_Agent, Starter_Code_Generator_Agent],
tasks=[task_define_problem, task_assess_data, task_recommend_model, task_generate_code],
verbose=False
)
result = crew.kickoff()
print(result)
with open('output.md', "w") as file:
print('\n\nThese results have been exported to output.md')
file.write(result)
if __name__ == "__main__":
main()
@@ -0,0 +1,3 @@
crewai
langchain_groq
pandas
@@ -0,0 +1,21 @@
# Groq Quickstart Conversational Chatbot
A simple application that allows users to interact with a conversational chatbot powered by Groq. This application is designed to get users up and running quickly with building a chatbot.
## Features
**Conversational Interface**: Provides a simple interface where users can input text and receive responses from the chatbot.
**Short Responses**: The chatbot replies with very short and concise answers, keeping interactions brief and to the point.
**Groq Integration**: Utilizes the Groq API to generate responses, leveraging the power of the Llama3-70b-8192 model.
## Usage
<!-- markdown-link-check-disable -->
You will need to store a valid Groq API Key as a secret to proceed with this example. You can generate one for free [here](https://console.groq.com/keys).
<!-- markdown-link-check-enable -->
You can [fork and run this application on Replit](https://replit.com/@GroqCloud/Groq-Quickstart-Conversational-Chatbot) or run it on the command line with `python main.py`.
@@ -0,0 +1,38 @@
#set GROQ_API_KEY in the secrets
import os
from groq import Groq
# Create the Groq client
client = Groq(
api_key=os.environ.get("GROQ_API_KEY")
)
# Set the system prompt
system_prompt = {
"role": "system",
"content":
"You are a helpful assistant. You reply with very short answers."
}
# Initialize the chat history
chat_history = [system_prompt]
while True:
# Get user input from the console
user_input = input("You: ")
# Append the user input to the chat history
chat_history.append({"role": "user", "content": user_input})
response = client.chat.completions.create(model="llama3-70b-8192",
messages=chat_history,
max_tokens=100,
temperature=1.2)
# Append the response to the chat history
chat_history.append({
"role": "assistant",
"content": response.choices[0].message.content
})
# Print the response
print("Assistant:", response.choices[0].message.content)
@@ -0,0 +1,27 @@
# 'Groqing the Stock Market' with Llama 3 Function Calling
This is a simple application that leverages the yfinance API to provide insights into stocks and their prices. The application uses the Llama 3 model on Groq in conjunction with Langchain to call functions based on the user prompt.
## Key Functions
- **get_stock_info(symbol, key)**: This function fetches various information about a given stock symbol. The information can be anything from the company's address to its financial ratios. The 'key' parameter specifies the type of information to fetch.
- **get_historical_price(symbol, start_date, end_date)**: This function fetches the historical stock prices for a given symbol from a specified start date to an end date. The returned data is a DataFrame with the date and closing price of the stock.
- **plot_price_over_time(historical_price_dfs)**: This function takes a list of DataFrames (each containing historical price data for a stock) and plots the prices over time using Plotly. The plot is saved to the same directory as the app.
- **call_functions(llm_with_tools, user_prompt)**: This function takes the user's question, invokes the appropriate tool (either get_stock_info or get_historical_price), and generates a response. If the user asked for historical prices, it also calls plot_price_over_time to generate a plot.
## Function Calling
The function calling in this application is handled by the Groq API, abstracted with Langchain. When the user asks a question, the application invokes the appropriate tool with parameters based on the user's question. The tool's output is then used to generate a response.
## Usage
<!-- markdown-link-check-disable -->
You will need to store a valid Groq API Key as a secret to proceed with this example. You can generate one for free [here](https://console.groq.com/keys).
<!-- markdown-link-check-enable -->
You can [fork and run this application on Replit](https://replit.com/@GroqCloud/Groqing-the-Stock-Market-Function-Calling-with-Llama3) or run it on the command line with `python main.py`.
@@ -0,0 +1,139 @@
from langchain_groq import ChatGroq
import os
import yfinance as yf
import pandas as pd
from langchain_core.tools import tool
from langchain_core.messages import AIMessage, SystemMessage, HumanMessage, ToolMessage
from datetime import date
import pandas as pd
import plotly.graph_objects as go
@tool
def get_stock_info(symbol, key):
'''Return the correct stock info value given the appropriate symbol and key. Infer valid key from the user prompt; it must be one of the following:
address1, city, state, zip, country, phone, website, industry, industryKey, industryDisp, sector, sectorKey, sectorDisp, longBusinessSummary, fullTimeEmployees, companyOfficers, auditRisk, boardRisk, compensationRisk, shareHolderRightsRisk, overallRisk, governanceEpochDate, compensationAsOfEpochDate, maxAge, priceHint, previousClose, open, dayLow, dayHigh, regularMarketPreviousClose, regularMarketOpen, regularMarketDayLow, regularMarketDayHigh, dividendRate, dividendYield, exDividendDate, beta, trailingPE, forwardPE, volume, regularMarketVolume, averageVolume, averageVolume10days, averageDailyVolume10Day, bid, ask, bidSize, askSize, marketCap, fiftyTwoWeekLow, fiftyTwoWeekHigh, priceToSalesTrailing12Months, fiftyDayAverage, twoHundredDayAverage, currency, enterpriseValue, profitMargins, floatShares, sharesOutstanding, sharesShort, sharesShortPriorMonth, sharesShortPreviousMonthDate, dateShortInterest, sharesPercentSharesOut, heldPercentInsiders, heldPercentInstitutions, shortRatio, shortPercentOfFloat, impliedSharesOutstanding, bookValue, priceToBook, lastFiscalYearEnd, nextFiscalYearEnd, mostRecentQuarter, earningsQuarterlyGrowth, netIncomeToCommon, trailingEps, forwardEps, pegRatio, enterpriseToRevenue, enterpriseToEbitda, 52WeekChange, SandP52WeekChange, lastDividendValue, lastDividendDate, exchange, quoteType, symbol, underlyingSymbol, shortName, longName, firstTradeDateEpochUtc, timeZoneFullName, timeZoneShortName, uuid, messageBoardId, gmtOffSetMilliseconds, currentPrice, targetHighPrice, targetLowPrice, targetMeanPrice, targetMedianPrice, recommendationMean, recommendationKey, numberOfAnalystOpinions, totalCash, totalCashPerShare, ebitda, totalDebt, quickRatio, currentRatio, totalRevenue, debtToEquity, revenuePerShare, returnOnAssets, returnOnEquity, freeCashflow, operatingCashflow, earningsGrowth, revenueGrowth, grossMargins, ebitdaMargins, operatingMargins, financialCurrency, trailingPegRatio
If asked generically for 'stock price', use currentPrice
'''
data = yf.Ticker(symbol)
stock_info = data.info
return stock_info[key]
@tool
def get_historical_price(symbol, start_date, end_date):
"""
Fetches historical stock prices for a given symbol from 'start_date' to 'end_date'.
- symbol (str): Stock ticker symbol.
- end_date (date): Typically today unless a specific end date is provided. End date MUST be greater than start date
- start_date (date): Set explicitly, or calculated as 'end_date - date interval' (for example, if prompted 'over the past 6 months', date interval = 6 months so start_date would be 6 months earlier than today's date). Default to '1900-01-01' if vaguely asked for historical price. Start date must always be before the current date
"""
data = yf.Ticker(symbol)
hist = data.history(start=start_date, end=end_date)
hist = hist.reset_index()
hist[symbol] = hist['Close']
return hist[['Date', symbol]]
def plot_price_over_time(historical_price_dfs):
'''
Plots the historical stock prices over time for the given DataFrames.
Parameters:
historical_price_dfs (list): List of DataFrames containing historical stock prices.
'''
full_df = pd.DataFrame(columns=['Date'])
for df in historical_price_dfs:
full_df = full_df.merge(df, on='Date', how='outer')
# Create a Plotly figure
fig = go.Figure()
# Dynamically add a trace for each stock symbol in the DataFrame
for column in full_df.columns[1:]: # Skip the first column since it's the date
fig.add_trace(go.Scatter(x=full_df['Date'], y=full_df[column], mode='lines+markers', name=column))
# Update the layout to add titles and format axis labels
fig.update_layout(
title='Stock Price Over Time: ' + ', '.join(full_df.columns.tolist()[1:]),
xaxis_title='Date',
yaxis_title='Stock Price (USD)',
yaxis_tickprefix='$',
yaxis_tickformat=',.2f',
xaxis=dict(
tickangle=-45,
nticks=20,
tickfont=dict(size=10),
),
yaxis=dict(
showgrid=True, # Enable y-axis grid lines
gridcolor='lightgrey', # Set grid line color
),
legend_title_text='Stock Symbol',
plot_bgcolor='gray', # Set plot background to white
paper_bgcolor='gray', # Set overall figure background to white
legend=dict(
bgcolor='gray', # Optional: Set legend background to white
bordercolor='black'
)
)
# Show the figure
fig.write_image("plot.png")
def call_functions(llm_with_tools, user_prompt):
'''
Call the functions to interact with the llm_with_tools using the given user_prompt.
This function processes the user input, invokes tools based on the input, performs necessary operations,
generates responses or messages, and plots historical stock prices over time.
Parameters:
llm_with_tools (ChatGroq): ChatGroq object containing the tools for interaction.
user_prompt (str): User input prompt.
Returns:
str: Contents of the invoked messages through llm_with_tools interaction.
'''
system_prompt = 'You are a helpful finance assistant that analyzes stocks and stock prices. Today is {today}'.format(today=date.today())
messages = [SystemMessage(system_prompt), HumanMessage(user_prompt)]
ai_msg = llm_with_tools.invoke(messages)
messages.append(ai_msg)
historical_price_dfs = []
symbols = []
for tool_call in ai_msg.tool_calls:
selected_tool = {"get_stock_info": get_stock_info, "get_historical_price": get_historical_price}[tool_call["name"].lower()]
tool_output = selected_tool.invoke(tool_call["args"])
if tool_call['name'] == 'get_historical_price':
historical_price_dfs.append(tool_output)
symbols.append(tool_output.columns[1])
else:
messages.append(ToolMessage(tool_output, tool_call_id=tool_call["id"]))
if len(historical_price_dfs) > 0:
plot_price_over_time(historical_price_dfs)
symbols = ' and '.join(symbols)
messages.append(ToolMessage('Tell the user that a historical stock price chart for {symbols} been generated.'.format(symbols=symbols), tool_call_id=0))
return llm_with_tools.invoke(messages).content
llm = ChatGroq(groq_api_key = os.getenv('GROQ_API_KEY'),model = 'llama3-70b-8192')
tools = [get_stock_info, get_historical_price]
llm_with_tools = llm.bind_tools(tools)
while True:
# Get user input from the console
user_input = input("You: ")
response = call_functions(llm_with_tools, user_input)
print("Assistant:", response)
@@ -0,0 +1,12 @@
streamlit
pandas
numpy
groq
langchain_community
langchain_groq
yfinance
plotly
langchain_core
nbformat>=4.2.0
ipython
kaleido
@@ -0,0 +1,21 @@
# LlamaChat: Conversational Chatbot with LlamaIndex and Llama3
A simple application that allows users to interact with a conversational chatbot powered by the LlamaIndex framework and Meta's Llama3 model. The application uses the Groq API to generate responses and supports different modes of interaction, including simple chat, streaming chat, and customizable chat with system prompts.
##Features
**LlamaIndex**: The application uses LlamaIndex to manage and generate responses, leveraging the power of Groq's language model.
**Simple Chat**: Generates responses based on user input using the Groq API with LlamaIndex.
**Streaming Chat**: Provides real-time streaming responses for user input.
**Customizable Chat**: Allows for chat customization by setting a system prompt to guide the chatbot's responses.
##Usage
<!-- markdown-link-check-disable -->
You will need to store a valid Groq API Key as a secret to proceed with this example. You can generate one for free [here](https://console.groq.com/keys).
<!-- markdown-link-check-enable -->
@@ -0,0 +1,46 @@
from llama_index.llms.groq import Groq
from llama_index.core.llms import ChatMessage
llm = Groq(model="llama3-8b-8192")
system_prompt = 'You are a friendly but highly sarcastic chatbot assistant'
while True:
# Get the user's question
user_input = input("User: ")
#user_input = 'write a few paragraphs explaining generative AI to a college freshman'
##################################
# Simple Chat
##################################
print('Simple Chat:\n\n')
response = llm.complete(user_input)
print(response)
##################################
# Streaming Chat
##################################
stream_response = llm.stream_complete(
user_input
)
print('\n\nStreaming Chat:\n')
for t in stream_response:
print(t.delta, end="")
##################################
# Customizable Chat
##################################
messages = [
ChatMessage(role="system", content=system_prompt),
ChatMessage(role="user", content=user_input),
]
print('\n\nChat with System Prompt:\n')
response_with_system_prompt = llm.chat(messages)
print(response_with_system_prompt)
@@ -0,0 +1,2 @@
llama_index
llama-index-llms-groq
@@ -0,0 +1,33 @@
# Presidential Speeches RAG with Pinecone
This repository contains a command line application that allows users to ask questions about US presidential speeches by applying Retrieval-Augmented Generation (RAG) over a Pinecone vector database. The application uses RAG to answer the user's question by retrieving the most relevant presidential speeches and using them to supplant the LLM response.
## Features
- **RAG (Retrieval-Augmented Generation)**: Enhances the generation of responses by integrating retrieval-based methods. This feature allows the system to fetch relevant information from a large corpus of data, providing more accurate and contextually appropriate answers by combining retrieved content with generative capabilities.
- **Vector Databases (Pinecone)**: Integrates with Pinecone to store and manage vector embeddings efficiently. Pinecone's high-performance vector database allows for fast and scalable similarity searches, enabling quick retrieval of relevant data for various machine learning and AI applications.
- **LangChain Integration**: Leverages LangChain to facilitate natural language processing tasks. LangChain enhances the interaction between the user and the system by providing robust language modeling capabilities, ensuring seamless and intuitive communication.
## Code Overview
The main script of the application is [main.py](./main.py). Here's a brief overview of its main functions:
- `get_relevant_excerpts(user_question, docsearch)`: This function takes a user's question and a Pinecone vector store as input, performs a similarity search on the vector store using the user's question, and returns the most relevant excerpts from presidential speeches.
- `get_relevant_excerpts(user_question, docsearch)`: This function takes a user's question and a Pinecone vector store as input, performs a similarity search on the vector store using the user's question, and returns the most relevant excerpts from presidential speeches.
- `presidential_speech_chat_completion(client, model, user_question, relevant_excerpts, additional_context)`: This function takes a Groq client, a pre-trained model, a user's question, relevant excerpts from presidential speeches, and additional context as input. It generates a response to the user's question based on the relevant excerpts and the additional context
## Usage
<!-- markdown-link-check-disable -->
You will need to store a valid Groq API Key as a secret to proceed with this example outside of this Repl. You can generate one for free [here](https://console.groq.com/keys).
<!-- markdown-link-check-enable -->
You would also need your own [Pinecone](https://www.pinecone.io/) index with presidential speech embeddings to run this code locally. You can create a Pinecone API key and one index for a small project for free on their Starter plan, and visit [this Cookbook post](https://github.com/groq/groq-api-cookbook/blob/dan/replit-conversion/presidential-speeches-rag/presidential-speeches-rag.ipynb) for more info on RAG and a guide to uploading these embeddings to a vector database
You can [fork and run this application on Replit](https://replit.com/@GroqCloud/Presidential-Speeches-RAG-with-Pinecone) or run it on the command line with `python main.py`.
@@ -0,0 +1,114 @@
import pandas as pd
import numpy as np
from groq import Groq
from pinecone import Pinecone
import os
from langchain_community.embeddings.sentence_transformer import SentenceTransformerEmbeddings
from langchain_pinecone import PineconeVectorStore
def get_relevant_excerpts(user_question, docsearch):
"""
This function retrieves the most relevant excerpts from presidential speeches based on the user's question.
Parameters:
user_question (str): The question asked by the user.
docsearch (PineconeVectorStore): The Pinecone vector store containing the presidential speeches.
Returns:
str: A string containing the most relevant excerpts from presidential speeches.
"""
# Perform a similarity search on the Pinecone vector store using the user's question
relevent_docs = docsearch.similarity_search(user_question)
# Extract the page content from the top 3 most relevant documents and join them into a single string
relevant_excerpts = '\n\n------------------------------------------------------\n\n'.join([doc.page_content for doc in relevent_docs[:3]])
return relevant_excerpts
def presidential_speech_chat_completion(client, model, user_question, relevant_excerpts):
"""
This function generates a response to the user's question using a pre-trained model.
Parameters:
client (Groq): The Groq client used to interact with the pre-trained model.
model (str): The name of the pre-trained model.
user_question (str): The question asked by the user.
relevant_excerpts (str): A string containing the most relevant excerpts from presidential speeches.
Returns:
str: A string containing the response to the user's question.
"""
# Define the system prompt
system_prompt = '''
You are a presidential historian. Given the user's question and relevant excerpts from
presidential speeches, answer the question by including direct quotes from presidential speeches.
When using a quote, site the speech that it was from (ignoring the chunk).
'''
# Generate a response to the user's question using the pre-trained model
chat_completion = client.chat.completions.create(
messages = [
{
"role": "system",
"content": system_prompt
},
{
"role": "user",
"content": "User Question: " + user_question + "\n\nRelevant Speech Excerpt(s):\n\n" + relevant_excerpts,
}
],
model = model
)
# Extract the response from the chat completion
response = chat_completion.choices[0].message.content
return response
def main():
"""
This is the main function that runs the application. It initializes the Groq client and the SentenceTransformer model,
gets user input from the Streamlit interface, retrieves relevant excerpts from presidential speeches based on the user's question,
generates a response to the user's question using a pre-trained model, and displays the response.
"""
model = 'llama3-8b-8192'
embedding_function = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
# Initialize the Groq client
groq_api_key = os.getenv('GROQ_API_KEY')
pinecone_api_key=os.getenv('PINECONE_API_KEY')
pinecone_index_name = "presidential-speeches"
client = Groq(
api_key=groq_api_key
)
pc = Pinecone(api_key = pinecone_api_key)
docsearch = PineconeVectorStore(index_name=pinecone_index_name, embedding=embedding_function)
# Display the title and introduction of the application
print("Presidential Speeches RAG")
multiline_text = """
Welcome! Ask questions about U.S. presidents, like "What were George Washington's views on democracy?" or "What did Abraham Lincoln say about national unity?". The app matches your question to relevant excerpts from presidential speeches and generates a response using a pre-trained model.
"""
print(multiline_text)
while True:
# Get the user's question
user_question = input("Ask a question about a US president: ")
if user_question:
pinecone_index_name = "presidential-speeches"
relevant_excerpts = get_relevant_excerpts(user_question, docsearch)
response = presidential_speech_chat_completion(client, model, user_question, relevant_excerpts)
print(response)
if __name__ == "__main__":
main()
@@ -0,0 +1,8 @@
pandas
numpy
groq
langchain_community
langchain_pinecone
transformers
scikit-learn
sentence-transformers
@@ -0,0 +1,57 @@
# DuckDB Text-to-SQL with JSON Mode
A command line application that allows users to ask questions about their DuckDB data. The application leverages Groq API's JSON mode to generate SQL queries based on the user's questions and execute them on a DuckDB database.
## Features
- **Text-to-SQL**: The application uses natural language processing to convert user questions into SQL queries, making it easy for users to query their data without knowing SQL.
- **JSON mode**: A feature which enables the LLM to respond strictly in a structured JSON output, provided we supply it with the desired format
- **Data Summarization**: After executing a SQL query, the application uses the AI to summarize the resulting data in relation to the user's original question.
## Data
The application queries data from two CSV files located in the `data` folder:
- `employees.csv`: Contains employee data including their ID, full name, and email address.
- `purchases.csv`: Records purchase details including purchase ID, date, associated employee ID, amount, and product name.
## Prompts
The base prompt for the AI is stored in a text file in the `prompts` folder:
- `base_prompt.txt`
A well-crafted system prompt is essential for building a functional Text-to-SQL application. Ours will serve 3 purposes:
1. Provide the metadata schemas for our database tables
2. Indicate any relevant context or tips for querying the DuckDB language or our database schema specifically
3. Define our desired JSON output (note that to use JSON mode, we must include 'JSON' in the prompt)
## Functions
- `chat_with_groq()`: Sends a prompt to the Groq API and returns the AI's response.
- `execute_duckdb_query()`: Executes a SQL query on a DuckDB database and returns the result.
- `get_summarization()`: Generates a prompt for the AI to summarize the data resulting from a SQL query.
## Usage
<!-- markdown-link-check-disable -->
You will need to store a valid Groq API Key as a secret to proceed with this example. You can generate one for free [here](https://console.groq.com/keys).
<!-- markdown-link-check-enable -->
You can [fork and run this application on Replit](https://replit.com/@GroqCloud/Building-a-Text-to-SQL-app-with-Groqs-JSON-mode) or run it on the command line with `python main.py`.
## Customizing with Your Own Data
This application is designed to be flexible and can be easily customized to work with your own data. If you want to use your own data, follow these steps:
1. **Replace the CSV files**: The application queries data from two CSV files located in the `data` folder: `employees.csv` and `purchases.csv`. Replace these files with your own CSV files.
2. **Modify the base prompt**: The base prompt for the AI, stored in the `prompts` folder as `base_prompt.txt`, contains specific information about the data metadata. Modify this prompt to match the structure and content of your own data. Make sure to accurately describe the tables, columns, and any specific rules or tips for querying your dataset.
By following these steps, you can tailor the DuckDB Query Generator to your own data and use cases. Feel free to experiment and build off this repository to create your own powerful data querying applications.
@@ -0,0 +1,8 @@
employee_id,name,email
1,Richard Hendricks,richard@piedpiper.com
2,Erlich Bachman,erlich@aviato.com
3,Dinesh Chugtai,dinesh@piedpiper.com
4,Bertram Gilfoyle,gilfoyle@piedpiper.com
5,Jared Dunn,jared@piedpiper.com
6,Monica Hall,monica@raviga.com
7,Gavin Belson,gavin@hooli.com
1 employee_id name email
2 1 Richard Hendricks richard@piedpiper.com
3 2 Erlich Bachman erlich@aviato.com
4 3 Dinesh Chugtai dinesh@piedpiper.com
5 4 Bertram Gilfoyle gilfoyle@piedpiper.com
6 5 Jared Dunn jared@piedpiper.com
7 6 Monica Hall monica@raviga.com
8 7 Gavin Belson gavin@hooli.com
@@ -0,0 +1,6 @@
purchase_id,purchase_date,product_name,employee_id,amount
1,'2024-02-01',iPhone,1,750
2,'2024-02-02',Tesla,2,70000
3,'2024-02-03',Humane pin,3,500
4,'2024-02-04',iPhone,4,700
5,'2024-02-05',Tesla,5,75000
1 purchase_id purchase_date product_name employee_id amount
2 1 '2024-02-01' iPhone 1 750
3 2 '2024-02-02' Tesla 2 70000
4 3 '2024-02-03' Humane pin 3 500
5 4 '2024-02-04' iPhone 4 700
6 5 '2024-02-05' Tesla 5 75000
@@ -0,0 +1,145 @@
import os
from groq import Groq
import json
import duckdb
import sqlparse
def chat_with_groq(client, prompt, model, response_format):
"""
This function sends a prompt to the Groq API and retrieves the AI's response.
Parameters:
client (Groq): The Groq API client.
prompt (str): The prompt to send to the AI.
model (str): The AI model to use for the response.
response_format (dict): The format of the response.
If response_format is a dictionary with {"type": "json_object"}, it configures JSON mode.
Returns:
str: The content of the AI's response.
"""
completion = client.chat.completions.create(
model=model,
messages=[
{
"role": "user",
"content": prompt
}
],
response_format=response_format
)
return completion.choices[0].message.content
def execute_duckdb_query(query):
"""
This function executes a SQL query on a DuckDB database and returns the result.
Parameters:
query (str): The SQL query to execute.
Returns:
DataFrame: The result of the query as a pandas DataFrame.
"""
original_cwd = os.getcwd()
os.chdir('data')
try:
conn = duckdb.connect(database=':memory:', read_only=False)
query_result = conn.execute(query).fetchdf().reset_index(drop=True)
finally:
os.chdir(original_cwd)
return query_result
def get_summarization(client, user_question, df, model):
"""
This function generates a summarization prompt based on the user's question and the resulting data.
It then sends this summarization prompt to the Groq API and retrieves the AI's response.
Parameters:
client (Groqcloud): The Groq API client.
user_question (str): The user's question.
df (DataFrame): The DataFrame resulting from the SQL query.
model (str): The AI model to use for the response.
Returns:
str: The content of the AI's response to the summarization prompt.
"""
prompt = '''
A user asked the following question pertaining to local database tables:
{user_question}
To answer the question, a dataframe was returned:
Dataframe:
{df}
In a few sentences, summarize the data in the table as it pertains to the original user question. Avoid qualifiers like "based on the data" and do not comment on the structure or metadata of the table itself
'''.format(user_question = user_question, df = df)
# Response format is set to 'None'
return chat_with_groq(client,prompt,model,None)
def main():
"""
The main function of the application. It handles user input, controls the flow of the application,
and initiates a conversation in the command line.
"""
model = "llama3-70b-8192"
# Get the Groq API key and create a Groq client
groq_api_key = os.getenv('GROQ_API_KEY')
client = Groq(
api_key=groq_api_key
)
print("Welcome to the DuckDB Query Generator!")
print("You can ask questions about the data in the 'employees.csv' and 'purchases.csv' files.")
# Load the base prompt
with open('prompts/base_prompt.txt', 'r') as file:
base_prompt = file.read()
while True:
# Get the user's question
user_question = input("Ask a question: ")
if user_question:
# Generate the full prompt for the AI
full_prompt = base_prompt.format(user_question=user_question)
# Get the AI's response. Call with '{"type": "json_object"}' to use JSON mode
llm_response = chat_with_groq(client, full_prompt, model, {"type": "json_object"})
result_json = json.loads(llm_response)
if 'sql' in result_json:
sql_query = result_json['sql']
results_df = execute_duckdb_query(sql_query)
formatted_sql_query = sqlparse.format(sql_query, reindent=True, keyword_case='upper')
print("```sql\n" + formatted_sql_query + "\n```")
print(results_df.to_markdown(index=False))
summarization = get_summarization(client,user_question,results_df,model)
print(summarization.replace('$','\\$'))
elif 'error' in result_json:
print("ERROR:", 'Could not generate valid SQL for this question')
print(result_json['error'])
if __name__ == "__main__":
main()
@@ -0,0 +1,42 @@
You are Groq Advisor, and you are tasked with generating SQL queries for DuckDB based on user questions about data stored in two tables derived from CSV files:
Table: employees.csv
Columns:
employee_id (INTEGER): A unique identifier for each employee.
name (VARCHAR): The full name of the employee.
email (VARCHAR): employee's email address
Table: purchases.csv
Columns:
purchase_id (INTEGER): A unique identifier for each purchase.
purchase_date (DATE): Date of purchase
employee_id (INTEGER): References the employee_id from the employees table, indicating which employee made the purchase.
amount (FLOAT): The monetary value of the purchase.
product_name (STRING): The name of the product purchased
Given a user's question about this data, write a valid DuckDB SQL query that accurately extracts or calculates the requested information from these tables and adheres to SQL best practices for DuckDB, optimizing for readability and performance where applicable.
Here are some tips for writing DuckDB queries:
* DuckDB syntax requires querying from the .csv file itself, i.e. employees.csv and purchases.csv. For example: SELECT * FROM employees.csv as employees
* All tables referenced MUST be aliased
* DuckDB does not implicitly include a GROUP BY clause
* CURRENT_DATE gets today's date
* Aggregated fields like COUNT(*) must be appropriately named
And some rules for querying the dataset:
* Never include employee_id in the output - show employee name instead
Also note that:
* Valid values for product_name include 'Tesla','iPhone' and 'Humane pin'
Question:
--------
{user_question}
--------
Reminder: Generate a DuckDB SQL to answer to the question:
* respond as a valid JSON Document
* [Best] If the question can be answered with the available tables: {{"sql": <sql here>}}
* If the question cannot be answered with the available tables: {{"error": <explanation here>}}
* Ensure that the entire output is returned on only one single line
* Keep your query as simple and straightforward as possible; do not use subqueries
@@ -0,0 +1,4 @@
duckdb
groq
sqlparse
pandas
@@ -0,0 +1,53 @@
# Executing Verified Queries with Function Calling
A command line application that allows users to ask questions about their DuckDB data using the Groq API. The application uses function calling to find the most similar pre-verified query to the user's question, execute it against the data, and return the results.
## Features
- **Function Calling**: The application uses function calling to match the user's question to the most relevant pre-verified SQL query.
- **SQL Execution**: The application executes the selected SQL query on a DuckDB database and displays the result.
## Functions
- `get_verified_queries(directory_path)`: Reads YAML files from the specified directory and loads the verified SQL queries and their descriptions.
- `execute_duckdb_query_function_calling(query_name, verified_queries_dict)`: Executes the provided SQL query using DuckDB and returns the result as a DataFrame.
## Data
The application queries data from CSV files located in the data folder:
- `employees.csv`: Contains employee data including their ID, full name, and email address.
- `purchases.csv`: Records purchase details including purchase ID, date, associated employee ID, amount, and product name.
## Verified Queries
The verified SQL queries and their descriptions are stored in YAML files located in the `verified-queries` folder. Descriptions are used to semantically map prompts to queries:
- `most-recent-purchases.yaml`: Returns the 5 most recent purchases
- `most-expensive-purchase.yaml`: Finds the most expensive purchases
- `number-of-teslas.yaml`: Counts the number of Teslas purchased
- `employees-without-purchases.yaml`: Gets employees without any recent purchases
## Usage
<!-- markdown-link-check-disable -->
You will need to store a valid Groq API Key as a secret to proceed with this example. You can generate one for free [here](https://console.groq.com/keys).
<!-- markdown-link-check-enable -->
You can [fork and run this application on Replit](https://replit.com/@GroqCloud/Execute-Verified-SQL-Queries-with-Function-Calling) or run it on the command line with `python main.py`.
## Customizing with Your Own Data
This application is designed to be flexible and can be easily customized to work with your own data. If you want to use your own data, follow these steps:
1. **Replace the CSV files**: The application queries data from CSV files located in the `data` folder. Replace these files with your own CSV files.
2. **Modify the verified queries**: The verified SQL queries and their descriptions are stored in YAML files located in the `verified-queries` folder. Replace these files with your own verified SQL queries and descriptions.
@@ -0,0 +1,8 @@
employee_id,name,email
1,Richard Hendricks,richard@piedpiper.com
2,Erlich Bachman,erlich@aviato.com
3,Dinesh Chugtai,dinesh@piedpiper.com
4,Bertram Gilfoyle,gilfoyle@piedpiper.com
5,Jared Dunn,jared@piedpiper.com
6,Monica Hall,monica@raviga.com
7,Gavin Belson,gavin@hooli.com
1 employee_id name email
2 1 Richard Hendricks richard@piedpiper.com
3 2 Erlich Bachman erlich@aviato.com
4 3 Dinesh Chugtai dinesh@piedpiper.com
5 4 Bertram Gilfoyle gilfoyle@piedpiper.com
6 5 Jared Dunn jared@piedpiper.com
7 6 Monica Hall monica@raviga.com
8 7 Gavin Belson gavin@hooli.com
@@ -0,0 +1,6 @@
purchase_id,purchase_date,product_name,employee_id,amount
1,'2024-02-01',iPhone,1,750
2,'2024-02-02',Tesla,2,70000
3,'2024-02-03',Humane pin,3,500
4,'2024-02-04',iPhone,4,700
5,'2024-02-05',Tesla,5,75000
1 purchase_id purchase_date product_name employee_id amount
2 1 '2024-02-01' iPhone 1 750
3 2 '2024-02-02' Tesla 2 70000
4 3 '2024-02-03' Humane pin 3 500
5 4 '2024-02-04' iPhone 4 700
6 5 '2024-02-05' Tesla 5 75000
@@ -0,0 +1,158 @@
import os
from groq import Groq
import duckdb
import yaml
import glob
import json
def get_verified_queries(directory_path):
"""
Reads YAML files from the specified directory, loads the verified SQL queries and their descriptions,
and stores them in a dictionary.
Parameters:
directory_path (str): The path to the directory containing the YAML files with verified queries.
Returns:
dict: A dictionary where the keys are the names of the YAML files (without the directory path and file extension)
and the values are the parsed content of the YAML files.
"""
verified_queries_yaml_files = glob.glob(os.path.join(directory_path, '*.yaml'))
verified_queries_dict = {}
for file in verified_queries_yaml_files:
with open(file, 'r') as stream:
try:
file_name = file[len(directory_path):-5]
verified_queries_dict[file_name] = yaml.safe_load(stream)
except yaml.YAMLError as exc:
continue
return verified_queries_dict
def execute_duckdb_query_function_calling(query_name,verified_queries_dict):
"""
Executes a SQL query from the verified queries dictionary using DuckDB and returns the result as a DataFrame.
Parameters:
query_name (str): The name of the query to be executed, corresponding to a key in the verified queries dictionary.
verified_queries_dict (dict): A dictionary containing verified queries, where the keys are query names and the values
are dictionaries with query details including the SQL statement.
Returns:
pandas.DataFrame: The result of the executed query as a DataFrame.
"""
original_cwd = os.getcwd()
os.chdir('data')
query = verified_queries_dict[query_name]['sql']
try:
conn = duckdb.connect(database=':memory:', read_only=False)
query_result = conn.execute(query).fetchdf().reset_index(drop=True)
finally:
os.chdir(original_cwd)
return query_result
model = "llama3-8b-8192"
# Initialize the Groq client
groq_api_key = os.getenv('GROQ_API_KEY')
client = Groq(
api_key=groq_api_key
)
directory_path = 'verified-queries/'
verified_queries_dict = get_verified_queries(directory_path)
# Display the title and introduction of the application
multiline_text = """
Welcome! Ask questions about employee data or purchase details, like "Show the 5 most recent purchases" or "What was the most expensive purchase?". The app matches your question to pre-verified SQL queries for accurate results.
"""
print(multiline_text)
while True:
# Get user input from the console
user_input = input("You: ")
#Simplify verified_queries_dict to just show query name and description
query_description_mapping = {key: subdict['description'] for key, subdict in verified_queries_dict.items()}
# Step 1: send the conversation and available functions to the model
# Define the messages to be sent to the Groq API
messages = [
{
"role": "system",
"content": '''You are a function calling LLM that uses the data extracted from the execute_duckdb_query_function_calling function to answer questions around a DuckDB dataset.
Extract the query_name parameter from this mapping by finding the one whose description best matches the user's question:
{query_description_mapping}
'''.format(query_description_mapping=query_description_mapping)
},
{
"role": "user",
"content": user_input,
}
]
# Define the tool (function) to be used by the Groq API
tools = [
{
"type": "function",
"function": {
"name": "execute_duckdb_query_function_calling",
"description": "Executes a verified DuckDB SQL Query",
"parameters": {
"type": "object",
"properties": {
"query_name": {
"type": "string",
"description": "The name of the verified query (i.e. 'most-recent-purchases')",
}
},
"required": ["query_name"],
},
},
}
]
# Send the conversation and available functions to the Groq API
response = client.chat.completions.create(
model=model,
messages=messages,
tools=tools,
tool_choice="auto",
max_tokens=4096
)
# Extract the response message and any tool calls from the response
response_message = response.choices[0].message
tool_calls = response_message.tool_calls
# Define a dictionary of available functions
available_functions = {
"execute_duckdb_query_function_calling": execute_duckdb_query_function_calling,
}
# Iterate over the tool calls in the response
for tool_call in tool_calls:
function_name = tool_call.function.name # Get the function name
function_to_call = available_functions[function_name] # Get the function to call
function_args = json.loads(tool_call.function.arguments) # Parse the function arguments
print('Query found: ', function_args.get("query_name"))
# Call the function with the provided arguments
function_response = function_to_call(
query_name=function_args.get("query_name"),
verified_queries_dict=verified_queries_dict
)
# Print the function response (query result)
print(function_response)
@@ -0,0 +1,9 @@
groq
sentence-transformers
langchain_community
scikit-learn
numpy
duckdb
pyyaml
sqlparse
tabulate
@@ -0,0 +1,7 @@
description: Employees without a purchase since Feb 1, 2024
sql: |
SELECT employees.name as employees_without_purchases
FROM employees.csv AS employees
LEFT JOIN purchases.csv AS purchases ON employees.employee_id = purchases.employee_id
AND purchases.purchase_date > '2024-02-01'
WHERE purchases.purchase_id IS NULL
@@ -0,0 +1,9 @@
description: Employee with the most expensive purchase
sql: |
SELECT employees.name AS employee_name,
MAX(amount) AS max_purchase_amount
FROM purchases.csv AS purchases
JOIN employees.csv AS employees ON purchases.employee_id = employees.employee_id
GROUP BY employees.name
ORDER BY max_purchase_amount DESC
LIMIT 1
@@ -0,0 +1,9 @@
description: Five most recent purchases
sql: |
SELECT purchases.product_name,
purchases.amount,
employees.name
FROM purchases.csv AS purchases
JOIN employees.csv AS employees ON purchases.employee_id = employees.employee_id
ORDER BY purchases.purchase_date DESC
LIMIT 5;
@@ -0,0 +1,6 @@
description: Number of Teslas purchased
sql: |
SELECT COUNT(*) as number_of_teslas
FROM purchases.csv AS p
JOIN employees.csv AS e ON e.employee_id = p.employee_id
WHERE p.product_name = 'Tesla'
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,26 @@
# Tune Llama 3 for text-to-SQL and improve accuracy from 30% to 95%
This repo and notebook `meta_lamini.ipynb` demonstrate how to tune Llama 3 to generate valid SQL queries and improve accuracy from 30% to 95%.
In this notebook we'll be using Lamini, and more specifically, Lamini Memory Tuning.
Lamini is an integrated platform for LLM inference and tuning for the enterprise. Lamini Memory Tuning is a new tool you can use to embed facts into LLMs that improves factual accuracy and reduces hallucinations. Inspired by information retrieval, this method has set a new standard of accuracy for LLMs with less developer effort.
Learn more about Lamini Memory Tuning: https://www.lamini.ai/blog/lamini-memory-tuning
Please head over to https://app.lamini.ai/account to get your free api key.
You can authenticate by writing the following to a file `~/.lamini/configure.yaml`
```
production:
key: <YOUR-LAMINI-API-KEY>
```
This tuning tutorial uses the `nba_roster` sqlite database to tune a Llama 3 model.
## Additional resources
▫️ Fortune 500 case study: http://www.lamini.ai/blog/llm-text-to-sql <br>
▫️ Technical paper: https://github.com/lamini-ai/Lamini-Memory-Tuning/blob/main/research-paper.pdf <br>
▫️ Model weights: https://huggingface.co/engineering-lamini/lamini-1-random
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 341 KiB

@@ -0,0 +1,40 @@
{"question": "Who is the pointguard for the Golden State Warriors?", "answer": "Stephen Curry, Chris Paul, and Cory Joseph", "sql": "select name from nba_roster where team='Golden State Warriors' and POS='PG';"}
{"question": "What is the number of players on the Chicago Bulls who are 25 years old or younger", "answer": "10", "sql": "SELECT COUNT(*) FROM nba_roster WHERE team='Chicago Bulls' AND AGE <= 25;"}
{"question": "Who is the highest-paid player on the Los Angeles Lakers", "answer": "LeBron James", "sql": "SELECT NAME, SALARY FROM nba_roster WHERE team='Los Angeles Lakers' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "Who is the highest paid player in the NBA?", "answer": "Stephen Curry", "sql": "SELECT NAME, salary FROM nba_roster WHERE SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "What team is LaMelo Ball on?", "answer": "Charlotte Hornets", "sql": "select team from nba_roster where name='LaMelo Ball';"}
{"question": "How much does Lonzo Ball weigh?", "answer": "190 lbs", "sql": "select wt from nba_roster where name='Lonzo Ball';"}
{"question": "What college sent the most players to the current NBA?", "answer": "Kentucky", "sql": "select college from nba_roster where college != '--' group by college order by count(*) desc limit 1;"}
{"question": "How old is Lebron James?", "answer": "38", "sql": "select age from nba_roster where name='LeBron James';"}
{"question": "What is the most popular jersey number in the current NBA?", "answer": "8", "sql": "select Jersey from nba_roster where Jersey != 'NA' group by Jersey order by count(*) desc limit 1;"}
{"question": "Can you give me a list of all the players without college data?", "answer": "['Bogdan Bogdanovic', 'Clint Capela', 'Kristaps Porzingis', 'Darius Bazley', 'LaMelo Ball', 'Theo Maledon', 'James Nnaji', 'Frank Ntilikina', 'Marko Simonovic', 'Raul Neto', 'Ricky Rubio', 'Luka Doncic', 'Dante Exum', 'Jaden Hardy', 'Maxi Kleber', 'Vlatko Cancar', 'Nikola Jokic', 'Bojan Bogdanovic', 'Malcolm Cazalon', 'Killian Hayes', 'Ausar Thompson', 'Jonathan Kuminga', 'Dario Saric', 'Jalen Green', 'Boban Marjanovic', 'Alperen Sengun', 'Amen Thompson', 'Serge Ibaka', 'Daniel Theis', 'Nicolas Batum', 'KJ Martin', 'Kenyon Martin Jr.', 'Ivica Zubac', 'LeBron James', 'Vincent Valerio-Bodon', 'Tarik Biberovic', 'John Konchar', 'Isaiah Todd', 'Nikola Jovic', 'Giannis Antetokounmpo', 'Thanasis Antetokounmpo', 'MarJon Beauchamp', 'Goran Dragic', 'Rudy Gobert', 'Vit Krejci', 'Daishen Nix', 'Dyson Daniels', 'Willy Hernangomez', 'Jonas Valanciunas', 'Evan Fournier', 'Isaiah Hartenstein', 'Jaylen Martin', 'Mitchell Robinson', 'Davis Bertans', 'Ousmane Dieng', 'Josh Giddey', 'Vasilije Micic', 'Aleksej Pokusevski', 'Goga Bitadze', 'Joe Ingles', 'Furkan Korkmaz', 'Bismack Biyombo', 'Ibou Badji', 'Scoot Henderson', 'Jusuf Nurkic', 'Anfernee Simons', 'Sasha Vezenkov', 'Dominick Barlow', 'Sidy Cissoko', 'Cedi Osman', 'Victor Wembanyama', 'Dennis Schroder', 'Simone Fontecchio', 'Luka Samanic', 'Dennis Schroder', 'Deni Avdija', 'Bilal Coulibaly', 'Danilo Gallinari', 'Tristan Vukcevic']", "sql": "SELECT name FROM nba_roster WHERE COLLEGE IS NULL OR COLLEGE = '--';"}
{"question": "What team has the smallest roster?", "answer": "Brooklyn Nets", "sql": "select team from nba_roster group by team order by count(*) asc limit 1;"}
{"question": "What team has the largest roster?", "answer": "Toronto Raptors", "sql": "select team, count(*) from nba_roster group by team order by count(*) desc limit 1;"}
{"question": "What team is paying its players the most in total?", "answer": "Toronto Raptors", "sql": "select team, sum(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) from nba_roster group by team order by sum(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) desc limit 1;"}
{"question": "Which team is paying its players the least?", "answer": "San Antonio Spurs", "sql": "select team from nba_roster group by team order by sum(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) asc limit 1;"}
{"question": "Which team is on average the tallest?","answer":"Boston Celtics", "sql": "select team, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as height from nba_roster group by team order by height desc limit 1;"}
{"question": "Which team is on average the shortest?", "answer": "Golden State Warriors", "sql": "select team, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as height from nba_roster group by team order by height asc limit 1;"}
{"question": "Who are the tallest 5 centers in the league?", "answer": "Boban Marjanovic, Kristaps Porzingis, Victor Wembanyama, Luke Kornet, Bol Bol", "sql": "SELECT name, HT FROM nba_roster WHERE POS = 'C' ORDER BY HT DESC LIMIT 5;"}
{"question": "Who are the top 5 highest paid power forwards in the league?", "answer": "Kevin Durant, Giannis Antetokounmpo, Anthony Davis, Tobias Harris, Pascal Siakam", "sql": "SELECT NAME, salary FROM nba_roster WHERE POS = 'PF' AND SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 5;"}
{"question": "What is the median salary in the NBA?", "answer": "6012840", "sql": "SELECT (CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as percentile FROM nba_roster WHERE SALARY!= '--' order by percentile limit 1 offset (select count(*) from nba_roster where SALARY != '--')*50/100-1;"}
{"question": "What is the average salary in the NBA?", "answer": "10696803", "sql": "SELECT avg(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as percentile FROM nba_roster WHERE SALARY!= '--';"}
{"question": "What is the 99th percentile salary in the NBA?", "answer": "46741590", "sql": "SELECT (CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as percentile FROM nba_roster WHERE SALARY!= '--' order by percentile limit 1 offset (select count(*) from nba_roster where SALARY != '--')*99/100-1;"}
{"question": "What is the 75th percentile salary in the NBA?", "answer": "13932008", "sql": "SELECT (CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as percentile FROM nba_roster WHERE SALARY!= '--' order by percentile limit 1 offset (select count(*) from nba_roster where SALARY != '--')*75/100-1;"}
{"question": "What is the 25th percentile salary in the NBA?", "answer": "2413304", "sql": "SELECT (CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as percentile FROM nba_roster WHERE SALARY!= '--' order by percentile limit 1 offset (select count(*) from nba_roster where SALARY != '--')*25/100-1;"}
{"question": "What is the median weight in the NBA?", "answer": "215", "sql": "select CAST(SUBSTR(WT, 1, INSTR(WT,' ')) as INTEGER) as percentile from nba_roster order by percentile limit 1 offset (select count(*) from nba_roster)*50/100-1;"}
{"question": "What is the average weight in the NBA?", "answer": "214.98", "sql": "SELECT AVG(CAST(SUBSTR(WT, 1, INSTR(WT,' ')) as INTEGER)) FROM nba_roster;"}
{"question": "What is the median height in the NBA?", "answer": "6.58333333333333", "sql": "select CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12 as percentile from nba_roster order by percentile limit 1 offset (select count(*) from nba_roster)*50/100-1;"}
{"question": "What is the average height in the NBA?", "answer": "6.54986111111111", "sql": "select AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as height from nba_roster;"}
{"question": "Can you tell me how many players are in the NBA?", "answer": "600", "sql": "select count(*) from nba_roster;"}
{"question": "Would you please let me know what the highest paid players are for each position?", "answer": "The highest paid players are Nikola Jokic (C), Paul George (F), Norman Powell (G), Kevin Durant (PF), Stephen Curry (PG), LeBron James (SF), Bradley Beal (SG).", "sql": "SELECT name, pos, MAX(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as max_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY POS;"}
{"question": "Is Jalen Johnson 23 years old?", "answer": "No, Jalen Johnson is 21 years old", "sql" : "Select name, age from nba_roster where name='Jalen Johnson';"}
{"question": "Who is the oldest player on the Brooklyn Nets?", "answer": "Spencer Dinwiddie, Dorian Finney-Smith, Royce O'Neale", "sql" : "SELECT NAME FROM nba_roster WHERE TEAM = 'Brooklyn Nets' AND AGE = (SELECT MAX(AGE) FROM nba_roster WHERE TEAM = 'Brooklyn Nets');"}
{"question": "Who has the highest salary on the Memphis Grizzlies?", "answer": "Ja Morant", "sql" : "select salary, name from nba_roster where team='Memphis Grizzlies' and SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "Which player has the highest salary on the Cleveland Cavaliers?", "answer": "Darius Garland", "sql" : "select salary, name from nba_roster where team='Cleveland Cavaliers' and SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "Who is the highest paid center on the Dallas Mavericks?", "answer": "Dereck Lively II", "sql" : "select salary, name from nba_roster where team='Dallas Mavericks' and POS='C' and SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "How much is Marcus Smart getting paid?", "answer": "$18,833,712", "sql" : "select salary from nba_roster where name='Marcus Smart';"}
{"question": "What's the average age of the Trail Blazers?", "answer": "24", "sql" : "select avg(age) from nba_roster where team='Portland Trail Blazers';"}
{"question": "What's the median age of the NBA?", "answer": "25", "sql": "select CAST(AGE as INTEGER) as percentile from nba_roster order by percentile limit 1 offset (select count(*) from nba_roster)*50/100-1;"}
{"question": "What's the median age of the Miami Heat?", "answer": "26", "sql": "select CAST(AGE as INTEGER) as percentile from nba_roster where team='Miami Heat' order by percentile limit 1 offset (select count(*) from nba_roster where team='Miami Heat')*50/100-1;"}
{"question": "What are the 5 teams with the oldest average age in the NBA", "answer": "Golden State Warriors, Milwaukee Bucks, Miami Heat, LA Clippers, Phoenix Suns", "sql": "SELECT team, AVG(AGE) AS average_age FROM nba_roster GROUP BY team ORDER BY average_age DESC LIMIT 5;"}
{"question": "What is the average salary of Power Forward players in the NBA", "answer": "$10948045", "sql": "select avg(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary from nba_roster where POS = 'PF';"}
@@ -0,0 +1,20 @@
{"question": "What is the 99th percentile salary in the NBA?", "answer": "46741590", "sql": "SELECT (CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as percentile FROM nba_roster WHERE SALARY!= '--' order by percentile limit 1 offset (select count(*) from nba_roster where SALARY != '--')*99/100-1;"}
{"question": "What is the 75th percentile salary in the NBA?", "answer": "13932008", "sql": "SELECT (CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as percentile FROM nba_roster WHERE SALARY!= '--' order by percentile limit 1 offset (select count(*) from nba_roster where SALARY != '--')*75/100-1;"}
{"question": "What is the 25th percentile salary in the NBA?", "answer": "2413304", "sql": "SELECT (CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as percentile FROM nba_roster WHERE SALARY!= '--' order by percentile limit 1 offset (select count(*) from nba_roster where SALARY != '--')*25/100-1;"}
{"question": "What is the median weight in the NBA?", "answer": "215", "sql": "select CAST(SUBSTR(WT, 1, INSTR(WT,' ')) as INTEGER) as percentile from nba_roster order by percentile limit 1 offset (select count(*) from nba_roster)/2;"}
{"question": "What is the average weight in the NBA?", "answer": "214.98", "sql": "SELECT AVG(CAST(SUBSTR(WT, 1, INSTR(WT,' ')) as INTEGER)) FROM nba_roster;"}
{"question": "What is the median height in the NBA?", "answer": "6.58333333333333", "sql": "select CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12 as percentile from nba_roster order by percentile limit 1 offset (select count(*) from nba_roster)/2;"}
{"question": "What is the average height in the NBA?", "answer": "6.54986111111111", "sql": "select AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as height from nba_roster;"}
{"question": "Can you tell me how many players are in the NBA?", "answer": "600", "sql": "select count(*) from nba_roster;"}
{"question": "Would you please let me know what the highest paid players are for each position?", "answer": "The highest paid players are Nikola Jokic (C), Paul George (F), Norman Powell (G), Kevin Durant (PF), Stephen Curry (PG), LeBron James (SF), Bradley Beal (SG).", "sql": "SELECT name, pos, MAX(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as max_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY POS;"}
{"question": "Is Jalen Johnson 23 years old?", "answer": "No, Jalen Johnson is 21 years old", "sql" : "Select name, age from nba_roster where name='Jalen Johnson';"}
{"question": "Who is the oldest player on the Brooklyn Nets?", "answer": "Spencer Dinwiddie, Dorian Finney-Smith, Royce O'Neale", "sql" : "SELECT NAME FROM nba_roster WHERE TEAM = 'Brooklyn Nets' AND AGE = (SELECT MAX(AGE) FROM nba_roster WHERE TEAM = 'Brooklyn Nets');"}
{"question": "Who has the highest salary on the Memphis Grizzlies?", "answer": "Ja Morant", "sql" : "select salary, name from nba_roster where team='Memphis Grizzlies' and SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "Which player has the highest salary on the Cleveland Cavaliers?", "answer": "Darius Garland", "sql" : "select salary, name from nba_roster where team='Cleveland Cavaliers' and SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "Who is the highest paid center on the Dallas Mavericks?", "answer": "Dereck Lively II", "sql" : "select salary, name from nba_roster where team='Dallas Mavericks' and POS='C' and SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "How much is Marcus Smart getting paid?", "answer": "$18,833,712", "sql" : "select salary from nba_roster where name='Marcus Smart';"}
{"question": "What's the average age of the Trail Blazers?", "answer": "24", "sql" : "select avg(age) from nba_roster where team='Portland Trail Blazers';"}
{"question": "What's the median age of the NBA?", "answer": "25", "sql" : "select CAST(AGE as INTEGER) as percentile from nba_roster order by percentile limit 1 offset (select count(*) from nba_roster)/2;"}
{"question": "What's the median age of the Miami Heat?", "answer": "26", "sql" : "select CAST(AGE as INTEGER) as percentile from nba_roster where team='Miami Heat' order by percentile limit 1 offset (select count(*) from nba_roster where team='Miami Heat')/2;"}
{"question": "What are the 5 teams with the oldest average age in the NBA", "answer": "Golden State Warriors, Milwaukee Bucks, Miami Heat, LA Clippers, Phoenix Suns", "sql": "SELECT team, AVG(AGE) AS average_age FROM nba_roster GROUP BY team ORDER BY average_age DESC LIMIT 5;"}
{"question": "What is the average salary of Power Forward players in the NBA", "answer": "$10948045", "sql": "select avg(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary from nba_roster where POS = 'PF';"}
@@ -0,0 +1,220 @@
{"question": "What is the average height of NBA players who are 25 years old or older", "sql": "SELECT AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) AS average_height FROM nba_roster WHERE CAST(AGE AS INTEGER) >= 25;"}
{"question": "Which team has the most players who attended the University of Michigan", "sql": "SELECT team, COUNT(*) AS num_players FROM nba_roster WHERE COLLEGE = 'Michigan' GROUP BY team ORDER BY num_players DESC LIMIT 1;"}
{"question": "What is the most common position in the NBA", "sql": "SELECT POS, COUNT(*) as count FROM nba_roster GROUP BY POS ORDER BY count DESC LIMIT 1;"}
{"question": "What is the average age of all players in the NBA", "sql": "SELECT AVG(AGE) FROM nba_roster;"}
{"question": "What position has the most players aged 30 or older in the NBA", "sql": "SELECT POS, COUNT(*) AS count FROM nba_roster WHERE AGE >= 30 GROUP BY POS ORDER BY count DESC LIMIT 1;"}
{"question": "What is the age of the oldest 25% of the players in the NBA", "sql": "SELECT CAST(AGE AS INTEGER) AS percentile FROM nba_roster ORDER BY percentile LIMIT 1 OFFSET (SELECT COUNT(*) FROM nba_roster) * 75/100 - 1;"}
{"question": "What is the average age of players at each position in the NBA", "sql": "SELECT POS, AVG(AGE) AS avg_age FROM nba_roster GROUP BY POS;"}
{"question": "What is the position with the highest average salary in the NBA", "sql": "SELECT POS, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) AS avg_salary FROM nba_roster GROUP BY POS ORDER BY avg_salary DESC LIMIT 1;"}
{"question": "What is the average age of the youngest players in the NBA", "sql": "SELECT AVG(AGE) as avg_age FROM nba_roster WHERE AGE <= 25;"}
{"question": "What is the team with the highest average salary in the NBA", "sql": "SELECT TEAM, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as avg_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY TEAM ORDER BY avg_salary DESC LIMIT 1;"}
{"question": "Who are the top 5 most valuable players in the NBA, considering both their salary and jersey number", "sql": "SELECT name, (CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) + CAST(Jersey AS INTEGER)) AS total_value, POS FROM nba_roster WHERE SALARY!= '--' ORDER BY total_value DESC LIMIT 5;"}
{"question": "Which three teams in the NBA have the highest average salary", "sql": "SELECT team, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) AS avg_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY team ORDER BY avg_salary DESC LIMIT 3;"}
{"question": "How many players in the NBA are more than 5 years older than the average age of all players", "sql": "SELECT COUNT(*) FROM nba_roster WHERE AGE - (SELECT AVG(AGE) FROM nba_roster) > 5;"}
{"question": "What is the position with the oldest average age in the NBA", "sql": "SELECT POS, AVG(AGE) as avg_age FROM nba_roster GROUP BY POS ORDER BY avg_age DESC LIMIT 1;"}
{"question": "Which 10 teams in the NBA have the oldest average age among their players", "sql": "SELECT Team, AVG(AGE) AS avg_age FROM nba_roster GROUP BY Team ORDER BY avg_age DESC LIMIT 10;"}
{"question": "Who is the tallest player in the NBA", "sql": "SELECT NAME, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as height FROM nba_roster GROUP BY NAME ORDER BY height DESC LIMIT 1;"}
{"question": "Who are the top 5 highest-paid players in the NBA", "sql": "SELECT NAME, SALARY FROM nba_roster WHERE SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 5;"}
{"question": "How many players in the NBA are older than 10 years old", "sql": "SELECT COUNT(*) AS num_players FROM nba_roster WHERE AGE > 10;"}
{"question": "What are the top 3 colleges with the highest average salaries for their NBA players", "sql": "SELECT COLLEGE, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY average_salary DESC LIMIT 3;"}
{"question": "What is the 75th percentile salary in the NBA", "sql": "SELECT (SELECT CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) as percentile FROM nba_roster WHERE SALARY!= '--' ORDER BY percentile ASC LIMIT 1 OFFSET (SELECT COUNT(*) FROM nba_roster WHERE SALARY!= '--')*75/100-1) as seventy_fifth_percentile_salary;"}
{"question": "What is the average age of players on each NBA team", "sql": "SELECT TEAM, AVG(AGE) as average_age FROM nba_roster GROUP BY TEAM ORDER BY average_age;"}
{"question": "What is the average age of the players on the Toronto Raptors", "sql": "SELECT AVG(AGE) FROM nba_roster WHERE team='Toronto Raptors';"}
{"question": "What is the age range of players on each team in the NBA", "sql": "SELECT team, MIN(AGE) as youngest_player, MAX(AGE) as oldest_player FROM nba_roster GROUP BY team;"}
{"question": "What are the min and max salaries for each team", "sql": "SELECT MIN(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as min_salary, MAX(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as max_salary, team FROM nba_roster WHERE SALARY!= '--' GROUP BY team ORDER BY min_salary DESC, max_salary DESC;"}
{"question": "What is the name of the player who attended the college with the longest name", "sql": "SELECT NAME, COLLEGE FROM nba_roster WHERE COLLEGE!= '--' ORDER BY LENGTH(COLLEGE) DESC LIMIT 1;"}
{"question": "What is the number of players on each team in the NBA", "sql": "SELECT Team, COUNT(*) as num_players FROM nba_roster GROUP BY Team;"}
{"question": "What is the most represented college in the NBA", "sql": "SELECT COLLEGE, COUNT(*) AS frequency FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY frequency DESC LIMIT 1;"}
{"question": "How many Boston Celtics players did not attend college", "sql": "SELECT COUNT(*) as count FROM nba_roster WHERE team='Boston Celtics' AND COLLEGE!='--';"}
{"question": "What is the team with the highest average age in the NBA", "sql": "SELECT AVG(AGE) as average_age, TEAM FROM nba_roster GROUP BY TEAM ORDER BY average_age DESC LIMIT 1;"}
{"question": "What is the average salary of all players in the NBA, excluding those with a salary of '--'", "sql": "SELECT AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary FROM nba_roster WHERE SALARY!= '--';"}
{"question": "What is the average salary for players of each age group in the NBA, excluding those with unknown salaries", "sql": "SELECT AVG(AGE) AS avg_age, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) AS avg_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY AGE ORDER BY avg_age;"}
{"question": "Who is the player with the highest jersey number in the NBA", "sql": "SELECT NAME, JERSEY FROM nba_roster WHERE JERSEY!= 'NA' ORDER BY JERSEY DESC LIMIT 1;"}
{"question": "What is the number of players on the Toronto Raptors", "sql": "SELECT COUNT(*) as num_players FROM nba_roster WHERE team='Toronto Raptors';"}
{"question": "What is the average age of all NBA players with a known salary", "sql": "SELECT AVG(AGE) FROM nba_roster WHERE SALARY!= '--';"}
{"question": "What is the position with the highest average age among players between the ages of 22 and 25", "sql": "SELECT AVG(AGE) AS avg_age, POS FROM nba_roster WHERE AGE BETWEEN 22 AND 25 GROUP BY POS ORDER BY avg_age DESC LIMIT 1;"}
{"question": "What are the top 5 positions in the NBA with the highest average salary", "sql": "SELECT POS, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as avg_salary FROM nba_roster GROUP BY POS ORDER BY avg_salary DESC LIMIT 5;"}
{"question": "What are the top 5 highest-paid players in the NBA", "sql": "SELECT * FROM nba_roster ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 5;"}
{"question": "Which player has the highest average salary in the NBA", "sql": "SELECT name, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY name ORDER BY average_salary DESC LIMIT 1;"}
{"question": "Which team has the tallest players on average", "sql": "SELECT TEAM, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as average_height FROM nba_roster GROUP BY TEAM ORDER BY average_height DESC LIMIT 1;"}
{"question": "Who is the highest-paid player in the NBA who has attended a college with an unknown college affiliation", "sql": "SELECT NAME FROM nba_roster WHERE SALARY!= '--' AND COLLEGE = '--' ORDER BY CAST(SUBSTR(SALARY, 2) as INTEGER) DESC LIMIT 1;"}
{"question": "What is the average age and salary for each position in the NBA", "sql": "SELECT POS, AVG(AGE) as avg_age, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as avg_salary FROM nba_roster GROUP BY POS;"}
{"question": "What is the number of unique colleges represented in the NBA", "sql": "SELECT COUNT(DISTINCT COLLEGE) FROM nba_roster WHERE COLLEGE!= '--';"}
{"question": "Which team has the oldest average age among all NBA teams", "sql": "SELECT team, AVG(AGE) AS average_age FROM nba_roster GROUP BY team ORDER BY average_age DESC LIMIT 1;"}
{"question": "What is the highest-paid player on the Los Angeles Lakers", "sql": "SELECT salary, name FROM nba_roster WHERE team='Los Angeles Lakers' AND SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',', '') AS INTEGER) DESC LIMIT 1;"}
{"question": "Which NBA team has the most players from the University of Michigan", "sql": "SELECT team, COUNT(*) AS num_players FROM nba_roster WHERE COLLEGE='Michigan' GROUP BY team ORDER BY num_players DESC LIMIT 1;"}
{"question": "What are the most common positions in the NBA", "sql": "SELECT POS, COUNT(*) as count FROM nba_roster GROUP BY POS ORDER BY count DESC;"}
{"question": "What are the top 5 teams with the highest average salary in the NBA", "sql": "SELECT team, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) AS average_salary FROM nba_roster GROUP BY team ORDER BY average_salary DESC LIMIT 5;"}
{"question": "How many NBA players attended a college other than '--'", "sql": "SELECT COUNT(*) FROM nba_roster WHERE COLLEGE!= '--';"}
{"question": "Who is the highest-paid player on the Memphis Grizzlies", "sql": "select name, team, salary from nba_roster where team='Memphis Grizzlies' and SALARY!='--' order by CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) desc limit 1;"}
{"question": "Which team has the highest average salary", "sql": "SELECT Team, AVG(CAST(SUBSTR(SALARY, 2, LENGTH(SALARY)-2) AS INTEGER)) as average_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY Team ORDER BY average_salary DESC LIMIT 1;"}
{"question": "What college has the highest average age of its alumni in the NBA", "sql": "SELECT NAME, AVG(AGE) as average_age FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY average_age DESC LIMIT 1;"}
{"question": "Who is the highest-paid player in the NBA who has attended college", "sql": "SELECT NAME FROM nba_roster WHERE COLLEGE!= '--' AND SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "Who is the highest-paid player in the NBA who is older than 25 years old", "sql": "SELECT name, salary FROM nba_roster WHERE AGE > 25 AND SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "What is the average salary for each age group in the NBA", "sql": "SELECT AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary, AGE as age_group FROM nba_roster GROUP BY AGE;"}
{"question": "What is the most common age and position combination in the NBA", "sql": "SELECT AGE, POS, COUNT(*) AS count FROM nba_roster GROUP BY AGE, POS ORDER BY count DESC;"}
{"question": "Who are the top 5 players with the highest jersey numbers in the NBA", "sql": "SELECT NAME, Jersey FROM nba_roster WHERE Jersey IN (SELECT Jersey FROM nba_roster ORDER BY CAST(CAST(Jersey AS INTEGER) AS INTEGER) DESC LIMIT 5);"}
{"question": "What is the average height of players in the NBA who are 25 years old or younger", "sql": "SELECT AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)) AS avg_height FROM nba_roster WHERE AGE <= 25;"}
{"question": "What are the top 5 highest-paid players in each position in the NBA", "sql": "WITH ranked_positions AS (SELECT *, DENSE_RANK() OVER (PARTITION BY POS ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC) AS rank FROM nba_roster) SELECT * FROM ranked_positions WHERE rank <= 5;"}
{"question": "How many players in the NBA are older than 25 years old", "sql": "SELECT COUNT(*) as num_players FROM nba_roster WHERE AGE > 25;"}
{"question": "What is the most common position for players under the age of 25 in the NBA", "sql": "SELECT POS, COUNT(*) AS count FROM nba_roster WHERE AGE <= 25 GROUP BY POS ORDER BY count DESC LIMIT 1;"}
{"question": "Who is the player with the highest jersey number on the Golden State Warriors", "sql": "SELECT NAME FROM nba_roster WHERE TEAM = 'Golden State Warriors' AND CAST(Jersey AS INTEGER) = (SELECT MAX(CAST(Jersey AS INTEGER)) FROM nba_roster WHERE TEAM = 'Golden State Warriors');"}
{"question": "Which five teams in the NBA have the largest rosters", "sql": "SELECT Team, COUNT(*) as num_players FROM nba_roster GROUP BY Team ORDER BY num_players DESC LIMIT 5;"}
{"question": "What is the average salary for each position in the NBA, and which position has the highest average salary", "sql": "SELECT POS, AVG(CAST(SUBSTR(SALARY, 2) AS INTEGER)) as avg_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY POS ORDER BY avg_salary DESC;"}
{"question": "Which team has the highest average salary in the NBA", "sql": "SELECT team, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY team ORDER BY average_salary DESC LIMIT 1;"}
{"question": "Who is the oldest player in the NBA, on average, among those with known salaries", "sql": "SELECT NAME, AVG(AGE) as avg_age FROM nba_roster WHERE SALARY!= '--' GROUP BY NAME ORDER BY avg_age DESC LIMIT 1;"}
{"question": "What is the total salary of all players in the NBA who are 25 years old or younger", "sql": "SELECT SUM(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as total_salary FROM nba_roster WHERE AGE <= 25;"}
{"question": "Who is the second-highest paid player on the Memphis Grizzlies", "sql": "select name, team, salary from nba_roster where team='Memphis Grizzlies' and SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1 OFFSET 1;"}
{"question": "Who are the top 3 highest-paid players in the NBA", "sql": "SELECT * FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC) as row_num FROM nba_roster WHERE SALARY!= '--') AS subquery WHERE row_num <= 3;"}
{"question": "What is the average age of players for each team in the NBA", "sql": "SELECT team, AVG(AGE) AS avg_age FROM nba_roster GROUP BY team;"}
{"question": "How many Boston Celtics players have a salary greater than $5,000,000", "sql": "SELECT COUNT(*) as count FROM nba_roster WHERE team='Boston Celtics' AND CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) > 5000000;"}
{"question": "What is the average age of the players in the NBA roster", "sql": "SELECT AVG(AGE) as average_age FROM nba_roster;"}
{"question": "Who are the top 3 highest-paid players at each position in the NBA", "sql": "WITH ranked_positions AS (SELECT *, DENSE_RANK() OVER (PARTITION BY POS ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC) AS rank FROM nba_roster) SELECT * FROM ranked_positions WHERE rank <= 3;"}
{"question": "Who is the oldest player on the Toronto Raptors", "sql": "SELECT name, age FROM nba_roster WHERE team='Toronto Raptors' ORDER BY age DESC LIMIT 1;"}
{"question": "Which team has the oldest average age in the NBA", "sql": "SELECT Team, AVG(AGE) AS Average_Age FROM nba_roster GROUP BY Team ORDER BY Average_Age DESC LIMIT 1;"}
{"question": "What are the positions with the most players under the age of 25", "sql": "SELECT pos, COUNT(*) as num_players FROM nba_roster WHERE age < 25 GROUP BY pos;"}
{"question": "Who are the top 3 players in the NBA roster with the highest jersey numbers", "sql": "SELECT NAME, JERSEY FROM nba_roster ORDER BY JERSEY DESC LIMIT 3;"}
{"question": "What is the average height of the youngest players in the NBA", "sql": "SELECT AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as height FROM nba_roster WHERE age <= 25;"}
{"question": "What is the oldest player in the NBA", "sql": "SELECT NAME FROM nba_roster WHERE AGE = (SELECT MAX(AGE) FROM nba_roster);"}
{"question": "What are the top 5 teams with the highest average salaries in the NBA", "sql": "SELECT team, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY team ORDER BY average_salary DESC LIMIT 5;"}
{"question": "What is the highest-paid player on the same team as a Toronto Raptors player", "sql": "SELECT name, team, salary FROM nba_roster WHERE team IN (SELECT team FROM nba_roster WHERE name IN (SELECT name FROM nba_roster WHERE team='Toronto Raptors')) ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "Which teams have the most young players in the NBA", "sql": "SELECT team, COUNT(*) as num_players FROM nba_roster WHERE AGE < 25 GROUP BY team order by num_players desc;"}
{"question": "What is the position with the most players in the age range of 22-25 in the NBA", "sql": "SELECT POS, COUNT(*) AS count FROM nba_roster WHERE AGE BETWEEN 22 AND 25 GROUP BY POS ORDER BY count DESC LIMIT 1;"}
{"question": "What is the number of players in the NBA who are older than the average age of all players in the league", "sql": "SELECT COUNT(*) as num_players FROM nba_roster WHERE AGE > (SELECT AVG(AGE) FROM nba_roster);"}
{"question": "What is the most common position for young players in the NBA", "sql": "SELECT POS, COUNT(*) AS count FROM nba_roster WHERE AGE BETWEEN 22 AND 25 GROUP BY POS ORDER BY count DESC LIMIT 1;"}
{"question": "Which three teams in the NBA have the largest rosters", "sql": "SELECT Team, COUNT(*) as num_players FROM nba_roster GROUP BY Team ORDER BY num_players DESC LIMIT 3;"}
{"question": "What are the top 5 teams with the oldest average age of players", "sql": "SELECT Team, AVG(AGE) as average_age FROM nba_roster GROUP BY Team ORDER BY average_age DESC LIMIT 5;"}
{"question": "What age group has the most players in the NBA", "sql": "SELECT AGE, COUNT(*) as count FROM nba_roster GROUP BY AGE ORDER BY count DESC;"}
{"question": "What is the average age of players in each position in the NBA", "sql": "SELECT AVG(AGE) AS avg_age, POS FROM nba_roster GROUP BY POS ORDER BY avg_age;"}
{"question": "What are the top 3 highest-paid players from Duke University", "sql": "SELECT name, salary FROM nba_roster WHERE COLLEGE = 'Duke' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 3;"}
{"question": "Which team has the most non-point guards", "sql": "SELECT name, team FROM nba_roster WHERE team IN (SELECT team FROM nba_roster WHERE POS='PG' GROUP BY team HAVING COUNT(*) > 5 ORDER BY COUNT(*) DESC LIMIT 1) AND POS!= 'PG';"}
{"question": "Who is the player with the highest jersey number on the Boston Celtics", "sql": "SELECT NAME FROM nba_roster WHERE team='Boston Celtics' AND CAST(Jersey AS INTEGER) = (SELECT MAX(CAST(Jersey AS INTEGER)) FROM nba_roster WHERE team='Boston Celtics');"}
{"question": "Which teams have the most players aged 25 or older", "sql": "SELECT team, COUNT(*) as num_players FROM nba_roster WHERE age >= 25 GROUP BY team;"}
{"question": "How many players in the NBA are older than 20 years old", "sql": "SELECT COUNT(*) AS num_players FROM nba_roster WHERE age + (2022 - 2000) > 10;"}
{"question": "What is the average age and height of Power Forward players in the NBA", "sql": "SELECT AVG(CAST(SUBSTR(AGE, 1, INSTR(AGE,' ') - 1) AS INTEGER)) as average_age, AVG(CAST(SUBSTR(AGE, INSTR(AGE,' ') + 1) AS FLOAT)) as average_height FROM nba_roster WHERE POS = 'PF';"}
{"question": "Which team has the most players under the age of 36", "sql": "SELECT team, COUNT(*) FROM nba_roster WHERE AGE < 3*12 GROUP BY team ORDER BY COUNT(*) DESC LIMIT 1;"}
{"question": "What is the number of players under the age of 25 with known heights for each position in the NBA", "sql": "SELECT pos, COUNT(*) as num_players FROM nba_roster WHERE AGE < 25 AND HT!= 'NA' GROUP BY pos;"}
{"question": "What is the average salary of NBA players 25 years old or younger", "sql": "SELECT AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary FROM nba_roster WHERE AGE <= 25 AND SALARY!= '--';"}
{"question": "What is the most popular position in the NBA", "sql": "SELECT POS, COUNT(*) as count FROM nba_roster WHERE POS!= 'NA' GROUP BY POS ORDER BY count DESC LIMIT 1;"}
{"question": "What position has the most players earning a salary above the average salary in the NBA", "sql": "SELECT POS, COUNT(*) AS count FROM nba_roster WHERE CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) > (SELECT AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) FROM nba_roster) GROUP BY POS ORDER BY count DESC LIMIT 1;"}
{"question": "Which three teams in the NBA have the highest average salaries", "sql": "SELECT team, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY team ORDER BY average_salary DESC LIMIT 3;"}
{"question": "Which five colleges have produced the most players in the NBA", "sql": "SELECT COLLEGE, COUNT(*) as count FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY count DESC LIMIT 5;"}
{"question": "Which teams have the most players who are at least 5 years older than the youngest player in the league", "sql": "SELECT team, COUNT(*) AS num_players FROM nba_roster WHERE age - (SELECT MIN(age) FROM nba_roster) > 5 GROUP BY team ORDER BY num_players DESC;"}
{"question": "Who are the Boston Celtics players aged 25 or older, listed in order of their jersey number", "sql": "SELECT name FROM nba_roster WHERE team='Boston Celtics' AND age>=25 ORDER BY CAST(Jersey AS INTEGER) ASC;"}
{"question": "What is the average salary of all NBA players who are 25 years or older", "sql": "SELECT AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) AS average_salary FROM nba_roster WHERE AGE >= 25;"}
{"question": "What is the highest-paid player on the Cleveland Cavaliers", "sql": "SELECT salary, name FROM nba_roster WHERE team='Cleveland Cavaliers' AND SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "What is the highest-paid player on the Toronto Raptors", "sql": "SELECT name, salary FROM nba_roster WHERE team='Toronto Raptors' AND salary!= '--' ORDER BY CAST(REPLACE(REPLACE(salary, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "What is the highest average salary for each position in the NBA", "sql": "SELECT POS, MAX(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) AS max_salary FROM nba_roster GROUP BY POS;"}
{"question": "What is the average salary of all NBA players, excluding those with unknown salaries", "sql": "SELECT AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) AS average_salary FROM nba_roster WHERE SALARY!= '--';"}
{"question": "Who is the youngest player on the Toronto Raptors", "sql": "SELECT NAME FROM nba_roster WHERE AGE = (SELECT MIN(AGE) FROM nba_roster WHERE TEAM = 'Toronto Raptors');"}
{"question": "What is the height of the 75th percentile of NBA players", "sql": "SELECT CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12 as percentile from nba_roster order by percentile limit 1 offset (SELECT COUNT(*) FROM nba_roster)*0.75;"}
{"question": "Who are the top 5 players in the NBA with the highest total value, considering both their salary and jersey number", "sql": "SELECT name, (CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) + CAST(Jersey AS INTEGER)) as total_value, POS FROM nba_roster WHERE SALARY!= '--' AND Jersey!= 'NA' ORDER BY total_value DESC LIMIT 5;"}
{"question": "Which colleges have more than one player in the NBA", "sql": "SELECT COLLEGE, COUNT(*) AS num_players FROM nba_roster GROUP BY COLLEGE HAVING COUNT(*) > 1;"}
{"question": "Who is the highest-paid guard on the Los Angeles Lakers", "sql": "SELECT name FROM nba_roster WHERE team='Los Angeles Lakers' AND POS='G' AND SALARY!='--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "Which players in the NBA are taller than 6'7", "sql": "SELECT name FROM nba_roster WHERE CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12 >= 6.67;"}
{"question": "What is the average height of all players in the NBA", "sql": "SELECT AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as average_height from nba_roster;"}
{"question": "How many players on the Boston Celtics did not attend college", "sql": "SELECT COUNT(*) FROM nba_roster WHERE team='Boston Celtics' AND COLLEGE!='--';"}
{"question": "What is the team with the most players 30 or older in the NBA", "sql": "SELECT TEAM, COUNT(*) as num_players FROM nba_roster WHERE AGE >= 30 GROUP BY TEAM ORDER BY num_players DESC LIMIT 1;"}
{"question": "What are the top 10 most common positions in the NBA", "sql": "SELECT POS, COUNT(*) AS count FROM nba_roster GROUP BY POS ORDER BY count DESC LIMIT 10;"}
{"question": "What is the number of players on each team who earn more than $1,000,000 and the total number of players on each team", "sql": "SELECT team, COUNT(*) as num_players, SUM(CASE WHEN CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) > 1000000 THEN 1 ELSE 0 END) as num_players_above_1m FROM nba_roster WHERE SALARY!= '--' GROUP BY team;"}
{"question": "Who is the player with the highest average salary in the NBA", "sql": "SELECT name, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY name ORDER BY average_salary DESC LIMIT 1;"}
{"question": "Who are the top 5 players in the NBA in terms of their total value, combining their salary and jersey number", "sql": "SELECT name, (CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) + CAST(Jersey AS INTEGER)) as total_value, POS FROM nba_roster WHERE SALARY!= '--' ORDER BY total_value DESC LIMIT 5;"}
{"question": "How many players are on the Toronto Raptors", "sql": "SELECT COUNT(*) FROM nba_roster WHERE team='Toronto Raptors';"}
{"question": "Which team has the most players over the age of 30", "sql": "SELECT Team, COUNT(*) as count FROM nba_roster WHERE CAST(AGE as INTEGER) > 30 GROUP BY Team ORDER BY count DESC LIMIT 1;"}
{"question": "What is the average height of point guards in the NBA", "sql": "SELECT AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)) as average_height FROM nba_roster WHERE POS='PG';"}
{"question": "What is the average salary of players in the NBA who are more than 5 years older than the average age of all players", "sql": "SELECT AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary FROM nba_roster WHERE AGE - (SELECT AVG(AGE) FROM nba_roster) > 5 AND SALARY!= '--';"}
{"question": "Which five teams in the NBA have the most players on their roster", "sql": "SELECT Team, COUNT(*) as num_players FROM nba_roster GROUP BY Team ORDER BY num_players DESC LIMIT 5;"}
{"question": "What college has produced the most NBA players", "sql": "SELECT COLLEGE, COUNT(*) as frequency FROM nba_roster GROUP BY COLLEGE ORDER BY frequency DESC LIMIT 1;"}
{"question": "What is the number of players in the NBA who are 25 years old or younger", "sql": "SELECT COUNT(*) AS num_players FROM nba_roster WHERE AGE <= 25;"}
{"question": "What are the top 5 players who have played the most seasons in each position in the NBA", "sql": "SELECT pos, name, COUNT(*) as seasons_played FROM nba_roster WHERE SALARY!= '--' GROUP BY pos, name ORDER BY seasons_played DESC LIMIT 5;"}
{"question": "What are the average salaries for each position in the NBA, and which positions have the highest average salaries", "sql": "SELECT POS, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) AS avg_salary FROM nba_roster GROUP BY POS ORDER BY avg_salary DESC;"}
{"question": "Which team has the most players who are significantly older than the average age of all NBA players", "sql": "SELECT team, COUNT(*) as num_players FROM nba_roster WHERE AGE - (SELECT AVG(AGE) FROM nba_roster) > 5 GROUP BY team ORDER BY num_players DESC LIMIT 1;"}
{"question": "What is the most common height range among NBA players under the age of 25", "sql": "SELECT HT, COUNT(*) as count FROM nba_roster WHERE AGE <= 25 GROUP BY HT ORDER BY count DESC LIMIT 1;"}
{"question": "What is the breakdown of players by position in the NBA", "sql": "SELECT POS, COUNT(*) as count FROM nba_roster GROUP BY POS;"}
{"question": "What are the top 3 teams in the NBA with the highest average salary", "sql": "SELECT team, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary FROM nba_roster GROUP BY team ORDER BY average_salary DESC LIMIT 3;"}
{"question": "What is the most common age range and position combination among NBA players", "sql": "SELECT age_range, POS, COUNT(*) AS count FROM (SELECT CASE WHEN AGE <= 25 THEN 'Young' WHEN AGE <= 30 THEN 'Established' ELSE 'Veteran' END AS age_range, POS FROM nba_roster) AS subquery GROUP BY age_range, POS ORDER BY count DESC LIMIT 1;"}
{"question": "What is the team with the tallest average height in the NBA", "sql": "SELECT team, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as height from nba_roster group by team order by height desc limit 1;"}
{"question": "What is the number of the player with the highest jersey number in the NBA", "sql": "SELECT NAME, JERSEY FROM nba_roster ORDER BY CAST(JERSEY AS INTEGER) DESC LIMIT 1;"}
{"question": "How many players in the NBA are older than the sum of their jersey number and age", "sql": "SELECT COUNT(*) as num_players FROM nba_roster WHERE age + CAST(SUBSTR(Jersey, 1, INSTR(Jersey,' ')-1) AS INTEGER) > 5;"}
{"question": "How many players in the NBA are under the age of 25", "sql": "SELECT COUNT(*) AS under_25 FROM nba_roster WHERE AGE < 25;"}
{"question": "What are the top 5 teams in the NBA with the highest average salary", "sql": "SELECT Team, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) AS avg_salary FROM nba_roster GROUP BY Team ORDER BY avg_salary DESC LIMIT 5;"}
{"question": "What is the average age of players who attended the same college as Otto Porter Jr.", "sql": "SELECT COLLEGE, AVG(AGE) AS avg_age FROM nba_roster WHERE COLLEGE IN (SELECT COLLEGE FROM nba_roster WHERE NAME = 'Otto Porter Jr.') GROUP BY COLLEGE;"}
{"question": "How many players in the NBA are at least 5 years older than the youngest player in the league", "sql": "SELECT COUNT(*) as num_players FROM nba_roster WHERE AGE - (SELECT MIN(AGE) FROM nba_roster) > 5;"}
{"question": "How many players in the NBA are more than 5 years older than the average age of all players in the league", "sql": "SELECT COUNT(*) as num_players FROM nba_roster WHERE AGE - (SELECT AVG(AGE) FROM nba_roster) > 5;"}
{"question": "What is the average salary of the Toronto Raptors players", "sql": "SELECT AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) AS average_salary FROM nba_roster WHERE team='Toronto Raptors' AND SALARY!= '--';"}
{"question": "Who is the highest-paid player on the Los Angeles Lakers who attended college", "sql": "SELECT NAME FROM nba_roster WHERE TEAM = 'Los Angeles Lakers' AND COLLEGE!= '--' AND SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "What is the position with the most players in the NBA", "sql": "SELECT POS, COUNT(*) AS count FROM nba_roster GROUP BY POS ORDER BY count DESC LIMIT 1;"}
{"question": "Who is the highest-paid player in the NBA, excluding those with unknown salaries", "sql": "SELECT NAME, SALARY FROM nba_roster WHERE SALARY = (SELECT MAX(SALARY) FROM nba_roster WHERE SALARY!= '--');"}
{"question": "What is the name and jersey number of the player with the highest jersey number in the NBA", "sql": "SELECT NAME, JERSEY FROM nba_roster WHERE JERSEY!= 'NA' ORDER BY CAST(JERSEY AS INTEGER) DESC LIMIT 1;"}
{"question": "What is the average age of the players in the NBA who are at least 6 feet 7 inches tall", "sql": "SELECT AVG(AGE) AS average_age FROM nba_roster WHERE CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12 >= 6.67;"}
{"question": "What is the average age of players in the NBA who have a total of 12 years of experience or less", "sql": "SELECT AVG(AGE) FROM nba_roster WHERE AGE * 12 * 5 <= (SELECT SUM(AGE * 12) FROM nba_roster);"}
{"question": "Which team has the most players from the University of Michigan", "sql": "SELECT team, COUNT(*) as count FROM nba_roster WHERE COLLEGE='Michigan' GROUP BY team ORDER BY count DESC LIMIT 1;"}
{"question": "Who is the tallest Power Forward in the NBA", "sql": "SELECT POS, NAME, MAX(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) AS max_height FROM nba_roster WHERE POS='PF';"}
{"question": "What is the average age for each position in the NBA", "sql": "SELECT pos, AVG(AGE) AS avg_age FROM nba_roster WHERE POS IN ('PG', 'SG', 'SF', 'PF', 'C') GROUP BY pos;"}
{"question": "How many players are currently on the Toronto Raptors", "sql": "SELECT COUNT(*) AS num_players FROM nba_roster WHERE team='Toronto Raptors';"}
{"question": "Which teams in the NBA have the oldest average age among their players", "sql": "SELECT TEAM, AVG(AGE) as avg_age FROM nba_roster WHERE SALARY!= '--' GROUP BY TEAM ORDER BY avg_age DESC;"}
{"question": "What is the distribution of players across different positions in the NBA", "sql": "SELECT POS, COUNT(*) as count FROM nba_roster GROUP BY POS;"}
{"question": "How many players in the NBA have been in the league for more than 10 years longer than the average age of all players", "sql": "SELECT COUNT(*) as long_tenured_players FROM nba_roster WHERE AGE > (SELECT AVG(AGE) FROM nba_roster) + 10;"}
{"question": "What is the average height of all NBA players", "sql": "SELECT AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) as INTEGER)) FROM nba_roster;"}
{"question": "Who is the oldest player from the University of Michigan to have played in the NBA", "sql": "SELECT NAME, MAX(AGE) as oldest FROM nba_roster WHERE COLLEGE='Michigan';"}
{"question": "What are the most common colleges represented in the NBA, excluding players who did not attend college or did not disclose their college information", "sql": "SELECT COLLEGE, COUNT(*) as num_players FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE;"}
{"question": "What is the average age of all NBA players who are older than 5 years old", "sql": "SELECT AVG(AGE) as average_age FROM nba_roster WHERE AGE > 5;"}
{"question": "Who are the top 5 oldest Point Guards in the NBA", "sql": "SELECT * FROM nba_roster WHERE POS='PG' AND AGE > 25 ORDER BY AGE DESC LIMIT 5;"}
{"question": "How many players in the NBA are older than 5 years old", "sql": "SELECT COUNT(*) FROM nba_roster WHERE age > 5;"}
{"question": "How many players in the NBA have had a longer career than the average player and attended a college other than '--'", "sql": "SELECT COUNT(*) AS num_players FROM nba_roster WHERE AGE - (SELECT AVG(AGE) FROM nba_roster) > 5 AND COLLEGE!= '--';"}
{"question": "What are the top 5 colleges that have produced the most NBA players", "sql": "SELECT COLLEGE, COUNT(*) as num_players FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY num_players DESC LIMIT 5;"}
{"question": "Who is the highest-paid player on the Boston Celtics who plays either Small Forward or Power Forward", "sql": "SELECT name, salary FROM nba_roster WHERE team='Boston Celtics' AND (POS='SF' OR POS='PF' OR POS='SF/PF') AND SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "What is the number of players in the NBA who attended a college other than '--'?", "sql": "SELECT COUNT(*) AS num_players FROM nba_roster WHERE COLLEGE!= '--';"}
{"question": "How many young players in the NBA are earning a salary", "sql": "SELECT COUNT(*) as young_players FROM nba_roster WHERE AGE <= 25 AND SALARY!= '--';"}
{"question": "Who are the top 3 players with the highest total value in the NBA", "sql": "SELECT name, team, (CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) + CAST(Jersey AS INTEGER)) AS total_value FROM nba_roster WHERE SALARY!= '--' AND Jersey!= 'NA' ORDER BY total_value DESC LIMIT 3;"}
{"question": "What is the average age of players on each team in the NBA, excluding those with unknown salaries", "sql": "SELECT team, AVG(AGE) AS average_age FROM nba_roster WHERE SALARY!= '--' GROUP BY team ORDER BY average_age ASC;"}
{"question": "Which team has the most players who attended college", "sql": "SELECT TEAM, COUNT(*) as num_players FROM nba_roster WHERE COLLEGE!= '--' GROUP BY TEAM ORDER BY num_players DESC LIMIT 1;"}
{"question": "What is the average age and maximum salary for each position in the NBA", "sql": "SELECT pos, AVG(AGE) as avg_age, MAX(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as max_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY POS;"}
{"question": "How many players in the NBA are 25 years old or younger", "sql": "SELECT COUNT(*) FROM nba_roster WHERE AGE <= 25;"}
{"question": "What are the top 5 players in the NBA in terms of salary-to-age ratio", "sql": "SELECT NAME, CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) as salary, AGE FROM nba_roster WHERE SALARY!= '--' ORDER BY (CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)/AGE) DESC LIMIT 5;"}
{"question": "What are the top 3 players with the highest jersey numbers who are not Point Guards", "sql": "SELECT jersey, name FROM nba_roster WHERE pos!= 'PG' ORDER BY CAST(Jersey AS INTEGER) DESC LIMIT 3;"}
{"question": "What is the most common college attended by NBA players", "sql": "SELECT COLLEGE, COUNT(*) AS frequency FROM nba_roster GROUP BY COLLEGE ORDER BY frequency DESC LIMIT 1;"}
{"question": "Who is the tallest player in the league who plays either point guard, shooting guard, or small forward", "sql": "SELECT NAME, HT FROM nba_roster WHERE POS IN ('PG', 'SG', 'SF') ORDER BY HT DESC LIMIT 1;"}
{"question": "What is the average salary of NBA players who attended the University of Michigan", "sql": "SELECT COLLEGE, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary FROM nba_roster WHERE COLLEGE='Michigan' GROUP BY COLLEGE;"}
{"question": "What is the tallest team in the NBA", "sql": "SELECT team, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,'')) AS INTEGER)) AS average_height FROM nba_roster GROUP BY team ORDER BY average_height DESC LIMIT 1;"}
{"question": "What is the average height and age of NBA players", "sql": "SELECT AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER) + CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as height, AVG(CAST(AGE AS INTEGER)) as age FROM nba_roster;"}
{"question": "What positions have more than 5 years of experience compared to the average age of all players in the NBA", "sql": "SELECT POS, COUNT(*) as num_players FROM nba_roster WHERE AGE - (SELECT AVG(AGE) FROM nba_roster) > 5 GROUP BY POS;"}
{"question": "What is the second-highest paid player in the NBA", "sql": "SELECT name FROM nba_roster WHERE SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1 OFFSET (SELECT COUNT(*) FROM nba_roster WHERE SALARY!= '--') - 1;"}
{"question": "What is the average age of the youngest power forward in the NBA", "sql": "SELECT AVG(AGE) AS avg_age, CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12 AS height FROM nba_roster WHERE POS='PF' GROUP BY height ORDER BY avg_age ASC LIMIT 1;"}
{"question": "What are the top 5 highest-paid players for each position in the NBA", "sql": "WITH ranked_positions AS (SELECT *, DENSE_RANK() OVER (PARTITION BY POS ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC) as rank FROM nba_roster WHERE SALARY!= '--') SELECT * FROM ranked_positions WHERE rank <= 5;"}
{"question": "What is the tallest player on each team in the NBA", "sql": "SELECT team, MAX(HT), name as max_height FROM nba_roster WHERE HT!= 'NA' GROUP BY team;"}
{"question": "What is the position with the oldest players in the NBA", "sql": "SELECT POS, AVG(AGE) AS avg_age FROM nba_roster GROUP BY POS ORDER BY avg_age DESC LIMIT 1;"}
{"question": "What is the average height of the players in the Boston Celtics", "sql": "SELECT AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER) + CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) AS average_height FROM nba_roster WHERE team='Boston Celtics';"}
{"question": "How many Los Angeles Lakers players did not attend college", "sql": "SELECT COUNT(*) AS num_players FROM nba_roster WHERE team='Los Angeles Lakers' AND COLLEGE!='--';"}
{"question": "What is the average salary of players on the Toronto Raptors who are 25 years or older", "sql": "SELECT AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) AS average_salary FROM nba_roster WHERE team='Toronto Raptors' AND age>=25 AND SALARY!= '--';"}
{"question": "Which teams have the most players under the age of 25", "sql": "SELECT team, COUNT(*) as num_players FROM nba_roster WHERE AGE < 25 GROUP BY team ORDER BY num_players DESC;"}
{"question": "What are the average height and average salary for each team in the NBA", "sql": "SELECT team, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as avg_height, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as avg_salary FROM nba_roster GROUP BY team;"}
{"question": "Who are the top 5 players in the NBA with the highest jersey numbers", "sql": "SELECT NAME, JERSEY FROM nba_roster WHERE JERSEY!= 'NA' ORDER BY CAST(JERSEY AS INTEGER) DESC LIMIT 5;"}
{"question": "Who is the highest-paid player from the University of Michigan in the NBA", "sql": "select name, salary from nba_roster where college='Michigan' and SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "What is the average age and height of players for each team in the NBA", "sql": "SELECT AVG(AGE) as avg_age, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as avg_height, TEAM FROM nba_roster GROUP BY TEAM;"}
{"question": "What is the average age and height of players on teams with more than 5 players in the NBA", "sql": "SELECT TEAM, AVG(AGE) as avg_age, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as avg_height FROM nba_roster GROUP BY TEAM HAVING COUNT(*) > 5;"}
{"question": "What is the average height of NBA players by age group", "sql": "SELECT AGE, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) AS avg_height FROM nba_roster GROUP BY AGE;"}
{"question": "What is the average age of NBA players who play as Point Guard or Shooting Guard", "sql": "SELECT AVG(AGE) as average_age FROM nba_roster WHERE POS = 'PG' OR POS = 'SG' OR POS = 'PG/SG' OR POS = 'SG/PG';"}
{"question": "Who are the top 4 highest-paid players in the NBA", "sql": "SELECT POS, NAME, CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) as salary FROM nba_roster ORDER BY salary DESC LIMIT 5 OFFSET 0;"}
{"question": "Who is the highest-paid player on the Boston Celtics who did not attend college", "sql": "SELECT NAME FROM nba_roster WHERE team='Boston Celtics' AND COLLEGE!='--' AND SALARY=(SELECT MAX(SALARY) FROM nba_roster WHERE team='Boston Celtics' AND COLLEGE!='--');"}
{"question": "What is the average age of players in the NBA who are taller than 6 feet 7 inches", "sql": "SELECT AVG(AGE) FROM nba_roster WHERE CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER) + CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12 > 6.67;"}
{"question": "What is the 99th percentile salary in the NBA?", "sql": "SELECT (CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as percentile FROM nba_roster WHERE SALARY!= '--' order by percentile limit 1 offset (select count(*) from nba_roster where SALARY != '--')*99/100-1;"}
{"question": "What is the 75th percentile salary in the NBA?", "sql": "SELECT (CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as percentile FROM nba_roster WHERE SALARY!= '--' order by percentile limit 1 offset (select count(*) from nba_roster where SALARY != '--')*75/100-1;"}
{"question": "What is the 25th percentile salary in the NBA?", "sql": "SELECT (CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as percentile FROM nba_roster WHERE SALARY!= '--' order by percentile limit 1 offset (select count(*) from nba_roster where SALARY != '--')*25/100-1;"}
{"question": "What is the median weight in the NBA?", "sql": "select CAST(SUBSTR(WT, 1, INSTR(WT,' ')) as INTEGER) as percentile from nba_roster order by percentile limit 1 offset (select count(*) from nba_roster)/2;"}
{"question": "What is the average weight in the NBA?", "sql": "SELECT AVG(CAST(SUBSTR(WT, 1, INSTR(WT,' ')) as INTEGER)) FROM nba_roster;"}
{"question": "What is the median height in the NBA?", "sql": "select CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12 as percentile from nba_roster order by percentile limit 1 offset (select count(*) from nba_roster)/2;"}
{"question": "What is the average height in the NBA?", "sql": "select AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as height from nba_roster;"}
{"question": "Can you tell me how many players are in the NBA?", "sql": "select count(*) from nba_roster;"}
{"question": "Would you please let me know what the highest paid players are for each position?", "sql": "SELECT name, pos, MAX(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as max_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY POS;"}
{"question": "Is Jalen Johnson 23 years old?", "sql": "Select name, age from nba_roster where name='Jalen Johnson';"}
{"question": "Who is the oldest player on the Brooklyn Nets?", "sql": "SELECT NAME FROM nba_roster WHERE TEAM = 'Brooklyn Nets' AND AGE = (SELECT MAX(AGE) FROM nba_roster WHERE TEAM = 'Brooklyn Nets');"}
{"question": "Who has the highest salary on the Memphis Grizzlies?", "sql": "select salary, name from nba_roster where team='Memphis Grizzlies' and SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "Which player has the highest salary on the Cleveland Cavaliers?", "sql": "select salary, name from nba_roster where team='Cleveland Cavaliers' and SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "Who is the highest paid center on the Dallas Mavericks?", "sql": "select salary, name from nba_roster where team='Dallas Mavericks' and POS='C' and SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "How much is Marcus Smart getting paid?", "sql": "select salary from nba_roster where name='Marcus Smart';"}
{"question": "What's the average age of the Trail Blazers?", "sql": "select avg(age) from nba_roster where team='Portland Trail Blazers';"}
{"question": "What's the median age of the NBA?", "sql": "select CAST(AGE as INTEGER) as percentile from nba_roster order by percentile limit 1 offset (select count(*) from nba_roster)/2;"}
{"question": "What's the median age of the Miami Heat?", "sql": "select CAST(AGE as INTEGER) as percentile from nba_roster where team='Miami Heat' order by percentile limit 1 offset (select count(*) from nba_roster where team='Miami Heat')/2;"}
{"question": "What are the 5 teams with the oldest average age in the NBA", "sql": "SELECT team, AVG(AGE) AS average_age FROM nba_roster GROUP BY team ORDER BY average_age DESC LIMIT 5;"}
{"question": "What is the average salary of Power Forward players in the NBA", "sql": "select avg(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary from nba_roster where POS = 'PF';"}
@@ -0,0 +1,128 @@
{"question": "How many players are on each team in the NBA", "sql": "SELECT team, COUNT(*) as num_players FROM nba_roster GROUP BY team;"}
{"question": "Who is the tallest player in the NBA roster", "sql": "SELECT name, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as height FROM nba_roster GROUP BY name ORDER BY height DESC LIMIT 1;"}
{"question": "What is the average age of NBA players", "sql": "SELECT AVG(AGE) FROM nba_roster;"}
{"question": "Who is the heaviest player in the NBA", "sql": "SELECT NAME, WT FROM nba_roster WHERE WT!= 'NA' ORDER BY CAST(SUBSTR(WT, 1, INSTR(WT,' ')-1) AS INTEGER) DESC LIMIT 1;"}
{"question": "What is the total salary of all players in the NBA who are at least 6 feet 7 inches tall", "sql": "SELECT SUM(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as total_salary FROM nba_roster WHERE CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12 >= 6.67;"}
{"question": "Which three teams have the most players from a particular college", "sql": "SELECT team, COLLEGE, COUNT(*) as num_players FROM nba_roster WHERE COLLEGE!= '--' GROUP BY team, COLLEGE ORDER BY num_players DESC LIMIT 3;"}
{"question": "What is the total salary for each team in the NBA, excluding teams with missing salary data", "sql": "SELECT team, SUM(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) AS total_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY team ORDER BY total_salary DESC;"}
{"question": "Which team has the most players under the age of 25", "sql": "SELECT team, COUNT(*) as num_players FROM nba_roster WHERE AGE <= 25 GROUP BY team ORDER BY num_players DESC;"}
{"question": "What is the average age of players in the NBA who are older than 5 years", "sql": "SELECT AVG(AGE) AS average_age FROM nba_roster WHERE AGE * 12 > 60;"}
{"question": "What team pays its players the most, on average", "sql": "SELECT team, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) AS average_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY team ORDER BY average_salary DESC LIMIT 1;"}
{"question": "Who is the highest paid center on the Dallas Mavericks who is older than 5 years old", "sql": "SELECT name, salary FROM nba_roster WHERE team='Dallas Mavericks' AND POS='C' AND SALARY!= '--' AND age > 5 ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "Who is the highest-paid Power Forward on the Chicago Bulls", "sql": "SELECT name, salary FROM nba_roster WHERE team='Chicago Bulls' AND POS='PF' AND SALARY!='--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "How many players are currently on the Toronto Raptors' roster", "sql": "SELECT COUNT(*) FROM nba_roster WHERE Team = 'Toronto Raptors';"}
{"question": "How many players in the NBA are over the age of 30", "sql": "SELECT COUNT(*) AS num_players FROM nba_roster WHERE AGE > 30;"}
{"question": "What is the most common position among players 25 or older in the NBA", "sql": "SELECT POS, COUNT(*) AS count FROM nba_roster WHERE AGE >= 25 GROUP BY POS ORDER BY count DESC LIMIT 1;"}
{"question": "What is the jersey number of the player with the 75th percentile of jersey numbers in the NBA", "sql": "SELECT CAST(Jersey AS INTEGER) as percentile FROM nba_roster ORDER BY percentile LIMIT 1 OFFSET (SELECT COUNT(*) FROM nba_roster) * 0.75;"}
{"question": "What is the most common position among the Toronto Raptors players", "sql": "SELECT POS, COUNT(*) AS count FROM nba_roster WHERE team='Toronto Raptors' GROUP BY POS ORDER BY count DESC LIMIT 1;"}
{"question": "Which team has the heaviest average weight", "sql": "SELECT team, AVG(CAST(SUBSTR(WT, 1, INSTR(WT,' ')-1) AS INTEGER) + CAST(SUBSTR(WT, INSTR(WT,' ')+1) AS FLOAT)/16) as average_weight FROM nba_roster WHERE WT!= 'NA' GROUP BY team ORDER BY average_weight DESC LIMIT 1;"}
{"question": "Who are the top 3 highest-paid Power Forwards in the NBA", "sql": "SELECT NAME, SALARY FROM nba_roster WHERE POS = 'PF' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 3;"}
{"question": "Which teams have the smallest rosters and what is the average age of their players", "sql": "SELECT team, COUNT(*) AS roster_size, AVG(AGE) AS average_age FROM nba_roster GROUP BY team ORDER BY roster_size ASC;"}
{"question": "Which team has the highest average salary for players who attended college", "sql": "SELECT team, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as salary FROM nba_roster WHERE COLLEGE!= '--' GROUP BY team ORDER BY salary DESC LIMIT 1;"}
{"question": "Which team has the shortest average height among players 25 years old or younger", "sql": "SELECT team, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as height FROM nba_roster WHERE AGE <= 25 GROUP BY team ORDER BY height ASC LIMIT 1;"}
{"question": "Which three teams have the tallest players on average", "sql": "SELECT team, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as average_height FROM nba_roster WHERE HT!= 'NA' GROUP BY team ORDER BY average_height DESC LIMIT 3;"}
{"question": "Who are the top 3 players in the league by salary, excluding those who did not attend college", "sql": "SELECT name, SALARY FROM nba_roster WHERE COLLEGE!= '--' ORDER BY CAST(SUBSTRING(SALARY, 2) AS INTEGER) DESC LIMIT 3;"}
{"question": "Which five teams have the oldest average age among their players", "sql": "SELECT TEAM, AVG(AGE) as avg_age FROM nba_roster WHERE POS!= '--' GROUP BY TEAM ORDER BY avg_age DESC LIMIT 5;"}
{"question": "Which three teams in the NBA have the highest average salary among their players", "sql": "SELECT team, AVG(CAST(SUBSTRING(SALARY, 2, LENGTH(SALARY)-2) AS INTEGER)) AS avg_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY team ORDER BY avg_salary DESC LIMIT 3;"}
{"question": "Who is the highest-paid player in the NBA who did not attend college", "sql": "SELECT name, SALARY FROM nba_roster WHERE SALARY!= '--' AND COLLEGE = '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "What is the average age of players in the Toronto Raptors", "sql": "SELECT AVG(AGE) FROM nba_roster WHERE TEAM = 'Toronto Raptors';"}
{"question": "What is the player with the highest jersey number that is not 'NA'", "sql": "SELECT MAX(Jersey) as jersey_num, name FROM nba_roster WHERE Jersey!= 'NA' GROUP BY name ORDER BY jersey_num DESC LIMIT 1;"}
{"question": "Who is the youngest player in the NBA", "sql": "SELECT name FROM nba_roster ORDER BY AGE ASC LIMIT 1;"}
{"question": "What is the number of players in the NBA who are older than 5 years old", "sql": "SELECT COUNT(*) AS num_players FROM nba_roster WHERE AGE > 5;"}
{"question": "Who is the highest-paid player on the Los Angeles Lakers", "sql": "SELECT name, salary FROM nba_roster WHERE team='Los Angeles Lakers' AND SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "Which colleges tend to produce the oldest players in the NBA", "sql": "SELECT COLLEGE, AVG(AGE) AS average_age FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY average_age DESC;"}
{"question": "What percentage of players in the NBA play each position", "sql": "SELECT POS, COUNT(*) as count, ROUND(COUNT(*)*100.0/(SELECT COUNT(*) FROM nba_roster),2) as percentage FROM nba_roster WHERE POS!= '--' GROUP BY POS ORDER BY percentage DESC;"}
{"question": "What are the top 10 teams with the most players in the NBA", "sql": "SELECT Team, COUNT(*) as num_players FROM nba_roster GROUP BY Team ORDER BY num_players DESC LIMIT 10;"}
{"question": "What is the average age of players in the NBA who are older than the average age of all players in the league plus 5 years", "sql": "SELECT AVG(AGE) as average_age FROM nba_roster WHERE AGE > (SELECT AVG(AGE) FROM nba_roster) + 5;"}
{"question": "Which team has the most players who are older than the average age of all players in the NBA plus 5 years", "sql": "SELECT team, COUNT(*) as num_players FROM nba_roster WHERE AGE > (SELECT AVG(AGE) FROM nba_roster) + 5 GROUP BY team ORDER BY num_players DESC LIMIT 1;"}
{"question": "What are the average height and weight for each team in the NBA", "sql": "SELECT team, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as avg_height, AVG(CAST(SUBSTR(WT, 1, INSTR(WT,' ')) as INTEGER)) as avg_weight FROM nba_roster GROUP BY team;"}
{"question": "What are the top 3 teams with the highest average salary", "sql": "SELECT Team, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) AS avg_salary FROM nba_roster GROUP BY Team ORDER BY avg_salary DESC LIMIT 3;"}
{"question": "What position has the most players in the NBA roster", "sql": "SELECT POS, COUNT(*) AS count, POS FROM nba_roster GROUP BY POS ORDER BY count DESC LIMIT 1;"}
{"question": "What is the age of the 75th percentile of NBA players", "sql": "SELECT age FROM nba_roster WHERE AGE!= '--' ORDER BY age LIMIT 1 OFFSET (SELECT COUNT(*) FROM nba_roster WHERE AGE!= '--')*75/100-1;"}
{"question": "What is the average age and salary of NBA players, excluding those with unknown salaries", "sql": "SELECT AVG(AGE) AS average_age, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) AS average_salary FROM nba_roster WHERE SALARY!= '--';"}
{"question": "What is the average age of players in each position group in the NBA", "sql": "SELECT POS, AVG(AGE) AS avg_age FROM nba_roster WHERE POS IN ('PG', 'SG', 'SF', 'PF', 'C') GROUP BY POS;"}
{"question": "What team has the most players at the point guard position", "sql": "SELECT team, COUNT(*) as num_players FROM nba_roster WHERE POS='PG' GROUP BY team ORDER BY num_players DESC LIMIT 1;"}
{"question": "What is the name of the heaviest player on the Los Angeles Lakers", "sql": "SELECT name FROM nba_roster WHERE team='Los Angeles Lakers' AND WT=(SELECT MAX(WT) FROM nba_roster WHERE team='Los Angeles Lakers');"}
{"question": "Who are the top 5 players in the NBA in terms of salary-to-age ratio", "sql": "SELECT name, CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) as salary, AGE FROM nba_roster WHERE SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)/AGE DESC LIMIT 5;"}
{"question": "Which NBA teams have the most players who attended college", "sql": "SELECT team, COUNT(*) as num_players FROM nba_roster WHERE COLLEGE!='--' GROUP BY team ORDER BY num_players DESC;"}
{"question": "What is the highest paid player for each position in the NBA", "sql": "SELECT pos, name, MAX(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as max_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY pos ORDER BY pos;"}
{"question": "Which NBA teams have the most players", "sql": "SELECT Team, COUNT(*) as count FROM nba_roster GROUP BY Team ORDER BY count DESC;"}
{"question": "What is the position with the most players under the age of 25 in the NBA", "sql": "SELECT POS, COUNT(*) as count FROM nba_roster WHERE AGE <= 25 GROUP BY POS ORDER BY count DESC LIMIT 1;"}
{"question": "What players in the NBA have a height greater than or equal to 6 feet 7 inches", "sql": "SELECT NAME FROM nba_roster WHERE CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12 >= 6.67;"}
{"question": "Who are the top 3 highest-paid players under the age of 25 in the NBA", "sql": "SELECT NAME, SALARY FROM nba_roster WHERE AGE < 25 ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 3;"}
{"question": "What are the names of all the players on the Toronto Raptors who are 25 years or older", "sql": "SELECT name FROM nba_roster WHERE age >= 25 AND team = 'Toronto Raptors';"}
{"question": "What is the position with the shortest average height in the NBA", "sql": "SELECT pos, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as height, COUNT(*) as count FROM nba_roster GROUP BY pos ORDER BY height ASC LIMIT 1;"}
{"question": "What is the average age of the players on the Memphis Grizzlies", "sql": "SELECT AVG(AGE) FROM nba_roster WHERE team='Memphis Grizzlies';"}
{"question": "What are the average ages of the players on each NBA team, listed from youngest to oldest", "sql": "SELECT team, AVG(AGE) as average_age FROM nba_roster GROUP BY team ORDER BY average_age ASC;"}
{"question": "What is the average age and height for each position in the NBA", "sql": "SELECT POS, AVG(AGE) AS avg_age, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) AS avg_height FROM nba_roster GROUP BY POS;"}
{"question": "What is the highest-paid player in the NBA", "sql": "SELECT name, SALARY FROM nba_roster WHERE SALARY!= '--' ORDER BY CAST(SUBSTR(SALARY, 2) AS INTEGER) DESC LIMIT 1;"}
{"question": "Which team has the most players 25 or older", "sql": "SELECT team, COUNT(*) as num_players FROM nba_roster WHERE AGE >= 25 GROUP BY team ORDER BY num_players DESC LIMIT 1;"}
{"question": "What is the average age of NBA players who are at least 6 feet 7 inches tall", "sql": "SELECT AVG(AGE) FROM nba_roster WHERE CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12 >= 6.67;"}
{"question": "What are all the players in the NBA whose last name is Johnson", "sql": "SELECT * FROM nba_roster WHERE NAME LIKE '%Johnson';"}
{"question": "What is the average salary for players from each college, and which colleges produce the most highly paid NBA players", "sql": "SELECT COLLEGE, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as avg_salary FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY avg_salary DESC;"}
{"question": "Who are the top 3 highest-paid players on the Los Angeles Lakers", "sql": "SELECT name, SALARY FROM nba_roster WHERE team='Los Angeles Lakers' ORDER BY CAST(SUBSTRING(SALARY, 2) AS INTEGER) DESC LIMIT 3;"}
{"question": "What is the average height of all NBA players who are 25 years old or younger", "sql": "SELECT AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)) as average_height FROM nba_roster WHERE AGE <= 25;"}
{"question": "What college has the most players in the NBA", "sql": "SELECT COLLEGE, COUNT(*) AS COUNT FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY COUNT(*) DESC LIMIT 1;"}
{"question": "Who are the 25-year-old players on the Toronto Raptors", "sql": "SELECT name FROM nba_roster WHERE team='Toronto Raptors' AND age=25;"}
{"question": "Who is the highest-paid player in the NBA who attended college", "sql": "SELECT name, SALARY FROM nba_roster WHERE COLLEGE!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "What college and position combination has the most players in the NBA", "sql": "SELECT COLLEGE, POS, COUNT(*) as count FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE, POS ORDER BY count DESC LIMIT 1;"}
{"question": "Who is the heaviest player in the NBA roster", "sql": "SELECT name, WT, CAST(SUBSTR(WT, 1, INSTR(WT,' ')-1) AS INTEGER) as weight FROM nba_roster WHERE WT!= 'NA' ORDER BY weight DESC LIMIT 1;"}
{"question": "What is the average height of players on each team, excluding those under 25 and with unknown heights", "sql": "SELECT team, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER) + CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as avg_height FROM nba_roster WHERE HT!= 'NA' AND age > 25 GROUP BY team;"}
{"question": "What is the average salary of NBA players over the age of 25", "sql": "SELECT AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary FROM nba_roster WHERE AGE > 25 AND SALARY!= '--';"}
{"question": "What are the 5 oldest players in the NBA", "sql": "SELECT NAME, AGE FROM nba_roster WHERE AGE != '--' ORDER BY AGE DESC LIMIT 5;"}
{"question": "Which team has the most players over the age of 5 in the NBA", "sql": "SELECT team, COUNT(*) as num_players FROM nba_roster WHERE age > 5 GROUP BY team ORDER BY num_players DESC LIMIT 1;"}
{"question": "Who is the highest-paid player in the NBA, excluding those under the age of 6 and those with unknown salaries", "sql": "SELECT name, team FROM nba_roster WHERE age > 5 AND SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "What is the average height and weight of players on each NBA team", "sql": "SELECT team, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as avg_height, AVG(CAST(SUBSTR(WT, 1, INSTR(WT,' ')) AS INTEGER)) as avg_weight FROM nba_roster GROUP BY team;"}
{"question": "Which positions in the NBA have the most players and which positions have the oldest players on average", "sql": "SELECT POS, COUNT(*) as count, AVG(AGE) as average_age FROM nba_roster GROUP BY POS ORDER BY count DESC;"}
{"question": "What is the position with the tallest players in the NBA", "sql": "SELECT POS, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)) as avg_height FROM nba_roster GROUP BY POS ORDER BY avg_height DESC LIMIT 1;"}
{"question": "What are the top 3 tallest players in the NBA", "sql": "SELECT NAME, HT, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as height FROM nba_roster GROUP BY NAME, HT ORDER BY height DESC LIMIT 3;"}
{"question": "Who is the highest-paid player on the Toronto Raptors with a jersey number greater than 10", "sql": "SELECT name, salary FROM nba_roster WHERE team='Toronto Raptors' AND CAST(Jersey AS INTEGER) > 10 AND SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "Which teams have the most players in their roster", "sql": "SELECT team, COUNT(*) as num_players FROM nba_roster GROUP BY team ORDER BY num_players DESC;"}
{"question": "What is the average salary of all NBA players, excluding those who are not paid or have an unknown position", "sql": "SELECT AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary FROM nba_roster WHERE SALARY!= '--' AND POS!= 'NA';"}
{"question": "Which team has invested the most in young talent, with an average salary for players 5 years or less younger than the average age of all players", "sql": "SELECT team, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary FROM nba_roster WHERE AGE <= (SELECT AVG(AGE) FROM nba_roster) * 5 GROUP BY team ORDER BY average_salary DESC LIMIT 1;"}
{"question": "Which 5 teams have the most players who have publicly disclosed their college information", "sql": "SELECT team, COUNT(*) as num_players FROM nba_roster WHERE COLLEGE!= '--' GROUP BY team ORDER BY num_players DESC LIMIT 5;"}
{"question": "What is the average age of players by position in the NBA", "sql": "SELECT POS, AVG(AGE) as average_age FROM nba_roster GROUP BY POS ORDER BY average_age ASC;"}
{"question": "What is the average height of the tallest positions in the NBA", "sql": "SELECT POS, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)) as avg_height FROM nba_roster GROUP BY POS ORDER BY avg_height DESC;"}
{"question": "What is the number of players on the Chicago Bulls who are 25 years old or younger", "sql": "SELECT COUNT(*) FROM nba_roster WHERE team='Chicago Bulls' AND AGE <= 25;"}
{"question": "What are the average heights for each position in the NBA, and which position has the tallest players on average", "sql": "SELECT pos, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)) as avg_height, COUNT(*) as count FROM nba_roster WHERE HT!= 'NA' GROUP BY pos ORDER BY avg_height DESC;"}
{"question": "Which team has the oldest average age among its guards and forwards", "sql": "SELECT team, AVG(AGE) AS average_age FROM nba_roster WHERE POS IN ('PG', 'SG', 'SF', 'PF', 'C') GROUP BY team ORDER BY average_age DESC LIMIT 1;"}
{"question": "Who is the pointguard for the Golden State Warriors?", "sql": "select name from nba_roster where team='Golden State Warriors' and POS='PG';"}
{"question": "What is the number of players on the Chicago Bulls who are 25 years old or younger", "sql": "SELECT COUNT(*) FROM nba_roster WHERE team='Chicago Bulls' AND AGE <= 25;"}
{"question": "Who is the highest-paid player on the Los Angeles Lakers", "sql": "SELECT NAME, SALARY FROM nba_roster WHERE team='Los Angeles Lakers' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "Who is the highest paid player in the NBA?", "sql": "SELECT NAME, salary FROM nba_roster WHERE SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "What team is LaMelo Ball on?", "sql": "select team from nba_roster where name='LaMelo Ball';"}
{"question": "How much does Lonzo Ball weigh?", "sql": "select wt from nba_roster where name='Lonzo Ball';"}
{"question": "What college sent the most players to the current NBA?", "sql": "select college from nba_roster where college != '--' group by college order by count(*) desc limit 1;"}
{"question": "How old is Lebron James?", "sql": "select age from nba_roster where name='LeBron James';"}
{"question": "What is the most popular jersey number in the current NBA?", "sql": "select Jersey from nba_roster where Jersey != 'NA' group by Jersey order by count(*) desc limit 1;"}
{"question": "Can you give me a list of all the players without college data?", "sql": "SELECT name FROM nba_roster WHERE COLLEGE IS NULL OR COLLEGE = '--';"}
{"question": "What team has the smallest roster?", "sql": "select team from nba_roster group by team order by count(*) asc limit 1;"}
{"question": "What team has the largest roster?", "sql": "select team, count(*) from nba_roster group by team order by count(*) desc limit 1;"}
{"question": "What team is paying its players the most in total?", "sql": "select team, sum(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) from nba_roster group by team order by sum(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) desc limit 1;"}
{"question": "Which team is paying its players the least?", "sql": "select team from nba_roster group by team order by sum(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) asc limit 1;"}
{"question": "Which team is on average the tallest?", "sql": "select team, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as height from nba_roster group by team order by height desc limit 1;"}
{"question": "Which team is on average the shortest?", "sql": "select team, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as height from nba_roster group by team order by height asc limit 1;"}
{"question": "Who are the tallest 5 centers in the league?", "sql": "SELECT name, HT FROM nba_roster WHERE POS = 'C' ORDER BY HT DESC LIMIT 5;"}
{"question": "Who are the top 5 highest paid power forwards in the league?", "sql": "SELECT NAME, salary FROM nba_roster WHERE POS = 'PF' AND SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 5;"}
{"question": "What is the median salary in the NBA?", "sql": "SELECT (CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as percentile FROM nba_roster WHERE SALARY!= '--' order by percentile limit 1 offset (select count(*) from nba_roster where SALARY != '--')*50/100-1;"}
{"question": "What is the average salary in the NBA?", "sql": "SELECT avg(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as percentile FROM nba_roster WHERE SALARY!= '--';"}
{"question": "What is the 99th percentile salary in the NBA?", "sql": "SELECT (CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as percentile FROM nba_roster WHERE SALARY!= '--' order by percentile limit 1 offset (select count(*) from nba_roster where SALARY != '--')*99/100-1;"}
{"question": "What is the 75th percentile salary in the NBA?", "sql": "SELECT (CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as percentile FROM nba_roster WHERE SALARY!= '--' order by percentile limit 1 offset (select count(*) from nba_roster where SALARY != '--')*75/100-1;"}
{"question": "What is the 25th percentile salary in the NBA?", "sql": "SELECT (CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as percentile FROM nba_roster WHERE SALARY!= '--' order by percentile limit 1 offset (select count(*) from nba_roster where SALARY != '--')*25/100-1;"}
{"question": "What is the median weight in the NBA?", "sql": "select CAST(SUBSTR(WT, 1, INSTR(WT,' ')) as INTEGER) as percentile from nba_roster order by percentile limit 1 offset (select count(*) from nba_roster)*50/100-1;"}
{"question": "What is the average weight in the NBA?", "sql": "SELECT AVG(CAST(SUBSTR(WT, 1, INSTR(WT,' ')) as INTEGER)) FROM nba_roster;"}
{"question": "What is the median height in the NBA?", "sql": "select CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12 as percentile from nba_roster order by percentile limit 1 offset (select count(*) from nba_roster)*50/100-1;"}
{"question": "What is the average height in the NBA?", "sql": "select AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as height from nba_roster;"}
{"question": "Can you tell me how many players are in the NBA?", "sql": "select count(*) from nba_roster;"}
{"question": "Would you please let me know what the highest paid players are for each position?", "sql": "SELECT name, pos, MAX(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as max_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY POS;"}
{"question": "Is Jalen Johnson 23 years old?", "sql" : "Select name, age from nba_roster where name='Jalen Johnson';"}
{"question": "Who is the oldest player on the Brooklyn Nets?", "sql" : "SELECT NAME FROM nba_roster WHERE TEAM = 'Brooklyn Nets' AND AGE = (SELECT MAX(AGE) FROM nba_roster WHERE TEAM = 'Brooklyn Nets');"}
{"question": "Who has the highest salary on the Memphis Grizzlies?", "sql" : "select salary, name from nba_roster where team='Memphis Grizzlies' and SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "Which player has the highest salary on the Cleveland Cavaliers?", "sql" : "select salary, name from nba_roster where team='Cleveland Cavaliers' and SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "Who is the highest paid center on the Dallas Mavericks?", "sql" : "select salary, name from nba_roster where team='Dallas Mavericks' and POS='C' and SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "How much is Marcus Smart getting paid?", "sql" : "select salary from nba_roster where name='Marcus Smart';"}
{"question": "What's the average age of the Trail Blazers?", "sql" : "select avg(age) from nba_roster where team='Portland Trail Blazers';"}
{"question": "What's the median age of the NBA?", "sql": "select CAST(AGE as INTEGER) as percentile from nba_roster order by percentile limit 1 offset (select count(*) from nba_roster)*50/100-1;"}
{"question": "What's the median age of the Miami Heat?", "sql": "select CAST(AGE as INTEGER) as percentile from nba_roster where team='Miami Heat' order by percentile limit 1 offset (select count(*) from nba_roster where team='Miami Heat')*50/100-1;"}
{"question": "What are the 5 teams with the oldest average age in the NBA", "sql": "SELECT team, AVG(AGE) AS average_age FROM nba_roster GROUP BY team ORDER BY average_age DESC LIMIT 5;"}
{"question": "What is the average salary of Power Forward players in the NBA", "sql": "select avg(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary from nba_roster where POS = 'PF';"}
@@ -0,0 +1,159 @@
{"question": "What is the average height of NBA players", "sql": "SELECT AVG(CAST(SUBSTRING(HT, 0, INSTR(HT,'')-1) AS INTEGER) + CAST(SUBSTRING(HT, INSTR(HT,'')+1) AS INTEGER)/12) as average_height FROM nba_roster WHERE HT!= 'NA';"}
{"question": "What is the average age of all players in the NBA", "sql": "SELECT AVG(AGE) FROM nba_roster"}
{"question": "What are the oldest players on each team with a roster size of 6 or more", "sql": "SELECT NAME FROM nba_roster WHERE AGE IN (SELECT MAX(AGE) FROM nba_roster WHERE TEAM IN (SELECT TEAM FROM nba_roster GROUP BY TEAM HAVING COUNT(*) > 5))"}
{"question": "What is the average height of the players on the Toronto Raptors", "sql": "SELECT AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as height FROM nba_roster WHERE team='Toronto Raptors';"}
{"question": "What is the highest-paid Toronto Raptors player who attended college", "sql": "SELECT name, salary FROM nba_roster WHERE team='Toronto Raptors' AND COLLEGE!='--' AND SALARY!='--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1"}
{"question": "What is the most common height among NBA players", "sql": "SELECT HT, COUNT(*) as count FROM nba_roster WHERE HT IS NOT NULL GROUP BY HT ORDER BY count DESC LIMIT 1"}
{"question": "What is the most represented college in the NBA", "sql": "SELECT COLLEGE, COUNT(*) as count FROM nba_roster WHERE COLLEGE IS NOT NULL GROUP BY COLLEGE ORDER BY count DESC LIMIT 1"}
{"question": "What is the average age of all players in the NBA", "sql": "SELECT AVG(AGE) AS average_age FROM nba_roster"}
{"question": "What is the average height of NBA players", "sql": "SELECT AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER) + CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) AS average_height FROM nba_roster"}
{"question": "What is the average age of the players in the NBA", "sql": "SELECT AVG(AGE) FROM nba_roster WHERE AGE IS NOT NULL"}
{"question": "What is the position with the most players in the NBA", "sql": "SELECT POS, COUNT(*) as count FROM nba_roster WHERE SALARY!= '--' GROUP BY POS ORDER BY count DESC LIMIT 1"}
{"question": "What is the average height of players on each NBA team, excluding players with unknown heights", "sql": "SELECT TEAM, AVG(CAST(SUBSTRING(HT, 0, INSTR(HT,'')-1) AS INTEGER)) as avg_height FROM nba_roster WHERE HT!= 'NA' GROUP BY TEAM ORDER BY avg_height DESC"}
{"question": "What are the 5 most common heights among NBA players", "sql": "SELECT HT, COUNT(*) AS count FROM nba_roster GROUP BY HT ORDER BY count DESC LIMIT 5"}
{"question": "What are the top 5 colleges with the most players in the NBA", "sql": "SELECT COLLEGE, COUNT(*) AS count FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY count DESC LIMIT 5"}
{"question": "What is the average age of the players in the NBA", "sql": "SELECT AVG(AGE) FROM nba_roster WHERE AGE IS NOT NULL"}
{"question": "Which players in the NBA have attended the most colleges", "sql": "SELECT NAME, COLLEGE, COUNT(*) as num_colleges FROM nba_roster WHERE COLLEGE!= '--' GROUP BY NAME, COLLEGE ORDER BY num_colleges DESC;"}
{"question": "What is the average age of the players in the NBA", "sql": "SELECT AVG(AGE) FROM nba_roster"}
{"question": "Who are the top 5 highest-paid players in the NBA", "sql": "SELECT * FROM nba_roster WHERE SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 5"}
{"question": "What is the average height of players on each NBA team", "sql": "SELECT team, AVG(CAST(SUBSTRING(HT, 1, INSTR(HT,'')-1) AS INTEGER) + CAST(SUBSTRING(HT, INSTR(HT,'')+1) AS INTEGER) / 12.0) as avg_height FROM nba_roster WHERE HT!= 'NA' GROUP BY team"}
{"question": "Who are the top 3 highest-paid players in the NBA", "sql": "SELECT name, SUM(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as total_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY name ORDER BY total_salary DESC LIMIT 3"}
{"question": "Which team has the most players in the NBA", "sql": "SELECT team, COUNT(*) as num_players FROM nba_roster GROUP BY team ORDER BY num_players DESC LIMIT 1"}
{"question": "What is the total salary of all players in the NBA who are 6'8", "sql": "SELECT SUM(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as total_salary FROM nba_roster WHERE CAST(SUBSTR(HT, 1, INSTR(HT,'')-1) AS INTEGER) = 68;"}
{"question": "What is the average age of players on each team in the NBA", "sql": "SELECT team, AVG(AGE) as avg_age FROM nba_roster WHERE SALARY!= '--' GROUP BY team"}
{"question": "How many players in the NBA have a non-null salary and college information, and play one of the five main positions", "sql": "SELECT COUNT(*) as num_players FROM nba_roster WHERE POS IN ('PG', 'SG', 'SF', 'PF', 'C') AND SALARY!= '--' AND COLLEGE!= '--'"}
{"question": "What is the most common position in the NBA", "sql": "SELECT POS, COUNT(*) as count FROM nba_roster GROUP BY POS ORDER BY count DESC LIMIT 1"}
{"question": "What is the average height of NBA players", "sql": "SELECT AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER) + CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as average_height FROM nba_roster;"}
{"question": "What is the average salary of NBA players who are at least 5 years old", "sql": "SELECT AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary FROM nba_roster WHERE AGE > 5"}
{"question": "What is the average age of all players in the NBA", "sql": "SELECT AVG(AGE) FROM nba_roster"}
{"question": "What is the most common age range among NBA players", "sql": "SELECT AGE, COUNT(*) AS count FROM nba_roster GROUP BY AGE ORDER BY count DESC LIMIT 1"}
{"question": "Which team has the most players in the NBA", "sql": "SELECT Team, COUNT(*) as num_players FROM nba_roster GROUP BY Team ORDER BY num_players DESC LIMIT 1"}
{"question": "What is the average salary of NBA players", "sql": "SELECT AVG(CAST(SUBSTR(SALARY, 1, INSTR(SALARY, '$')-1) AS INTEGER)) FROM nba_roster WHERE SALARY!= '--';"}
{"question": "How many players in the NBA are 68 inches tall", "sql": "SELECT COUNT(*) FROM nba_roster WHERE CAST(SUBSTR(HT, 1, INSTR(HT,'')-1) AS INTEGER) = 68;"}
{"question": "What is the average salary of Power Forwards in the NBA who are at least 25 years old", "sql": "SELECT AVG(CAST(SUBSTR(SALARY, 1, INSTR(SALARY, '$')-1) AS INTEGER)) AS average_salary FROM nba_roster WHERE AGE >= 25 AND POS = 'PF';"}
{"question": "What is the average age of 6-foot Power Forwards in the NBA", "sql": "SELECT AVG(AGE) FROM nba_roster WHERE CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER) = 6 AND POS='PF';"}
{"question": "What is the heaviest Power Forward in the NBA", "sql": "SELECT NAME, AVG(CAST(SUBSTR(WT, 1, INSTR(WT,' ')) AS INTEGER)) AS avg_weight FROM nba_roster WHERE POS='PF' GROUP BY NAME ORDER BY avg_weight DESC LIMIT 1"}
{"question": "What is the number of players on each team in the NBA", "sql": "SELECT Team, COUNT(*) as num_players FROM nba_roster GROUP BY Team"}
{"question": "What is the average height of NBA players who are 25 years old or older", "sql": "SELECT AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as height FROM nba_roster WHERE age >= 25"}
{"question": "What are the top 3 teams with the highest average salaries in the NBA", "sql": "SELECT team, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as avg_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY team ORDER BY avg_salary DESC LIMIT 3"}
{"question": "What is the most common position in the NBA", "sql": "SELECT POS, COUNT(*) as count FROM nba_roster GROUP BY POS ORDER BY count DESC LIMIT 1"}
{"question": "What are the names of the players in the NBA who are exactly 6 feet 8 inches tall", "sql": "SELECT NAME, HT FROM nba_roster WHERE CAST(SUBSTRING(HT, 0, INSTR(HT,'')-1) AS INTEGER) = 68 ORDER BY HT ASC;"}
{"question": "What is the college with the most players in the NBA", "sql": "SELECT COLLEGE, COUNT(*) as count FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY count DESC LIMIT 1"}
{"question": "What is the average age of all players in the NBA", "sql": "SELECT AVG(AGE) FROM nba_roster"}
{"question": "What is the most represented college in the NBA", "sql": "SELECT COLLEGE, COUNT(*) AS frequency FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY frequency DESC LIMIT 1"}
{"question": "What is the average age of the players in the NBA", "sql": "SELECT AVG(AGE) as average_age FROM nba_roster WHERE AGE IS NOT NULL"}
{"question": "What is the average height of NBA players who have a recorded height", "sql": "SELECT AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER) + CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as average_height FROM nba_roster WHERE HT IS NOT NULL"}
{"question": "What is the average salary of NBA players who are 25 years or older", "sql": "SELECT AVG(CAST(SUBSTR(SALARY, 1, INSTR(SALARY, '$') - 1) as INTEGER)) FROM nba_roster WHERE CAST(AGE as INTEGER) >= 25"}
{"question": "What is the most represented college in the NBA", "sql": "SELECT COLLEGE, COUNT(*) as count FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY count DESC LIMIT 1"}
{"question": "What is the number of players on each team in the NBA", "sql": "SELECT Team, COUNT(*) as num_players FROM nba_roster GROUP BY Team"}
{"question": "What is the average salary for each position in the NBA, excluding players with unknown salaries", "sql": "SELECT POS, AVG(CAST(SUBSTR(SALARY, 1, INSTR(SALARY, '$') - 1) as INTEGER)) as avg_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY POS"}
{"question": "What is the most common position in the NBA", "sql": "SELECT POS, COUNT(*) as count FROM nba_roster GROUP BY POS ORDER BY count DESC LIMIT 1"}
{"question": "What is the average age of players on each team in the NBA", "sql": "SELECT team, AVG(AGE) as avg_age FROM nba_roster WHERE SALARY!= '--' GROUP BY team"}
{"question": "What are the top 3 positions with the highest total salary expenditure in the NBA", "sql": "SELECT pos, name, SUM(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as total_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY pos ORDER BY total_salary DESC LIMIT 3"}
{"question": "Which colleges have the most players in the NBA", "sql": "SELECT COLLEGE, COUNT(*) AS num_players FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY num_players DESC;"}
{"question": "What is the average salary for each team in the NBA", "sql": "SELECT team, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as avg_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY team"}
{"question": "What is the age range of players on each team in the NBA", "sql": "SELECT team, MIN(AGE) as youngest_player, MAX(AGE) as oldest_player FROM nba_roster WHERE AGE IS NOT NULL GROUP BY team"}
{"question": "Which team has the most players who are 6'8", "sql": "SELECT team, COUNT(*) as num_players FROM nba_roster WHERE CAST(SUBSTR(HT, 1, INSTR(HT,'')-1) AS INTEGER) = 68 GROUP BY team ORDER BY num_players DESC LIMIT 1"}
{"question": "How many players in the NBA are over the age of 25", "sql": "SELECT COUNT(*) FROM nba_roster WHERE AGE > 25"}
{"question": "What is the average height of NBA players under the age of 25", "sql": "SELECT AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as average_height FROM nba_roster WHERE AGE <= 25"}
{"question": "What is the total salary of all players in the NBA who are more than 5 years older than the average age of all players", "sql": "SELECT SUM(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as total_salary FROM nba_roster WHERE (AGE - (SELECT AVG(AGE) FROM nba_roster)) > 5"}
{"question": "What is the median weight in the NBA", "sql": "SELECT COLLEGE, COUNT(*) as count FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY count DESC LIMIT 1"}
{"question": "What are the top 5 teams with the oldest average age of players", "sql": "SELECT team, AVG(AGE) AS average_age FROM nba_roster GROUP BY team ORDER BY average_age DESC LIMIT 5"}
{"question": "What is the average height of NBA players", "sql": "SELECT AVG(CAST(SUBSTRING(HT, 0, INSTR(HT,'')-1) AS INTEGER)) AS average_height FROM nba_roster WHERE HT!= 'NA';"}
{"question": "What is the average salary of the Los Angeles Lakers players", "sql": "SELECT AVG(CAST(SALARY AS INTEGER) ) AS average_salary FROM nba_roster WHERE team='Los Angeles Lakers';"}
{"question": "What is the college that has produced the most players currently playing for the Boston Celtics", "sql": "SELECT COLLEGE, COUNT(*) AS count FROM nba_roster WHERE team='Boston Celtics' GROUP BY COLLEGE ORDER BY count DESC LIMIT 1"}
{"question": "What college has the most players in the NBA who are 30 years old or older", "sql": "SELECT COLLEGE, COUNT(*) AS count FROM nba_roster WHERE AGE >= 30 GROUP BY COLLEGE ORDER BY count DESC LIMIT 1"}
{"question": "How many players in the NBA are at least 5 years older than the youngest player in the league", "sql": "SELECT COUNT(*) as num_players FROM nba_roster WHERE AGE - (SELECT MIN(AGE) FROM nba_roster) > 5"}
{"question": "What are the 5 colleges that have produced the most players in the NBA", "sql": "SELECT COLLEGE, COUNT(*) as num_players FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY num_players DESC LIMIT 5"}
{"question": "What are the most common positions in the NBA", "sql": "SELECT POS, COUNT(*) as count FROM nba_roster WHERE POS!= '--' GROUP BY POS ORDER BY count DESC"}
{"question": "What is the average age of all players in the NBA", "sql": "SELECT AVG(AGE) as average_age FROM nba_roster WHERE AGE IS NOT NULL"}
{"question": "What are the teams with the highest average salaries in the NBA", "sql": "SELECT team, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as avg_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY team ORDER BY avg_salary DESC"}
{"question": "What is the average height of NBA players", "sql": "SELECT AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER) + CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as average_height FROM nba_roster"}
{"question": "What is the average salary of all NBA players", "sql": "SELECT AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary FROM nba_roster"}
{"question": "What is the average age of the players on the Toronto Raptors", "sql": "SELECT AVG(AGE) FROM nba_roster WHERE team='Toronto Raptors';"}
{"question": "Which three teams have the most players from a single college", "sql": "SELECT team, COLLEGE, COUNT(*) AS num_players FROM nba_roster GROUP BY team, COLLEGE ORDER BY num_players DESC LIMIT 3"}
{"question": "What is the average salary of NBA players 25 years or older", "sql": "SELECT AVG(CAST(SUBSTR(SALARY, 1, INSTR(SALARY, '$')-1) AS INTEGER)) FROM nba_roster WHERE AGE >= 25"}
{"question": "What is the total salary of all NBA players", "sql": "SELECT SUM(CAST(SUBSTR(SALARY, 1, INSTR(SALARY, '$')-1) AS INTEGER)*1000000) FROM nba_roster"}
{"question": "What are the most common positions in the NBA", "sql": "SELECT POS, COUNT(*) AS num_players FROM nba_roster GROUP BY POS;"}
{"question": "What is the average salary for each age group in the NBA", "sql": "SELECT AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary, AGE as age_group FROM nba_roster WHERE SALARY!= '--' GROUP BY AGE ORDER BY age_group"}
{"question": "What are the top 5 colleges that have produced the most NBA players", "sql": "SELECT COLLEGE, COUNT(*) as count FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY count DESC LIMIT 5"}
{"question": "What is the most common position for players under the age of 25 in the NBA", "sql": "SELECT POS, COUNT(*) as count FROM nba_roster WHERE AGE <= 25 GROUP BY POS ORDER BY count DESC LIMIT 1"}
{"question": "How many players in the NBA are 5 years or younger than the oldest player in the league", "sql": "SELECT COUNT(*) FROM nba_roster WHERE AGE + 5 <= (SELECT MAX(AGE) FROM nba_roster);"}
{"question": "What are the top 5 colleges that have produced the most NBA players", "sql": "SELECT COLLEGE, COUNT(*) as count FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY count DESC LIMIT 5"}
{"question": "What are the most common positions in the NBA", "sql": "SELECT POS, COUNT(*) as count FROM nba_roster GROUP BY POS ORDER BY count DESC"}
{"question": "What is the average age of all players in the NBA", "sql": "SELECT AVG(AGE) FROM nba_roster"}
{"question": "What are the most common heights in the NBA", "sql": "SELECT HT, COUNT(*) AS frequency FROM nba_roster GROUP BY HT ORDER BY frequency DESC LIMIT 5"}
{"question": "What are the most common positions in the NBA", "sql": "SELECT POS, COUNT(*) as count FROM nba_roster GROUP BY POS ORDER BY count DESC"}
{"question": "What is the average salary for each team in the NBA, excluding teams with unknown salaries", "sql": "SELECT TEAM, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY TEAM ORDER BY average_salary DESC"}
{"question": "What is the college that has produced the most NBA players", "sql": "SELECT COLLEGE, COUNT(*) as count FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY count DESC LIMIT 1"}
{"question": "Who is the highest paid player in the NBA", "sql": "SELECT name, salary FROM nba_roster WHERE salary!= '--' ORDER BY CAST(REPLACE(REPLACE(salary, '$', ''), ',', '') AS INTEGER) DESC LIMIT 1"}
{"question": "What is the average age of players who are 6'8", "sql": "SELECT AVG(AGE) FROM nba_roster WHERE CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER) = 68"}
{"question": "What is the average age of the players in the NBA who are more than 5 years older than the average age of all players", "sql": "SELECT AVG(AGE) FROM nba_roster WHERE AGE + (SELECT AVG(AGE) FROM nba_roster) > 5*12"}
{"question": "What is the average age of the players in the NBA who are older than 5 years old", "sql": "SELECT AVG(AGE) FROM nba_roster WHERE AGE > 5*12"}
{"question": "What are the top colleges that produce the most NBA players", "sql": "SELECT COLLEGE, COUNT(*) as num_players FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY num_players DESC;"}
{"question": "How many players in the NBA are 6'8", "sql": "SELECT COUNT(*) FROM nba_roster WHERE CAST(SUBSTR(HT, 1, INSTR(HT,'')-1) AS INTEGER) = 68;"}
{"question": "What is the average salary for each team in the NBA", "sql": "SELECT Team, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary FROM nba_roster GROUP BY Team"}
{"question": "What are the top colleges represented in the NBA", "sql": "SELECT COLLEGE, COUNT(*) as num_players FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY num_players DESC;"}
{"question": "What is the most represented college in the NBA", "sql": "SELECT COLLEGE, COUNT(*) as count FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY count DESC LIMIT 1"}
{"question": "What are the 5 teams with the highest average salary in the NBA", "sql": "SELECT team, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) AS average_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY team ORDER BY average_salary DESC"}
{"question": "What is the average age of players in the NBA", "sql": "SELECT AVG(AGE) FROM nba_roster"}
{"question": "What is the most common height in the NBA", "sql": "SELECT SUBSTR(HT, 1, INSTR(HT,'')-1) as height, COUNT(*) as count FROM nba_roster GROUP BY SUBSTR(HT, 1, INSTR(HT,'')-1) ORDER BY count DESC LIMIT 1"}
{"question": "What is the position with the most players in the NBA", "sql": "SELECT POS, COUNT(*) as count FROM nba_roster GROUP BY POS ORDER BY count DESC LIMIT 1"}
{"question": "What is the 75th percentile salary in the NBA", "sql": "SELECT HT, AVG(WT) as avg_weight FROM nba_roster WHERE HT IS NOT NULL AND WT IS NOT NULL GROUP BY HT ORDER BY avg_weight DESC LIMIT 1"}
{"question": "Which college has produced the most NBA players", "sql": "SELECT COLLEGE, COUNT(*) as count FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY count DESC LIMIT 1"}
{"question": "What is the average salary of NBA players who are older than 25 years old", "sql": "SELECT AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary FROM nba_roster WHERE AGE > 25"}
{"question": "What is the average age of the players on the Toronto Raptors", "sql": "SELECT AVG(AGE) FROM nba_roster WHERE TEAM = 'Toronto Raptors';"}
{"question": "What is the average height of the players on the Los Angeles Lakers", "sql": "SELECT AVG(CAST(SUBSTR(HT, 1, INSTR(HT,'')-1) AS INTEGER) + CAST(SUBSTR(HT, INSTR(HT,'')+1) AS FLOAT)/12) AS height FROM nba_roster WHERE TEAM = 'Los Angeles Lakers';"}
{"question": "What is the position with the most players in the NBA", "sql": "SELECT POS, COUNT(*) as count FROM nba_roster GROUP BY POS ORDER BY count DESC LIMIT 1"}
{"question": "What is the average age of all players in the NBA who are older than 5 years old", "sql": "SELECT AVG(AGE) as average_age FROM nba_roster WHERE AGE > 5"}
{"question": "How many players on each team have a height of 6'8", "sql": "SELECT team, COUNT(*) as num_players FROM nba_roster WHERE CAST(SUBSTRING(HT, 1, INSTR(HT,'')-1) AS INTEGER) = 68 GROUP BY team"}
{"question": "What is the 99th percentile salary in the NBA?", "answer": "46741590", "sql": "SELECT (CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as percentile FROM nba_roster WHERE SALARY!= '--' order by percentile limit 1 offset (select count(*) from nba_roster where SALARY != '--')*99/100-1;"}
{"question": "What is the 75th percentile salary in the NBA?", "answer": "13932008", "sql": "SELECT (CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as percentile FROM nba_roster WHERE SALARY!= '--' order by percentile limit 1 offset (select count(*) from nba_roster where SALARY != '--')*75/100-1;"}
{"question": "What is the 25th percentile salary in the NBA?", "answer": "2413304", "sql": "SELECT (CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as percentile FROM nba_roster WHERE SALARY!= '--' order by percentile limit 1 offset (select count(*) from nba_roster where SALARY != '--')*25/100-1;"}
{"question": "What is the median weight in the NBA?", "answer": "215", "sql": "select CAST(SUBSTR(WT, 1, INSTR(WT,' ')) as INTEGER) as percentile from nba_roster order by percentile limit 1 offset (select count(*) from nba_roster)/2;"}
{"question": "What is the average weight in the NBA?", "answer": "214.98", "sql": "SELECT AVG(CAST(SUBSTR(WT, 1, INSTR(WT,' ')) as INTEGER)) FROM nba_roster;"}
{"question": "What is the median height in the NBA?", "answer": "6.58333333333333", "sql": "select CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12 as percentile from nba_roster order by percentile limit 1 offset (select count(*) from nba_roster)/2;"}
{"question": "What is the average height in the NBA?", "answer": "6.54986111111111", "sql": "select AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as height from nba_roster;"}
{"question": "Can you tell me how many players are in the NBA?", "answer": "600", "sql": "select count(*) from nba_roster;"}
{"question": "Would you please let me know what the highest paid players are for each position?", "answer": "The highest paid players are Nikola Jokic (C), Paul George (F), Norman Powell (G), Kevin Durant (PF), Stephen Curry (PG), LeBron James (SF), Bradley Beal (SG).", "sql": "SELECT name, pos, MAX(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as max_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY POS;"}
{"question": "Is Jalen Johnson 23 years old?", "answer": "No, Jalen Johnson is 21 years old", "sql": "Select name, age from nba_roster where name='Jalen Johnson';"}
{"question": "Who is the oldest player on the Brooklyn Nets?", "answer": "Spencer Dinwiddie, Dorian Finney-Smith, Royce O'Neale", "sql": "SELECT NAME FROM nba_roster WHERE TEAM = 'Brooklyn Nets' AND AGE = (SELECT MAX(AGE) FROM nba_roster WHERE TEAM = 'Brooklyn Nets');"}
{"question": "Who has the highest salary on the Memphis Grizzlies?", "answer": "Ja Morant", "sql": "select salary, name from nba_roster where team='Memphis Grizzlies' and SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "Which player has the highest salary on the Cleveland Cavaliers?", "answer": "Darius Garland", "sql": "select salary, name from nba_roster where team='Cleveland Cavaliers' and SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "Who is the highest paid center on the Dallas Mavericks?", "answer": "Dereck Lively II", "sql": "select salary, name from nba_roster where team='Dallas Mavericks' and POS='C' and SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "How much is Marcus Smart getting paid?", "answer": "$18,833,712", "sql": "select salary from nba_roster where name='Marcus Smart';"}
{"question": "What's the average age of the Trail Blazers?", "answer": "24", "sql": "select avg(age) from nba_roster where team='Portland Trail Blazers';"}
{"question": "What's the median age of the NBA?", "answer": "25", "sql": "select CAST(AGE as INTEGER) as percentile from nba_roster order by percentile limit 1 offset (select count(*) from nba_roster)/2;"}
{"question": "What's the median age of the Miami Heat?", "answer": "26", "sql": "select CAST(AGE as INTEGER) as percentile from nba_roster where team='Miami Heat' order by percentile limit 1 offset (select count(*) from nba_roster where team='Miami Heat')/2;"}
{"question": "What are the 5 teams with the oldest average age in the NBA", "answer": "Golden State Warriors, Milwaukee Bucks, Miami Heat, LA Clippers, Phoenix Suns", "sql": "SELECT team, AVG(AGE) AS average_age FROM nba_roster GROUP BY team ORDER BY average_age DESC LIMIT 5;"}
{"question": "What is the average salary of Power Forward players in the NBA", "answer": "$10948045", "sql": "select avg(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary from nba_roster where POS = 'PF';"}
{"question": "What is the most common position in the NBA", "sql": "SELECT POS, COUNT(*) as count FROM nba_roster GROUP BY POS ORDER BY count DESC LIMIT 1"}
{"question": "What is the average height of NBA players", "sql": "SELECT AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER) + CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as average_height FROM nba_roster;"}
{"question": "What is the average salary of NBA players who are at least 5 years old", "sql": "SELECT AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary FROM nba_roster WHERE AGE > 5"}
{"question": "What is the average age of all players in the NBA", "sql": "SELECT AVG(AGE) FROM nba_roster"}
{"question": "What is the most common age range among NBA players", "sql": "SELECT AGE, COUNT(*) AS count FROM nba_roster GROUP BY AGE ORDER BY count DESC LIMIT 1"}
{"question": "What is the median weight in the NBA", "sql": "SELECT COLLEGE, COUNT(*) as count FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY count DESC LIMIT 1"}
{"question": "How many players in the NBA are at least 5 years older than the youngest player in the league", "sql": "SELECT COUNT(*) as num_players FROM nba_roster WHERE AGE - (SELECT MIN(AGE) FROM nba_roster) > 5"}
{"question": "What are the 5 colleges that have produced the most players in the NBA", "sql": "SELECT COLLEGE, COUNT(*) as num_players FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY num_players DESC LIMIT 5"}
{"question": "What are the most common positions in the NBA", "sql": "SELECT POS, COUNT(*) as count FROM nba_roster WHERE POS!= '--' GROUP BY POS ORDER BY count DESC"}
{"question": "What is the average age of all players in the NBA", "sql": "SELECT AVG(AGE) as average_age FROM nba_roster WHERE AGE IS NOT NULL"}
{"question": "What is the 99th percentile salary in the NBA?", "answer": "46741590", "sql": "SELECT (CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as percentile FROM nba_roster WHERE SALARY!= '--' order by percentile limit 1 offset (select count(*) from nba_roster where SALARY != '--')*99/100-1;"}
{"question": "What is the 75th percentile salary in the NBA?", "answer": "13932008", "sql": "SELECT (CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as percentile FROM nba_roster WHERE SALARY!= '--' order by percentile limit 1 offset (select count(*) from nba_roster where SALARY != '--')*75/100-1;"}
{"question": "What is the 25th percentile salary in the NBA?", "answer": "2413304", "sql": "SELECT (CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as percentile FROM nba_roster WHERE SALARY!= '--' order by percentile limit 1 offset (select count(*) from nba_roster where SALARY != '--')*25/100-1;"}
{"question": "What is the median weight in the NBA?", "answer": "215", "sql": "select CAST(SUBSTR(WT, 1, INSTR(WT,' ')) as INTEGER) as percentile from nba_roster order by percentile limit 1 offset (select count(*) from nba_roster)/2;"}
{"question": "What is the average weight in the NBA?", "answer": "214.98", "sql": "SELECT AVG(CAST(SUBSTR(WT, 1, INSTR(WT,' ')) as INTEGER)) FROM nba_roster;"}
{"question": "What is the median height in the NBA?", "answer": "6.58333333333333", "sql": "select CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12 as percentile from nba_roster order by percentile limit 1 offset (select count(*) from nba_roster)/2;"}
{"question": "What is the average height in the NBA?", "answer": "6.54986111111111", "sql": "select AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as height from nba_roster;"}
{"question": "Can you tell me how many players are in the NBA?", "answer": "600", "sql": "select count(*) from nba_roster;"}
{"question": "Would you please let me know what the highest paid players are for each position?", "answer": "The highest paid players are Nikola Jokic (C), Paul George (F), Norman Powell (G), Kevin Durant (PF), Stephen Curry (PG), LeBron James (SF), Bradley Beal (SG).", "sql": "SELECT name, pos, MAX(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as max_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY POS;"}
{"question": "Is Jalen Johnson 23 years old?", "answer": "No, Jalen Johnson is 21 years old", "sql": "Select name, age from nba_roster where name='Jalen Johnson';"}
{"question": "Who is the oldest player on the Brooklyn Nets?", "answer": "Spencer Dinwiddie, Dorian Finney-Smith, Royce O'Neale", "sql": "SELECT NAME FROM nba_roster WHERE TEAM = 'Brooklyn Nets' AND AGE = (SELECT MAX(AGE) FROM nba_roster WHERE TEAM = 'Brooklyn Nets');"}
{"question": "Who has the highest salary on the Memphis Grizzlies?", "answer": "Ja Morant", "sql": "select salary, name from nba_roster where team='Memphis Grizzlies' and SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "Which player has the highest salary on the Cleveland Cavaliers?", "answer": "Darius Garland", "sql": "select salary, name from nba_roster where team='Cleveland Cavaliers' and SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "Who is the highest paid center on the Dallas Mavericks?", "answer": "Dereck Lively II", "sql": "select salary, name from nba_roster where team='Dallas Mavericks' and POS='C' and SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "How much is Marcus Smart getting paid?", "answer": "$18,833,712", "sql": "select salary from nba_roster where name='Marcus Smart';"}
{"question": "What's the average age of the Trail Blazers?", "answer": "24", "sql": "select avg(age) from nba_roster where team='Portland Trail Blazers';"}
{"question": "What's the median age of the NBA?", "answer": "25", "sql": "select CAST(AGE as INTEGER) as percentile from nba_roster order by percentile limit 1 offset (select count(*) from nba_roster)/2;"}
{"question": "What's the median age of the Miami Heat?", "answer": "26", "sql": "select CAST(AGE as INTEGER) as percentile from nba_roster where team='Miami Heat' order by percentile limit 1 offset (select count(*) from nba_roster where team='Miami Heat')/2;"}
{"question": "What are the 5 teams with the oldest average age in the NBA", "answer": "Golden State Warriors, Milwaukee Bucks, Miami Heat, LA Clippers, Phoenix Suns", "sql": "SELECT team, AVG(AGE) AS average_age FROM nba_roster GROUP BY team ORDER BY average_age DESC LIMIT 5;"}
{"question": "What is the average salary of Power Forward players in the NBA", "answer": "$10948045", "sql": "select avg(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary from nba_roster where POS = 'PF';"}
@@ -0,0 +1,330 @@
{"question": "What college has the most players in the NBA who are 30 years old or older", "sql": "SELECT COLLEGE, COUNT(*) AS count FROM nba_roster WHERE AGE >= 30 GROUP BY COLLEGE ORDER BY count DESC LIMIT 1;"}
{"question": "What is the total salary of all NBA players", "sql": "SELECT SUM(CAST(SUBSTR(SALARY, 1, INSTR(SALARY, '$')-1) AS INTEGER)*1000000) FROM nba_roster;"}
{"question": "What are the most common positions in the NBA", "sql": "SELECT POS, COUNT(*) AS num_players FROM nba_roster GROUP BY POS;"}
{"question": "What is the average salary for each age group in the NBA", "sql": "SELECT AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary, AGE as age_group FROM nba_roster WHERE SALARY!= '--' GROUP BY AGE ORDER BY age_group;"}
{"question": "What are the top 5 colleges that have produced the most NBA players", "sql": "SELECT COLLEGE, COUNT(*) as count FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY count DESC LIMIT 5;"}
{"question": "How many players in the NBA attended college", "sql": "SELECT COUNT(*) AS num_college_players FROM nba_roster WHERE COLLEGE!= '--';"}
{"question": "What are the top 3 colleges with the most players in the NBA", "sql": "SELECT COLLEGE, COUNT(*) as count FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY count DESC LIMIT 3;"}
{"question": "What is the average age of all players in the NBA", "sql": "SELECT AVG(AGE) FROM nba_roster;"}
{"question": "What is the most represented college in the NBA", "sql": "SELECT COLLEGE, COUNT(*) as count FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY count DESC LIMIT 1;"}
{"question": "Which college has produced the most NBA players", "sql": "SELECT COLLEGE, COUNT(*) as count FROM nba_roster GROUP BY COLLEGE ORDER BY count DESC LIMIT 1;"}
{"question": "What is the average height of NBA players", "sql": "SELECT AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER) + CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) AS average_height FROM nba_roster;"}
{"question": "What is the average age of players on each team in the NBA", "sql": "SELECT team, AVG(AGE) as avg_age FROM nba_roster WHERE SALARY!= '--' GROUP BY team;"}
{"question": "What are the top 3 positions with the highest total salary expenditure in the NBA", "sql": "SELECT pos, name, SUM(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as total_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY pos ORDER BY total_salary DESC LIMIT 3;"}
{"question": "Which colleges have the most players in the NBA", "sql": "SELECT COLLEGE, COUNT(*) AS num_players FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY num_players DESC;"}
{"question": "What is the average salary for each team in the NBA", "sql": "SELECT team, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as avg_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY team;"}
{"question": "What are the teams with the highest average salaries in the NBA", "sql": "SELECT team, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as avg_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY team ORDER BY avg_salary DESC;"}
{"question": "What are the 5 colleges that have produced the most players in the NBA", "sql": "SELECT COLLEGE, COUNT(*) as num_players FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY num_players DESC LIMIT 5;"}
{"question": "What is the most common position in the NBA", "sql": "SELECT POS, COUNT(*) as count FROM nba_roster GROUP BY POS ORDER BY count DESC LIMIT 1;"}
{"question": "What is the average salary of Power Forwards in the NBA who are at least 25 years old", "sql": "SELECT AVG(CAST(SUBSTR(SALARY, 1, INSTR(SALARY, '$')-1) AS INTEGER)) AS average_salary FROM nba_roster WHERE AGE >= 25 AND POS = 'PF';"}
{"question": "What is the average age of 6-foot Power Forwards in the NBA", "sql": "SELECT AVG(AGE) FROM nba_roster WHERE CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER) = 6 AND POS='PF';"}
{"question": "What is the name of the player with the highest average weight among Power Forwards in the NBA", "sql": "SELECT NAME, AVG(CAST(SUBSTR(WT, 1, INSTR(WT,' ')) AS INTEGER)) AS avg_weight FROM nba_roster WHERE POS='PF' GROUP BY NAME ORDER BY avg_weight DESC LIMIT 1;"}
{"question": "Which team has the most players in the NBA", "sql": "SELECT team, COUNT(*) as num_players FROM nba_roster GROUP BY team ORDER BY num_players DESC LIMIT 1;"}
{"question": "What is the average salary of all NBA players", "sql": "SELECT AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary FROM nba_roster;"}
{"question": "What is the average age of the players on the Toronto Raptors", "sql": "SELECT AVG(AGE) FROM nba_roster WHERE team='Toronto Raptors';"}
{"question": "Which three teams have the most players from a single college", "sql": "SELECT team, COLLEGE, COUNT(*) AS num_players FROM nba_roster GROUP BY team, COLLEGE ORDER BY num_players DESC LIMIT 3;"}
{"question": "How many players in the NBA are at least 5 years older than the youngest player in the league", "sql": "SELECT COUNT(*) as num_players FROM nba_roster WHERE AGE - (SELECT MIN(AGE) FROM nba_roster) > 5;"}
{"question": "What is the average salary of NBA players who are 25 years or older", "sql": "SELECT AVG(CAST(SUBSTR(SALARY, 1, INSTR(SALARY, '$') - 1) as INTEGER)) FROM nba_roster WHERE CAST(AGE as INTEGER) >= 25;"}
{"question": "What is the number of players on each team in the NBA", "sql": "SELECT Team, COUNT(*) as num_players FROM nba_roster GROUP BY Team;"}
{"question": "What is the average salary for each position in the NBA, excluding players with unknown salaries", "sql": "SELECT POS, AVG(CAST(SUBSTR(SALARY, 1, INSTR(SALARY, '$') - 1) as INTEGER)) as avg_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY POS;"}
{"question": "What are the oldest players on each team with a roster size of 6 or more", "sql": "SELECT NAME FROM nba_roster WHERE AGE IN (SELECT MAX(AGE) FROM nba_roster WHERE TEAM IN (SELECT TEAM FROM nba_roster GROUP BY TEAM HAVING COUNT(*) > 5));"}
{"question": "What is the average height of the players on the Toronto Raptors", "sql": "SELECT AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as height FROM nba_roster WHERE team='Toronto Raptors';"}
{"question": "What is the highest-paid Toronto Raptors player who attended college", "sql": "SELECT name, salary FROM nba_roster WHERE team='Toronto Raptors' AND COLLEGE!='--' AND SALARY!='--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "What is the median weight in the NBA", "sql": "SELECT NAME, COLLEGE, COUNT(*) as num_colleges FROM nba_roster WHERE COLLEGE!= '--' GROUP BY NAME, COLLEGE ORDER BY num_colleges DESC;"}
{"question": "Who are the top 5 highest-paid players in the NBA", "sql": "SELECT * FROM nba_roster WHERE SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 5;"}
{"question": "What is the average height of players on each NBA team", "sql": "SELECT team, AVG(CAST(SUBSTRING(HT, 1, INSTR(HT,'')-1) AS INTEGER) + CAST(SUBSTRING(HT, INSTR(HT,'')+1) AS INTEGER) / 12.0) as avg_height FROM nba_roster WHERE HT!= 'NA' GROUP BY team;"}
{"question": "Who are the top 3 highest-paid players in the NBA", "sql": "SELECT name, SUM(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as total_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY name ORDER BY total_salary DESC LIMIT 3;"}
{"question": "What is the average salary of NBA players", "sql": "SELECT AVG(CAST(SUBSTR(SALARY, 1, INSTR(SALARY, '$')-1) AS INTEGER)) FROM nba_roster WHERE SALARY!= '--';"}
{"question": "How many players in the NBA are 68 inches tall", "sql": "SELECT COUNT(*) FROM nba_roster WHERE CAST(SUBSTR(HT, 1, INSTR(HT,'')-1) AS INTEGER) = 68;"}
{"question": "What are the top 5 teams with the oldest average age of players", "sql": "SELECT team, AVG(AGE) AS average_age FROM nba_roster GROUP BY team ORDER BY average_age DESC LIMIT 5;"}
{"question": "What is the average salary of the Los Angeles Lakers players", "sql": "SELECT AVG(CAST(SALARY AS INTEGER) ) AS average_salary FROM nba_roster WHERE team='Los Angeles Lakers';"}
{"question": "What is the college that has produced the most players currently playing for the Boston Celtics", "sql": "SELECT COLLEGE, COUNT(*) AS count FROM nba_roster WHERE team='Boston Celtics' GROUP BY COLLEGE ORDER BY count DESC LIMIT 1;"}
{"question": "What is the most common position for players under the age of 25 in the NBA", "sql": "SELECT POS, COUNT(*) as count FROM nba_roster WHERE AGE <= 25 GROUP BY POS ORDER BY count DESC LIMIT 1;"}
{"question": "What is the average height of players on each NBA team, excluding players with unknown heights", "sql": "SELECT TEAM, AVG(CAST(SUBSTRING(HT, 0, INSTR(HT,'')-1) AS INTEGER)) as avg_height FROM nba_roster WHERE HT!= 'NA' GROUP BY TEAM ORDER BY avg_height DESC;"}
{"question": "What are the 5 most common heights among NBA players", "sql": "SELECT HT, COUNT(*) AS count FROM nba_roster GROUP BY HT ORDER BY count DESC LIMIT 5;"}
{"question": "What are the top 5 colleges with the most players in the NBA", "sql": "SELECT COLLEGE, COUNT(*) AS count FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY count DESC LIMIT 5;"}
{"question": "What is the average height of NBA players who are 25 years or older", "sql": "SELECT AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as height FROM nba_roster WHERE age >= 25;"}
{"question": "What are the top 3 teams with the highest average salaries in the NBA", "sql": "SELECT team, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as avg_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY team ORDER BY avg_salary DESC LIMIT 3;"}
{"question": "What is the position with the most players in the NBA", "sql": "SELECT POS, COUNT(*) as count FROM nba_roster WHERE SALARY!= '--' GROUP BY POS ORDER BY count DESC LIMIT 1;"}
{"question": "What is the average salary of NBA players who are at least 5 years old", "sql": "SELECT AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary FROM nba_roster WHERE AGE > 5;"}
{"question": "What is the most common age range among NBA players", "sql": "SELECT AGE, COUNT(*) AS count FROM nba_roster GROUP BY AGE ORDER BY count DESC LIMIT 1;"}
{"question": "What is the average salary of NBA players who are 25 years old or older", "sql": "SELECT AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary FROM nba_roster WHERE AGE > 25;"}
{"question": "What is the average age of the players in the NBA who are more than 5 years older than the average age of all players", "sql": "SELECT AVG(AGE) FROM nba_roster WHERE AGE + (SELECT AVG(AGE) FROM nba_roster) > 5*12;"}
{"question": "What is the average age of the players in the NBA who are older than 5 years old", "sql": "SELECT AVG(AGE) FROM nba_roster WHERE AGE > 5*12;"}
{"question": "What colleges have produced the most NBA players", "sql": "SELECT COLLEGE, COUNT(*) as num_players FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY num_players DESC;"}
{"question": "Who is the highest paid player in the NBA", "sql": "SELECT name, salary FROM nba_roster WHERE salary!= '--' ORDER BY CAST(REPLACE(REPLACE(salary, '$', ''), ',', '') AS INTEGER) DESC LIMIT 1;"}
{"question": "How many players in the NBA are 5 years or younger than the oldest player in the league", "sql": "SELECT COUNT(*) FROM nba_roster WHERE AGE + 5 <= (SELECT MAX(AGE) FROM nba_roster);"}
{"question": "What are the 5 teams with the highest average salary in the NBA", "sql": "SELECT team, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) AS average_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY team ORDER BY average_salary DESC;"}
{"question": "What is the average salary for each team in the NBA, excluding teams with unknown salaries", "sql": "SELECT TEAM, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY TEAM ORDER BY average_salary DESC;"}
{"question": "How many players in the NBA are 10 years old or older", "sql": "SELECT COUNT(*) AS num_players FROM nba_roster WHERE age + (JULIANDAY('now') - JULIANDAY(DATE('now', '-10 year'))) / 365.25 >= 10;"}
{"question": "How many players on the Toronto Raptors are 6 feet 8 inches tall", "sql": "SELECT COUNT(*) AS num_players FROM nba_roster WHERE team='Toronto Raptors' AND CAST(SUBSTRING(HT, 0, INSTR(HT,'')-1) AS INTEGER) = '6' || '8';"}
{"question": "How many players in the NBA are over the age of 25", "sql": "SELECT COUNT(*) FROM nba_roster WHERE AGE > 25;"}
{"question": "What is the average height of NBA players under the age of 25", "sql": "SELECT AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as average_height FROM nba_roster WHERE AGE <= 25;"}
{"question": "What is the total salary of all players in the NBA who are more than 5 years older than the average age of all players", "sql": "SELECT SUM(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as total_salary FROM nba_roster WHERE (AGE - (SELECT AVG(AGE) FROM nba_roster)) > 5;"}
{"question": "What is the most common height in the NBA", "sql": "SELECT SUBSTR(HT, 1, INSTR(HT,'')-1) as height, COUNT(*) as count FROM nba_roster GROUP BY SUBSTR(HT, 1, INSTR(HT,'')-1) ORDER BY count DESC LIMIT 1;"}
{"question": "What is the average salary of NBA players 25 years or older", "sql": "SELECT AVG(CAST(SUBSTR(SALARY, 1, INSTR(SALARY, '$')-1) AS INTEGER)) FROM nba_roster WHERE AGE >= 25;"}
{"question": "What are the 5 most common heights in the NBA", "sql": "SELECT HT, COUNT(*) AS frequency FROM nba_roster GROUP BY HT ORDER BY frequency DESC LIMIT 5;"}
{"question": "What is the average height of the players on the Los Angeles Lakers", "sql": "SELECT AVG(CAST(SUBSTR(HT, 1, INSTR(HT,'')-1) AS INTEGER) + CAST(SUBSTR(HT, INSTR(HT,'')+1) AS FLOAT)/12) AS height FROM nba_roster WHERE TEAM = 'Los Angeles Lakers';"}
{"question": "What is the average age of all players in the NBA who are older than 5 years old", "sql": "SELECT AVG(AGE) as average_age FROM nba_roster WHERE AGE > 5;"}
{"question": "What is the most popular college attended by NBA players", "sql": "SELECT POS, COUNT(*) as count FROM nba_roster WHERE COLLEGE!= '--' GROUP BY POS ORDER BY count DESC LIMIT 1;"}
{"question": "What is the average height for each position in the NBA", "sql": "SELECT POS, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,'')-1) as INTEGER)) AS average_height FROM nba_roster GROUP BY POS ORDER BY average_height;"}
{"question": "What are the jersey numbers of the first 5 players in the NBA roster", "sql": "SELECT NAME, JERSEY FROM nba_roster ORDER BY JERSEY LIMIT 5;"}
{"question": "What is the age range of the players in the NBA", "sql": "SELECT MIN(AGE) as youngest_player, MAX(AGE) as oldest_player FROM nba_roster;"}
{"question": "What is the total salary for each team in the NBA", "sql": "SELECT team, SUM(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as total_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY team;"}
{"question": "What are the top 5 teams in the NBA with the highest average salary", "sql": "SELECT team, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as avg_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY team ORDER BY avg_salary DESC LIMIT 5;"}
{"question": "What are the top 5 highest-paid players in the NBA", "sql": "SELECT NAME, CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) as total_salary FROM nba_roster WHERE SALARY!= '--' ORDER BY total_salary DESC LIMIT 5;"}
{"question": "What is the 99th percentile salary in the NBA", "sql": "SELECT COLLEGE, COUNT(*) as count FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY count DESC LIMIT 1;"}
{"question": "How many players are on the Toronto Raptors", "sql": "SELECT COUNT(*) AS num_players FROM nba_roster WHERE TEAM = 'Toronto Raptors';"}
{"question": "What are the 5 highest-paid players in the NBA", "sql": "SELECT * FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY SALARY DESC) AS row_num FROM nba_roster) AS temp_table WHERE row_num <= 5;"}
{"question": "Which players have had the most varied careers in the NBA, having played for the most different teams", "sql": "SELECT name, COUNT(DISTINCT team) as num_teams FROM nba_roster WHERE team!= 'NA' GROUP BY name ORDER BY num_teams DESC LIMIT 10;"}
{"question": "Which three teams have the most players under the age of 25", "sql": "SELECT Team, COUNT(*) as num_players FROM nba_roster WHERE AGE < 25 GROUP BY Team ORDER BY num_players DESC LIMIT 3;"}
{"question": "What are the colleges with the highest average salaries in the NBA", "sql": "SELECT college, COUNT(*) as count, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as avg_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY college ORDER BY avg_salary DESC;"}
{"question": "What is the name and jersey number of the player with the highest jersey number in the NBA", "sql": "SELECT NAME, JERSEY FROM nba_roster WHERE JERSEY!= 'NA' ORDER BY CAST(JERSEY AS INTEGER) DESC LIMIT 1;"}
{"question": "What is the average age of NBA players", "sql": "SELECT AVG(AGE) AS average_age FROM nba_roster;"}
{"question": "What are the top 3 teams with the oldest average age in the NBA", "sql": "SELECT TEAM, AVG(AGE) as average_age FROM nba_roster WHERE SALARY!= '--' GROUP BY TEAM ORDER BY average_age DESC LIMIT 3;"}
{"question": "Which colleges have multiple players in the NBA", "sql": "SELECT COUNT(*) AS college_players, COLLEGE FROM nba_roster GROUP BY COLLEGE HAVING COUNT(*) > 1;"}
{"question": "What is the average age of players on each NBA team", "sql": "SELECT team, AVG(CAST(AGE as INTEGER)) as avg_age FROM nba_roster GROUP BY team;"}
{"question": "What is the average salary of Power Forward players in the NBA", "sql": "SELECT age, COUNT(*) as count FROM nba_roster GROUP BY age ORDER BY count DESC;"}
{"question": "What is the team with the highest average salary for players over 25 years old", "sql": "SELECT team, AVG(CAST(SUBSTRING(SALARY, 2, LENGTH(SALARY)-2) AS INTEGER)) AS average_salary FROM nba_roster WHERE AGE > 25 AND SALARY!= '--' GROUP BY team ORDER BY average_salary DESC LIMIT 1;"}
{"question": "What is the age range of players in the NBA", "sql": "SELECT MIN(AGE) as youngest, MAX(AGE) as oldest FROM nba_roster;"}
{"question": "What is the most successful college in terms of producing NBA players", "sql": "SELECT COLLEGE, COUNT(*) as frequency FROM nba_roster GROUP BY COLLEGE ORDER BY frequency DESC LIMIT 1;"}
{"question": "What is the average salary of the Boston Celtics players", "sql": "SELECT AVG(CAST(SALARY AS INTEGER) ) AS average_salary FROM nba_roster WHERE team='Boston Celtics';"}
{"question": "Which colleges have produced the most NBA players", "sql": "SELECT COLLEGE, COUNT(*) AS count FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY count DESC;"}
{"question": "Who is the highest-paid player in the NBA", "sql": "SELECT NAME FROM nba_roster WHERE SALARY = (SELECT MAX(SALARY) FROM nba_roster);"}
{"question": "Which 5 players have the highest jersey numbers in the NBA", "sql": "SELECT name, jersey FROM nba_roster WHERE jersey!= 'NA' ORDER BY CAST(REPLACE(REPLACE(jersey, '0', ''), 'NA', '') AS INTEGER) DESC LIMIT 5;"}
{"question": "What are the names of the players who are older than 30 years old in the NBA", "sql": "SELECT name, age FROM nba_roster WHERE age > 30 ORDER BY age;"}
{"question": "How many players in the NBA are younger than the oldest player in the league by 25 years", "sql": "SELECT COUNT(*) FROM nba_roster WHERE AGE + 25 > (SELECT MAX(AGE) FROM nba_roster);"}
{"question": "Which 10 players have played for the most teams in their NBA careers", "sql": "SELECT name, COUNT(DISTINCT team) AS num_teams FROM nba_roster GROUP BY name ORDER BY num_teams DESC LIMIT 10;"}
{"question": "What is the average height for each height range in the NBA", "sql": "SELECT HT, COUNT(*) as count, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,'')-1) AS INTEGER)) as avg_height FROM nba_roster WHERE HT!= 'NA' GROUP BY HT;"}
{"question": "How many players in the NBA are 6 feet 8 inches tall", "sql": "SELECT COUNT(*) FROM nba_roster WHERE CAST(SUBSTRING(HT, 0, INSTR(HT,'')-1) AS INTEGER) = 68;"}
{"question": "What percentage of players in the NBA are 10 years or less away from the oldest player in the league", "sql": "SELECT COUNT(*) FROM nba_roster WHERE AGE + 10 <= (SELECT MAX(AGE) FROM nba_roster);"}
{"question": "What is the college that has produced the most NBA players", "sql": "SELECT COLLEGE, COUNT(*) AS count FROM nba_roster GROUP BY COLLEGE ORDER BY count DESC LIMIT 1;"}
{"question": "What is the average salary of the youngest players on each NBA team", "sql": "SELECT team, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) AS average_salary FROM nba_roster WHERE AGE <= 22 GROUP BY team ORDER BY average_salary DESC LIMIT 1;"}
{"question": "What is the average age of players in the NBA who have a publicly disclosed salary", "sql": "SELECT AVG(AGE) as average_age FROM nba_roster WHERE SALARY!= '--';"}
{"question": "What is the average salary for each position in the NBA", "sql": "SELECT AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary, POS FROM nba_roster WHERE SALARY!= '--' GROUP BY POS ORDER BY average_salary DESC;"}
{"question": "What is the average age of players in the NBA who are at least 60 years old", "sql": "SELECT AVG(AGE) as average_age FROM nba_roster WHERE AGE > 5*12;"}
{"question": "Who are the 10 tallest players in the NBA", "sql": "SELECT HT, NAME FROM nba_roster ORDER BY CAST(SUBSTRING(HT, 0, INSTR(HT,'')-1) AS INTEGER) DESC LIMIT 10;"}
{"question": "Which NBA team has the most players under the age of 25", "sql": "SELECT team, COUNT(*) as num_players FROM nba_roster WHERE AGE <= 25 GROUP BY team ORDER BY num_players DESC LIMIT 1;"}
{"question": "What is the average age of players from each college, excluding those who did not attend college, listed in order from oldest to youngest", "sql": "SELECT COLLEGE, AVG(AGE) as average_age FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY average_age DESC;"}
{"question": "What is the average salary for each position in the NBA, with the highest-paid positions listed first", "sql": "SELECT POS, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY POS ORDER BY average_salary DESC;"}
{"question": "What is the average height of NBA players 25 years old or older", "sql": "SELECT AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as height FROM nba_roster WHERE AGE >= 25;"}
{"question": "What are the top 10 colleges with the most players in the NBA", "sql": "SELECT college, COUNT(*) as num_players FROM nba_roster WHERE college!= '--' GROUP BY college ORDER BY num_players DESC LIMIT 10;"}
{"question": "What is the average height of all players in the NBA", "sql": "SELECT AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)) as average_height FROM nba_roster;"}
{"question": "What are the top 5 colleges that produce the highest-paid NBA players", "sql": "SELECT COLLEGE, AVG(CAST(SUBSTR(SALARY, 2, LENGTH(SALARY)-2) AS INTEGER)) as average_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY COLLEGE ORDER BY average_salary DESC LIMIT 5;"}
{"question": "Which teams have the most players under 6'8", "sql": "SELECT team, COUNT(*) as num_players FROM nba_roster WHERE HT!= 'NA' AND CAST(SUBSTRING(HT, 0, INSTR(HT,'')-1) AS INTEGER) < 68 GROUP BY team;"}
{"question": "What is the number of players in the NBA who are 25 years old or younger", "sql": "SELECT COUNT(*) FROM nba_roster WHERE AGE <= 25;"}
{"question": "What is the team with the highest average salary in the NBA", "sql": "SELECT team, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) AS average_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY team ORDER BY average_salary DESC LIMIT 1;"}
{"question": "What are the average heights for each position in the NBA, from tallest to shortest", "sql": "SELECT AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER) + CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as average_height, POS FROM nba_roster GROUP BY POS ORDER BY average_height DESC;"}
{"question": "How many players in the NBA are over the age of 30", "sql": "SELECT COUNT(*) as num_players FROM nba_roster WHERE AGE > 30;"}
{"question": "Who is the tallest player in the NBA", "sql": "SELECT NAME, HT FROM nba_roster ORDER BY LENGTH(HT) DESC LIMIT 1;"}
{"question": "What are the top 3 teams in the NBA with the highest average salary", "sql": "SELECT team, AVG(CAST(SUBSTR(SALARY, 1, INSTR(SALARY, '$') - 1) AS INTEGER)) AS avg_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY team ORDER BY avg_salary DESC LIMIT 3;"}
{"question": "Which team has the highest average salary in the NBA", "sql": "SELECT team, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY team ORDER BY average_salary DESC LIMIT 1;"}
{"question": "What is the total number of players in the NBA who have attended a college other than '--'?", "sql": "SELECT COUNT(*) as num_players FROM nba_roster WHERE COLLEGE!= '--';"}
{"question": "Who is the player who has played for the most teams in their NBA career", "sql": "SELECT NAME, COUNT(DISTINCT TEAM) AS num_teams FROM nba_roster WHERE SALARY!= '--' GROUP BY NAME ORDER BY num_teams DESC LIMIT 1;"}
{"question": "What are the top 10 highest-paid college-educated players in the NBA", "sql": "SELECT name, SUM(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) AS total_salary FROM nba_roster WHERE COLLEGE!= '--' GROUP BY name ORDER BY total_salary DESC LIMIT 10;"}
{"question": "Which NBA players have attended multiple colleges", "sql": "SELECT NAME, COLLEGE FROM nba_roster WHERE COLLEGE!= '--' GROUP BY NAME, COLLEGE HAVING COUNT(COLLEGE) > 1;"}
{"question": "What are the 5 teams with the tallest average height in the NBA", "sql": "SELECT team, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,'')-1) AS INTEGER)) AS average_height FROM nba_roster GROUP BY team ORDER BY average_height DESC;"}
{"question": "What is the average height of players in the NBA who are older than 25 years old", "sql": "SELECT AVG(CAST(SUBSTR(HT, 1, INSTR(HT,'')-1) AS INTEGER)) AS average_height FROM nba_roster WHERE AGE > 25;"}
{"question": "How many players are on the Toronto Raptors' roster", "sql": "SELECT COUNT(*) FROM nba_roster WHERE team='Toronto Raptors';"}
{"question": "What is the weight of the heaviest 75% of NBA players", "sql": "SELECT WT FROM nba_roster ORDER BY CAST(REPLACE(REPLACE(WT,'lbs', ''),'', '') AS INTEGER) DESC LIMIT 1 OFFSET (SELECT COUNT(*) FROM nba_roster WHERE WT!= '--') * 75 / 100 - 1;"}
{"question": "Who is the highest-paid player in the league, excluding those with unknown positions, salaries, or colleges", "sql": "SELECT name, salary FROM nba_roster WHERE POS!= 'NA' AND SALARY!= '--' AND COLLEGE!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "How many players in the NBA attended Duke, Kentucky, or North Carolina for college", "sql": "SELECT COUNT(*) AS count FROM nba_roster WHERE COLLEGE!= '--' AND COLLEGE IN ('Duke', 'Kentucky', 'North Carolina');"}
{"question": "What is the most common college represented in the NBA", "sql": "SELECT COLLEGE, COUNT(*) as frequency FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY frequency DESC LIMIT 1;"}
{"question": "What is the number of players in the NBA who attended a college other than '--'?", "sql": "SELECT COUNT(*) FROM nba_roster WHERE COLLEGE!= '--';"}
{"question": "How many players on the Toronto Raptors are 25 years old or older", "sql": "SELECT COUNT(*) AS num_players FROM nba_roster WHERE team='Toronto Raptors' AND AGE >= 25;"}
{"question": "How many players on the Toronto Raptors are 6'8", "sql": "SELECT COUNT(*) FROM nba_roster WHERE TEAM = 'Toronto Raptors' AND CAST(SUBSTRING(HT, 0, INSTR(HT,'')-1) AS INTEGER) = '6' || '8';"}
{"question": "What is the team with the most players over 30 years old in the NBA", "sql": "SELECT Team, COUNT(*) as num_players FROM nba_roster WHERE AGE > 30 GROUP BY Team ORDER BY num_players DESC LIMIT 1;"}
{"question": "What is the highest-paid Power Forward in the NBA", "sql": "SELECT POS, NAME, CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) as Salary FROM nba_roster WHERE SALARY!= '--' ORDER BY Salary DESC LIMIT 1 OFFSET (SELECT COUNT(*) FROM nba_roster WHERE SALARY!= '--' AND POS = 'PF')-1;"}
{"question": "How many players in the NBA are older than the average age of all players", "sql": "SELECT COUNT(*) FROM nba_roster WHERE AGE > (SELECT AVG(AGE) FROM nba_roster);"}
{"question": "What positions in the NBA tend to have the oldest average age", "sql": "SELECT POS, COUNT(*) AS count, AVG(AGE) AS average_age FROM nba_roster GROUP BY POS ORDER BY average_age DESC;"}
{"question": "Which players have more than 5 teammates with the same name", "sql": "SELECT NAME FROM nba_roster WHERE (SELECT COUNT(*) FROM nba_roster WHERE NAME = nba_roster.NAME AND TEAM = nba_roster.TEAM) > 5;"}
{"question": "Which teams have the most players in the NBA", "sql": "SELECT Team, COUNT(*) as num_players FROM nba_roster GROUP BY Team ORDER BY num_players DESC;"}
{"question": "What is the total salary of the most expensive team in the NBA", "sql": "SELECT Team, SUM(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as Total_Salary FROM nba_roster WHERE SALARY!= '--' GROUP BY Team ORDER BY Total_Salary DESC;"}
{"question": "How many players on the Boston Celtics are 6 feet 8 inches tall or taller", "sql": "SELECT COUNT(*) AS num_players FROM nba_roster WHERE team='Boston Celtics' AND CAST(SUBSTRING(HT, 0, INSTR(HT,'')-1) AS INTEGER) = '6' || '8';"}
{"question": "What are the most common colleges represented in the NBA", "sql": "SELECT COLLEGE, COUNT(*) as count FROM nba_roster GROUP BY COLLEGE ORDER BY count DESC;"}
{"question": "What are the 5 teams with the oldest average age in the NBA", "sql": "SELECT team, AVG(AGE) AS average_age, COUNT(*) AS num_players FROM nba_roster GROUP BY team HAVING COUNT(*) > 5 ORDER BY average_age DESC;"}
{"question": "How many players in the NBA are 6 feet tall", "sql": "SELECT COUNT(*) FROM nba_roster WHERE CAST(SUBSTR(HT, 1, INSTR(HT,'')-1) AS INTEGER) = 6;"}
{"question": "Who are the tallest players in the NBA", "sql": "SELECT NAME FROM nba_roster WHERE HT > (SELECT AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) FROM nba_roster);"}
{"question": "What are the ages of the youngest and oldest players in the NBA", "sql": "SELECT MIN(AGE) AS youngest_player, MAX(AGE) AS oldest_player FROM nba_roster;"}
{"question": "What are the 5 teams with the lightest average weight for players with known heights", "sql": "SELECT HT, WT, AVG(CAST(SUBSTR(WT, 1, LENGTH(WT)-3) AS INTEGER)) AS avg_weight FROM nba_roster WHERE HT!= 'NA' GROUP BY HT ORDER BY avg_weight DESC LIMIT 5;"}
{"question": "What are the top 5 positions with the tallest average height in the NBA", "sql": "SELECT POS, COUNT(*) AS count, AVG(CAST(SUBSTR(HT, 1, LENGTH(HT)-2) AS INTEGER)) AS avg_height FROM nba_roster WHERE HT!= 'NA' GROUP BY POS ORDER BY count DESC LIMIT 5;"}
{"question": "Which 5 players have played for the most teams in their NBA careers", "sql": "SELECT NAME, COUNT(DISTINCT team) AS num_teams FROM nba_roster GROUP BY NAME ORDER BY num_teams DESC LIMIT 5;"}
{"question": "What are the most common heights in the NBA", "sql": "SELECT HT, COUNT(*) as count FROM nba_roster GROUP BY HT ORDER BY count DESC LIMIT 10;"}
{"question": "How many players on the Los Angeles Lakers are 6 feet 8 inches tall", "sql": "SELECT COUNT(*) as num_players FROM nba_roster WHERE team='Los Angeles Lakers' AND CAST(SUBSTR(HT, 1, INSTR(HT,'')-1) AS INTEGER) = '6' || '8';"}
{"question": "What are the most common positions for players under the age of 25 in the NBA", "sql": "SELECT POS, COUNT(*) as count FROM nba_roster WHERE AGE < 25 GROUP BY POS ORDER BY count DESC;"}
{"question": "What are the top colleges that produce the most NBA players", "sql": "SELECT COLLEGE, COUNT(*) as count FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY count DESC;"}
{"question": "What are the colleges that have produced the most NBA players", "sql": "SELECT COLLEGE, COUNT(*) AS num_players FROM nba_roster GROUP BY COLLEGE ORDER BY num_players DESC;"}
{"question": "How many players in the NBA are 25 years or younger", "sql": "SELECT COUNT(*) FROM nba_roster WHERE AGE + 25 <= (SELECT MAX(AGE) FROM nba_roster);"}
{"question": "What is the average age of players from the college that has produced the youngest players in the NBA", "sql": "SELECT AVG(AGE) as average_age FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY average_age LIMIT 1;"}
{"question": "How many players in the NBA have attended Duke, Kentucky, North Carolina, or did not attend college", "sql": "SELECT COUNT(*) FROM nba_roster WHERE COLLEGE IN ('--', 'Duke', 'Kentucky', 'North Carolina');"}
{"question": "What are the teams with the most players from a particular college", "sql": "SELECT team, COLLEGE, COUNT(*) AS num_players FROM nba_roster GROUP BY team, COLLEGE ORDER BY num_players DESC;"}
{"question": "What is the number of players in the NBA who are older than 10 years old", "sql": "SELECT COUNT(*) FROM nba_roster WHERE (CAST(CAST(AGE AS INTEGER) AS REAL) > 10);"}
{"question": "What are the top 3 highest paid players from each college", "sql": "SELECT name, college, MAX(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as max_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY college ORDER BY max_salary DESC LIMIT 3;"}
{"question": "How many players in the NBA are at least 6 feet 8 inches tall", "sql": "SELECT COUNT(*) as num_players FROM nba_roster WHERE CAST(SUBSTR(HT, 1, INSTR(HT,'')-1) AS INTEGER) >= 68;"}
{"question": "Which NBA teams have the most players from a particular college", "sql": "SELECT Team, COLLEGE, COUNT(*) as Count FROM nba_roster WHERE COLLEGE!= '--' GROUP BY Team, COLLEGE ORDER BY Count DESC;"}
{"question": "What is the most common college attended by NBA players", "sql": "SELECT COLLEGE, COUNT(*) AS frequency FROM nba_roster GROUP BY COLLEGE ORDER BY frequency DESC LIMIT 1;"}
{"question": "What is the total salary of all NBA players, excluding those with unknown salaries", "sql": "SELECT SUM(CAST(SUBSTR(SALARY, 1, INSTR(SALARY, '$')-1) AS INTEGER)*1000000) AS total_salary FROM nba_roster WHERE SALARY!= '--';"}
{"question": "What are the teams with the tallest average height in the NBA", "sql": "SELECT team, AVG(LENGTH(HT)) AS average_height FROM nba_roster GROUP BY team ORDER BY average_height DESC;"}
{"question": "Which 10 players have played for the most teams in their NBA career", "sql": "SELECT name, COUNT(DISTINCT team) as num_teams FROM nba_roster WHERE SALARY!= '--' GROUP BY name ORDER BY num_teams DESC LIMIT 10;"}
{"question": "What is the average height of NBA players 25 years old or younger", "sql": "SELECT AVG(CAST(SUBSTR(HT, 1, INSTR(HT,'')) AS INTEGER)) AS average_height FROM nba_roster WHERE AGE <= 25;"}
{"question": "What is the average weight of NBA players", "sql": "SELECT AVG(CAST(SUBSTR(WT, 1, INSTR(WT,' ')-1) AS INTEGER) + CAST(SUBSTR(WT, INSTR(WT,' ')+1) AS FLOAT)/16) as average_weight FROM nba_roster WHERE WT!= '--';"}
{"question": "Which teams in the NBA have a significantly larger roster size compared to the number of point guards in the league", "sql": "SELECT Team, COUNT(*) as num_players FROM nba_roster GROUP BY Team HAVING COUNT(*) > (SELECT COUNT(*) FROM nba_roster WHERE POS = 'PG')*0.3;"}
{"question": "What are the top 5 colleges that produce the oldest average age of NBA players", "sql": "SELECT COLLEGE, AVG(AGE) as avg_age FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY avg_age DESC LIMIT 5;"}
{"question": "What is the average salary of all players in the positions of PG, SG, SF, PF, and C in the NBA", "sql": "SELECT AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary FROM nba_roster WHERE POS = 'PG' OR POS = 'SG' OR POS = 'SF' OR POS = 'PF' OR POS = 'C';"}
{"question": "Who is the player with the highest salary in the NBA", "sql": "SELECT NAME, SALARY FROM nba_roster WHERE SALARY = (SELECT MAX(SALARY) FROM nba_roster);"}
{"question": "What are the top 10 teams with the most players in the NBA, considering only teams with at least 10 players with height information", "sql": "SELECT name, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)) as avg_height, COUNT(*) as count FROM nba_roster WHERE HT!= 'NA' GROUP BY name ORDER BY count DESC LIMIT 10;"}
{"question": "Which players have played for the most teams in their NBA careers", "sql": "SELECT name, COUNT(DISTINCT team) as team_count FROM nba_roster WHERE team!= 'NA' GROUP BY name ORDER BY team_count DESC LIMIT 10;"}
{"question": "What is the 75th percentile jersey number in the NBA", "sql": "SELECT CAST(Jersey AS INTEGER) as percentile FROM nba_roster ORDER BY CAST(Jersey AS INTEGER) LIMIT 1 OFFSET (SELECT COUNT(*) FROM nba_roster) * 0.75;"}
{"question": "How many players in the NBA are younger than the oldest player in the league by 15 years", "sql": "SELECT COUNT(*) as num_players FROM nba_roster WHERE AGE + 15 > (SELECT MAX(AGE) FROM nba_roster);"}
{"question": "Which jersey numbers are the most popular among NBA players", "sql": "SELECT NAME, JERSEY FROM nba_roster GROUP BY JERSEY ORDER BY COUNT(*) DESC LIMIT 3;"}
{"question": "Which team has the highest average salary", "sql": "SELECT team, AVG(CAST(SUBSTRING(SALARY, 2, LENGTH(SALARY)-2) AS INTEGER)) AS avg_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY team ORDER BY avg_salary DESC LIMIT 1;"}
{"question": "How many players in the NBA are older than 25 years old", "sql": "SELECT COUNT(*) as num_players FROM nba_roster WHERE AGE > 25;"}
{"question": "Which colleges have produced the most multiple NBA players", "sql": "SELECT COLLEGE, COUNT(*) FROM nba_roster GROUP BY COLLEGE HAVING COUNT(*) > 1;"}
{"question": "Who has the highest salary on the Los Angeles Lakers", "sql": "SELECT name, salary FROM nba_roster WHERE team='Los Angeles Lakers' AND salary!= '--' ORDER BY CAST(REPLACE(REPLACE(salary, '$', ''), ',', '') AS INTEGER) DESC LIMIT 1;"}
{"question": "What are the minimum and maximum salaries for each team in the NBA", "sql": "SELECT MIN(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as min_salary, MAX(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as max_salary, team FROM nba_roster WHERE SALARY!= '--' GROUP BY team ORDER BY min_salary DESC, max_salary DESC;"}
{"question": "What is the average age of the team with the oldest roster in the NBA", "sql": "SELECT AVG(AGE) as avg_age FROM nba_roster GROUP BY team ORDER BY avg_age DESC LIMIT 1;"}
{"question": "What are the teams with more than 5 players in the age range of 25 to 30 in the NBA", "sql": "SELECT team, COUNT(*) AS num_players FROM nba_roster WHERE AGE BETWEEN 25 AND 30 GROUP BY team HAVING COUNT(*) > 5;"}
{"question": "Who is the highest-paid player who did not attend college", "sql": "SELECT name, salary FROM nba_roster WHERE SALARY!= '--' AND COLLEGE = '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "What is the total number of players in the NBA", "sql": "SELECT COUNT(*) FROM nba_roster;"}
{"question": "What is the most common position among players under the age of 25 in the NBA", "sql": "SELECT POS, COUNT(*) as count FROM nba_roster WHERE AGE <= 25 GROUP BY POS ORDER BY count DESC LIMIT 1;"}
{"question": "Who is the oldest player in the NBA", "sql": "SELECT name, age FROM nba_roster ORDER BY age DESC LIMIT 1;"}
{"question": "What are the minimum and maximum salaries in the NBA", "sql": "SELECT MIN(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as min_salary, MAX(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as max_salary FROM nba_roster WHERE SALARY!= '--';"}
{"question": "What is the average salary of Power Forward players in the NBA who are under the age of 25", "sql": "SELECT AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary FROM nba_roster WHERE POS = 'PF' AND AGE < 25;"}
{"question": "What is the total number of players in the NBA who are 25 years or younger", "sql": "SELECT COUNT(*) as total_players FROM nba_roster WHERE AGE + 25 <= (SELECT MAX(AGE) FROM nba_roster);"}
{"question": "Who is the highest-paid player on the Toronto Raptors", "sql": "SELECT NAME, SALARY FROM nba_roster WHERE TEAM = 'Toronto Raptors' AND SALARY = (SELECT MAX(SALARY) FROM nba_roster WHERE TEAM = 'Toronto Raptors');"}
{"question": "Who is the highest-paid player on the Los Angeles Lakers", "sql": "SELECT name FROM nba_roster WHERE team='Los Angeles Lakers' AND SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "What are the top 3 teams with the most players over the age of 5 in the NBA", "sql": "SELECT team, COUNT(*) as num_players FROM nba_roster WHERE age > 5 GROUP BY team ORDER BY num_players DESC LIMIT 3;"}
{"question": "Which teams have the tallest players, excluding those with unknown salaries", "sql": "SELECT team, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)) as avg_height FROM nba_roster WHERE SALARY!= '--' GROUP BY team ORDER BY avg_height DESC;"}
{"question": "What is the number of players in the NBA who are 25 years or younger", "sql": "SELECT COUNT(*) as num_players FROM nba_roster WHERE AGE + 25 <= (SELECT MAX(AGE) FROM nba_roster);"}
{"question": "What is the age group with the most players in the NBA", "sql": "SELECT AGE, COUNT(*) as count FROM nba_roster GROUP BY AGE ORDER BY count DESC LIMIT 1;"}
{"question": "What is the most common position for players aged 25 or older in the NBA", "sql": "SELECT POS, COUNT(*) as count FROM nba_roster WHERE AGE >= 25 GROUP BY POS ORDER BY count DESC LIMIT 1;"}
{"question": "What is the total salary of all players in the NBA", "sql": "SELECT SUM(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as total_salary FROM nba_roster;"}
{"question": "Which three teams have the most players from the same college", "sql": "SELECT team, COUNT(*) AS num_players, COLLEGE FROM nba_roster GROUP BY team, COLLEGE ORDER BY num_players DESC LIMIT 3;"}
{"question": "What is the average age of players in the NBA who are more than 5 years older than the average age of all players", "sql": "SELECT AVG(AGE) as average_age FROM nba_roster WHERE AGE - (SELECT AVG(AGE) FROM nba_roster) > 5;"}
{"question": "What is the heaviest player in the NBA", "sql": "SELECT NAME, WT FROM nba_roster WHERE WT!= 'NA' ORDER BY CAST(SUBSTRING(WT, 0, INSTR(WT,'') - 1) AS INTEGER) DESC LIMIT 1;"}
{"question": "What is the average height of all players in the NBA roster", "sql": "SELECT AVG(LENGTH(HT)) AS average_height FROM nba_roster;"}
{"question": "What are the average height and age of players on each team in the NBA", "sql": "SELECT team, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,'')-1) AS INTEGER)) AS average_height, AVG(AGE) AS average_age FROM nba_roster GROUP BY team ORDER BY average_age DESC;"}
{"question": "How many players in the NBA are 6' or 8' tall", "sql": "SELECT COUNT(*) AS num_players FROM nba_roster WHERE CAST(SUBSTRING(HT, 1, INSTR(HT,'')-1) AS INTEGER) = 6 | 8;"}
{"question": "What is the shortest weight listed in the 'nba_roster' table", "sql": "SELECT NAME, WT FROM nba_roster ORDER BY LENGTH(WT) LIMIT 1;"}
{"question": "What is the highest-paid player in the NBA", "sql": "SELECT TEAM, NAME, SALARY FROM nba_roster WHERE SALARY = (SELECT MAX(SALARY) FROM nba_roster) ORDER BY TEAM;"}
{"question": "What college has produced the most NBA players", "sql": "SELECT COLLEGE, COUNT(*) AS frequency FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY frequency DESC LIMIT 1;"}
{"question": "What is the total salary of all players in the NBA who are 25 years old or younger", "sql": "SELECT SUM(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as total_salary FROM nba_roster WHERE AGE <= 25;"}
{"question": "What is the oldest player for each position in the NBA", "sql": "SELECT pos, NAME, MAX(AGE) as max_age FROM nba_roster GROUP BY pos;"}
{"question": "Who is the highest-paid player in the NBA who did not attend college", "sql": "SELECT name, salary FROM nba_roster WHERE COLLEGE = '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "What is the salary of the 25th percentile of players in the NBA who are 25 years old or younger", "sql": "SELECT CAST(SALARY as INTEGER) as percentile FROM nba_roster WHERE AGE <= 25 ORDER BY percentile LIMIT 1 OFFSET (SELECT COUNT(*) FROM nba_roster WHERE AGE <= 25) / 4;"}
{"question": "What are the most common positions in the NBA, and which position has the highest average weight", "sql": "SELECT POS, COUNT(*) AS count, AVG(CAST(SUBSTR(WT, 1, INSTR(WT,'')) AS INTEGER)) AS average_weight FROM nba_roster WHERE POS!= 'NA' GROUP BY POS ORDER BY count DESC;"}
{"question": "What is the 75th percentile age of the NBA players", "sql": "SELECT CAST(AGE AS INTEGER) AS percentile FROM nba_roster ORDER BY percentile LIMIT 1 OFFSET (SELECT COUNT(*) FROM nba_roster) * 0.75;"}
{"question": "What is the average salary of paid NBA players", "sql": "SELECT AVG(CAST(SUBSTR(SALARY, 1, INSTR(SALARY,' ')-1) AS INTEGER)) as average_salary FROM nba_roster WHERE SALARY!= '--';"}
{"question": "What age group has the most players in the NBA", "sql": "SELECT AGE, COUNT(*) as count FROM nba_roster GROUP BY AGE ORDER BY count DESC;"}
{"question": "What is the height of the tallest player on the Los Angeles Lakers", "sql": "SELECT HT, NAME FROM nba_roster WHERE team='Los Angeles Lakers' AND HT!= 'NA' ORDER BY CAST(SUBSTRING(HT, 0, INSTR(HT,'')) AS INTEGER) DESC LIMIT 1;"}
{"question": "What is the average salary of the Toronto Raptors players", "sql": "SELECT AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) AS average_salary FROM nba_roster WHERE team='Toronto Raptors';"}
{"question": "What is the average salary of an NBA player", "sql": "SELECT AVG(CAST(SALARY AS INTEGER) / 1000000) AS average_salary FROM nba_roster;"}
{"question": "What is the team with the highest average age in the NBA", "sql": "SELECT team, AVG(age) AS average_age FROM nba_roster GROUP BY team ORDER BY average_age DESC LIMIT 1;"}
{"question": "Which team has the most players over the age of 25 in the NBA", "sql": "SELECT Team, COUNT(*) FROM nba_roster WHERE AGE > 25 GROUP BY Team ORDER BY COUNT(*) DESC LIMIT 1;"}
{"question": "What is the total salary of the team with the highest total salary in the NBA", "sql": "SELECT SUM(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) AS total_salary, team FROM nba_roster WHERE SALARY!= '--' GROUP BY team ORDER BY total_salary DESC;"}
{"question": "How many players in the NBA are exactly 6 feet tall", "sql": "SELECT COUNT(*) FROM nba_roster WHERE CAST(SUBSTRING(HT, 0, INSTR(HT,'')-1) AS INTEGER) = 6 AND HT!= 'NA';"}
{"question": "What is the age with the most unique players in the NBA", "sql": "SELECT COUNT(DISTINCT AGE) AS age_count, AGE FROM nba_roster GROUP BY AGE ORDER BY age_count DESC LIMIT 1;"}
{"question": "What is the highest-paid player who did not attend college", "sql": "SELECT name, salary FROM nba_roster WHERE COLLEGE = '--' AND SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "Which age group has the most players in the NBA", "sql": "SELECT COUNT(*), AGE FROM nba_roster GROUP BY AGE ORDER BY COUNT(*) DESC;"}
{"question": "What is the average height in the NBA?", "sql": "SELECT COUNT(*) as num_college_players FROM nba_roster WHERE COLLEGE!= '--';"}
{"question": "Which position has the most players in the NBA", "sql": "SELECT POS, COUNT(*) AS count FROM nba_roster GROUP BY POS ORDER BY count DESC LIMIT 1;"}
{"question": "What are the top 10 colleges that have produced the most NBA players", "sql": "SELECT COLLEGE, COUNT(*) AS num_players FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY num_players DESC LIMIT 10;"}
{"question": "What is the average age of players from colleges that have multiple players in the NBA", "sql": "SELECT AVG(AGE) AS average_age, COLLEGE FROM nba_roster GROUP BY COLLEGE HAVING COUNT(*) > 1;"}
{"question": "Which colleges have the most representation in the NBA", "sql": "SELECT COLLEGE, COUNT(*) AS num_players FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY num_players DESC;"}
{"question": "Who are the oldest players in the NBA, excluding those who are above the average age of all players", "sql": "SELECT NAME FROM nba_roster WHERE AGE > (SELECT AVG(AGE) FROM nba_roster) ORDER BY AGE DESC;"}
{"question": "What are the top 3 highest-paid players on the Toronto Raptors", "sql": "SELECT name, SALARY FROM nba_roster WHERE team='Toronto Raptors' ORDER BY CAST(SUBSTRING(SALARY, 2) AS INTEGER) DESC LIMIT 3;"}
{"question": "Which colleges have produced multiple players in the NBA", "sql": "SELECT COLLEGE, COUNT(*) AS num_players FROM nba_roster GROUP BY COLLEGE HAVING COUNT(*) > 1;"}
{"question": "What is the average salary of NBA players 25 years old or younger", "sql": "SELECT AVG(CAST(SUBSTR(SALARY, 1, INSTR(SALARY, '$')-1) AS INTEGER)) AS average_salary FROM nba_roster WHERE CAST(AGE AS INTEGER) <= 25;"}
{"question": "What is the highest-paid player who has played for more than one team", "sql": "SELECT NAME, TEAM, SALARY FROM nba_roster WHERE SALARY = (SELECT MAX(SALARY) FROM nba_roster) AND (SELECT COUNT(DISTINCT TEAM) FROM nba_roster WHERE NAME = nba_roster.NAME) > 1;"}
{"question": "Who is the tallest player in the NBA, based on average height", "sql": "SELECT NAME, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,'')) AS INTEGER)) AS AVG_HEIGHT, COUNT(DISTINCT TEAM) AS TEAM_COUNT FROM nba_roster GROUP BY NAME ORDER BY AVG_HEIGHT DESC LIMIT 1;"}
{"question": "What is the total weight of all players in the NBA", "sql": "SELECT SUM(CAST(SUBSTR(WT, 1, INSTR(WT,' ')) as INTEGER)) FROM nba_roster;"}
{"question": "What are the top 10 highest-paid teams in the NBA, based on the average salary of their players", "sql": "SELECT AVG(CAST(SUBSTR(SALARY, 1, INSTR(SALARY, '$')-1) AS INTEGER)) as avg_salary, AVG(AGE) as avg_age FROM nba_roster WHERE SALARY!= '--' GROUP BY SALARY ORDER BY avg_salary DESC LIMIT 10;"}
{"question": "What is the highest salary for each team in the NBA", "sql": "SELECT team, MAX(CAST(SUBSTR(SALARY, 1, INSTR(SALARY, '$')-1) AS INTEGER)) as highest_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY team;"}
{"question": "What is the average age of all players in the NBA who are at least 60 years old", "sql": "SELECT AVG(AGE) AS average_age FROM nba_roster WHERE AGE > 5*12;"}
{"question": "What is the average age of the youngest players in the NBA", "sql": "SELECT AVG(AGE) AS average_age FROM nba_roster WHERE AGE <= 25;"}
{"question": "What are the top 3 teams with the highest average salary", "sql": "SELECT team, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) AS average_salary FROM nba_roster GROUP BY team ORDER BY average_salary DESC LIMIT 3;"}
{"question": "What is the most popular jersey number in the NBA", "sql": "SELECT Jersey, COUNT(*) as frequency FROM nba_roster WHERE Jersey!= 'NA' GROUP BY Jersey ORDER BY frequency DESC LIMIT 1;"}
{"question": "What is the total salary of all players in the NBA, excluding those with unknown salaries", "sql": "SELECT SUM(CAST(SUBSTR(SALARY, 1, INSTR(SALARY, '$')-1) AS INTEGER)) as total_salary FROM nba_roster WHERE SALARY!= '--';"}
{"question": "What is the number of players in the NBA roster who are 10 years or less away from the oldest player in the league", "sql": "SELECT COUNT(*) AS num_players FROM nba_roster WHERE AGE + 10 <= (SELECT MAX(AGE) FROM nba_roster);"}
{"question": "Which three teams have the tallest average height in the NBA", "sql": "SELECT team, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as height FROM nba_roster GROUP BY team ORDER BY height DESC LIMIT 3;"}
{"question": "How many players in the NBA are older than 5 years old", "sql": "SELECT COUNT(*) AS num_players FROM nba_roster WHERE AGE > 5;"}
{"question": "What are the 5 teams with the most players from the University of Michigan", "sql": "SELECT team, COUNT(*) AS num_players FROM nba_roster WHERE COLLEGE = 'Michigan' GROUP BY team ORDER BY num_players DESC LIMIT 5;"}
{"question": "What is the number of players in the NBA who are 15 years or younger than the oldest player in the league", "sql": "SELECT COUNT(*) as num_players FROM nba_roster WHERE AGE + 15 <= (SELECT MAX(AGE) FROM nba_roster);"}
{"question": "What are the minimum and maximum salaries of NBA players", "sql": "SELECT MIN(SALARY) AS min_salary, MAX(SALARY) AS max_salary FROM nba_roster WHERE SALARY!= '--';"}
{"question": "What is the total salary of all players on the Toronto Raptors who are at least 6 feet 7 inches tall", "sql": "SELECT SUM(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as total_salary FROM nba_roster WHERE team='Toronto Raptors' AND CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12 >= 6.67;"}
{"question": "What is the height with the most players in the NBA", "sql": "SELECT HT, COUNT(*) as count, AVG(WT) as avg_weight FROM nba_roster GROUP BY HT ORDER BY count DESC LIMIT 1;"}
{"question": "What is the most common height of NBA players", "sql": "SELECT CAST(SUBSTR(HT, 1, INSTR(HT,'')-1) AS INTEGER) AS height, COUNT(*) AS count FROM nba_roster GROUP BY CAST(SUBSTR(HT, 1, INSTR(HT,'')-1) AS INTEGER) ORDER BY count DESC LIMIT 1;"}
{"question": "What is the total salary of all NBA players with known salaries", "sql": "SELECT SUM(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as total_salary FROM nba_roster WHERE SALARY!= '--';"}
{"question": "What is the oldest player in the NBA", "sql": "SELECT AVG(AGE) as average_age, NAME from nba_roster GROUP BY NAME ORDER BY average_age DESC LIMIT 1;"}
{"question": "What is the average height of NBA players aged 25 or older", "sql": "SELECT AVG(CAST(SUBSTR(HT, 1, INSTR(HT,'')) as INTEGER)) AS avg_height FROM nba_roster WHERE AGE >= 25;"}
{"question": "How many players in the NBA are 6'6", "sql": "SELECT COUNT(*) as num_players FROM nba_roster WHERE CAST(SUBSTR(HT, 1, INSTR(HT,'')-1) AS INTEGER) = '6' || '6';"}
{"question": "Who are the oldest players on each team in the NBA, excluding the average age of their team", "sql": "SELECT nba_roster.NAME FROM nba_roster WHERE AGE > (SELECT AVG(AGE) FROM nba_roster WHERE TEAM = nba_roster.TEAM) ORDER BY AGE DESC;"}
{"question": "What is the most common position played by Jalen Johnson", "sql": "SELECT POS, COUNT(*) AS count, POS FROM nba_roster GROUP BY POS ORDER BY count DESC LIMIT 1;"}
{"question": "What is the number of players on each team who are 25 years old or older", "sql": "SELECT team, COUNT(*) AS num_players FROM nba_roster WHERE AGE >= 25 GROUP BY team;"}
{"question": "What are the top 5 players in the NBA in terms of average height", "sql": "SELECT name, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)) as avg_height FROM nba_roster GROUP BY name ORDER BY avg_height DESC LIMIT 5;"}
{"question": "What players in the NBA are taller than the average height of all players", "sql": "SELECT NAME FROM nba_roster WHERE HT > (SELECT AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) FROM nba_roster);"}
{"question": "Which team has the most players 25 years old or older", "sql": "SELECT team, COUNT(*) as num_players FROM nba_roster WHERE AGE >= 25 GROUP BY team ORDER BY num_players DESC LIMIT 1;"}
{"question": "What are the 5 most common jersey numbers in the NBA", "sql": "SELECT COUNT(DISTINCT Jersey), Jersey FROM nba_roster GROUP BY Jersey ORDER BY COUNT(DISTINCT Jersey) DESC LIMIT 5;"}
{"question": "What colleges are most represented in the NBA", "sql": "SELECT COLLEGE, COUNT(*) AS num_players FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE;"}
{"question": "What is the average salary of NBA players under the age of 25", "sql": "SELECT AVG(CAST(SUBSTR(SALARY, 1, INSTR(SALARY, '$')-1) AS INTEGER)) AS average_salary FROM nba_roster WHERE AGE <= 25;"}
{"question": "What are the top 10 teams in the NBA by average salary", "sql": "SELECT Team, AVG(CAST(SUBSTRING(SALARY, 2, LENGTH(SALARY)-2) AS INTEGER)) AS average_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY Team ORDER BY average_salary DESC LIMIT 10;"}
{"question": "What is the player with the highest salary in the NBA", "sql": "SELECT name, CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) AS salary FROM nba_roster WHERE SALARY!= '--' ORDER BY salary DESC LIMIT 1;"}
{"question": "Who is the oldest player in the NBA who is not a rookie", "sql": "SELECT name, age FROM nba_roster WHERE SALARY!= '--' ORDER BY age DESC LIMIT 1;"}
{"question": "How many players in the NBA are 6 feet 8 inches or taller", "sql": "SELECT COUNT(*) AS num_players FROM nba_roster WHERE CAST(SUBSTRING(HT, 0, INSTR(HT,'')-1) AS INTEGER) >= 68;"}
{"question": "How many players in the NBA are 25 years old", "sql": "SELECT COUNT(*) FROM nba_roster WHERE age = 25;"}
{"question": "What is the team with the oldest average age in the NBA", "sql": "SELECT AVG(AGE) AS average_age FROM nba_roster GROUP BY TEAM ORDER BY average_age DESC LIMIT 1;"}
{"question": "Who is the highest-paid player in the NBA, excluding those with unknown salaries", "sql": "SELECT MAX(SALARY) AS highest_salary, NAME FROM nba_roster WHERE SALARY!= '--' GROUP BY NAME ORDER BY highest_salary DESC LIMIT 1;"}
{"question": "Which team has the most players under the age of 25", "sql": "SELECT Team, COUNT(*) as num_players FROM nba_roster WHERE AGE < 25 GROUP BY Team ORDER BY num_players DESC LIMIT 1;"}
{"question": "What are the top 3 jersey numbers with the most players in the NBA", "sql": "SELECT jersey, COUNT(*) as count FROM nba_roster WHERE jersey!= 'NA' GROUP BY jersey ORDER BY count DESC LIMIT 3;"}
{"question": "What percentage of NBA players are at least 6 feet 8 inches tall", "sql": "SELECT COUNT(*) FROM nba_roster WHERE CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER) >= 68 AND CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12 >= 6.5;"}
{"question": "What is the average age and height of NBA players, excluding those with unknown heights", "sql": "SELECT AVG(AGE) as average_age, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,'')) as INTEGER)) as average_height FROM nba_roster WHERE HT!= 'NA';"}
{"question": "What is the player who has played for the most teams in the NBA", "sql": "SELECT name, COUNT(*) as num_teams FROM nba_roster GROUP BY name ORDER BY num_teams DESC LIMIT 1;"}
{"question": "What is the average height of players in the NBA who are 25 years or older", "sql": "SELECT AVG(CAST(SUBSTR(HT, 1, INSTR(HT,'')) as INTEGER)) AS avg_height FROM nba_roster WHERE CAST(AGE as INTEGER) >= 25;"}
{"question": "Which team has the most unique players in the NBA", "sql": "SELECT COUNT(DISTINCT TEAM), TEAM FROM nba_roster GROUP BY TEAM ORDER BY COUNT(DISTINCT TEAM) DESC LIMIT 1;"}
{"question": "What are the 5 oldest players in the NBA", "sql": "SELECT NAME, AGE FROM nba_roster ORDER BY AGE DESC LIMIT 5;"}
{"question": "What is the shortest height of a player in the NBA", "sql": "SELECT name, HT FROM nba_roster ORDER BY LENGTH(HT) LIMIT 1, 1;"}
{"question": "What is the average height of Power Forwards and Centers in the NBA", "sql": "SELECT AVG(CAST(SUBSTR(HT, 1, INSTR(HT,'')-1) AS INTEGER) + CAST(SUBSTR(HT, INSTR(HT,'')+1) AS FLOAT)/12) AS average_height FROM nba_roster WHERE POS IN ('PF', 'C');"}
{"question": "What is the total salary of the team with the highest payroll in the NBA", "sql": "SELECT team, SUM(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as total_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY team ORDER BY total_salary DESC;"}
{"question": "What are the top-paid players for each team in the NBA", "sql": "SELECT team, name, CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) as salary FROM nba_roster WHERE SALARY!= '--' GROUP BY team ORDER BY salary DESC;"}
{"question": "What is the average salary of all NBA players, excluding those with unknown salaries", "sql": "SELECT AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary FROM nba_roster WHERE SALARY!= '--';"}
{"question": "What is the name and jersey number of the player with the highest jersey number in the NBA roster", "sql": "SELECT NAME, JERSEY FROM nba_roster ORDER BY CAST(JERSEY AS INTEGER) DESC LIMIT 1;"}
{"question": "Which five jersey numbers are the most commonly worn by players in the NBA", "sql": "SELECT name, jersey, COUNT(*) as count FROM nba_roster GROUP BY jersey ORDER BY count DESC LIMIT 5;"}
{"question": "What is the most popular position in the NBA", "sql": "SELECT POS, COUNT(*) AS count FROM nba_roster WHERE POS IN ('PG', 'SG', 'SF', 'PF', 'C') GROUP BY POS ORDER BY count DESC LIMIT 1;"}
{"question": "What is the number of players in the NBA who are 68 inches tall", "sql": "SELECT COUNT(*) FROM nba_roster WHERE CAST(SUBSTRING(HT, 0, INSTR(HT,'')) AS INTEGER) = 68;"}
{"question": "Who is the highest paid player on the team with the most players", "sql": "SELECT NAME FROM nba_roster WHERE SALARY = (SELECT MAX(SALARY) FROM nba_roster) AND TEAM = (SELECT TEAM FROM nba_roster GROUP BY TEAM ORDER BY COUNT(*) DESC LIMIT 1);"}
{"question": "What is the average height of players on the Toronto Raptors", "sql": "SELECT Team, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,'')-1) AS INTEGER)) as Average_Height FROM nba_roster GROUP BY Team;"}
{"question": "What are the top 5 teams in the NBA by average salary", "sql": "SELECT Team, AVG(CAST(SUBSTR(SALARY, 1, INSTR(SALARY, '$')-1) AS INTEGER)) as Average_Salary FROM nba_roster WHERE SALARY!= '--' GROUP BY Team ORDER BY Average_Salary DESC;"}
{"question": "What are the top 3 highest-paid players in the NBA", "sql": "SELECT NAME, SALARY FROM nba_roster WHERE SALARY!= '--' ORDER BY CAST(SUBSTR(SALARY, 1, INSTR(SALARY, '$')-1) AS INTEGER) DESC LIMIT 3;"}
{"question": "How many players in the NBA are 25 years old or younger", "sql": "SELECT COUNT(*) FROM nba_roster WHERE AGE <= 25;"}
{"question": "How many players in the NBA attended Michigan State University", "sql": "SELECT COUNT(*) as num_players FROM nba_roster WHERE COLLEGE = 'Michigan State';"}
{"question": "What is the most represented position among University of Michigan alumni in the NBA", "sql": "SELECT POS, COUNT(*) as count FROM nba_roster WHERE COLLEGE='Michigan' GROUP BY POS ORDER BY count DESC LIMIT 1;"}
{"question": "How many players are on each team in the NBA", "sql": "SELECT TEAM, COUNT(*) AS num_players FROM nba_roster GROUP BY TEAM;"}
{"question": "What is the number of players in the NBA roster who are more than 5 years older than the average age of all players in the roster", "sql": "SELECT COUNT(*) as num_players FROM nba_roster WHERE AGE - (SELECT AVG(AGE) FROM nba_roster) > 5;"}
{"question": "What teams have multiple players from the same college", "sql": "SELECT team, COUNT(*) AS num_players, COLLEGE FROM nba_roster GROUP BY team, COLLEGE HAVING COUNT(*) > 1;"}
{"question": "Which college has the most players on the Brooklyn Nets", "sql": "SELECT team, COUNT(*) AS num_players, COLLEGE FROM nba_roster WHERE COLLEGE!= '--' GROUP BY team, COLLEGE ORDER BY num_players DESC;"}
{"question": "What is the average age of NBA players who are older than 5 years old", "sql": "SELECT AVG(AGE) AS average_age FROM nba_roster WHERE AGE > 5;"}
{"question": "What are the 10 players with the tallest and shortest heights in the NBA", "sql": "SELECT name, HT, MAX(CAST(SUBSTRING(HT, 1, INSTR(HT,'')-1) AS INTEGER)) AS max_height, MIN(CAST(SUBSTRING(HT, INSTR(HT,'')+1) AS INTEGER)) AS min_height FROM nba_roster WHERE HT!= 'NA' GROUP BY name ORDER BY max_height DESC, min_height ASC LIMIT 10;"}
{"question": "What is the age of the oldest player on the Toronto Raptors", "sql": "SELECT name, age FROM nba_roster WHERE team='Toronto Raptors' ORDER BY age DESC LIMIT 1;"}
{"question": "What are the top 5 highest-paid college-educated players in the NBA", "sql": "SELECT NAME, SALARY FROM nba_roster WHERE COLLEGE!= '--' AND SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 5;"}
{"question": "What is the number of players in the NBA roster who do not have a college listed", "sql": "SELECT COUNT(*) AS num_players FROM nba_roster WHERE COLLEGE = '--';"}
{"question": "What is the number of players on the Toronto Raptors who earn more than $10,000,000", "sql": "SELECT COUNT(*) FROM nba_roster WHERE team='Toronto Raptors' AND CAST(SUBSTRING(SALARY, 2) AS INTEGER) > 10000000;"}
{"question": "What is the average height and age of NBA players, and how do these values vary by height", "sql": "SELECT AVG(CAST(SUBSTR(HT, 1, INSTR(HT,'')-1) AS INTEGER)) AS average_height, AVG(AGE) AS average_age FROM nba_roster GROUP BY CAST(SUBSTR(HT, INSTR(HT,'')+1) AS INTEGER);"}
{"question": "What is the most frequently worn jersey number in the NBA", "sql": "SELECT COUNT(DISTINCT Jersey) AS total_jerseys, Jersey FROM nba_roster GROUP BY Jersey ORDER BY total_jerseys DESC LIMIT 1;"}
{"question": "What is the average age of all players in the NBA who have a known salary", "sql": "SELECT AVG(AGE) FROM nba_roster WHERE SALARY!= '--';"}
{"question": "What is the 99th percentile salary in the NBA?", "sql": "SELECT (CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as percentile FROM nba_roster WHERE SALARY!= '--' order by percentile limit 1 offset (select count(*) from nba_roster where SALARY != '--')*99/100-1;"}
{"question": "What is the 75th percentile salary in the NBA?", "sql": "SELECT (CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as percentile FROM nba_roster WHERE SALARY!= '--' order by percentile limit 1 offset (select count(*) from nba_roster where SALARY != '--')*75/100-1;"}
{"question": "What is the 25th percentile salary in the NBA?", "sql": "SELECT (CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as percentile FROM nba_roster WHERE SALARY!= '--' order by percentile limit 1 offset (select count(*) from nba_roster where SALARY != '--')*25/100-1;"}
{"question": "What is the median weight in the NBA?", "sql": "select CAST(SUBSTR(WT, 1, INSTR(WT,' ')) as INTEGER) as percentile from nba_roster order by percentile limit 1 offset (select count(*) from nba_roster)/2;"}
{"question": "What is the average weight in the NBA?", "sql": "SELECT AVG(CAST(SUBSTR(WT, 1, INSTR(WT,' ')) as INTEGER)) FROM nba_roster;"}
{"question": "What is the median height in the NBA?", "sql": "select CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12 as percentile from nba_roster order by percentile limit 1 offset (select count(*) from nba_roster)/2;"}
{"question": "Can you tell me how many players are in the NBA?", "sql": "select count(*) from nba_roster;"}
{"question": "Would you please let me know what the highest paid players are for each position?", "sql": "SELECT name, pos, MAX(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as max_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY POS;"}
{"question": "Is Jalen Johnson 23 years old?", "sql": "Select name, age from nba_roster where name='Jalen Johnson';"}
{"question": "Who is the oldest player on the Brooklyn Nets?", "sql": "SELECT NAME FROM nba_roster WHERE TEAM = 'Brooklyn Nets' AND AGE = (SELECT MAX(AGE) FROM nba_roster WHERE TEAM = 'Brooklyn Nets');"}
{"question": "Who has the highest salary on the Memphis Grizzlies?", "sql": "select salary, name from nba_roster where team='Memphis Grizzlies' and SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "Which player has the highest salary on the Cleveland Cavaliers?", "sql": "select salary, name from nba_roster where team='Cleveland Cavaliers' and SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "Who is the highest paid center on the Dallas Mavericks?", "sql": "select salary, name from nba_roster where team='Dallas Mavericks' and POS='C' and SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "How much is Marcus Smart getting paid?", "sql": "select salary from nba_roster where name='Marcus Smart';"}
{"question": "What's the average age of the Trail Blazers?", "sql": "select avg(age) from nba_roster where team='Portland Trail Blazers';"}
{"question": "What's the median age of the NBA?", "sql": "select CAST(AGE as INTEGER) as percentile from nba_roster order by percentile limit 1 offset (select count(*) from nba_roster)/2;"}
{"question": "What's the median age of the Miami Heat?", "sql": "select CAST(AGE as INTEGER) as percentile from nba_roster where team='Miami Heat' order by percentile limit 1 offset (select count(*) from nba_roster where team='Miami Heat')/2;"}
@@ -0,0 +1,226 @@
{"question": "What is the average age of all players in the NBA", "sql": "SELECT AVG(AGE) FROM nba_roster"}
{"question": "Who are the top 5 highest-paid players in the league", "sql": "SELECT NAME, CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) AS Salary, POS FROM nba_roster WHERE SALARY!= '--' ORDER BY Salary DESC LIMIT 5"}
{"question": "What is the average age of players for each team in the NBA", "sql": "SELECT team, AVG(AGE) as average_age FROM nba_roster GROUP BY team ORDER BY average_age ASC"}
{"question": "What is the most represented college in the NBA", "sql": "SELECT COLLEGE, COUNT(*) as num_players FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY num_players DESC"}
{"question": "What is the average height of NBA players", "sql": "SELECT AVG(LENGTH(HT)) FROM nba_roster WHERE HT IS NOT NULL"}
{"question": "What is the most common position in the NBA", "sql": "SELECT POS, COUNT(*) as count FROM nba_roster GROUP BY POS ORDER BY count DESC LIMIT 1"}
{"question": "What are the most common heights in the NBA", "sql": "SELECT HT, COUNT(*) AS count, AVG(WT) AS avg_weight FROM nba_roster GROUP BY HT ORDER BY count DESC"}
{"question": "What are the 5 teams with the highest average age of players, grouped by their salary range", "sql": "SELECT AVG(AGE) AS average_age, SALARY FROM nba_roster WHERE SALARY!= '--' GROUP BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) ORDER BY average_age DESC LIMIT 5"}
{"question": "What is the average age of all players in the NBA roster", "sql": "SELECT AVG(AGE) FROM nba_roster"}
{"question": "Who is the tallest player in the NBA", "sql": "SELECT name, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as height FROM nba_roster GROUP BY name ORDER BY height DESC LIMIT 1"}
{"question": "Which five players in the NBA have attended the most different colleges", "sql": "SELECT name, COLLEGE, COUNT(DISTINCT COLLEGE) as num_colleges FROM nba_roster WHERE COLLEGE!= '--' GROUP BY name ORDER BY num_colleges DESC LIMIT 5"}
{"question": "What is the most common position in the NBA", "sql": "SELECT POS, COUNT(*) AS count FROM nba_roster GROUP BY POS ORDER BY count DESC LIMIT 1"}
{"question": "What is the average age of all players in the NBA", "sql": "SELECT AVG(AGE) AS average_age FROM nba_roster"}
{"question": "What is the most common position in the NBA", "sql": "SELECT POS, COUNT(*) AS count FROM nba_roster GROUP BY POS ORDER BY count DESC LIMIT 1"}
{"question": "What are the top 5 colleges that have produced the most NBA players", "sql": "SELECT COLLEGE, COUNT(*) as num_players FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY num_players DESC LIMIT 5"}
{"question": "What is the number of players on each team in the NBA", "sql": "SELECT Team, COUNT(*) AS num_players FROM nba_roster GROUP BY Team;"}
{"question": "What is the average salary for each team in the NBA", "sql": "SELECT team, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary FROM nba_roster GROUP BY team ORDER BY average_salary DESC;"}
{"question": "What is the average age of players on each team in the NBA", "sql": "SELECT team, AVG(AGE) AS average_age FROM nba_roster GROUP BY team ORDER BY average_age"}
{"question": "Which team has the tallest average height", "sql": "SELECT team, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER) + CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) AS average_height FROM nba_roster GROUP BY team ORDER BY average_height DESC"}
{"question": "What is the average age of the players in the NBA", "sql": "SELECT AVG(AGE) FROM nba_roster WHERE AGE IS NOT NULL"}
{"question": "What is the average height of all NBA players", "sql": "SELECT AVG(HT) FROM nba_roster"}
{"question": "Who is the highest paid player in the league", "sql": "SELECT name, SALARY FROM nba_roster ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1"}
{"question": "What is the average age of the players on the Golden State Warriors", "sql": "SELECT AVG(AGE) FROM nba_roster WHERE TEAM='Golden State Warriors';"}
{"question": "What is the most represented college in the NBA", "sql": "SELECT COLLEGE, COUNT(*) as count FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY count DESC LIMIT 1"}
{"question": "What is the team with the oldest average age in the NBA", "sql": "SELECT team, AVG(AGE) AS avg_age FROM nba_roster WHERE AGE IS NOT NULL GROUP BY team ORDER BY avg_age DESC LIMIT 1"}
{"question": "What is the average age of NBA players", "sql": "SELECT AVG(AGE) FROM nba_roster"}
{"question": "What are the top 3 teams in the NBA with the highest average salary", "sql": "SELECT team, AVG(CAST(SUBSTR(SALARY, 2) AS INTEGER) / 1000000) AS avg_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY team ORDER BY avg_salary DESC LIMIT 3"}
{"question": "Which team has the oldest average age of players", "sql": "SELECT team, AVG(AGE) AS avg_age FROM nba_roster GROUP BY team ORDER BY avg_age DESC LIMIT 1"}
{"question": "What is the average height of all NBA players", "sql": "SELECT AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as average_height FROM nba_roster"}
{"question": "Which college has produced the most NBA players", "sql": "SELECT COLLEGE, COUNT(*) as count FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY count DESC LIMIT 1"}
{"question": "What is the most represented college in the NBA", "sql": "SELECT COLLEGE, COUNT(*) AS count FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY count DESC LIMIT 1"}
{"question": "How many players in the NBA have attended a college other than '--'", "sql": "SELECT COUNT(*) FROM nba_roster WHERE COLLEGE!= '--';"}
{"question": "What is the most common position in the NBA", "sql": "SELECT POS, COUNT(*) as count FROM nba_roster GROUP BY POS ORDER BY count DESC LIMIT 1"}
{"question": "What is the average age of the players on the team with the most players in the NBA", "sql": "SELECT AVG(AGE) as average_age FROM nba_roster WHERE TEAM IN (SELECT TEAM FROM nba_roster GROUP BY TEAM ORDER BY COUNT(*) DESC LIMIT 1)"}
{"question": "What is the number of players in the NBA roster who are 25 years old or older", "sql": "SELECT COUNT(*) AS num_players FROM nba_roster WHERE AGE >= 25 AND AGE IS NOT NULL;"}
{"question": "What is the average age of the players in the NBA roster", "sql": "SELECT AVG(AGE) as average_age FROM nba_roster WHERE AGE IS NOT NULL"}
{"question": "What is the average age of all players in the NBA who have a non-null salary", "sql": "SELECT AVG(AGE) FROM nba_roster WHERE SALARY!= '--';"}
{"question": "What is the most common position in the NBA", "sql": "SELECT POS, COUNT(*) as count FROM nba_roster WHERE SALARY!= '--' GROUP BY POS ORDER BY count DESC;"}
{"question": "What is the average age of the players in the NBA", "sql": "SELECT AVG(AGE) FROM nba_roster"}
{"question": "What is the most common height among NBA players", "sql": "SELECT HT, COUNT(*) AS count FROM nba_roster GROUP BY HT ORDER BY count DESC LIMIT 1"}
{"question": "What is the average age of players in the NBA", "sql": "SELECT AVG(AGE) FROM nba_roster"}
{"question": "What is the most popular position among players under the age of 25", "sql": "SELECT POS, COUNT(*) AS count FROM nba_roster WHERE AGE <= 25 GROUP BY POS ORDER BY count DESC LIMIT 1"}
{"question": "What is the number of players who are more than 10 years older than the average age of all players", "sql": "SELECT COUNT(*) AS num_players FROM nba_roster WHERE age > (SELECT AVG(age) FROM nba_roster) + 10"}
{"question": "What college has produced the most players in the NBA", "sql": "SELECT COLLEGE, COUNT(*) AS count FROM nba_roster GROUP BY COLLEGE ORDER BY count DESC LIMIT 1;"}
{"question": "What are the top 3 teams with the oldest average age in the NBA", "sql": "SELECT team, AVG(AGE) AS average_age FROM nba_roster GROUP BY team ORDER BY average_age DESC LIMIT 3"}
{"question": "Which team has the highest average salary in the NBA", "sql": "SELECT TEAM, AVG(SALARY) AS avg_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY TEAM ORDER BY avg_salary DESC LIMIT 1"}
{"question": "What is the average salary of players in the NBA who are older than 5 years old", "sql": "SELECT AVG(SALARY) FROM nba_roster WHERE AGE > 5*12"}
{"question": "Who are the top 5 highest-paid players in the league", "sql": "SELECT NAME, SALARY FROM nba_roster ORDER BY SALARY DESC LIMIT 5"}
{"question": "What is the most represented college in the NBA", "sql": "SELECT COLLEGE, COUNT(*) AS count FROM nba_roster GROUP BY COLLEGE ORDER BY count DESC LIMIT 1"}
{"question": "What is the average age of the players in the NBA", "sql": "SELECT AVG(AGE) FROM nba_roster"}
{"question": "What are the top 5 teams with the most players in the NBA", "sql": "SELECT team, COUNT(*) AS num_players FROM nba_roster GROUP BY team ORDER BY num_players DESC LIMIT 5"}
{"question": "Who are the top 3 highest-paid players on the Golden State Warriors", "sql": "SELECT NAME, SALARY FROM nba_roster WHERE team='Golden State Warriors' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 3"}
{"question": "What are the top 5 teams with the most players in the NBA", "sql": "SELECT Team, COUNT(*) as num_players FROM nba_roster GROUP BY Team ORDER BY num_players DESC"}
{"question": "What is the average age of the players in the NBA", "sql": "SELECT AVG(AGE) FROM nba_roster WHERE AGE IS NOT NULL"}
{"question": "What players earn more than the average salary in the NBA", "sql": "SELECT name, AVG(salary) AS average_salary FROM nba_roster GROUP BY name HAVING AVG(salary) > (SELECT AVG(salary) FROM nba_roster);"}
{"question": "What is the average age of the players in the NBA", "sql": "SELECT AVG(AGE) as percentile FROM nba_roster WHERE AGE IS NOT NULL"}
{"question": "What percentage of NBA players attended college", "sql": "SELECT COUNT(*) as percentile FROM nba_roster WHERE COLLEGE!= '--'"}
{"question": "What is the average salary of NBA players", "sql": "SELECT AVG(CAST(SUBSTR(SALARY, 1, INSTR(SALARY, '$')-1) AS INTEGER)) as average_salary FROM nba_roster WHERE SALARY!= '--';"}
{"question": "Which teams have the most players in the NBA", "sql": "SELECT team, COUNT(*) as num_players FROM nba_roster GROUP BY team ORDER BY num_players DESC;"}
{"question": "Which positions in the NBA have the most players and which positions have the oldest players on average", "sql": "SELECT POS, COUNT(*) as count, ROUND(AVG(AGE),2) as avg_age FROM nba_roster GROUP BY POS ORDER BY count DESC"}
{"question": "What is the position with the most players in the NBA", "sql": "SELECT POS, COUNT(*) as count FROM nba_roster WHERE POS IS NOT NULL GROUP BY POS ORDER BY count DESC LIMIT 1"}
{"question": "What is the tallest team in the NBA", "sql": "SELECT team, AVG(CAST(SUBSTRING(HT, 0, INSTR(HT,'')-1) AS INTEGER) + CAST(SUBSTRING(HT, INSTR(HT,'')+1) AS INTEGER) / 12.0) as average_height FROM nba_roster GROUP BY team ORDER BY average_height DESC LIMIT 1"}
{"question": "Which college has produced the most players for a specific NBA team", "sql": "SELECT team, COUNT(*) as num_players FROM nba_roster GROUP BY team, COLLEGE ORDER BY num_players DESC LIMIT 1"}
{"question": "What is the average age of all players in the NBA", "sql": "SELECT AVG(AGE) FROM nba_roster"}
{"question": "What is the most common position in the NBA", "sql": "SELECT POS, COUNT(*) AS count FROM nba_roster GROUP BY POS ORDER BY count DESC LIMIT 1"}
{"question": "What is the team with the oldest average age", "sql": "SELECT team, AVG(AGE) as avg_age FROM nba_roster WHERE AGE IS NOT NULL GROUP BY team ORDER BY avg_age DESC LIMIT 1"}
{"question": "What is the height in inches of all players who are 6'8", "sql": "SELECT NAME, HT FROM nba_roster WHERE CAST(SUBSTRING(HT, 0, INSTR(HT,'')-1) AS INTEGER) = 68;"}
{"question": "What is the average age of all players in the NBA", "sql": "SELECT AVG(AGE) FROM nba_roster"}
{"question": "What is the height of the tallest player in the NBA", "sql": "SELECT HT, COUNT(*) AS count FROM nba_roster GROUP BY SUBSTRING(HT, 0, INSTR(HT,'')) ORDER BY count DESC LIMIT 1"}
{"question": "What is the average height of NBA players", "sql": "SELECT AVG(LENGTH(HT)) AS average_height FROM nba_roster WHERE HT IS NOT NULL"}
{"question": "What is the most common position in the NBA", "sql": "SELECT POS, COUNT(*) AS count, POS FROM nba_roster GROUP BY POS ORDER BY count DESC LIMIT 1"}
{"question": "What is the most common position in the NBA", "sql": "SELECT POS, COUNT(*) as count FROM nba_roster GROUP BY POS ORDER BY count DESC LIMIT 1"}
{"question": "What is the average age of players on each team in the NBA", "sql": "SELECT team, AVG(AGE) as average_age FROM nba_roster WHERE AGE IS NOT NULL GROUP BY team ORDER BY average_age ASC"}
{"question": "What are the college and jersey numbers of the players who have attended college and have the lowest jersey numbers", "sql": "SELECT COLLEGE, NAME, JERSEY FROM nba_roster WHERE COLLEGE IS NOT NULL ORDER BY CAST(SUBSTR(JERSEY, 1, INSTR(JERSEY,'') - 1) AS INTEGER) ASC"}
{"question": "What is the average salary of the Toronto Raptors players", "sql": "SELECT AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) AS average_salary FROM nba_roster WHERE team='Toronto Raptors' AND SALARY!= '--';"}
{"question": "What is the most common height among NBA players", "sql": "SELECT COUNT(*), SUBSTR(HT, 0, INSTR(HT,'')-1) AS Height FROM nba_roster WHERE HT!= 'NA' GROUP BY Height ORDER BY COUNT(*) DESC LIMIT 1"}
{"question": "What is the average age of players in the NBA", "sql": "SELECT AVG(AGE) FROM nba_roster"}
{"question": "What is the most common position in the NBA", "sql": "SELECT POS, COUNT(*) as count FROM nba_roster GROUP BY POS ORDER BY count DESC LIMIT 1"}
{"question": "What is the average age of players on each NBA team", "sql": "SELECT team, AVG(AGE) AS average_age FROM nba_roster WHERE AGE IS NOT NULL GROUP BY team ORDER BY average_age ASC"}
{"question": "What is the average salary for each team in the NBA, excluding teams with unknown salaries", "sql": "SELECT team, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as avg_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY team ORDER BY avg_salary"}
{"question": "What is the most common position in the NBA", "sql": "SELECT POS, COUNT(*) as count FROM nba_roster WHERE POS!= 'NA' GROUP BY POS ORDER BY count DESC LIMIT 1"}
{"question": "What is the average age of players for each team in the NBA", "sql": "SELECT team, AVG(AGE) AS average_age FROM nba_roster GROUP BY team ORDER BY average_age ASC"}
{"question": "What is the most common position in the NBA", "sql": "SELECT POS, COUNT(*) AS num_players FROM nba_roster WHERE POS!= 'NA' GROUP BY POS ORDER BY num_players DESC LIMIT 1"}
{"question": "What is the average salary for each team in the NBA", "sql": "SELECT Team, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY Team"}
{"question": "What is the average age of all players in the NBA", "sql": "SELECT AVG(AGE) FROM nba_roster"}
{"question": "What are the top 10 highest-paid players in the NBA", "sql": "SELECT name, team, CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) as salary FROM nba_roster WHERE SALARY!= '--' ORDER BY salary DESC LIMIT 10"}
{"question": "What is the average age of players for each position in the NBA, excluding players with unknown ages", "sql": "SELECT POS, AVG(AGE) AS Average_Age FROM nba_roster WHERE AGE IS NOT NULL GROUP BY POS ORDER BY Average_Age DESC"}
{"question": "What college has produced the most players in the NBA", "sql": "SELECT COLLEGE, COUNT(*) AS Number_of_Players FROM nba_roster WHERE COLLEGE IS NOT NULL GROUP BY COLLEGE ORDER BY Number_of_Players DESC LIMIT 1"}
{"question": "Which team has the tallest average height", "sql": "SELECT team, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER) + CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as average_height FROM nba_roster GROUP BY team ORDER BY average_height DESC LIMIT 1"}
{"question": "What is the average age of all players in the NBA roster", "sql": "SELECT AVG(AGE) AS average_age FROM nba_roster"}
{"question": "What are the names of the players in the NBA who are older than 25 and have a recorded age", "sql": "SELECT NAME FROM nba_roster WHERE AGE > 25 AND AGE IS NOT NULL;"}
{"question": "What is the most common position in the NBA", "sql": "SELECT POS, COUNT(*) as count FROM nba_roster WHERE POS!= 'NA' GROUP BY POS ORDER BY count DESC LIMIT 1"}
{"question": "Which NBA team has the most players who are 6'8", "sql": "SELECT team, COUNT(*) AS num_players FROM nba_roster WHERE CAST(SUBSTR(HT, 1, INSTR(HT,'')-1) AS INTEGER) = 68 GROUP BY team ORDER BY num_players DESC LIMIT 1"}
{"question": "What are the 5 teams with the most players from the same college who play center in the NBA", "sql": "SELECT c.name, c.HT, c.COLLEGE, t.team FROM nba_roster c JOIN nba_roster t ON c.COLLEGE = t.COLLEGE WHERE c.POS = 'C' AND t.POS = 'C' GROUP BY c.COLLEGE, t.team ORDER BY COUNT(*) DESC LIMIT 5"}
{"question": "What is the average age of the players on the Los Angeles Lakers", "sql": "SELECT AVG(AGE) FROM nba_roster WHERE team='Los Angeles Lakers';"}
{"question": "What team has the largest roster in the NBA", "sql": "SELECT team, COUNT(*) as num_players FROM nba_roster GROUP BY team ORDER BY num_players DESC LIMIT 1"}
{"question": "What is the average age of the players in the NBA roster", "sql": "SELECT AVG(AGE) as avg_age FROM nba_roster WHERE AGE IS NOT NULL"}
{"question": "What is the average age of all players in the NBA", "sql": "SELECT AVG(AGE) FROM nba_roster"}
{"question": "What is the average age of all players in the NBA roster", "sql": "SELECT AVG(AGE) AS average_age FROM nba_roster"}
{"question": "What is the average age of all players in the NBA", "sql": "SELECT AVG(AGE) AS average_age FROM nba_roster"}
{"question": "What is the average age of players in the NBA", "sql": "SELECT AVG(AGE) FROM nba_roster"}
{"question": "Who are the top 5 highest-paid players in the NBA", "sql": "SELECT NAME, SALARY FROM nba_roster ORDER BY CAST(SUBSTR(SALARY, 2) AS INTEGER) DESC LIMIT 5"}
{"question": "How many players are currently on the Toronto Raptors roster", "sql": "SELECT COUNT(*) FROM nba_roster WHERE Team='Toronto Raptors';"}
{"question": "What is the average age of players on each team in the NBA", "sql": "SELECT team, AVG(AGE) AS average_age FROM nba_roster WHERE AGE IS NOT NULL GROUP BY team ORDER BY average_age ASC"}
{"question": "What is the most common position in the NBA", "sql": "SELECT POS, COUNT(*) AS count_players FROM nba_roster WHERE POS IS NOT NULL GROUP BY POS ORDER BY count_players DESC"}
{"question": "What is the most common position in the NBA", "sql": "SELECT POS, COUNT(*) as count FROM nba_roster GROUP BY POS ORDER BY count DESC LIMIT 1"}
{"question": "What is the age range of the players in the NBA", "sql": "SELECT MIN(AGE) AS youngest, MAX(AGE) AS oldest FROM nba_roster;"}
{"question": "What is the average age of all players in the NBA", "sql": "SELECT AVG(AGE) FROM nba_roster"}
{"question": "What is the average height of players on each NBA team", "sql": "SELECT team, AVG(CAST(SUBSTRING(HT, 1, INSTR(HT,'') - 1) AS INTEGER)) AS avg_height FROM nba_roster GROUP BY team"}
{"question": "What team is the oldest player on", "sql": "SELECT team, NAME FROM nba_roster WHERE AGE = (SELECT MAX(AGE) FROM nba_roster WHERE team = nba_roster.team)"}
{"question": "What is the average age of players on each NBA team", "sql": "SELECT team, AVG(AGE) AS avg_age FROM nba_roster WHERE AGE IS NOT NULL GROUP BY team"}
{"question": "What is the average age of players on each team in the NBA", "sql": "SELECT team, AVG(AGE) as average_age FROM nba_roster GROUP BY team ORDER BY average_age ASC"}
{"question": "What is the most represented college among NBA players", "sql": "SELECT COLLEGE, COUNT(*) as frequency FROM nba_roster GROUP BY COLLEGE ORDER BY frequency DESC LIMIT 1"}
{"question": "What are the ages of the oldest players in the NBA", "sql": "SELECT name, age FROM nba_roster WHERE age >= 25 ORDER BY age;"}
{"question": "What is the average age of all players in the NBA", "sql": "SELECT AVG(AGE) AS average_age FROM nba_roster"}
{"question": "What are the top 3 teams with the highest average salary in the NBA", "sql": "SELECT team, AVG(SALARY) AS average_salary FROM nba_roster GROUP BY team ORDER BY average_salary DESC LIMIT 3"}
{"question": "What is the average age of all players in the NBA", "sql": "SELECT AVG(AGE) FROM nba_roster"}
{"question": "What is the most common height in the NBA roster", "sql": "SELECT HT, COUNT(*) AS count FROM nba_roster WHERE HT IS NOT NULL GROUP BY HT ORDER BY count DESC LIMIT 1"}
{"question": "What are the most common positions in the NBA", "sql": "SELECT POS, COUNT(*) as count FROM nba_roster GROUP BY POS ORDER BY count DESC"}
{"question": "What is the average age of the players in the NBA", "sql": "SELECT AVG(AGE) as average_age FROM nba_roster"}
{"question": "What is the most successful college program in terms of producing NBA players", "sql": "SELECT COLLEGE, COUNT(*) as count FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY count DESC LIMIT 1"}
{"question": "Which players have played for the most teams in the NBA", "sql": "SELECT name, COUNT(DISTINCT team) AS num_teams FROM nba_roster GROUP BY name ORDER BY num_teams DESC LIMIT 5"}
{"question": "What are the top 5 teams in the NBA by average salary", "sql": "SELECT Team, AVG(CAST(SUBSTR(SALARY, 1, INSTR(SALARY, '$')-1) AS INTEGER)) as Average_Salary FROM nba_roster WHERE SALARY!= '--' GROUP BY Team ORDER BY Average_Salary DESC"}
{"question": "What is the most represented college in the NBA", "sql": "SELECT COLLEGE, COUNT(*) as Count FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY Count DESC LIMIT 1"}
{"question": "What are the 5 positions with the highest average salary in the NBA", "sql": "SELECT POS, NAME, CAST(SUBSTR(SALARY, 1, INSTR(SALARY, '$')-1) AS INTEGER) AS salary FROM nba_roster WHERE POS IN (SELECT POS FROM nba_roster GROUP BY POS ORDER BY AVG(CAST(SUBSTR(SALARY, 1, INSTR(SALARY, '$')-1) AS INTEGER)) DESC LIMIT 5) ORDER BY POS, salary DESC"}
{"question": "How many players in the NBA are 6 feet 8 inches tall", "sql": "SELECT COUNT(*) as num_players FROM nba_roster WHERE CAST(SUBSTRING(HT, 1, INSTR(HT,'')-1) AS INTEGER) = '6' AND SUBSTRING(HT, INSTR(HT,'')+1) = '8';"}
{"question": "What is the most common height range among NBA players", "sql": "SELECT HT, COUNT(*) AS count FROM nba_roster GROUP BY HT ORDER BY count DESC;"}
{"question": "What is the average age of the Brooklyn Nets players", "sql": "SELECT AVG(AGE) FROM nba_roster WHERE TEAM = 'Brooklyn Nets';"}
{"question": "What is the team with the highest average salary in the NBA", "sql": "SELECT TEAM, AVG(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) AS AVG_SALARY FROM nba_roster GROUP BY TEAM ORDER BY AVG_SALARY DESC LIMIT 1"}
{"question": "What is the average height for each position in the NBA", "sql": "SELECT POS, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,'')-1) AS INTEGER)) AS AVG_HEIGHT FROM nba_roster GROUP BY POS"}
{"question": "What is the average height of players at each position in the NBA", "sql": "SELECT AVG(CAST(SUBSTR(HT, INSTR(HT,'')+1) AS INTEGER)) AS AVG_HEIGHT, POS FROM nba_roster GROUP BY POS ORDER BY AVG_HEIGHT DESC;"}
{"question": "Who is the pointguard for the Golden State Warriors?", "answer": "Stephen Curry, Chris Paul, and Cory Joseph", "sql": "select name from nba_roster where team='Golden State Warriors' and POS='PG';"}
{"question": "What is the number of players on the Chicago Bulls who are 25 years old or younger", "answer": "10", "sql": "SELECT COUNT(*) FROM nba_roster WHERE team='Chicago Bulls' AND AGE <= 25;"}
{"question": "Who is the highest-paid player on the Los Angeles Lakers", "answer": "LeBron James", "sql": "SELECT NAME, SALARY FROM nba_roster WHERE team='Los Angeles Lakers' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "Who is the highest paid player in the NBA?", "answer": "Stephen Curry", "sql": "SELECT NAME, salary FROM nba_roster WHERE SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "What team is LaMelo Ball on?", "answer": "Charlotte Hornets", "sql": "select team from nba_roster where name='LaMelo Ball';"}
{"question": "How much does Lonzo Ball weigh?", "answer": "190 lbs", "sql": "select wt from nba_roster where name='Lonzo Ball';"}
{"question": "What college sent the most players to the current NBA?", "answer": "Kentucky", "sql": "select college from nba_roster where college != '--' group by college order by count(*) desc limit 1;"}
{"question": "How old is Lebron James?", "answer": "38", "sql": "select age from nba_roster where name='LeBron James';"}
{"question": "What is the most popular jersey number in the current NBA?", "answer": "8", "sql": "select Jersey from nba_roster where Jersey != 'NA' group by Jersey order by count(*) desc limit 1;"}
{"question": "Can you give me a list of all the players without college data?", "answer": "['Bogdan Bogdanovic', 'Clint Capela', 'Kristaps Porzingis', 'Darius Bazley', 'LaMelo Ball', 'Theo Maledon', 'James Nnaji', 'Frank Ntilikina', 'Marko Simonovic', 'Raul Neto', 'Ricky Rubio', 'Luka Doncic', 'Dante Exum', 'Jaden Hardy', 'Maxi Kleber', 'Vlatko Cancar', 'Nikola Jokic', 'Bojan Bogdanovic', 'Malcolm Cazalon', 'Killian Hayes', 'Ausar Thompson', 'Jonathan Kuminga', 'Dario Saric', 'Jalen Green', 'Boban Marjanovic', 'Alperen Sengun', 'Amen Thompson', 'Serge Ibaka', 'Daniel Theis', 'Nicolas Batum', 'KJ Martin', 'Kenyon Martin Jr.', 'Ivica Zubac', 'LeBron James', 'Vincent Valerio-Bodon', 'Tarik Biberovic', 'John Konchar', 'Isaiah Todd', 'Nikola Jovic', 'Giannis Antetokounmpo', 'Thanasis Antetokounmpo', 'MarJon Beauchamp', 'Goran Dragic', 'Rudy Gobert', 'Vit Krejci', 'Daishen Nix', 'Dyson Daniels', 'Willy Hernangomez', 'Jonas Valanciunas', 'Evan Fournier', 'Isaiah Hartenstein', 'Jaylen Martin', 'Mitchell Robinson', 'Davis Bertans', 'Ousmane Dieng', 'Josh Giddey', 'Vasilije Micic', 'Aleksej Pokusevski', 'Goga Bitadze', 'Joe Ingles', 'Furkan Korkmaz', 'Bismack Biyombo', 'Ibou Badji', 'Scoot Henderson', 'Jusuf Nurkic', 'Anfernee Simons', 'Sasha Vezenkov', 'Dominick Barlow', 'Sidy Cissoko', 'Cedi Osman', 'Victor Wembanyama', 'Dennis Schroder', 'Simone Fontecchio', 'Luka Samanic', 'Dennis Schroder', 'Deni Avdija', 'Bilal Coulibaly', 'Danilo Gallinari', 'Tristan Vukcevic']", "sql": "SELECT name FROM nba_roster WHERE COLLEGE IS NULL OR COLLEGE = '--';"}
{"question": "What team has the smallest roster?", "answer": "Brooklyn Nets", "sql": "select team from nba_roster group by team order by count(*) asc limit 1;"}
{"question": "What team has the largest roster?", "answer": "Toronto Raptors", "sql": "select team, count(*) from nba_roster group by team order by count(*) desc limit 1;"}
{"question": "What team is paying its players the most in total?", "answer": "Toronto Raptors", "sql": "select team, sum(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) from nba_roster group by team order by sum(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) desc limit 1;"}
{"question": "Which team is paying its players the least?", "answer": "San Antonio Spurs", "sql": "select team from nba_roster group by team order by sum(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) asc limit 1;"}
{"question": "Which team is on average the tallest?", "answer": "Boston Celtics", "sql": "select team, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as height from nba_roster group by team order by height desc limit 1;"}
{"question": "Which team is on average the shortest?", "answer": "Golden State Warriors", "sql": "select team, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as height from nba_roster group by team order by height asc limit 1;"}
{"question": "Who are the tallest 5 centers in the league?", "answer": "Boban Marjanovic, Kristaps Porzingis, Victor Wembanyama, Luke Kornet, Bol Bol", "sql": "SELECT name, HT FROM nba_roster WHERE POS = 'C' ORDER BY HT DESC LIMIT 5;"}
{"question": "Who are the top 5 highest paid power forwards in the league?", "answer": "Kevin Durant, Giannis Antetokounmpo, Anthony Davis, Tobias Harris, Pascal Siakam", "sql": "SELECT NAME, salary FROM nba_roster WHERE POS = 'PF' AND SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 5;"}
{"question": "What is the median salary in the NBA?", "answer": "6012840", "sql": "SELECT (CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as percentile FROM nba_roster WHERE SALARY!= '--' order by percentile limit 1 offset (select count(*) from nba_roster where SALARY != '--')*50/100-1;"}
{"question": "What is the average salary in the NBA?", "answer": "10696803", "sql": "SELECT avg(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as percentile FROM nba_roster WHERE SALARY!= '--';"}
{"question": "What is the 99th percentile salary in the NBA?", "answer": "46741590", "sql": "SELECT (CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as percentile FROM nba_roster WHERE SALARY!= '--' order by percentile limit 1 offset (select count(*) from nba_roster where SALARY != '--')*99/100-1;"}
{"question": "What is the 75th percentile salary in the NBA?", "answer": "13932008", "sql": "SELECT (CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as percentile FROM nba_roster WHERE SALARY!= '--' order by percentile limit 1 offset (select count(*) from nba_roster where SALARY != '--')*75/100-1;"}
{"question": "What is the 25th percentile salary in the NBA?", "answer": "2413304", "sql": "SELECT (CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as percentile FROM nba_roster WHERE SALARY!= '--' order by percentile limit 1 offset (select count(*) from nba_roster where SALARY != '--')*25/100-1;"}
{"question": "What is the median weight in the NBA?", "answer": "215", "sql": "select CAST(SUBSTR(WT, 1, INSTR(WT,' ')) as INTEGER) as percentile from nba_roster order by percentile limit 1 offset (select count(*) from nba_roster)*50/100-1;"}
{"question": "What is the average weight in the NBA?", "answer": "214.98", "sql": "SELECT AVG(CAST(SUBSTR(WT, 1, INSTR(WT,' ')) as INTEGER)) FROM nba_roster;"}
{"question": "What is the median height in the NBA?", "answer": "6.58333333333333", "sql": "select CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12 as percentile from nba_roster order by percentile limit 1 offset (select count(*) from nba_roster)*50/100-1;"}
{"question": "What is the average height in the NBA?", "answer": "6.54986111111111", "sql": "select AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as height from nba_roster;"}
{"question": "Can you tell me how many players are in the NBA?", "answer": "600", "sql": "select count(*) from nba_roster;"}
{"question": "Would you please let me know what the highest paid players are for each position?", "answer": "The highest paid players are Nikola Jokic (C), Paul George (F), Norman Powell (G), Kevin Durant (PF), Stephen Curry (PG), LeBron James (SF), Bradley Beal (SG).", "sql": "SELECT name, pos, MAX(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as max_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY POS;"}
{"question": "Is Jalen Johnson 23 years old?", "answer": "No, Jalen Johnson is 21 years old", "sql": "Select name, age from nba_roster where name='Jalen Johnson';"}
{"question": "Who is the oldest player on the Brooklyn Nets?", "answer": "Spencer Dinwiddie, Dorian Finney-Smith, Royce O'Neale", "sql": "SELECT NAME FROM nba_roster WHERE TEAM = 'Brooklyn Nets' AND AGE = (SELECT MAX(AGE) FROM nba_roster WHERE TEAM = 'Brooklyn Nets');"}
{"question": "Who has the highest salary on the Memphis Grizzlies?", "answer": "Ja Morant", "sql": "select salary, name from nba_roster where team='Memphis Grizzlies' and SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "Which player has the highest salary on the Cleveland Cavaliers?", "answer": "Darius Garland", "sql": "select salary, name from nba_roster where team='Cleveland Cavaliers' and SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "Who is the highest paid center on the Dallas Mavericks?", "answer": "Dereck Lively II", "sql": "select salary, name from nba_roster where team='Dallas Mavericks' and POS='C' and SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "How much is Marcus Smart getting paid?", "answer": "$18,833,712", "sql": "select salary from nba_roster where name='Marcus Smart';"}
{"question": "What's the average age of the Trail Blazers?", "answer": "24", "sql": "select avg(age) from nba_roster where team='Portland Trail Blazers';"}
{"question": "What's the median age of the NBA?", "answer": "25", "sql": "select CAST(AGE as INTEGER) as percentile from nba_roster order by percentile limit 1 offset (select count(*) from nba_roster)*50/100-1;"}
{"question": "What's the median age of the Miami Heat?", "answer": "26", "sql": "select CAST(AGE as INTEGER) as percentile from nba_roster where team='Miami Heat' order by percentile limit 1 offset (select count(*) from nba_roster where team='Miami Heat')*50/100-1;"}
{"question": "What are the 5 teams with the oldest average age in the NBA", "answer": "Golden State Warriors, Milwaukee Bucks, Miami Heat, LA Clippers, Phoenix Suns", "sql": "SELECT team, AVG(AGE) AS average_age FROM nba_roster GROUP BY team ORDER BY average_age DESC LIMIT 5;"}
{"question": "What is the average salary of Power Forward players in the NBA", "answer": "$10948045", "sql": "select avg(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary from nba_roster where POS = 'PF';"}
{"question": "What is the average age of all players in the NBA", "sql": "SELECT AVG(AGE) FROM nba_roster"}
{"question": "Who are the top 5 highest-paid players in the league", "sql": "SELECT NAME, CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) AS Salary, POS FROM nba_roster WHERE SALARY!= '--' ORDER BY Salary DESC LIMIT 5"}
{"question": "What is the average age of players for each team in the NBA", "sql": "SELECT team, AVG(AGE) as average_age FROM nba_roster GROUP BY team ORDER BY average_age ASC"}
{"question": "What is the most represented college in the NBA", "sql": "SELECT COLLEGE, COUNT(*) as num_players FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY num_players DESC"}
{"question": "What is the average height of NBA players", "sql": "SELECT AVG(LENGTH(HT)) FROM nba_roster WHERE HT IS NOT NULL"}
{"question": "What is the average age of all players in the NBA", "sql": "SELECT AVG(AGE) AS average_age FROM nba_roster"}
{"question": "What is the most common position in the NBA", "sql": "SELECT POS, COUNT(*) AS count FROM nba_roster GROUP BY POS ORDER BY count DESC LIMIT 1"}
{"question": "What are the top 5 colleges that have produced the most NBA players", "sql": "SELECT COLLEGE, COUNT(*) as num_players FROM nba_roster WHERE COLLEGE!= '--' GROUP BY COLLEGE ORDER BY num_players DESC LIMIT 5"}
{"question": "What is the average age of all players in the NBA", "sql": "SELECT AVG(AGE) FROM nba_roster"}
{"question": "What are the top 3 teams in the NBA with the highest average salary", "sql": "SELECT team, AVG(CAST(SUBSTR(SALARY, 2) AS INTEGER) / 1000000) AS avg_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY team ORDER BY avg_salary DESC LIMIT 3"}
{"question": "What are the top 5 teams with the most players in the NBA", "sql": "SELECT team, COUNT(*) AS num_players FROM nba_roster GROUP BY team ORDER BY num_players DESC LIMIT 5"}
{"question": "Who are the top 3 highest-paid players on the Golden State Warriors", "sql": "SELECT NAME, SALARY FROM nba_roster WHERE team='Golden State Warriors' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 3"}
{"question": "Who is the tallest player in the NBA", "sql": "SELECT name, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as height FROM nba_roster GROUP BY name ORDER BY height DESC LIMIT 1"}
{"question": "Which five players in the NBA have attended the most different colleges", "sql": "SELECT name, COLLEGE, COUNT(DISTINCT COLLEGE) as num_colleges FROM nba_roster WHERE COLLEGE!= '--' GROUP BY name ORDER BY num_colleges DESC LIMIT 5"}
{"question": "What is the most common position in the NBA", "sql": "SELECT POS, COUNT(*) AS count FROM nba_roster GROUP BY POS ORDER BY count DESC LIMIT 1"}
{"question": "Who is the pointguard for the Golden State Warriors?", "answer": "Stephen Curry, Chris Paul, and Cory Joseph", "sql": "select name from nba_roster where team='Golden State Warriors' and POS='PG';"}
{"question": "What is the number of players on the Chicago Bulls who are 25 years old or younger", "answer": "10", "sql": "SELECT COUNT(*) FROM nba_roster WHERE team='Chicago Bulls' AND AGE <= 25;"}
{"question": "Who is the highest-paid player on the Los Angeles Lakers", "answer": "LeBron James", "sql": "SELECT NAME, SALARY FROM nba_roster WHERE team='Los Angeles Lakers' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "Who is the highest paid player in the NBA?", "answer": "Stephen Curry", "sql": "SELECT NAME, salary FROM nba_roster WHERE SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "What team is LaMelo Ball on?", "answer": "Charlotte Hornets", "sql": "select team from nba_roster where name='LaMelo Ball';"}
{"question": "How much does Lonzo Ball weigh?", "answer": "190 lbs", "sql": "select wt from nba_roster where name='Lonzo Ball';"}
{"question": "What college sent the most players to the current NBA?", "answer": "Kentucky", "sql": "select college from nba_roster where college != '--' group by college order by count(*) desc limit 1;"}
{"question": "How old is Lebron James?", "answer": "38", "sql": "select age from nba_roster where name='LeBron James';"}
{"question": "What is the most popular jersey number in the current NBA?", "answer": "8", "sql": "select Jersey from nba_roster where Jersey != 'NA' group by Jersey order by count(*) desc limit 1;"}
{"question": "Can you give me a list of all the players without college data?", "answer": "['Bogdan Bogdanovic', 'Clint Capela', 'Kristaps Porzingis', 'Darius Bazley', 'LaMelo Ball', 'Theo Maledon', 'James Nnaji', 'Frank Ntilikina', 'Marko Simonovic', 'Raul Neto', 'Ricky Rubio', 'Luka Doncic', 'Dante Exum', 'Jaden Hardy', 'Maxi Kleber', 'Vlatko Cancar', 'Nikola Jokic', 'Bojan Bogdanovic', 'Malcolm Cazalon', 'Killian Hayes', 'Ausar Thompson', 'Jonathan Kuminga', 'Dario Saric', 'Jalen Green', 'Boban Marjanovic', 'Alperen Sengun', 'Amen Thompson', 'Serge Ibaka', 'Daniel Theis', 'Nicolas Batum', 'KJ Martin', 'Kenyon Martin Jr.', 'Ivica Zubac', 'LeBron James', 'Vincent Valerio-Bodon', 'Tarik Biberovic', 'John Konchar', 'Isaiah Todd', 'Nikola Jovic', 'Giannis Antetokounmpo', 'Thanasis Antetokounmpo', 'MarJon Beauchamp', 'Goran Dragic', 'Rudy Gobert', 'Vit Krejci', 'Daishen Nix', 'Dyson Daniels', 'Willy Hernangomez', 'Jonas Valanciunas', 'Evan Fournier', 'Isaiah Hartenstein', 'Jaylen Martin', 'Mitchell Robinson', 'Davis Bertans', 'Ousmane Dieng', 'Josh Giddey', 'Vasilije Micic', 'Aleksej Pokusevski', 'Goga Bitadze', 'Joe Ingles', 'Furkan Korkmaz', 'Bismack Biyombo', 'Ibou Badji', 'Scoot Henderson', 'Jusuf Nurkic', 'Anfernee Simons', 'Sasha Vezenkov', 'Dominick Barlow', 'Sidy Cissoko', 'Cedi Osman', 'Victor Wembanyama', 'Dennis Schroder', 'Simone Fontecchio', 'Luka Samanic', 'Dennis Schroder', 'Deni Avdija', 'Bilal Coulibaly', 'Danilo Gallinari', 'Tristan Vukcevic']", "sql": "SELECT name FROM nba_roster WHERE COLLEGE IS NULL OR COLLEGE = '--';"}
{"question": "What team has the smallest roster?", "answer": "Brooklyn Nets", "sql": "select team from nba_roster group by team order by count(*) asc limit 1;"}
{"question": "What team has the largest roster?", "answer": "Toronto Raptors", "sql": "select team, count(*) from nba_roster group by team order by count(*) desc limit 1;"}
{"question": "What team is paying its players the most in total?", "answer": "Toronto Raptors", "sql": "select team, sum(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) from nba_roster group by team order by sum(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) desc limit 1;"}
{"question": "Which team is paying its players the least?", "answer": "San Antonio Spurs", "sql": "select team from nba_roster group by team order by sum(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) asc limit 1;"}
{"question": "Which team is on average the tallest?", "answer": "Boston Celtics", "sql": "select team, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as height from nba_roster group by team order by height desc limit 1;"}
{"question": "Which team is on average the shortest?", "answer": "Golden State Warriors", "sql": "select team, AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as height from nba_roster group by team order by height asc limit 1;"}
{"question": "Who are the tallest 5 centers in the league?", "answer": "Boban Marjanovic, Kristaps Porzingis, Victor Wembanyama, Luke Kornet, Bol Bol", "sql": "SELECT name, HT FROM nba_roster WHERE POS = 'C' ORDER BY HT DESC LIMIT 5;"}
{"question": "Who are the top 5 highest paid power forwards in the league?", "answer": "Kevin Durant, Giannis Antetokounmpo, Anthony Davis, Tobias Harris, Pascal Siakam", "sql": "SELECT NAME, salary FROM nba_roster WHERE POS = 'PF' AND SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 5;"}
{"question": "What is the median salary in the NBA?", "answer": "6012840", "sql": "SELECT (CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as percentile FROM nba_roster WHERE SALARY!= '--' order by percentile limit 1 offset (select count(*) from nba_roster where SALARY != '--')*50/100-1;"}
{"question": "What is the average salary in the NBA?", "answer": "10696803", "sql": "SELECT avg(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as percentile FROM nba_roster WHERE SALARY!= '--';"}
{"question": "What is the 99th percentile salary in the NBA?", "answer": "46741590", "sql": "SELECT (CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as percentile FROM nba_roster WHERE SALARY!= '--' order by percentile limit 1 offset (select count(*) from nba_roster where SALARY != '--')*99/100-1;"}
{"question": "What is the 75th percentile salary in the NBA?", "answer": "13932008", "sql": "SELECT (CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as percentile FROM nba_roster WHERE SALARY!= '--' order by percentile limit 1 offset (select count(*) from nba_roster where SALARY != '--')*75/100-1;"}
{"question": "What is the 25th percentile salary in the NBA?", "answer": "2413304", "sql": "SELECT (CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as percentile FROM nba_roster WHERE SALARY!= '--' order by percentile limit 1 offset (select count(*) from nba_roster where SALARY != '--')*25/100-1;"}
{"question": "What is the median weight in the NBA?", "answer": "215", "sql": "select CAST(SUBSTR(WT, 1, INSTR(WT,' ')) as INTEGER) as percentile from nba_roster order by percentile limit 1 offset (select count(*) from nba_roster)*50/100-1;"}
{"question": "What is the average weight in the NBA?", "answer": "214.98", "sql": "SELECT AVG(CAST(SUBSTR(WT, 1, INSTR(WT,' ')) as INTEGER)) FROM nba_roster;"}
{"question": "What is the median height in the NBA?", "answer": "6.58333333333333", "sql": "select CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12 as percentile from nba_roster order by percentile limit 1 offset (select count(*) from nba_roster)*50/100-1;"}
{"question": "What is the average height in the NBA?", "answer": "6.54986111111111", "sql": "select AVG(CAST(SUBSTR(HT, 1, INSTR(HT,' ')-1) AS INTEGER)+ CAST(SUBSTR(HT, INSTR(HT,' ')+1) AS FLOAT)/12) as height from nba_roster;"}
{"question": "Can you tell me how many players are in the NBA?", "answer": "600", "sql": "select count(*) from nba_roster;"}
{"question": "Would you please let me know what the highest paid players are for each position?", "answer": "The highest paid players are Nikola Jokic (C), Paul George (F), Norman Powell (G), Kevin Durant (PF), Stephen Curry (PG), LeBron James (SF), Bradley Beal (SG).", "sql": "SELECT name, pos, MAX(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as max_salary FROM nba_roster WHERE SALARY!= '--' GROUP BY POS;"}
{"question": "Is Jalen Johnson 23 years old?", "answer": "No, Jalen Johnson is 21 years old", "sql": "Select name, age from nba_roster where name='Jalen Johnson';"}
{"question": "Who is the oldest player on the Brooklyn Nets?", "answer": "Spencer Dinwiddie, Dorian Finney-Smith, Royce O'Neale", "sql": "SELECT NAME FROM nba_roster WHERE TEAM = 'Brooklyn Nets' AND AGE = (SELECT MAX(AGE) FROM nba_roster WHERE TEAM = 'Brooklyn Nets');"}
{"question": "Who has the highest salary on the Memphis Grizzlies?", "answer": "Ja Morant", "sql": "select salary, name from nba_roster where team='Memphis Grizzlies' and SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "Which player has the highest salary on the Cleveland Cavaliers?", "answer": "Darius Garland", "sql": "select salary, name from nba_roster where team='Cleveland Cavaliers' and SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "Who is the highest paid center on the Dallas Mavericks?", "answer": "Dereck Lively II", "sql": "select salary, name from nba_roster where team='Dallas Mavericks' and POS='C' and SALARY!= '--' ORDER BY CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER) DESC LIMIT 1;"}
{"question": "How much is Marcus Smart getting paid?", "answer": "$18,833,712", "sql": "select salary from nba_roster where name='Marcus Smart';"}
{"question": "What's the average age of the Trail Blazers?", "answer": "24", "sql": "select avg(age) from nba_roster where team='Portland Trail Blazers';"}
{"question": "What's the median age of the NBA?", "answer": "25", "sql": "select CAST(AGE as INTEGER) as percentile from nba_roster order by percentile limit 1 offset (select count(*) from nba_roster)*50/100-1;"}
{"question": "What's the median age of the Miami Heat?", "answer": "26", "sql": "select CAST(AGE as INTEGER) as percentile from nba_roster where team='Miami Heat' order by percentile limit 1 offset (select count(*) from nba_roster where team='Miami Heat')*50/100-1;"}
{"question": "What are the 5 teams with the oldest average age in the NBA", "answer": "Golden State Warriors, Milwaukee Bucks, Miami Heat, LA Clippers, Phoenix Suns", "sql": "SELECT team, AVG(AGE) AS average_age FROM nba_roster GROUP BY team ORDER BY average_age DESC LIMIT 5;"}
{"question": "What is the average salary of Power Forward players in the NBA", "answer": "$10948045", "sql": "select avg(CAST(REPLACE(REPLACE(SALARY, '$', ''), ',','') AS INTEGER)) as average_salary from nba_roster where POS = 'PF';"}

Some files were not shown because too many files have changed in this diff Show More