File size: 3,064 Bytes
d7b2b59
 
 
c847dfc
7343d8f
c847dfc
7343d8f
c847dfc
6a059c6
c847dfc
2c38455
 
7343d8f
 
 
 
2c38455
7343d8f
c847dfc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
---
license: apache-2.0
---

# Mistral-7B function calling

This is a merged model of the https://huggingface.co/cognitivecomputations/dolphin-2_6-mistral-7b and sft function calling lora [here](https://huggingface.co/Yhyu13/dolphin-2.6-mistral-7b-dpo-laser-function-calling-lora)

This model is acompanied with pr on textgen-webui to enable its function calling ability like GPTs: [Add function calling ability to openai extension](https://github.com/oobabooga/text-generation-webui/pull/5185)

## Caution

This model is recommanded over my [ph-2 variant](https://huggingface.co/Yhyu13/dolphin-2_6-phi-2-sft-glaive-function-calling-v2-ep1) since this model has more grounding ability on following function calling prompt

This model is by far my best function calling model, it can achieve 10/10 success on [openai function calling cook book](https://github.com/openai/openai-cookbook/blob/main/examples/How_to_call_functions_with_chat_models.ipynb)

## Detail

The function calling is wrapped in simple xml tag for eaiser identification.

```
<functioncall> {\"name\": \"calculate_loan_payment\", \"arguments\": '{\"principal\": 50000, \"interest_rate\": 5, \"loan_term\": 10}'} </functioncall>
```

that can be extracted like this 

```
import re
import json

input_str = "<functioncall> {\"name\": \"calculate_loan_payment\", \"arguments\": '{\"principal\": 50000, \"interest_rate\": 5, \"loan_term\": 10}'} </functioncall>"

# Define the pattern to match the JSON string within the functioncall tags
pattern = r'<functioncall>(.*?)</functioncall>'

# Use re.search to find the matched pattern
match = re.search(pattern, input_str, re.DOTALL)

if match:
    json_str = match.group(1)
    # Remove the single quotes surrounding the inner JSON string
    json_str = json_str.replace("'", "")
    
    # Load the JSON string into a Python dictionary
    json_data = json.loads(json_str)
    print(json_data)
else:
    print("No match found.")
```

Or, if you want to faithfully keep the single quotes that wrapps the `arguments` value (where openai does it like this, which makes `json.loads` fail shortly on the original `json_str`), use `ast.literal_eval` for the rescue.

```
if match:
    import ast
    json_str = match.group(1)
    json_str = json_str.strip()
    """
    https://www.datasciencebyexample.com/2023/03/16/what-to-do-when-single-quotes-in-json-string/
    """
    json_dict = ast.literal_eval(json_str)
    print(json_dict['name'], json_dict['arguments'])
else:
    print("No match found.")
```

Hopefully, this model can be a drop-in replacement for apps (e.g. memgpt) that require function calling ability from LLMs.

Another note on interpreting function call result:

Function response has been put between `<functionresponse>` in order to be identified as a function call result (which could be evaluted behind the scene, and its result in principle should be interpreted as part of the user input), which then will be processed by the assistant for form a conversational response.

```
<functionresponse> jons_str </functionresponse>
```