AFS
在 API 呼叫中,您可以描述多個函式讓模型選擇,並輸出包含選中的函數名稱及參數的 JSON Object,讓應用或代理人程式調用模型選擇的函式。Chat Completions API 不會調用該函式而是生成 JSON 讓您可用於在代碼中調用函式。
<s>[INST] <<FUNC>>
{{ function定義 }}
<</FUNC>>
<<SYS>>
{{ system_prompt }}
<</SYS>>
{{ user_message }} [/INST] <|func_start|> {{ function call 的內容}} <|func_end|> </s><s><<RET>> name="This_is_function_name"
{{ function 回傳的結果 }}
<</RET>>
給定包含對話的訊息列表,模型將回傳生成訊息或呼叫函式。
開發者提供函式列表並對大語言模型輸入問題
Field | Type | Require | Description |
---|---|---|---|
functions | array | Optional | A list of functions the model may generate JSON inputs for. |
export API_KEY={API_KEY}
export API_URL=https://{DOMAIN_URL}/text-generation
curl -X POST "${API_URL}/api/models/conversation" \
-H "accept: application/json" \
-H "X-API-KEY:${API_KEY}" \
-H "content-type: application/json" \
-d '{
"model": "Llama-2-7b-chat-hf",
"messages": [
{
"role": "user",
"content": "What is the weather like in Boston?"
}],
"functions": [
{
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"]
}
},
"required": ["location"]
}
}],
"parameters": {
"show_probabilities": false,
"max_new_tokens": 50,
"frequence_penalty": 1,
"temperature": 0.5,
"top_k": 100,
"top_p": 0.93
},
"stream": false
}'
<s>[INST] <<FUNC>>
{
"name": "get_current_weather",
"description": "Get the current weather",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
}
},
"required": ["location"]
}
},
{
"name": "func_one",
"description": "function 1",
"parameters": {
"properties": {
"para1": {
"description": "parameter 1",
"type": "string"
},
"para2": {
"description": "parameter 2",
"type": "integer"
}
},
"required": ["para1"],
"type": "object"
}
}
{
"name": "func_two",
"description": "function 2",
"parameters": {
"properties": {
"para1": {
"description": "parameter 1",
"type": "string"
},
"para2": {
"description": "parameter 2",
"type": "integer"
}
},
"required": ["para1"],
"type": "object"
}
}
<</FUNC>>
<<SYS>>
You are a helpful, respectful and honest assistant.
<</SYS>>
What is the weather like in New York, NY? [/INST]
將 prompt 輸入模型後得到函式呼叫的結果
開發者解析大語言模型輸出的結構化資料,取得函式與對應的參數後,呼叫 API 並獲得回傳的結果
Field | Type | Require | Description |
---|---|---|---|
function_call | string or object | Optional | JSON format that adheres to the function signature |
{
"generated_text": "",
"function_call": {
"name": "get_current_weather",
"arguments": {
"location": "Boston"
}
},
"details":null,
"total_time_taken": "1.18 sec",
"prompt_tokens": 181,
"generated_tokens": 45,
"total_tokens": 226,
"finish_reason": "function_call"
}
將 API 回傳的結果放到對話內容並傳給大語言模型做總結
Field | value |
---|---|
role | function |
name | The function name to call |
content | The response message from the API |
Example of RESTful HTTP Request
export API_KEY={API_KEY}
export API_URL=https://{DOMAIN_URL}/text-generation
curl -X POST "${API_URL}/api/models/conversation" \
-H "accept: application/json" \
-H "X-API-KEY:${API_KEY}" \
-H "content-type: application/json" \
-d '{
"model": "Llama-2-7b-chat-hf",
"messages": [
{"role": "user", "content": "What is the weather like in Boston?"},
{"role": "assistant", "content": null, "function_call": {"name": "get_current_weather", "arguments": {"location": "Boston, MA"}}},
{"role": "function", "name": "get_current_weather", "content": "{\"temperature\": \"22\", \"unit\": \"celsius\", \"description\": \"Sunny\"}"}
],
"functions": [
{
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"]
}
},
"required": ["location"]
}
}],
"parameters": {
"show_probabilities": false,
"max_new_tokens": 50,
"frequence_penalty": 1,
"temperature": 0.5,
"top_k": 100,
"top_p": 0.93
},
"stream": false
}'
Example of RESTful HTTP Response
{
"generated_text":" The current weather in Boston is sunny with a temperature of 22 degrees Celsius. ",
"details":null,
"total_time_taken":"0.64 sec",
"prompt_tokens":230,
"generated_tokens":23,
"total_tokens":253,
"finish_reason":"eos_token"
}
Use prompt
<s>[INST] <<FUNC>>
{
"name": "get_current_weather",
"description": "Get the current weather",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
}
},
"required": ["location"]
}
},
{
"name": "func_one",
"description": "function 1",
"parameters": {
"properties": {
"para1": {
"description": "parameter 1",
"type": "string"
},
"para2": {
"description": "parameter 2",
"type": "integer"
}
},
"required": ["para1"],
"type": "object"
}
},
{
"name": "func_two",
"description": "function 2",
"parameters": {
"properties": {
"para1": {
"description": "parameter 1",
"type": "string"
},
"para2": {
"description": "parameter 2",
"type": "integer"
}
},
"required": ["para1"],
"type": "object"
}
}
<</FUNC>>
<<SYS>>
You are a helpful, respectful and honest assistant.
<</SYS>>
What is the weather like in New York, NY? [/INST] <|func_start|> {"name":"get_current_weather","arguments":{"location":"New York, NY"}} <|func_end|> </s><s><<RET>> name="get_current_weather"
{"Temperature": "57F", "Condition": "Raining"}
<</RET>>
將 prompt 輸入模型後得到總結:
使用者須申請 FFM,並向服務人員取得以下資訊:
Feb 19, 2025Code Llama Visual Studio Code Extension AI 輔助開發工具,可為開發人員提供即時的程式設計協助,包括編輯或產生程式碼、解釋程式碼、產生註解、除錯及單元測試等,並支援主流的程式語言如 Python、C、Java、PHP、TypeScript (JavaScript)、C# 和 Bash。
Nov 7, 2024Meta 在4月19日推出新一代大語言模型 Llama 3,有 80 億和 700 億參數兩種版本,在經過 ASUS AI 核心團隊調整過後,可以與函式一起使用,由大語言模型判斷是否呼叫函式。如果請求中包含一個或多個函式,則模型會根據提示的上下文決定是否需要呼叫函式。當模型確定應該使用某個函式時,會以該函式參數的格式化資料 (JSON) 來進行輸出。
Jun 7, 2024Source
May 2, 2024or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up