Views
No views yet
1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3
4# Load the model
5model = AutoModelForCausalLM.from_pretrained("tinyllama-function-calling-cpu-optimized")
6tokenizer = AutoTokenizer.from_pretrained("tinyllama-function-calling-cpu-optimized")
7
8# Example prompt for function calling
9prompt = """### Instruction:
10Given the available functions and the user query, determine which function(s) to call and with what arguments.
11
12Available functions:
13{
14 "name": "get_exchange_rate",
15 "description": "Get the exchange rate between two currencies",
16 "parameters": {
17 "type": "object",
18 "properties": {
19 "base_currency": {
20 "type": "string",
21 "description": "The currency to convert from"
22 },
23 "target_currency": {
24 "type": "string",
25 "description": "The currency to convert to"
26 }
27 },
28 "required": [
29 "base_currency",
30 "target_currency"
31 ]
32 }
33}
34
35User query: What is the exchange rate from USD to EUR?
36
37### Response:"""
38
39# Tokenize and generate response
40inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
41with torch.no_grad():
42 outputs = model.generate(
43 **inputs,
44 max_new_tokens=150,
45 do_sample=True,
46 temperature=0.7,
47 top_k=50,
48 top_p=0.95
49 )
50
51response = tokenizer.decode(outputs[0], skip_special_tokens=True)
52print(response)