• OllamaFunctions 学习笔记


    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/

  • 相关阅读:
    软件测试 -进阶篇
    VMware Ubuntu16.04 下网络连接不了 修复(unkonwn host)
    可以 DIY 装修的商城系统,你也能拥有!
    第二章:String类
    Notification通知框样式添加
    文件操作-
    Java项目:SSM图书馆图书管理借阅书籍管理系统
    深入浅出org.springframework.util
    2021中国自动驾驶环卫场景商业化应用研究报告
    PDF Shaper Pro v12.8 全能PDF工具箱中文版
  • 原文地址:https://blog.csdn.net/engchina/article/details/138006370