Update README.md
Browse files
README.md
CHANGED
|
@@ -58,6 +58,10 @@ print(tokenizer.decode(response, skip_special_tokens=True))
|
|
| 58 |
```
|
| 59 |
|
| 60 |
### Function Calling
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
```python
|
| 62 |
functions_metadata = [
|
| 63 |
{
|
|
@@ -81,9 +85,41 @@ functions_metadata = [
|
|
| 81 |
}
|
| 82 |
]
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
messages = [
|
| 85 |
{ "role": "system", "content": f"""You are a helpful assistant with access to the following functions: \n {str(functions_metadata)}\n\nTo use these functions respond with:\n<functioncall> {{ "name": "function_name", "arguments": {{ "arg_1": "value_1", "arg_1": "value_1", ... }} }} </functioncall>\n\nEdge cases you must handle:\n - If there are no functions that match the user request, you will respond politely that you cannot help."""},
|
| 86 |
{ "role": "user", "content": "What is the temperature in Tokyo right now?"},
|
|
|
|
| 87 |
{ "role": "assistant", "content": """<functioncall> {"name": "get_temperature", "arguments": '{"city": "Tokyo"}'} </functioncall>"""},
|
| 88 |
{ "role": "user", "content": """<function_response> {"temperature":30 C} </function_response>"""}
|
| 89 |
]
|
|
|
|
| 58 |
```
|
| 59 |
|
| 60 |
### Function Calling
|
| 61 |
+
Function calling requires two step inferences, below is the example:
|
| 62 |
+
|
| 63 |
+
## Step 1:
|
| 64 |
+
|
| 65 |
```python
|
| 66 |
functions_metadata = [
|
| 67 |
{
|
|
|
|
| 85 |
}
|
| 86 |
]
|
| 87 |
|
| 88 |
+
messages = [
|
| 89 |
+
{ "role": "system", "content": f"""You are a helpful assistant with access to the following functions: \n {str(functions_metadata)}\n\nTo use these functions respond with:\n<functioncall> {{ "name": "function_name", "arguments": {{ "arg_1": "value_1", "arg_1": "value_1", ... }} }} </functioncall>\n\nEdge cases you must handle:\n - If there are no functions that match the user request, you will respond politely that you cannot help."""},
|
| 90 |
+
{ "role": "user", "content": "What is the temperature in Tokyo right now?"}
|
| 91 |
+
]
|
| 92 |
+
|
| 93 |
+
input_ids = tokenizer.apply_chat_template(
|
| 94 |
+
messages,
|
| 95 |
+
add_generation_prompt=True,
|
| 96 |
+
return_tensors="pt"
|
| 97 |
+
).to(model.device)
|
| 98 |
+
|
| 99 |
+
terminators = [
|
| 100 |
+
tokenizer.eos_token_id,
|
| 101 |
+
tokenizer.convert_tokens_to_ids("<|eot_id|>")
|
| 102 |
+
]
|
| 103 |
+
|
| 104 |
+
outputs = model.generate(
|
| 105 |
+
input_ids,
|
| 106 |
+
max_new_tokens=256,
|
| 107 |
+
eos_token_id=terminators,
|
| 108 |
+
do_sample=True,
|
| 109 |
+
temperature=0.6,
|
| 110 |
+
top_p=0.9,
|
| 111 |
+
)
|
| 112 |
+
response = outputs[0][input_ids.shape[-1]:]
|
| 113 |
+
print(tokenizer.decode(response, skip_special_tokens=True))
|
| 114 |
+
# >> <functioncall> {"name": "get_temperature", "arguments": '{"city": "Tokyo"}'} </functioncall>"""}
|
| 115 |
+
```
|
| 116 |
+
## Step 2:
|
| 117 |
+
|
| 118 |
+
```python
|
| 119 |
messages = [
|
| 120 |
{ "role": "system", "content": f"""You are a helpful assistant with access to the following functions: \n {str(functions_metadata)}\n\nTo use these functions respond with:\n<functioncall> {{ "name": "function_name", "arguments": {{ "arg_1": "value_1", "arg_1": "value_1", ... }} }} </functioncall>\n\nEdge cases you must handle:\n - If there are no functions that match the user request, you will respond politely that you cannot help."""},
|
| 121 |
{ "role": "user", "content": "What is the temperature in Tokyo right now?"},
|
| 122 |
+
// You will get the previous prediction, extract it will the tag <functioncall>, execute it and append it to the messages like below:
|
| 123 |
{ "role": "assistant", "content": """<functioncall> {"name": "get_temperature", "arguments": '{"city": "Tokyo"}'} </functioncall>"""},
|
| 124 |
{ "role": "user", "content": """<function_response> {"temperature":30 C} </function_response>"""}
|
| 125 |
]
|