Views
No views yet
1xlam_system = (
2 "You are an AI assistant for function calling. "
3 "For politically sensitive questions, security and privacy issues, "
4 "and other non-computer science questions, you will refuse to answer"
5)
6
7def to_xlam_tools(tools:list|dict):
8 if not isinstance(tools, list): tools = [tools]
9 xlam_tools = []
10 for tool in tools:
11 assert isinstance(tool, dict)
12 xlam_tools.append( {
13 "name": tools["name"],
14 "description": tools["description"],
15 "parameters": {k: v for k, v in tools["parameters"].get("properties", {}).items()}
16 })
17 return xlam_tools
18
19TASK_INSTRUCTION = '''You are an expert in composing functions. You are given a question and a set of possible functions.
20Based on the question, you will need to make one or more function/tool calls to achieve the purpose.
21If none of the functions can be used, point it out and refuse to answer.
22If the given question lacks the parameters required by the function, fill the parameters as None.'''
23
24FORMAT_INSTRUCTION = '''The output MUST strictly adhere to the following JSON format, and NO other text MUST be included.
25The example format is as follows. Please make sure the parameter type is correct. If no function call is needed, please make tool_calls an empty list '[]'.
26[TRIPLE_BACKTICK]
27{ "tool_calls": [
28 {"name": "func_name1", "arguments": {"argument1": "value1", "argument2": "value2"}},
29 ... (more tool calls as required)
30] }
31[TRIPLE_BACKTICK]
32'''
33
34def get_prompt(xlam_tools:list|dict, query:str ):
35 if not isinstance(xlam_tools, str): xlam_tools = json.dumps(xlam_tools)
36 prompt = f"<instruction>\n{TASK_INSTRUCTION}\n</instruction>\n\n"
37 prompt += f"<available tools>\n{xlam_tools}\n</available tools>\n\n"
38 prompt += f"<tool format>\n{FORMAT_INSTRUCTION}\n</tool format>\n\n"
39 prompt += f"<query>\n{query.strip()}\n<query>\n\n"
40 return prompt
411from transformers import AutoModelForCausalLM, AutoTokenizer
2user_msg = '''<instruction>
3You are an expert in composing functions. You are given a question and a set of possible functions.
4Based on the question, you will need to make one or more function/tool calls to achieve the purpose.
5If none of the functions can be used, point it out and refuse to answer.
6If the given question lacks the parameters required by the function, fill the parameters as None.
7</instruction>
8
9<available tools>
10[{"name": "messages_from_telegram_channel", "description": "Fetches the last 10 messages or a specific message from a given public Telegram channel.", "parameters": {"channel": {"description": "The @username of the public Telegram channel.", "type": "str", "default": "telegram"}, "idmessage": {"description": "The ID of a specific message to retrieve. If not provided, the function will return the last 10 messages.", "type": "str, optional", "default": ""}}}, {"name": "shopify", "description": "Checks the availability of a given username on Shopify using the Toolbench RapidAPI.", "parameters": {"username": {"description": "The username to check for availability on Shopify.", "type": "str", "default": "username"}}}, {"name": "generate_a_face", "description": "Generates a face image using an AI service and returns the result as a JSON object or text. It utilizes the Toolbench RapidAPI service.", "parameters": {"ai": {"description": "The AI model identifier to be used for face generation.", "type": "str", "default": "1"}}}]
11</available tools>
12
13<tool format>
14The output MUST strictly adhere to the following JSON format, and NO other text MUST be included.
15The example format is as follows. Please make sure the parameter type is correct. If no function call is needed, please make tool_calls an empty list '[]'.
16[TRIPLE_BACKTICK]
17{ "tool_calls": [
18 {"name": "func_name1", "arguments": {"argument1": "value1", "argument2": "value2"}},
19 ... (more tool calls as required)
20] }
21[TRIPLE_BACKTICK]
22</tool format>
23
24<query>
25Check if the username 'ShopMaster123' is available on Shopify.
26</query>'''
27
28messages = [dict(role='user', content=user_msg)]
29label = { "tool_calls": [{"name": "shopify", "arguments": {"username": "ShopMaster123"}}] }
30
31
32tokenizer = AutoTokenizer.from_pretrained(
33 "objects76/qwen2-xlam", trust_remote_code=True)
34
35model = AutoModelForCausalLM.from_pretrained(
36 "objects76/qwen2-xlam", trust_remote_code=True,
37 torch_dtype="auto",
38 device_map="cuda",
39)
40model.config.use_cache = True
41model.eval()
42
43input_ids = tokenizer.apply_chat_template(
44 messages,
45 tokenize=True,
46 add_generation_prompt=True,
47 max_length=tokenizer.model_max_length,
48 padding=False, truncation=True,
49 return_tensors='pt',
50 ).to(model.device)
51
52outputs = model.generate(
53 input_ids = input_ids, # attention_mask=attention_mask,
54 max_new_tokens=1024,
55 eos_token_id=tokenizer.eos_token_id,
56 pad_token_id=tokenizer.pad_token_id,
57 # do_sample=True, temperature=0.01, top_p= 0.01,
58 use_cache=True)
59
60response = tokenizer.decode(outputs[0, input_ids.shape[-1]:], skip_special_tokens=True)
61print('response=', response)
62print('label=', label)1
2system = ('Your task is to extract specific information from the given text.'
3 ' Please provide the requested information in the format shown in the examples below.'
4 )
5
6fewshot_example = '''\
7Example 1:
8Text: John Smith is a 35-year-old software engineer from New York. He has been working at TechCorp for 5 years.
9Name: John Smith
10Age: 35
11Occupation: Software Engineer
12Location: New York
13Company: TechCorp
14Years of Experience: 5
15
16Example 2:
17Text: Sarah Johnson, a 28-year-old marketing specialist, recently moved to San Francisco to join StartupX as their new Head of Marketing.
18Name: Sarah Johnson
19Age: 28
20Occupation: Marketing Specialist
21Location: San Francisco
22Company: StartupX
23Position: Head of Marketing
24
25Now, extract the information from the following text:
26Text: Michael Brown, 42, is a senior data scientist at DataInc in Chicago. He has been in the field for over a decade and specializes in machine learning algorithms.
27'''
28
29answer_from_gpt = '''\
30Name: Michael Brown
31Age: 42
32Occupation: Senior Data Scientist
33Location: Chicago
34Company: DataInc
35Years of Experience: Over a decade
36Specialization: Machine Learning Algorithms
37'''
38
39messages = [
40 {"role": "system", "content": system.strip()},
41 {"role": "user", "content": fewshot_example.strip()},
42]