!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){var t=null;try{t=new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}return t}()||function(){var t=null;try{t=localStorage.getItem("theme")}catch(t){}return t}();t(null!==e?e:"light")}(),document.documentElement.setAttribute("data-announcement-bar-initially-dismissed",function(){try{return"true"===localStorage.getItem("docusaurus.announcement.dismiss")}catch(t){}return!1}())

[Skip to main content](#__docusaurus_skipToContent_fallback)

LangChain v0.2 is coming soon! Preview the new docs [here](/v0.2/docs/introduction/).

[

![🦜️🔗 Langchain](/v0.1/img/brand/wordmark.png)![🦜️🔗 Langchain](/v0.1/img/brand/wordmark-dark.png)

](/v0.1/)[Docs](/v0.1/docs/get_started/introduction/)[Use cases](/v0.1/docs/use_cases/)[Integrations](/v0.1/docs/integrations/platforms/)[API Reference](https://api.js.langchain.com)

[More](#)

*   [People](/v0.1/docs/people/)
*   [Community](/v0.1/docs/community/)
*   [Tutorials](/v0.1/docs/additional_resources/tutorials/)
*   [Contributing](/v0.1/docs/contributing/)

[v0.1](#)

*   [v0.2](https://js.langchain.com/v0.2/docs/introduction)
*   [v0.1](/v0.1/docs/get_started/introduction/)

[🦜🔗](#)

*   [LangSmith](https://smith.langchain.com)
*   [LangSmith Docs](https://docs.smith.langchain.com)
*   [LangChain Hub](https://smith.langchain.com/hub)
*   [LangServe](https://github.com/langchain-ai/langserve)
*   [Python Docs](https://python.langchain.com/)

[Chat](https://chatjs.langchain.com)[](https://github.com/langchain-ai/langchainjs)

Search

*   [Providers](/v0.1/docs/integrations/platforms/)
    
    *   [Providers](/v0.1/docs/integrations/platforms/)
    *   [Anthropic](/v0.1/docs/integrations/platforms/anthropic/)
    *   [AWS](/v0.1/docs/integrations/platforms/aws/)
    *   [Google](/v0.1/docs/integrations/platforms/google/)
    *   [Microsoft](/v0.1/docs/integrations/platforms/microsoft/)
    *   [OpenAI](/v0.1/docs/integrations/platforms/openai/)
*   [Components](/v0.1/docs/integrations/components/)
    
    *   [LLMs](/v0.1/docs/integrations/llms/)
        
    *   [Chat models](/v0.1/docs/integrations/chat/)
        
        *   [Chat models](/v0.1/docs/integrations/chat/)
        *   [Alibaba Tongyi](/v0.1/docs/integrations/chat/alibaba_tongyi/)
        *   [Anthropic](/v0.1/docs/integrations/chat/anthropic/)
        *   [Anthropic Tools](/v0.1/docs/integrations/chat/anthropic_tools/)
        *   [Azure OpenAI](/v0.1/docs/integrations/chat/azure/)
        *   [Baidu Wenxin](/v0.1/docs/integrations/chat/baidu_wenxin/)
        *   [Bedrock](/v0.1/docs/integrations/chat/bedrock/)
        *   [Cloudflare Workers AI](/v0.1/docs/integrations/chat/cloudflare_workersai/)
        *   [Cohere](/v0.1/docs/integrations/chat/cohere/)
        *   [Fake LLM](/v0.1/docs/integrations/chat/fake/)
        *   [Fireworks](/v0.1/docs/integrations/chat/fireworks/)
        *   [Friendli](/v0.1/docs/integrations/chat/friendli/)
        *   [Google GenAI](/v0.1/docs/integrations/chat/google_generativeai/)
        *   [(Legacy) Google PaLM/VertexAI](/v0.1/docs/integrations/chat/google_palm/)
        *   [Google Vertex AI](/v0.1/docs/integrations/chat/google_vertex_ai/)
        *   [Groq](/v0.1/docs/integrations/chat/groq/)
        *   [Llama CPP](/v0.1/docs/integrations/chat/llama_cpp/)
        *   [Minimax](/v0.1/docs/integrations/chat/minimax/)
        *   [Mistral AI](/v0.1/docs/integrations/chat/mistral/)
        *   [NIBittensorChatModel](/v0.1/docs/integrations/chat/ni_bittensor/)
        *   [Ollama](/v0.1/docs/integrations/chat/ollama/)
        *   [Ollama Functions](/v0.1/docs/integrations/chat/ollama_functions/)
        *   [OpenAI](/v0.1/docs/integrations/chat/openai/)
        *   [PremAI](/v0.1/docs/integrations/chat/premai/)
        *   [PromptLayer OpenAI](/v0.1/docs/integrations/chat/prompt_layer_openai/)
        *   [TogetherAI](/v0.1/docs/integrations/chat/togetherai/)
        *   [WebLLM](/v0.1/docs/integrations/chat/web_llm/)
        *   [YandexGPT](/v0.1/docs/integrations/chat/yandex/)
        *   [ZhipuAI](/v0.1/docs/integrations/chat/zhipuai/)
    *   [Document loaders](/v0.1/docs/integrations/document_loaders/)
        
    *   [Document transformers](/v0.1/docs/integrations/document_transformers/)
        
    *   [Document compressors](/v0.1/docs/integrations/document_compressors/)
        
    *   [Text embedding models](/v0.1/docs/integrations/text_embedding/)
        
    *   [Vector stores](/v0.1/docs/integrations/vectorstores/)
        
    *   [Retrievers](/v0.1/docs/integrations/retrievers/)
        
    *   [Tools](/v0.1/docs/integrations/tools/)
        
    *   [Agents and toolkits](/v0.1/docs/integrations/toolkits/)
        
    *   [Chat Memory](/v0.1/docs/integrations/chat_memory/)
        
    *   [Stores](/v0.1/docs/integrations/stores/)
        

*   [](/v0.1/)
*   [Components](/v0.1/docs/integrations/components/)
*   [Chat models](/v0.1/docs/integrations/chat/)
*   Ollama Functions

On this page

Ollama Functions
================

LangChain offers an experimental wrapper around open source models run locally via [Ollama](https://github.com/jmorganca/ollama) that gives it the same API as OpenAI Functions.

Note that more powerful and capable models will perform better with complex schema and/or multiple functions. The examples below use [Mistral](https://ollama.ai/library/mistral).

Setup[​](#setup "Direct link to Setup")
---------------------------------------

Follow [these instructions](https://github.com/jmorganca/ollama) to set up and run a local Ollama instance.

Initialize model[​](#initialize-model "Direct link to Initialize model")
------------------------------------------------------------------------

You can initialize this wrapper the same way you'd initialize a standard `ChatOllama` instance:

    import { OllamaFunctions } from "langchain/experimental/chat_models/ollama_functions";const model = new OllamaFunctions({  temperature: 0.1,  model: "mistral",});

Passing in functions[​](#passing-in-functions "Direct link to Passing in functions")
------------------------------------------------------------------------------------

You can now pass in functions the same way as OpenAI:

    import { OllamaFunctions } from "langchain/experimental/chat_models/ollama_functions";import { HumanMessage } from "@langchain/core/messages";const model = new OllamaFunctions({  temperature: 0.1,  model: "mistral",}).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"}'      }    }  }*/

#### API Reference:

*   [OllamaFunctions](https://api.js.langchain.com/classes/langchain_experimental_chat_models_ollama_functions.OllamaFunctions.html) from `langchain/experimental/chat_models/ollama_functions`
*   [HumanMessage](https://api.js.langchain.com/classes/langchain_core_messages.HumanMessage.html) from `@langchain/core/messages`

Using for extraction[​](#using-for-extraction "Direct link to Using for extraction")
------------------------------------------------------------------------------------

    import { z } from "zod";import { zodToJsonSchema } from "zod-to-json-schema";import { OllamaFunctions } from "langchain/experimental/chat_models/ollama_functions";import { JsonOutputFunctionsParser } from "langchain/output_parsers";import { PromptTemplate } from "@langchain/core/prompts";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 declarationconst 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 OllamaFunctions({  temperature: 0.1,  model: "mistral",}).bind({  functions: [    {      name: "information_extraction",      description: "Extracts the relevant information from the passage.",      parameters: {        type: "object",        properties: zodToJsonSchema(schema),      },    },  ],  function_call: {    name: "information_extraction",  },});// Use a JsonOutputFunctionsParser to get the parsed JSON response directly.const chain = await 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(response);/*  {    people: [      { name: 'Alex', height: 5, hairColor: 'blonde' },      { name: 'Claudia', height: 6, hairColor: 'orange' }    ]  }*/

#### API Reference:

*   [OllamaFunctions](https://api.js.langchain.com/classes/langchain_experimental_chat_models_ollama_functions.OllamaFunctions.html) from `langchain/experimental/chat_models/ollama_functions`
*   [JsonOutputFunctionsParser](https://api.js.langchain.com/classes/langchain_output_parsers.JsonOutputFunctionsParser.html) from `langchain/output_parsers`
*   [PromptTemplate](https://api.js.langchain.com/classes/langchain_core_prompts.PromptTemplate.html) from `@langchain/core/prompts`

You can see a LangSmith trace of what this looks like here: [https://smith.langchain.com/public/31457ea4-71ca-4e29-a1e0-aa80e6828883/r](https://smith.langchain.com/public/31457ea4-71ca-4e29-a1e0-aa80e6828883/r)

Customization[​](#customization "Direct link to Customization")
---------------------------------------------------------------

Behind the scenes, this uses Ollama's JSON mode to constrain output to JSON, then passes tools schemas as JSON schema into the prompt.

Because different models have different strengths, it may be helpful to pass in your own system prompt. Here's an example:

    import { OllamaFunctions } from "langchain/experimental/chat_models/ollama_functions";import { HumanMessage } from "@langchain/core/messages";// 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 OllamaFunctions({  temperature: 0.1,  model: "mistral",  toolSystemPromptTemplate,}).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"}'      }    }  }*/

#### API Reference:

*   [OllamaFunctions](https://api.js.langchain.com/classes/langchain_experimental_chat_models_ollama_functions.OllamaFunctions.html) from `langchain/experimental/chat_models/ollama_functions`
*   [HumanMessage](https://api.js.langchain.com/classes/langchain_core_messages.HumanMessage.html) from `@langchain/core/messages`

* * *

#### Help us out by providing feedback on this documentation page:

[

Previous

Ollama

](/v0.1/docs/integrations/chat/ollama/)[

Next

OpenAI

](/v0.1/docs/integrations/chat/openai/)

*   [Setup](#setup)
*   [Initialize model](#initialize-model)
*   [Passing in functions](#passing-in-functions)
*   [Using for extraction](#using-for-extraction)
*   [Customization](#customization)

Community

*   [Discord](https://discord.gg/cU2adEyC7w)
*   [Twitter](https://twitter.com/LangChainAI)

GitHub

*   [Python](https://github.com/langchain-ai/langchain)
*   [JS/TS](https://github.com/langchain-ai/langchainjs)

More

*   [Homepage](https://langchain.com)
*   [Blog](https://blog.langchain.dev)

Copyright © 2024 LangChain, Inc.