(English below)
Model Card cho ricepaper/vi-gemma-2-9b-function-calling
Mรด tแบฃ Mรด hรฌnh
ricepaper/vi-gemma-2-9b-function-calling lร mรด hรฌnh ngรดn ngแปฏ lแปn ฤฦฐแปฃc tinh chแปnh tแปซ google/gemma-2-9b-it cho khแบฃ nฤng hiแปu vร thแปฑc thi single/multi function call (gแปi hร m) tแปi ฦฐu cho 2 ngรดn ngแปฏ chรญnh: tiแบฟng Viแปt vร tiแบฟng Anh. Mรด hรฌnh ฤฦฐแปฃc huแบฅn luyแปn vแปi tแบญp dแปฏ liแปu phong phรบ bao gแปm cรกc ฤoแบกn hแปi thoแบกi chแปฉa function call theo ฤแปnh dแบกng ChatML, kแบฟt hแปฃp vแปi tแบญp dแปฏ liแปu ฤa ngรดn ngแปฏ ฤฦฐแปฃc dแปch sang tiแบฟng Viแปt.
Mแปฅc ฤรญch Sแปญ dแปฅng
Mรด hรฌnh nร y phรน hแปฃp cho cรกc แปฉng dแปฅng yรชu cแบงu:
- Xรขy dแปฑng chatbot cรณ khแบฃ nฤng tฦฐฦกng tรกc vแปi ngฦฐแปi dรนng vร thแปฑc thi cรกc tรกc vแปฅ cแปฅ thแป thรดng qua function call.
- Tแบกo cรกc hแป thแปng hแปi ฤรกp tแปฑ ฤแปng cรณ khแบฃ nฤng truy xuแบฅt thรดng tin tแปซ cรกc nguแปn dแปฏ liแปu khรกc nhau.
- Phรกt triแปn cรกc แปฉng dแปฅng xแปญ lรฝ ngรดn ngแปฏ tแปฑ nhiรชn nรขng cao nhฦฐ tรณm tแบฏt vฤn bแบฃn, dแปch mรกy, tแบกo vฤn bแบฃn.
- Xรขy dแปฑng agent: Tแบกo cรกc agent thรดng minh cรณ khแบฃ nฤng tฦฐฦกng tรกc vแปi mรดi trฦฐแปng vร thแปฑc hiแปn cรกc hร nh ฤแปng dแปฑa trรชn ngรดn ngแปฏ.
- Hแป thแปng multi-agent: Phรกt triแปn cรกc hแป thแปng ฤa tรกc tแปญ, trong ฤรณ cรกc agent cรณ thแป giao tiแบฟp vร hแปฃp tรกc vแปi nhau ฤแป giแบฃi quyแบฟt cรกc vแบฅn ฤแป phแปฉc tแบกp.
Cรกch sแปญ dแปฅng
1. Cร i ฤแบทt cรกc thฦฐ viแปn cแบงn thiแบฟt:
! pip install transformers torch
2. Khแปi tแบกo tokenizer vร model:
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
import json
# Khแปi tแบกo tokenizer vร model
tokenizer = AutoTokenizer.from_pretrained("ricepaper/vi-gemma-2-9b-function-calling")
model = AutoModelForCausalLM.from_pretrained(
"ricepaper/vi-gemma-2-9b-function-calling",
device_map="auto",
torch_dtype=torch.bfloat16,
)
3. Xรขy dแปฑng hร m xแปญ lรฝ user query:
def process_user_query(user_query, messages, available_tools):
"""
Xแปญ lรฝ user query, tแบกo response, kiแปm tra vร thแปฑc thi function call (nแบฟu cรณ).
Args:
user_query (str): Query tแปซ ngฦฐแปi dรนng.
messages (list): List messages hiแปn tแบกi trong conversation.
available_tools (dict): Dictionary chแปฉa cรกc function cรณ sแบตn.
Returns:
str: Response cuแปi cรนng sau khi xแปญ lรฝ function call (nแบฟu cรณ).
"""
# Thรชm user query vร o messages
messages.append({"role": "user", "content": user_query})
# Tแบกo response tแปซ model
input_ids = tokenizer.apply_chat_template(
messages,
add_generation_prompt=True,
return_tensors="pt"
).to(model.device)
outputs = model.generate(
input_ids,
max_new_tokens=300,
# ... (Cรกc tham sแป generate khรกc)
)
response = tokenizer.decode(outputs[0][input_ids.shape[-1]:], skip_special_tokens=True)
try:
# Chuyแปn ฤแปi chuแปi JSON thร nh list Python
response_list = json.loads(response)
# Thรชm response vร o messages nแบฟu cรณ functioncall
messages.append({"role": "assistant", "content": response})
except json.JSONDecodeError:
# Nแบฟu response khรดng phแบฃi JSON, coi nhฦฐ khรดng cรณ function call
response_list = []
# Khแปi tแบกo list function_responses ฤแป lฦฐu kแบฟt quแบฃ
function_responses = []
# Duyแปt qua tแปซng phแบงn tแปญ trong list
for response_dict in response_list:
if "name" in response_dict and "arguments" in response_dict:
function_name = response_dict.get("name")
function_args = response_dict.get("arguments")
if function_name in available_tools:
# Thแปฑc hiแปn function call
print(f"Calling function {function_name} with arguments {function_args}\n")
function_to_call = available_tools[function_name]
function_response = function_to_call(**function_args)
# Lฦฐu kแบฟt quแบฃ dฦฐแปi dแบกng dictionary
function_responses.append({
"name": function_name,
"response": function_response
})
else:
print(f"Function {function_name} not found")
# Thรชm list function_responses vร o messages
if function_responses:
messages.append({
"role": "user",
"content": f"FUNCTION RESPONSES:\n{json.dumps(function_responses, ensure_ascii=False)}"
})
print(messages[-1].get("content"))
# Tแบกo response mแปi sau khi xแปญ lรฝ function call
input_ids = tokenizer.apply_chat_template(
messages,
add_generation_prompt=True,
return_tensors="pt"
).to(model.device)
outputs = model.generate(
input_ids,
max_new_tokens=300,
# ... (Cรกc tham sแป generate khรกc)
)
response = tokenizer.decode(outputs[0][input_ids.shape[-1]:], skip_special_tokens=True)
return response
4. Tแบกo cรกc hร m hแป trแปฃ vร khai bรกo danh sรกch tools:
## Hร m mรด phแปng hแป trแปฃ tรญnh boa cho mแปt hรณa ฤฦกn
def calculate_tip(bill_amount: float, tip_percentage: float) -> str:
"""Tรญnh sแป tiแปn boa cho mแปt hรณa ฤฦกn vร trแบฃ vแป mแปt chuแปi mรด tแบฃ kแบฟt quแบฃ.
Args:
bill_amount: Tแปng sแป tiแปn cแปงa hรณa ฤฦกn.
tip_percentage: Tแปท lแป tiแปn boa.
Returns:
Mแปt chuแปi mรด tแบฃ sแป tiแปn boa vร tแปng sแป tiแปn phแบฃi trแบฃ.
"""
tip_amount = bill_amount * (tip_percentage / 100)
total_amount = bill_amount + tip_amount
return f"Sแป tiแปn boa lร : {tip_amount:.2f}\nTแปng sแป tiแปn phแบฃi trแบฃ lร : {total_amount:.2f}"
# Khai bรกo danh sรกch tools
tools = """
{
"name": "calculate_tip",
"description": "Tรญnh sแป tiแปn boa cho mแปt hรณa ฤฦกn",
"parameters": {
"type": "object",
"properties": {
"bill_amount": {
"type": "number",
"description": "Tแปng sแป tiแปn cแปงa hรณa ฤฦกn"
},
"tip_percentage": {
"type": "number",
"description": "Tแปท lแป tiแปn boa"
}
},
"required": [
"bill_amount",
"tip_percentage"
]
}
},
"""
# Tแบกo dictionary รกnh xแบก tรชn hร m vแปi hร m tฦฐฦกng แปฉng
available_tools = {
"calculate_tip": calculate_tip,
}
5. Tแบกo lแปch sแปญ trรฒ chuyแปn vร sแปญ dแปฅng:
# Tแบกo lแปch sแปญ trรฒ chuyแปn mแปi
messages = [
{"role": "user", "content": f"""Bแบกn lร mแปt trแปฃ lรฝ hแปฏu รญch vแปi quyแปn truy cแบญp vร o cรกc chแปฉc nฤng sau. Sแปญ dแปฅng chรบng nแบฟu cแบงn thiแบฟt {tools}"""},
{"role": "assistant", "content": "Xin chร o, tรดi cรณ thแป giรบp gรฌ cho bแบกn?"},
]
# Sแปญ dแปฅng
res = process_user_query("Tรดi cแบงn trแปฃ giรบp tรญnh tiแปn boa cho hรณa ฤฦกn cแปงa mรฌnh. Tแปng sแป tiแปn lร 50 USD vร tรดi muแปn ฤแป lแบกi 15% tiแปn boa?", messages, available_tools)
messages.append({"role": "assistant", "content": res})
print("\n"+res)
# Calling function calculate_tip with arguments {'bill_amount': 50, 'tip_percentage': 15}
# FUNCTION RESPONSES:
# [{"name": "calculate_tip", "response": "Sแป tiแปn boa lร : 7.50\nTแปng sแป tiแปn phแบฃi trแบฃ lร : 57.50"}]
# Sแป tiแปn boa cho hรณa ฤฦกn cแปงa bแบกn lร 7,50 USD. Tแปng sแป tiแปn phแบฃi trแบฃ lร 57,50 USD.
messages
# [{'role': 'user',
# 'content': 'Bแบกn lร mแปt trแปฃ lรฝ hแปฏu รญch vแปi quyแปn truy cแบญp vร o cรกc chแปฉc nฤng sau. Sแปญ dแปฅng chรบng nแบฟu cแบงn thiแบฟt \n{\n "name": "calculate_tip",\n "description": "Tรญnh sแป tiแปn boa cho mแปt hรณa ฤฦกn",\n "parameters": {\n "type": "object",\n "properties": {\n "bill_amount": {\n "type": "number",\n "description": "Tแปng sแป tiแปn cแปงa hรณa ฤฦกn"\n },\n "tip_percentage": {\n "type": "number",\n "description": "Tแปท lแป tiแปn boa"\n }\n },\n "required": [\n "bill_amount",\n "tip_percentage"\n ]\n }\n},\n'},
# {'role': 'assistant', 'content': 'Xin chร o, tรดi cรณ thแป giรบp gรฌ cho bแบกn?'},
# {'role': 'user',
# 'content': 'Tรดi cแบงn trแปฃ giรบp tรญnh tiแปn boa cho hรณa ฤฦกn cแปงa mรฌnh. Tแปng sแป tiแปn lร 50 USD vร tรดi muแปn ฤแป lแบกi 15% tiแปn boa?'},
# {'role': 'assistant',
# 'content': '[{"name": "calculate_tip", "arguments": {"bill_amount": 50, "tip_percentage": 15}}]'},
# {'role': 'user',
# 'content': 'FUNCTION RESPONSES:\n[{"name": "calculate_tip", "response": "Sแป tiแปn boa lร : 7.50\\nTแปng sแป tiแปn phแบฃi trแบฃ lร : 57.50"}]'},
# {'role': 'assistant',
# 'content': 'Sแป tiแปn boa cho hรณa ฤฦกn cแปงa bแบกn lร 7,50 USD. Tแปng sแป tiแปn phแบฃi trแบฃ lร 57,50 USD.'}]
Lฦฐu รฝ
- Mรด hรฌnh cรณ thแป yรชu cแบงu scale chแบฅt lฦฐแปฃng vร cแบฅu hรฌnh phแบงn cแปฉng phรน hแปฃp ฤแป hoแบกt ฤแปng hiแปu quแบฃ.
- Kแบฟt quแบฃ cแปงa function call phแปฅ thuแปc vร o chแบฅt lฦฐแปฃng cแปงa hร m hแป trแปฃ ฤฦฐแปฃc cung cแบฅp.
- Ngฦฐแปi dรนng cรณ thแป thay ฤแปi cรกc tham sแป generate cแปงa mรด hรฌnh ฤแป ฤiแปu chแปnh ฤแป dร i vร nแปi dung cแปงa response.
English model card version:
Model Card for ricepaper/vi-gemma-2-9b-function-calling
Model Description
ricepaper/vi-gemma-2-9b-function-calling is a large language model fine-tuned from google/gemma-2-9b-it for understanding and executing single/multi function calls, optimized for 2 main languages: Vietnamese and English. The model is trained on a rich dataset of conversations containing function calls in ChatML format, combined with multilingual data translated into Vietnamese.
Intended Uses
This model is suitable for applications requiring:
- Building chatbots that can interact with users and perform specific tasks through function calls.
- Creating automated question answering systems capable of retrieving information from various data sources.
- Developing advanced natural language processing applications such as text summarization, machine translation, and text generation.
- Building agents: Creating intelligent agents capable of interacting with the environment and performing actions based on language.
- Multi-agent systems: Developing multi-agent systems where agents can communicate and collaborate to solve complex problems.
How to Use
1. Install necessary libraries:
! pip install transformers torch
2. Initialize the tokenizer and model:
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
import json
# Initialize the tokenizer and model
tokenizer = AutoTokenizer.from_pretrained("ricepaper/vi-gemma-2-9b-function-calling")
model = AutoModelForCausalLM.from_pretrained(
"ricepaper/vi-gemma-2-9b-function-calling",
device_map="auto",
torch_dtype=torch.bfloat16,
)
3. Build a function to process user queries:
def process_user_query(user_query, messages, available_tools):
"""
Processes user queries, generates responses, checks for, and executes function calls (if any).
Args:
user_query (str): The query from the user.
messages (list): The list of current messages in the conversation.
available_tools (dict): A dictionary containing available functions.
Returns:
str: The final response after processing function calls (if any).
"""
# Add the user query to the messages
messages.append({"role": "user", "content": user_query})
# Generate a response from the model
input_ids = tokenizer.apply_chat_template(
messages,
add_generation_prompt=True,
return_tensors="pt"
).to(model.device)
outputs = model.generate(
input_ids,
max_new_tokens=300,
# ... (Other generate parameters)
)
response = tokenizer.decode(outputs[0][input_ids.shape[-1]:], skip_special_tokens=True)
try:
# Convert the JSON string to a Python list
response_list = json.loads(response)
# Add the response to messages if there's a function call
messages.append({"role": "assistant", "content": response})
except json.JSONDecodeError:
# If the response is not JSON, assume no function call
response_list = []
# Initialize a list to store function responses
function_responses = []
# Iterate through each element in the list
for response_dict in response_list:
if "name" in response_dict and "arguments" in response_dict:
function_name = response_dict.get("name")
function_args = response_dict.get("arguments")
if function_name in available_tools:
# Execute the function call
print(f"Calling function {function_name} with arguments {function_args}\n")
function_to_call = available_tools[function_name]
function_response = function_to_call(**function_args)
# Store the result as a dictionary
function_responses.append({
"name": function_name,
"response": function_response
})
else:
print(f"Function {function_name} not found")
# Add the list of function responses to the messages
if function_responses:
messages.append({
"role": "user",
"content": f"FUNCTION RESPONSES:\n{json.dumps(function_responses, ensure_ascii=False)}"
})
print(messages[-1].get("content"))
# Generate a new response after processing function calls
input_ids = tokenizer.apply_chat_template(
messages,
add_generation_prompt=True,
return_tensors="pt"
).to(model.device)
outputs = model.generate(
input_ids,
max_new_tokens=300,
# ... (Other generate parameters)
)
response = tokenizer.decode(outputs[0][input_ids.shape[-1]:], skip_special_tokens=True)
return response
4. Create helper functions and declare the tools list:
## Function simulating tip calculation for a bill
def calculate_tip(bill_amount: float, tip_percentage: float) -> str:
"""Calculates the tip amount for a bill and returns a string describing the result.
Args:
bill_amount: The total amount of the bill.
tip_percentage: The tip percentage.
Returns:
A string describing the tip amount and the total amount to be paid.
"""
tip_amount = bill_amount * (tip_percentage / 100)
total_amount = bill_amount + tip_amount
return f"The tip amount is: {tip_amount:.2f}\nThe total amount to be paid is: {total_amount:.2f}"
# Declare the tools list
tools = """
{
"name": "calculate_tip",
"description": "Calculate the tip amount for a bill",
"parameters": {
"type": "object",
"properties": {
"bill_amount": {
"type": "number",
"description": "The total bill amount"
},
"tip_percentage": {
"type": "number",
"description": "The tip percentage"
}
},
"required": [
"bill_amount",
"tip_percentage"
]
}
},
"""
# Create a dictionary mapping function names to their corresponding functions
available_tools = {
"calculate_tip": calculate_tip,
}
5. Create a new conversation history and use the model:
# Create a new conversation history
messages = [
{"role": "user", "content": f"""You are a helpful assistant with access to the following functions. Use them if necessary {tools}"""},
{"role": "assistant", "content": "Hello, how can I assist you?"},
]
# Use the model
res = process_user_query("I need help calculating the tip for my bill. The total is $50 and I would like to leave a 15% tip.", messages, available_tools)
messages.append({"role": "assistant", "content": res})
print("\n"+res)
# Calling function calculate_tip with arguments {'bill_amount': 50, 'tip_percentage': 15}
# FUNCTION RESPONSES:
# [{"name": "calculate_tip", "response": "The tip amount is: 7.50\nThe total amount to be paid is: 57.50"}]
# The tip amount for your bill is $7.50. The total amount to be paid is $57.50.
messages
# [{'role': 'user',
# 'content': 'You are a helpful assistant with access to the following functions. Use them if necessary \n{\n "name": "calculate_tip",\n "description": "Calculate the tip amount for a bill",\n "parameters": {\n "type": "object",\n "properties": {\n "bill_amount": {\n "type": "number",\n "description": "The total bill amount"\n },\n "tip_percentage": {\n "type": "number",\n "description": "The tip percentage"\n }\n },\n "required": [\n "bill_amount",\n "tip_percentage"\n ]\n }\n},\n'},
# {'role': 'assistant', 'content': 'Hello, how can I assist you?'},
# {'role': 'user',
# 'content': 'I need help calculating the tip for my bill. The total is $50 and I would like to leave a 15% tip.'},
# {'role': 'assistant',
# 'content': '[{"name": "calculate_tip", "arguments": {"bill_amount": 50, "tip_percentage": 15}}]'},
# {'role': 'user',
# 'content': 'FUNCTION RESPONSES:\n[{"name": "calculate_tip", "response": "The tip amount is: 7.50\\nThe total amount to be paid is: 57.50"}]'},
# {'role': 'assistant',
# 'content': 'The tip amount for your bill is $7.50. The total amount to be paid is $57.50.'}]
Uploaded model
- Developed by: hiieu, himmeow the coder, cuctrinh
- Training data provided by: Fifth Civil Defender - 5CD
- License: apache-2.0
- Finetuned from model : unsloth/gemma-2-9b-it-bnb-4bit
This gemma model was trained 2x faster with Unsloth and Huggingface's TRL library.
- Downloads last month
- 21
Model tree for ricepaper/vi-gemma-2-9b-function-calling
Base model
google/gemma-2-9b