Ollama 插件提供 Ollama 支持的任何本地 LLM 的接口。
安装
npm i --save genkitx-ollama
配置
该插件要求您首先安装并运行 ollama 服务器。您可以关注 如需查看相关说明,请访问 https://ollama.com/download
您可以使用 Ollama CLI 下载您感兴趣的模型。例如:
ollama pull gemma
如需使用此插件,请在调用 configureGenkit()
时指定它。
import { ollama } from 'genkitx-ollama';
export default configureGenkit({
plugins: [
ollama({
models: [
{
name: 'gemma',
type: 'generate', // type: 'chat' | 'generate' | undefined
},
],
serverAddress: 'http://127.0.0.1:11434', // default local address
}),
],
});
身份验证
如果您想要访问需要自定义标头(静态、 (例如 API 密钥)或动态(如身份验证标头),您可以在 ollama 配置插件中指定这些变量:
静态标头:
ollama({
models: [{ name: 'gemma'}],
requestHeaders: {
'api-key': 'API Key goes here'
},
serverAddress: 'https://my-deployment',
}),
您还可以为每个请求动态设置标头。以下示例展示了如何使用 Google Auth 库:
import { GoogleAuth } from 'google-auth-library';
import { ollama, OllamaPluginParams } from 'genkitx-ollama';
import { configureGenkit, isDevEnv } from '@genkit-ai/core';
const ollamaCommon = { models: [{ name: 'gemma:2b' }] };
const ollamaDev = {
...ollamaCommon,
serverAddress: 'http://127.0.0.1:11434',
} as OllamaPluginParams;
const ollamaProd = {
...ollamaCommon,
serverAddress: 'https://my-deployment',
requestHeaders: async (params) => {
const headers = await fetchWithAuthHeader(params.serverAddress);
return { Authorization: headers['Authorization'] };
},
} as OllamaPluginParams;
export default configureGenkit({
plugins: [
ollama(isDevEnv() ? ollamaDev : ollamaProd),
],
});
// Function to lazily load GoogleAuth client
let auth: GoogleAuth;
function getAuthClient() {
if (!auth) {
auth = new GoogleAuth();
}
return auth;
}
// Function to fetch headers, reusing tokens when possible
async function fetchWithAuthHeader(url: string) {
const client = await getIdTokenClient(url);
const headers = await client.getRequestHeaders(url); // Auto-manages token refresh
return headers;
}
async function getIdTokenClient(url: string) {
const auth = getAuthClient();
const client = await auth.getIdTokenClient(url);
return client;
}
用法
此插件不会静态导出模型引用。指定 模型:
const llmResponse = await generate({
model: 'ollama/gemma',
prompt: 'Tell me a joke.',
});