Views
No views yet
from transformers import AutoModelForCausalLM, AutoTokenizer, TextStreamer1device = "cuda"
2
3model = AutoModelForCausalLM.from_pretrained("InterSync/Mistral-7B-Instruct-v0.2-Function-Calling")
4tokenizer = AutoTokenizer.from_pretrained("InterSync/Mistral-7B-Instruct-v0.2-Function-Calling")streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)1tools = [
2 {
3 "type": "function",
4 "function": {
5 "name": "get_current_weather",
6 "description": "Get the current weather",
7 "parameters": {
8 "type": "object",
9 "properties": {
10 "location": {
11 "type": "string",
12 "description": "The city and state, e.g. San Francisco, CA",
13 },
14 "format": {
15 "type": "string",
16 "enum": ["celsius", "fahrenheit"],
17 "description": "The temperature unit to use. Infer this from the user's location.",
18 },
19 },
20 "required": ["location", "format"],
21 },
22 }
23 }
24]1messages = [
2 {
3 "role": "user",
4 "content": (
5 "You are Mistral with function-calling supported. You are provided with function signatures within <tools></tools> XML tags. "
6 "You may call one or more functions to assist with the user query. Don't make assumptions about what values to plug into functions. "
7 "Here are the available tools:\n"
8 "<tools>\n"
9 f"{tools}\n"
10 "</tools>\n\n"
11 "For each function call, return a JSON object with the function name and arguments within <tool_call></tool_call> XML tags as follows:\n"
12 "<tool_call>\n"
13 "{'arguments': <args-dict>, 'name': <function-name>}\n"
14 "</tool_call>"
15 )
16 },
17 {
18 "role": "assistant",
19 "content": "How can I help you today?"
20 },
21 {
22 "role": "user",
23 "content": "What is the current weather in San Francisco?"
24 },
25]1inputs = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt")
2model_inputs = inputs.to(device)1model.to(device)
2generate_ids = model.generate(model_inputs, streamer=streamer, do_sample=True, max_length=4096)
3decoded = tokenizer.batch_decode(generate_ids)1<tool_call>
2{"arguments": {"location": "San Francisco, CA", "format": "celsius"}, "name": "get_current_weather"}
3</tool_call>