in this input way give for google/gemma-3-12b-it-qat-int4-unquantized this llm deployment
Assistant
To adapt the FastAPI application for the Google Gemma 3-12B model, you'll need to make a few changes to the code. Here's how you can do it:
Update the model name to Google Gemma 3-12B.
Ensure the tokenizer and model are correctly loaded.
Adjust the inference function to handle the specific input format and parameters for the Google Gemma model.
Here's the updated code:
python
from fastapi import FastAPI, Query
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
app = FastAPI()
MODEL_NAME = "google/gemma-3-12b-it-qat-int4-unquantized"
print("Loading model and tokenizer...")
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
MODEL_NAME,
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
device_map="auto",
trust_remote_code=True
)
model.eval()
print("Model loaded successfully!")
@app.get("/inference")
async def generate_gemma_response(
role: str = Query(...),
prompt: str = Query(...),
content: str = Query(...),
max_tokens: int = Query(256),
temperature: float = Query(0.7),
top_p: float = Query(0.95),
top_k: int = Query(50),
):
messages = [{"role": role, "question": prompt, "content": content}]
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
enable_thinking=False
)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
with torch.no_grad():
generated_ids = model.generate(
**model_inputs,
max_new_tokens=max_tokens,
temperature=temperature,
top_p=top_p,
top_k=top_k,
do_sample=True,
eos_token_id=tokenizer.eos_token_id,
)
output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
THINK_END_TOKEN_ID = 151668 # This might need to be adjusted for the Gemma model
try:
index = len(output_ids) - output_ids[::-1].index(THINK_END_TOKEN_ID)
except ValueError:
index = 0
thinking_content = tokenizer.decode(output_ids[:index], skip_special_tokens=True).strip("\n")
final_content = tokenizer.decode(output_ids[index:], skip_special_tokens=True).strip("\n")
return {
"prompt": prompt,
"thinking_content": thinking_content,
"response": final_content
}
Key Changes:
Model Name: Updated to google/gemma-3-12b-it-qat-int4-unquantized.
Tokenizer and Model Loading: Ensured the tokenizer and model are loaded correctly.
Inference Function: Adjusted the inference function to handle the input format and parameters for the Gemma model.
Make sure to verify the THINK_END_TOKEN_ID for the Gemma model, as it might differ from the DeepSeek model. If the Gemma model doesn't use a specific think end token, you might need to adjust the logic accordingly.