!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)

You are viewing the **preview** v0.2 docs. View the **stable** v0.1 docs [here](/v0.1/docs/get_started/introduction/). Leave feedback on the v0.2 docs [here](https://github.com/langchain-ai/langchainjs/discussions/5386).

[

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

](/v0.2/)[Integrations](/v0.2/docs/integrations/platforms/)[API Reference](https://v02.api.js.langchain.com)

[More](#)

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

[v0.2](#)

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

*   [](/v0.2/)
*   [Components](/v0.2/docs/integrations/components)
*   [Chat models](/v0.2/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/community/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://v02.api.js.langchain.com/classes/langchain_community_experimental_chat_models_ollama_functions.OllamaFunctions.html) from `@langchain/community/experimental/chat_models/ollama_functions`
*   [HumanMessage](https://v02.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/community/experimental/chat_models/ollama_functions";import { PromptTemplate } from "@langchain/core/prompts";import { JsonOutputFunctionsParser } from "@langchain/core/output_parsers/openai_functions";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://v02.api.js.langchain.com/classes/langchain_community_experimental_chat_models_ollama_functions.OllamaFunctions.html) from `@langchain/community/experimental/chat_models/ollama_functions`
*   [PromptTemplate](https://v02.api.js.langchain.com/classes/langchain_core_prompts.PromptTemplate.html) from `@langchain/core/prompts`
*   [JsonOutputFunctionsParser](https://v02.api.js.langchain.com/classes/langchain_core_output_parsers_openai_functions.JsonOutputFunctionsParser.html) from `@langchain/core/output_parsers/openai_functions`

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/community/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://v02.api.js.langchain.com/classes/langchain_community_experimental_chat_models_ollama_functions.OllamaFunctions.html) from `@langchain/community/experimental/chat_models/ollama_functions`
*   [HumanMessage](https://v02.api.js.langchain.com/classes/langchain_core_messages.HumanMessage.html) from `@langchain/core/messages`

* * *

#### Was this page helpful?

  

#### You can leave detailed feedback [on GitHub](https://github.com/langchain-ai/langchainjs/issues/new?assignees=&labels=03+-+Documentation&projects=&template=documentation.yml&title=DOC%3A+%3CPlease+write+a+comprehensive+title+after+the+%27DOC%3A+%27+prefix%3E).

[

Previous

Ollama

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

Next

OpenAI

](/v0.2/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.