Views
No views yet
transforemrs>=4.51.3.1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3MODEL_PATH = "THUDM/GLM-Z1-Rumination-32B-0414"
4
5tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
6model = AutoModelForCausalLM.from_pretrained(MODEL_PATH, device_map="auto")
7
8message = [{"role": "user", "content": "Let a, b be positive real numbers such that ab = a + b + 3. Determine the range of possible values for a + b."}]
9
10inputs = tokenizer.apply_chat_template(
11 message,
12 return_tensors="pt",
13 add_generation_prompt=True,
14 return_dict=True,
15).to(model.device)
16
17generate_kwargs = {
18 "input_ids": inputs["input_ids"],
19 "attention_mask": inputs["attention_mask"],
20 "temperature": 0.95,
21 "top_p": 0.7,
22 "do_sample": True,
23}
24out = model.generate(**generate_kwargs)
25print(tokenizer.decode(out[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True))function calls:search: Search using a keyword and return search resultsclick: Click on a specific webpage in the search results to view detailsopen: Open a fixed URL to view detailed contentfinsih: Complete information gathering and begin writing1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3MODEL_PATH = "THUDM/GLM-Z1-Rumination-32B-0414"
4
5tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
6model = AutoModelForCausalLM.from_pretrained(MODEL_PATH, device_map="auto")
7
8messages = [{"role": "user", "content": "Let a, b be positive real numbers such that ab = a + b + 3. Determine the range of possible values for a + b."}]
9
10generate_kwargs = {
11 "temperature": 0.95,
12 "top_p": 0.7,
13 "do_sample": True,
14}
15
16def get_assistant():
17 inputs = tokenizer.apply_chat_template(
18 messages,
19 return_tensors="pt",
20 add_generation_prompt=True,
21 return_dict=True,
22 ).to(model.device)
23 out = model.generate(input_ids=input["input_ids"], **generate_kwargs)
24 return tokenizer.decode(out[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True).strip()
25
26def get_observation(function_name, args):
27 if fucntion_name == "search":
28 mock_search_res = [
29 {"title": "t1", "url":"url1", "snippet": "snippet_content_1"},
30 {"title": "t2", "url":"url2", "snippet": "snippet_content_2"}
31 ]
32 content = "\n\n".join([f"【{i}†{res['title']}†{res['url']}\n{res['snippet']}】"] for i, res in mock_search_res)
33 elif function_name == "click":
34 mock_click_res = "main content"
35 content = mock_click_res
36 elif function_name == "open":
37 mock_open_res = "main_content"
38 content = mock_open_res
39 else:
40 raise ValueError("unspport function name!")
41
42def get_func_name_args(llm_text):
43 function_call = re.sub(r'.*?</think>', '', llm_text, flags=re.DOTALL)
44 function_call = json.loads(function_call)
45 action = function_call['name']
46 params = function_call['arguments']
47 return action, params
48
49def pipeline():
50 end_str = "{\"name\": \"finish\", \"arguments\": {}}"
51 response = get_assistant()
52 messages.append({"role": "assistant", "content": response})
53 max_turns, turns = 35, 1
54 while not response.endswith(end_str) and turns < max_turns:
55 action, params = get_func_name_args(response)
56 observation = get_observation(action, params)
57 messages.append({"role": "observation", "content": observation})
58 response = get_assistant()
59 messages.append({"role": "assistant", "content": response})
60 turns += 1
61
62 if response.endswith(end_str):
63 final_answer = get_assistant()
64 else:
65 final_answer = None
66 return final_answer
67
68pipeline()