当前位置:   article > 正文

OllamaFunctions 学习笔记_ollama function call

ollama function call

OllamaFunctions 学习笔记

0. 引言

此文章展示了如何使用 Ollama 的实验性包装器,为其提供与 OpenAI Functions 相同的 API。

1. 使用方法

您可以按照与初始化标准 ChatOllama 实例类似的方式初始化 OllamaFunctions:

from langchain_experimental.llms.ollama_functions import OllamaFunctions

llm = OllamaFunctions(base_url="http://xxx.xxx.xxx.xxx:11434", model="gpt-4", temperature=0.0)
  • 1
  • 2
  • 3

然后,您可以绑定使用 JSON Schema 参数和 function_call 参数定义的函数,以强制模型调用给定函数:

model = model.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"],
            },
        }
    ],
    function_call={"name": "get_current_weather"},
)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

使用此模型调用函数会产生与提供的架构匹配的 JSON 输出:

from langchain_core.messages import HumanMessage

model.invoke("what is the weather in Boston?")
  • 1
  • 2
  • 3

输出,

AIMessage(content='', additional_kwargs={'function_call': {'name': 'get_current_weather', 'arguments': '{"location": "Boston, MA", "unit": "celsius"}'}})
  • 1

2. 用于提取

您可以在此处使用函数调用做的一件有用的事情是以结构化格式从给定输入中提取属性:

from langchain.chains import create_extraction_chain

# Schema
schema = {
    "properties": {
        "name": {"type": "string"},
        "height": {"type": "integer"},
        "hair_color": {"type": "string"},
    },
    "required": ["name", "height"],
}

# Input
input = """Alex is 5 feet tall. Claudia is 1 feet taller than Alex and jumps higher than him. Claudia is a brunette and Alex is blonde."""

# Run chain
llm = OllamaFunctions(model="mistral", temperature=0)
chain = create_extraction_chain(schema, llm)
chain.run(input)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

输出,

[{'name': 'Alex', 'height': 5, 'hair_color': 'blonde'},
 {'name': 'Claudia', 'height': 6, 'hair_color': 'brunette'}]
  • 1
  • 2

完结!

refer: https://python.langchain.com/docs/integrations/chat/ollama_functions/

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号