正在生成内容

Firebase Genkit 提供了一个简单的界面,支持使用 LLM 生成内容。

模型

Firebase Genkit 中的模型是库和抽象,可让您访问各种 Google 和非 Google LLM。

模型经过全面插桩,以实现可观测性,并附带 Genkit 开发者界面提供的工具集成。您可以使用模型运行程序试用任何模型。

在 Genkit 中使用模型时,首先需要配置要使用的模型。模型配置由插件系统执行。在此示例中,您将配置 Vertex AI 插件,该插件会提供 Gemini 模型。

import { configureGenkit } from '@genkit-ai/core';
import { firebase } from '@genkit-ai/firebase';
import { vertexAI } from '@genkit-ai/vertexai';

configureGenkit({
  plugins: [vertexAI()],
});

如需使用插件提供的模型,您可以通过名称(例如 'vertexai/gemini-1.0-pro')来引用它们,也可以通过某些插件导出模型引用对象来引用模型功能和选项的其他类型信息。

import { geminiPro } 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 等

如需了解设置和使用信息,请参阅各个插件的文档。

如何生成内容

generate 是用于处理模型的辅助函数。

只需调用模型即可:

import { generate } from '@genkit-ai/ai';
import { geminiPro } from '@genkit-ai/vertexai';

(async () => {
  const llmResponse = await generate({
    model: geminiPro,
    prompt: 'Tell me a joke.',
  });

  console.log(await llmResponse.text());
})();

您可以为该模型传入各种模型选项,包括为特定 LLM 指定自定义模型。

const response = await generate({
  model: geminiPro,
  prompt,
  config: {
    temperature: 1,
    stopSequences: ['abc'],
  },
});

如果模型支持多模态输入,您可以将图片作为输入传入:

const result = await generate({
  model: geminiProVision,
  prompt: [
    { text: 'describe the following image:' },
    { media: { url: imageUrl, contentType: 'image/jpeg' } },
  ],
});

或者,从本地文件:

const result = await generate({
  model: geminiProVision,
  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: geminiPro,
  prompt: 'Tell me a joke.',
  tools: [myTool],
  config: {
    temperature: 0.5,
  },
});

这将自动调用这些工具,以满足用户提示。

您可以指定 returnToolRequests: true 以手动控制工具调用。

const llmResponse = await generate({
  model: geminiPro,
  prompt: 'Tell me a joke.',
  tools: [myTool],
  returnToolRequests: true,
  config: {
    temperature: 0.5,
  },
});

也可以流式传输支持该功能的模型的输出:

await generate({
  model: geminiPro,
  prompt: 'Tell me a very long joke.',
  streamingCallback: (chunk) => {
    console.log(chunk);
  },
});

添加检索器上下文

检索器中的文档可以直接传递给 generate 以提供依据上下文:

const docs = await companyPolicyRetriever({ query: question });

await generate({
  model: geminiPro,
  prompt: `Answer using the available context from company policy: ${question}`,
  context: docs,
});

文档上下文会自动附加到发送给模型的提示内容。

正在录制消息记录

Genkit 模型支持维护发送到模型的消息及其响应的历史记录,您可以使用这些历史记录构建交互式体验,例如聊天机器人。

如需通过模型响应生成消息历史记录,请调用 toHistory() 方法:

let response = await generate({
  model: geminiPro,
  prompt: "How do you say 'dog' in French?",
});
let history = response.toHistory();

您可以序列化此历史记录并将其保留在数据库或会话存储空间中。然后,传递历史记录,并在日后调用 generate() 时进行提示:

response = await generate({
  model: geminiPro,
  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: geminiPro,
  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: geminiPro,
  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());