nehcgs commited on
Commit
9344179
·
verified ·
1 Parent(s): a8e0096

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +130 -0
README.md ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: other
3
+ license_name: katanemo-research
4
+ license_link: >-
5
+ https://huggingface.co/katanemolabs/Arch-Function-Chat-1.5B/blob/main/LICENSE
6
+ base_model:
7
+ - Qwen/Qwen2.5-Coder-1.5B-Instruct
8
+ language:
9
+ - en
10
+ pipeline_tag: text-generation
11
+ library_name: transformers
12
+ ---
13
+
14
+ # katanemo/Arch-Function-Chat-1.5B
15
+
16
+ ## Overview
17
+ The Arch-Function-Chat collection builds upon the Katanemo's [Arch-Function](https://huggingface.co/collections/katanemo/arch-function-66f209a693ea8df14317ad68) collection by extending its capabilities beyond basic function calling. This new collection maintains the state-of-the-art function calling abilities of the original while adding powerful new features that make it even more versatile in real-world applications.
18
+
19
+ In addition to the core function calling capabilities, this enhanced collection now offers:
20
+
21
+ - **Intent matching**: Automatically identifies user intent and maps it to the most appropriate functions
22
+ - **Parameter gathering**: Generates natural follow-up questions to collect missing required parameters
23
+ - **Result interpretation**: Provides human-friendly responses based on function execution results
24
+ - **Multi-turn dialogue management**: Maintains context throughout complex interactions
25
+
26
+ # Requirements
27
+ The code of Arch-Function-Chat-1.5B has been in the Hugging Face `transformers` library and we advise you to install latest version:
28
+ ```bash
29
+ pip install transformers>=4.37.0
30
+ ```
31
+
32
+
33
+ # How to use
34
+ We use the following example to illustrate how to use our model to perform function calling tasks. Please note that, our model works best with our provided prompt format. It allows us to extract JSON output that is similar to the [OpenAI's function calling](https://platform.openai.com/docs/guides/function-calling).
35
+
36
+
37
+ ### Quickstart
38
+ ````python
39
+ import json
40
+ from typing import Any, Dict, List
41
+ from transformers import AutoModelForCausalLM, AutoTokenizer
42
+
43
+ model_name = "katanemo/Arch-Function-Chat-1.5B"
44
+ model = AutoModelForCausalLM.from_pretrained(
45
+ model_name, device_map="auto", torch_dtype="auto", trust_remote_code=True
46
+ )
47
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
48
+
49
+ # Please use our provided prompt for best performance
50
+ TASK_PROMPT = (
51
+ "You are a helpful assistant designed to assist with the user query by making one or more function calls if needed."
52
+ "\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>\n{tools}\n</tools>"
53
+ "\n\nYour task is to decide which functions are needed and collect missing parameters if necessary."
54
+ )
55
+
56
+ FORMAT_PROMPT = (
57
+ "\n\nBased on your analysis, provide your response in one of the following JSON formats:"
58
+ '\n1. If no functions are needed:\n```json\n{"response": "Your response text here"}\n```'
59
+ '\n2. If functions are needed but some required parameters are missing:\n```json\n{"required_functions": ["func_name1", "func_name2", ...], "clarification": "Text asking for missing parameters"}\n```'
60
+ '\n3. If functions are needed and all required parameters are available:\n```json\n{"tool_calls": [{"name": "func_name1", "arguments": {"argument1": "value1", "argument2": "value2"}},... (more tool calls as required)]}\n```'
61
+ )
62
+
63
+ # Define available tools
64
+ get_weather_api = {
65
+ "type": "function",
66
+ "function": {
67
+ "name": "get_weather",
68
+ "description": "Get the current weather for a location",
69
+ "parameters": {
70
+ "type": "object",
71
+ "properties": {
72
+ "location": {
73
+ "type": "str",
74
+ "description": "The city and state, e.g. San Francisco, New York",
75
+ },
76
+ "unit": {
77
+ "type": "str",
78
+ "enum": ["celsius", "fahrenheit"],
79
+ "description": "The unit of temperature to return",
80
+ },
81
+ },
82
+ "required": ["location"],
83
+ },
84
+ },
85
+ }
86
+
87
+ openai_format_tools = [get_weather_api]
88
+
89
+
90
+ def convert_tools(tools: List[Dict[str, Any]]):
91
+ converted = [json.dumps(tool["function"], ensure_ascii=False) for tool in tools]
92
+ return "\n".join(converted)
93
+
94
+ # Helper function to create the system prompt for our model
95
+ def format_prompt(tools: List[Dict[str, Any]]):
96
+ tools = convert_tools(tools)
97
+ return TASK_PROMPT.format(tools=tools) + FORMAT_PROMPT
98
+
99
+ system_prompt = format_prompt(openai_format_tools)
100
+
101
+ messages = [
102
+ {"role": "system", "content": system_prompt},
103
+ {"role": "user", "content": "What is the weather in Seattle?"},
104
+ ]
105
+
106
+ inputs = tokenizer.apply_chat_template(
107
+ messages, add_generation_prompt=True, return_tensors="pt"
108
+ ).to(model.device)
109
+
110
+ outputs = model.generate(
111
+ inputs,
112
+ max_new_tokens=512,
113
+ do_sample=False,
114
+ num_return_sequences=1,
115
+ eos_token_id=tokenizer.eos_token_id,
116
+ )
117
+
118
+ response = tokenizer.decode(outputs[0][len(inputs[0]) :], skip_special_tokens=True)
119
+ print(response)
120
+ ````
121
+
122
+ Then you should be able to see the following output string in JSON format:
123
+ ````python
124
+ ```json
125
+ {"tool_calls": [{"name": "get_weather", "arguments": {"location": "Seattle"}}]}
126
+ ```
127
+ ````
128
+
129
+ # License
130
+ Katanemo Arch-Function collection is distributed under the [Katanemo license](https://huggingface.co/katanemolabs/Arch-Function-Chat-1.5B/blob/main/LICENSE).