Firebase Genkit 提供簡單的介面,方便您透過大型語言模型生成內容。
模型
Firebase Genkit 中的模型是一種程式庫和抽象化機制, 是由多個 Google 和非 Google LLM
完整檢測模型的觀測能力,隨附工具 Genkit 開發人員使用者介面提供的整合工具,您可以使用 模型執行器
在 Genkit 中使用模型時,必須先設定要使用的模型, 以及要使用的專案模型設定是由外掛程式系統執行。於 這個範例需要設定 Vertex AI 外掛程式 我們來看評估分類模型成效時 的喚回度和精確度指標
import { configureGenkit } from '@genkit-ai/core';
import { firebase } from '@genkit-ai/firebase';
import { vertexAI } from '@genkit-ai/vertexai';
configureGenkit({
plugins: [vertexAI()],
});
如要使用外掛程式提供的模型,您可以依據名稱 (例如
'vertexai/gemini-1.5-flash'
) 或某些外掛程式會匯出 Model ref 物件,這些物件會
會提供模型功能和選項的額外資訊。
import { gemini15Flash } from '@genkit-ai/vertexai';
支援的模型
Genkit 透過外掛程式系統提供模型支援。下列外掛程式 的正式支援:
外掛程式 | 模型 |
---|---|
Google 生成式 AI | Gemini Pro、Gemini Pro Vision |
Google Vertex AI | Gemini Pro、Gemini Pro Vision、Gemini 1.5 Flash、Gemini 1.5 Pro、Imagen2 |
奧拉馬 | 包含 Gemma、Llama 2、Mistral 等多種本地模型 |
如需設定與使用資訊,請參閱每個外掛程式的說明文件。另外還有
你可以透過 Google Cloud
在 npmjs.org 搜尋開頭為 genkitx-
的套件。
如何產生內容
generate
是處理模型的輔助函式。
如要直接呼叫模型,請按照下列步驟操作:
import { generate } from '@genkit-ai/ai';
import { gemini15Flash } from '@genkit-ai/vertexai';
(async () => {
const llmResponse = await generate({
model: gemini15Flash,
prompt: 'Tell me a joke.',
});
console.log(await llmResponse.text());
})();
您可以針對該模型傳入各種模型選項,包括指定 專屬的自訂模型
const response = await generate({
model: gemini15Flash,
prompt,
config: {
temperature: 1,
stopSequences: ['abc'],
},
});
如果模型支援多模態輸入,您就能傳入圖片做為輸入內容:
const result = await generate({
model: gemini15Flash,
prompt: [
{ text: 'describe the following image:' },
{ media: { url: imageUrl, contentType: 'image/jpeg' } },
],
});
或從本機檔案:
const result = await generate({
model: gemini15Flash,
prompt: [
{ text: 'describe the following image:' },
{
data: {
url: fs.readFileSync(__dirname + '/image.jpeg', {
encoding: 'base64',
flag: 'r',
}),
contentType: 'image/jpeg',
},
},
],
});
工具和函式呼叫
Model
也支援工具和函式呼叫。工具支援取決於
特定模型
const myTool = action(
{
name: 'myJoke',
description: 'useful when you need a joke to tell.',
inputSchema: z.object({ subject: z.string() }),
outputSchema: z.string(),
},
async (input) => 'haha Just kidding no joke! got you'
);
const llmResponse = await generate({
model: gemini15Flash,
prompt: 'Tell me a joke.',
tools: [myTool],
config: {
temperature: 0.5,
},
});
這個外掛程式會自動呼叫工具,以完成使用者提示。
您可以指定 returnToolRequests: true
來手動控制工具呼叫。
const llmResponse = await generate({
model: gemini15Flash,
prompt: 'Tell me a joke.',
tools: [myTool],
returnToolRequests: true,
config: {
temperature: 0.5,
},
});
新增擷取器內容
擷取器的文件可直接傳遞至 generate
以提供
依據情境:
const docs = await companyPolicyRetriever({ query: question });
await generate({
model: gemini15Flash,
prompt: `Answer using the available context from company policy: ${question}`,
context: docs,
});
文件情境會自動附加至提示內容 傳送至模型
正在錄製訊息記錄
Genkit 模型支援保留傳送至模型的訊息記錄 和回應中的回應。您可以藉此打造互動體驗,例如 聊天機器人
如要從模型回應產生訊息記錄,請呼叫 toHistory()
方法:
let response = await generate({
model: gemini15Flash,
prompt: "How do you say 'dog' in French?",
});
let history = response.toHistory();
您可以序列化這項記錄,並保存在資料庫或工作階段儲存空間中。
接著,請在日後呼叫 generate()
時傳送記錄和提示:
response = await generate({
model: gemini15Flash,
prompt: 'How about in Spanish?',
history,
});
history = response.toHistory();
如果您使用的模型支援 system
角色,您就可以使用初始值
記錄來設定系統訊息:
let history: MessageData[] = [
{ role: 'system', content: [{ text: 'Talk like a pirate.' }] },
];
let response = await generate({
model: gemini15Flash,
prompt: "How do you say 'dog' in French?",
history,
});
history = response.toHistory();
串流回應
Genkit 支援透過 generateStream()
方法分區塊串流模型回應:
// import { generateStream } from '@genkit-ai/ai';
const { response, stream } = await generateStream({
model: gemini15Flash,
prompt: 'Tell a long story about robots and ninjas.',
});
for await (const chunk of stream()) {
console.log(chunk.text());
}
// you can also await the full response
console.log((await response()).text());