forked from langchain-ai/langchainjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow custom system prompt for Ollama functions (langchain-ai#3264)
- Loading branch information
1 parent
4dbc702
commit 0e18ab0
Showing
3 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
examples/src/models/chat/ollama_functions/custom_prompt.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { OllamaFunctions } from "langchain/experimental/chat_models/ollama_functions"; | ||
import { HumanMessage } from "langchain/schema"; | ||
import { PromptTemplate } from "langchain/prompts"; | ||
|
||
// 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 toolSystemPrompt = | ||
PromptTemplate.fromTemplate(`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 OllamaFunctions({ | ||
temperature: 0.1, | ||
model: "mistral", | ||
toolSystemPrompt, | ||
}).bind({ | ||
functions: [ | ||
{ | ||
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"], | ||
}, | ||
}, | ||
], | ||
// You can set the `function_call` arg to force the model to use a function | ||
function_call: { | ||
name: "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"}' | ||
} | ||
} | ||
} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters