
2024年您產(chǎn)品必備的10大AI API推薦
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"},
"format": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "The temperature unit to use. Infer this from the users location."},
},
"required": ["location", "format"],
})
打印出來prompt如下:
在input側(cè),mistral-nemo的做法是直接將用戶提供的tool schema轉(zhuǎn)為string,并包裹在特殊的tag(AVAILABLE_TOOLS)之中,然后插入到user query之前。
既然都是組裝Prompt,我們拿它和Kor的Prompt做個對比:
可以發(fā)現(xiàn),mistral-nemo的prompt更精簡(不包含Your goal is …. 、All output must be in JSON format…. 等內(nèi)容)。
接著,我們來看output側(cè)。
mistral-nemo的輸出結(jié)果如下:
看起來,這是一個普通的text generation過程,通過特殊標(biāo)記(TOOL_CALLS)來表明,這是一個tool_call message,而非常見的text message;同時nemo支持同時call多個tools,每個call為一個字典,其中包含function name和arguments參數(shù)(json格式)。
總結(jié)一下,mistral-nemo這樣實現(xiàn)FC:
通過分析mistral-nemo,可以猜測,各家LLM公司有自己的FC prompt template,既體現(xiàn)在input側(cè),也體現(xiàn)在output側(cè)。
動手實踐是學(xué)習(xí)的好方法,本期我們?nèi)匀贿x用第一期的2個練習(xí)(中文翻譯器、評價解析)。
練習(xí)部分的所有代碼,都已整理在下方git,建議讀者實際運行代碼來學(xué)習(xí):
https://github.com/duanyu/structured_generation_with_llm/blob/main/Lecture2_Function_Calling.ipynb
考慮到排版,筆者直接將截圖貼在下面。
本文介紹了第二種進(jìn)行Structured Generation的技術(shù):Function Calling。FC是Agent的基石,structured generation則是“副產(chǎn)品”;讀者在實際使用中,可以將FC與Kor(或者自己寫的prompt)做對比,選擇效果更好的方案。
需要提到的是,F(xiàn)C雖然經(jīng)過了fine-tuning,輸出結(jié)構(gòu)的穩(wěn)定性有一定保證,但若未使用constrain decoding技術(shù),那么仍然不是100%魯棒的;同時,筆者在練習(xí)中發(fā)現(xiàn),當(dāng)使用glm-4-flash/air/airx模型時,F(xiàn)C難以有效加入few-shot examples,但在第一期練習(xí)中,glm4-9b-chat + Kor對few-shot examples十分友好,這可能是FC的一個問題(但也可能是用法不對,歡迎有經(jīng)驗的讀者指正)。
文章轉(zhuǎn)自微信公眾號@漫談NLP