LangChain 提供了一个实验性包装器,用于围绕通过 Ollama 本地运行的开源模型,使其具有与 OpenAI Functions 相同的 API。
Note that more powerful and capable models will perform better with complex schema and/or multiple functions. The examples below 使用 Mistral.
设置
按照 这些说明 设置并运行本地 Ollama 实例。
初始化模型
您可以按照初始化标准 ChatOllama instance. OllamaFunctions 仅在以下版本可用 @langchain/community (非 @langchain/ollama):
const model = new OllamaFunctions({
temperature: 0.1,
model: "mistral",
});
传入函数
现在您可以像使用 OpenAI 一样传入函数:
const model = new ChatOllama({
temperature: 0.1,
model: "mistral",
})
.bindTools([
{
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"],
},
},
])
.withConfig({
// You can set the `tool_choice` arg to force the model to use a function
tool_choice: "get_current_weather",
});
const response = await model.invoke([
new HumanMessage({
content: "What's the weather in Boston?",
}),
]);
console.log(response);
/*
AIMessage {
content: '',
additional_kwargs: {
function_call: {
name: 'get_current_weather',
arguments: '{"location":"Boston, MA","unit":"fahrenheit"}'
}
}
}
*/
用于提取
const EXTRACTION_TEMPLATE = `Extract and save the relevant entities mentioned in the following passage together with their properties.
Passage:
{input}
`;
const prompt = PromptTemplate.fromTemplate(EXTRACTION_TEMPLATE);
// Use Zod for easier schema declaration
const schema = z.object({
people: z.array(
z.object({
name: z.string().describe("The name of a person"),
height: z.number().describe("The person's height"),
hairColor: z.optional(z.string()).describe("The person's hair color"),
})
),
});
const model = new ChatOllama({
temperature: 0.1,
model: "mistral",
})
.bindTools([
{
name: "information_extraction",
description: "Extracts the relevant information from the passage.",
schema,
},
])
.withConfig({
tool_choice: "information_extraction",
});
// Use a JsonOutputFunctionsParser to get the parsed JSON response directly.
const chain = prompt.pipe(model).pipe(new JsonOutputFunctionsParser());
const response = await chain.invoke({
input:
"Alex is 5 feet tall. Claudia is 1 foot taller than Alex and jumps higher than him. Claudia has orange hair and Alex is blonde.",
});
console.log(JSON.stringify(response, null, 2));
/*
{
"people": [
{
"name": "Alex",
"height": 5,
"hairColor": "blonde"
},
{
"name": "Claudia",
"height": {
"$num": 1,
"add": [
{
"name": "Alex",
"prop": "height"
}
]
},
"hairColor": "orange"
}
]
}
*/
自定义
在幕后,这使用了 Ollama 的 JSON 模式来将输出约束为 JSON,然后将工具模式作为 JSON Schema 传入提示词。
由于不同模型有不同的优势,传入您自己的系统提示词可能会有所帮助。以下是一个示例:
// Custom system prompt to format tools. You must encourage the model
// to wrap output in a JSON object with "tool" and "tool_input" properties.
const toolSystemPromptTemplate = `You have access to the following tools:
{tools}
To use a tool, respond with a JSON object with the following structure:
{{
"tool": <name of the called tool>,
"tool_input": <parameters for the tool matching the above JSON schema>
}}`;
const model = new ChatOllama({
temperature: 0.1,
model: "mistral",
})
.bindTools([
{
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"],
},
},
])
.withConfig({
// You can set the `tool_choice` arg to force the model to use a function
tool_choice: "get_current_weather",
});
const response = await model.invoke([
new SystemMessage(toolSystemPromptTemplate),
new HumanMessage({
content: "What's the weather in Boston?",
}),
]);
console.log(response);
/*
AIMessage {
content: '',
additional_kwargs: {
function_call: {
name: 'get_current_weather',
arguments: '{"location":"Boston, MA","unit":"fahrenheit"}'
}
}
}
*/