plaguss commited on
Commit
839a756
·
verified ·
1 Parent(s): 9e364fd

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +119 -5
README.md CHANGED
@@ -18,12 +18,126 @@ It has been trained using [TRL](https://github.com/huggingface/trl).
18
  ## Quick start
19
 
20
  ```python
21
- from transformers import pipeline
 
 
22
 
23
- question = "If you had a time machine, but could only go to the past or the future once and never return, which would you choose and why?"
24
- generator = pipeline("text-generation", model="plaguss/Llama-3.2-1B-Instruct-v2-FC", device="cuda")
25
- output = generator([{"role": "user", "content": question}], max_new_tokens=128, return_full_text=False)[0]
26
- print(output["generated_text"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  ```
28
 
29
  ## Training procedure
 
18
  ## Quick start
19
 
20
  ```python
21
+ import json
22
+ import re
23
+ from typing import Optional
24
 
25
+ from jinja2 import Template
26
+ import torch
27
+ from transformers import AutoModelForCausalLM, AutoTokenizer
28
+ from transformers.utils import get_json_schema
29
+
30
+
31
+ system_prompt = Template("""You are an expert in composing functions. You are given a question and a set of possible functions.
32
+ Based on the question, you will need to make one or more function/tool calls to achieve the purpose.
33
+ If none of the functions can be used, point it out and refuse to answer.
34
+ If the given question lacks the parameters required by the function, also point it out.
35
+
36
+ You have access to the following tools:
37
+ <tools>{{ tools }}</tools>
38
+
39
+ The output MUST strictly adhere to the following format, and NO other text MUST be included.
40
+ The example format is as follows. Please make sure the parameter type is correct. If no function call is needed, please make the tool calls an empty list '[]'.
41
+ <tool_call>[
42
+ {"name": "func_name1", "arguments": {"argument1": "value1", "argument2": "value2"}},
43
+ ... (more tool calls as required)
44
+ ]</tool_call>""")
45
+
46
+
47
+ def prepare_messages(
48
+ query: str,
49
+ tools: Optional[dict[str, any]] = None,
50
+ history: Optional[list[dict[str, str]]] = None
51
+ ) -> list[dict[str, str]]:
52
+ """Prepare the system and user messages for the given query and tools.
53
+
54
+ Args:
55
+ query: The query to be answered.
56
+ tools: The tools available to the user. Defaults to None, in which case if a
57
+ list without content will be passed to the model.
58
+ history: Exchange of messages, including the system_prompt from
59
+ the first query. Defaults to None, the first message in a conversation.
60
+ """
61
+ if tools is None:
62
+ tools = []
63
+ if history:
64
+ messages = history.copy()
65
+ messages.append({"role": "user", "content": query})
66
+ else:
67
+ messages = [
68
+ {"role": "system", "content": system_prompt.render(tools=json.dumps(tools))},
69
+ {"role": "user", "content": query}
70
+ ]
71
+ return messages
72
+
73
+
74
+ def parse_response(text: str) -> str | dict[str, any]:
75
+ """Parses a response from the model, returning either the
76
+ parsed list with the tool calls parsed, or the
77
+ model thought or response if couldn't generate one.
78
+
79
+ Args:
80
+ text: Response from the model.
81
+ """
82
+ pattern = r"<tool_call>(.*?)</tool_call>"
83
+ matches = re.findall(pattern, text, re.DOTALL)
84
+ if matches:
85
+ return json.loads(matches[0])
86
+ return text
87
+
88
+
89
+ model_name_llama = "plaguss/Llama-3.2-1B-Instruct-v2-FC"
90
+ model = AutoModelForCausalLM.from_pretrained(model_name_llama, device_map="auto", torch_dtype="auto", trust_remote_code=True)
91
+ tokenizer = AutoTokenizer.from_pretrained(model_name_llama)
92
+
93
+ from datetime import datetime
94
+ import random
95
+
96
+ def get_current_time() -> str:
97
+ """Returns the current time in 24-hour format.
98
+
99
+ Returns:
100
+ str: Current time in HH:MM:SS format.
101
+ """
102
+ return datetime.now().strftime("%H:%M:%S")
103
+
104
+
105
+ def get_random_number_between(min: int, max: int) -> int:
106
+ """
107
+ Gets a random number between min and max.
108
+
109
+ Args:
110
+ min: The minimum number.
111
+ max: The maximum number.
112
+
113
+ Returns:
114
+ A random number between min and max.
115
+ """
116
+ return random.randint(min, max)
117
+
118
+
119
+ tools = [get_json_schema(get_random_number_between), get_json_schema(get_current_time)]
120
+
121
+ toolbox = {"get_random_number_between": get_random_number_between, "get_current_time": get_current_time}
122
+
123
+ query = "Give me a number between 1 and 300"
124
+ query = "Can you give me the hour?"
125
+
126
+ messages = prepare_messages(query, tools=tools)
127
+
128
+ inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
129
+ outputs = model.generate(inputs, max_new_tokens=512, do_sample=False, num_return_sequences=1, eos_token_id=tokenizer.eos_token_id)
130
+ result = tokenizer.decode(outputs[0][len(inputs[0]):], skip_special_tokens=True)
131
+
132
+ tool_calls = parse_response(result)
133
+ # [{'name': 'get_random_number_between', 'arguments': {'min': 1, 'max': 300}}
134
+
135
+ # Get tool responses
136
+ tool_responses = [toolbox.get(tc["name"])(*tc["arguments"].values()) for tc in tool_calls]
137
+ # ['07:20:47']
138
+
139
+ tool_response = get_random_number_between(*tool_calls[0].get("arguments").values())
140
+ # 45
141
  ```
142
 
143
  ## Training procedure