Instructions to use defog/sqlcoder-7b-2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use defog/sqlcoder-7b-2 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="defog/sqlcoder-7b-2")# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("defog/sqlcoder-7b-2") model = AutoModelForCausalLM.from_pretrained("defog/sqlcoder-7b-2") - llama-cpp-python
How to use defog/sqlcoder-7b-2 with llama-cpp-python:
# !pip install llama-cpp-python from llama_cpp import Llama llm = Llama.from_pretrained( repo_id="defog/sqlcoder-7b-2", filename="sqlcoder-7b-q5_k_m.gguf", )
output = llm( "Once upon a time,", max_tokens=512, echo=True ) print(output)
- Inference
- Notebooks
- Google Colab
- Kaggle
- Local Apps
- llama.cpp
How to use defog/sqlcoder-7b-2 with llama.cpp:
Install from brew
brew install llama.cpp # Start a local OpenAI-compatible server with a web UI: llama-server -hf defog/sqlcoder-7b-2:Q5_K_M # Run inference directly in the terminal: llama-cli -hf defog/sqlcoder-7b-2:Q5_K_M
Install from WinGet (Windows)
winget install llama.cpp # Start a local OpenAI-compatible server with a web UI: llama-server -hf defog/sqlcoder-7b-2:Q5_K_M # Run inference directly in the terminal: llama-cli -hf defog/sqlcoder-7b-2:Q5_K_M
Use pre-built binary
# Download pre-built binary from: # https://github.com/ggerganov/llama.cpp/releases # Start a local OpenAI-compatible server with a web UI: ./llama-server -hf defog/sqlcoder-7b-2:Q5_K_M # Run inference directly in the terminal: ./llama-cli -hf defog/sqlcoder-7b-2:Q5_K_M
Build from source code
git clone https://github.com/ggerganov/llama.cpp.git cd llama.cpp cmake -B build cmake --build build -j --target llama-server llama-cli # Start a local OpenAI-compatible server with a web UI: ./build/bin/llama-server -hf defog/sqlcoder-7b-2:Q5_K_M # Run inference directly in the terminal: ./build/bin/llama-cli -hf defog/sqlcoder-7b-2:Q5_K_M
Use Docker
docker model run hf.co/defog/sqlcoder-7b-2:Q5_K_M
- LM Studio
- Jan
- vLLM
How to use defog/sqlcoder-7b-2 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "defog/sqlcoder-7b-2" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "defog/sqlcoder-7b-2", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/defog/sqlcoder-7b-2:Q5_K_M
- SGLang
How to use defog/sqlcoder-7b-2 with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "defog/sqlcoder-7b-2" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "defog/sqlcoder-7b-2", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "defog/sqlcoder-7b-2" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "defog/sqlcoder-7b-2", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Ollama
How to use defog/sqlcoder-7b-2 with Ollama:
ollama run hf.co/defog/sqlcoder-7b-2:Q5_K_M
- Unsloth Studio new
How to use defog/sqlcoder-7b-2 with Unsloth Studio:
Install Unsloth Studio (macOS, Linux, WSL)
curl -fsSL https://unsloth.ai/install.sh | sh # Run unsloth studio unsloth studio -H 0.0.0.0 -p 8888 # Then open http://localhost:8888 in your browser # Search for defog/sqlcoder-7b-2 to start chatting
Install Unsloth Studio (Windows)
irm https://unsloth.ai/install.ps1 | iex # Run unsloth studio unsloth studio -H 0.0.0.0 -p 8888 # Then open http://localhost:8888 in your browser # Search for defog/sqlcoder-7b-2 to start chatting
Using HuggingFace Spaces for Unsloth
# No setup required # Open https://huggingface.co/spaces/unsloth/studio in your browser # Search for defog/sqlcoder-7b-2 to start chatting
- Docker Model Runner
How to use defog/sqlcoder-7b-2 with Docker Model Runner:
docker model run hf.co/defog/sqlcoder-7b-2:Q5_K_M
- Lemonade
How to use defog/sqlcoder-7b-2 with Lemonade:
Pull the model
# Download Lemonade from https://lemonade-server.ai/ lemonade pull defog/sqlcoder-7b-2:Q5_K_M
Run and chat with the model
lemonade run user.sqlcoder-7b-2-Q5_K_M
List all available models
lemonade list
Impossible to prompt
I am sorry, but for me it is impossible to prompt.
I am using the model provide through ollama https://ollama.com/library/sqlcoder
Promptformat in ollamas modelfile is:
TEMPLATE """{{ .Prompt }}"""
PARAMETER stop "<|endoftext|>"
My test prompt looks like this:
if prompt := st.chat_input("Enter prompt here.."):
db_schema = f"""
CREATE TABLE stadium (
stadium_id number, --unique id for each stadium
location text, --city and state
name text, --name of the stadium
capacity number, --maximum number of people that can be seated
highest number, --highest number of people that have attended a concert
lowest number, --lowest number of people that have attended a concert
average number --average number of people that have attended a concert
)
CREATE TABLE singer (
singer_id number, --unique id for each singer
name text, --name of the singer
country text, --country of origin of the singer
song_name text, --name of the song
song_release_year text, --year the song was released
age number, --age of the singer
is_male bool --whether or not the singer is male
)
CREATE TABLE concert (
concert_id number, --unique id for each concert
concert_name text, --name of the concert
theme text, --theme of the concert
stadium_id text, --id of the stadium where the concert takes place
year text --year the concert takes place
)
CREATE TABLE singer_in_concert (
concert_id number, --id of the concert
singer_id text --id of the singer
)
-- stadium.stadium_id can be joined with concert.stadium_id
-- singer.singer_id can be joined with singer_in_concert.singer_id
-- concert.concert_id can be joined with singer_in_concert.concert_id
"""
prompt: "Among the artists having concerts in year 2020, which artist has a song whose title equals the name of the stadium on which the concert takes place?"
prompt_format_sqlcoder = f"""
### Task
Generate a single SQL query without any additional Information to answer [QUESTION]{prompt}[/QUESTION]
### Database Schema
The query will run on a database with the following schema:
{db_schema}
### Answer
Given the database schema, here is the SQL query that [QUESTION]{prompt}[/QUESTION]
[SQL]
"""
Then i call ollama api with the data:
data = {
"model": model,
"prompt": prompt_format_sqlcoder,
"stream": stream
}
It is not following instructions, hallucinates tables and allways generates completly different querys, even with the exact same prompt.
I would apreciate some help with the prompt format for the use with ollama
Hi @HuggySSO , the link you shared seemed to be the 4-bit quantized version of our model, just to confirm were you testing on the half-precision or 4-bit quantized version?
I've not used ollama before, but while testing on our model in half-precision on vllm, we got the following results with the following prompt:
prompt = "### Task\nGenerate a SQL query to answer the following question:\n`Among the artists having concerts in year 2020, which artist has a song whose title equals the name of the stadium on which the concert takes place?`\n\n### Schema\nCREATE TABLE stadium (\nstadium_id number, --unique id for each stadium\nlocation text, --city and state\nname text, --name of the stadium\ncapacity number, --maximum number of people that can be seated\nhighest number, --highest number of people that have attended a concert\nlowest number, --lowest number of people that have attended a concert\naverage number --average number of people that have attended a concert\n)\n\nCREATE TABLE singer (\nsinger_id number, --unique id for each singer\nname text, --name of the singer\ncountry text, --country of origin of the singer\nsong_name text, --name of the song\nsong_release_year text, --year the song was released\nage number, --age of the singer\nis_male bool --whether or not the singer is male\n)\n\nCREATE TABLE concert (\nconcert_id number, --unique id for each concert\nconcert_name text, --name of the concert\ntheme text, --theme of the concert\nstadium_id text, --id of the stadium where the concert takes place\nyear text --year the concert takes place\n)\n\nCREATE TABLE singer_in_concert (\nconcert_id number, --id of the concert\nsinger_id text --id of the singer\n)\n\n-- stadium.stadium_id can be joined with concert.stadium_id\n-- singer.singer_id can be joined with singer_in_concert.singer_id\n-- concert.concert_id can be joined with singer_in_concert.concert_id\n\n### Answer\nGiven the database schema above, here is the SQL query that answers the question:\n```sql\n"
result
SELECT s.name FROM singer s JOIN singer_in_concert sic ON s.singer_id = sic.singer_id JOIN concert c ON sic.concert_id = c.concert_id JOIN stadium st ON c.stadium_id = st.stadium_id WHERE s.song_name = st.name AND c.year = '2020'
Does this result match what you expected?
I was testing on different models, the one from the ollama link (15b), and the huggingface version sqlcoder-7b-q5_k_m.gguf
Now, with this prompt format the answers are much more coherent. Thanks a lot!
Unfortunatly there are no indentation in the answer, I don't know if that is part of the prompt
Glad to hear! Yes we intentionally avoid indentation in the answer (during finetuning), as that will increase the number of tokens to be generated and increase the latency and compute demands. We often use the sql directly with a SQL client (eg psycopg2 in python), and hence don't require it to be formatted. That said, if you would like to format the results, you can do so usingsqlparse.format(my_sql_string, reindent=True)
Feel free to check out their docs for more formatting options:
https://sqlparse.readthedocs.io/en/stable/