码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • OllamaFunctions 学习笔记


    OllamaFunctions 学习笔记

    • 0. 引言
    • 1. 使用方法
    • 2. 用于提取

    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/

  • 相关阅读:
    Spring--bean的生命周期
    讲座回顾丨基于 OpenYurt 和 EdgeX 的云边端协同新可能
    C#知识|上位机UI设计-详情窗体设计思路及流程(实例)
    计算机毕业设计Java电影周边产品查找系统(源码+系统+mysql数据库+lw文档)
    Tesla P40千元级大显卡主机装机实践
    86.(cesium篇)cesium叠加面接收阴影效果(gltf模型)
    国产压力测试工具的主要作用
    redis集群监控
    C++(21):特殊工具与技术
    Day667.开发避免踩坑注意点 -Java业务开发常见错误
  • 原文地址:https://blog.csdn.net/engchina/article/details/138006370
  • 最新文章
  • 【JVM】编译执行与解释执行的区别是什么?JVM 使用哪种方式?
    用 Hashids 优雅解决 C 端自增 ID 暴露问题
    V8引擎 精品漫游指南--Ignition篇(上) 指令 栈帧 槽位 调用约定 内存布局 基础内容
    LLVM Pass快速入门(四):代码插桩
    milkup:桌面端 markdown AI续写和即时渲染
    基于项目工程构建SBOM(软件物料清单)的研究
    鸿蒙应用开发UI基础第二节:鸿蒙应用程序框架核心解析与实操
    .NET 中如何快速实现 List 集合去重?
    扣子Coze实战:从0到1打造抖音+小红书热点监控智能体
    浅谈数据访问层
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号