码农知识堂 - 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/

  • 相关阅读:
    linux安装Zookeeper3.5.7详解
    ChatGPT4 完成数据分析结构分析,动态饼图可视化
    3D开发工具HOOPS Publish如何快速创建交互式3D PDF文档?
    微信小程序--WXML模板(页面逻辑)-2
    程序员都看不懂的代码
    Transformer与强化学习结合提升物联网智能决策
    电脑上的歌曲怎么传到苹果手机上面,怎么把电脑上的歌导入苹果手机
    学习笔记|串口与PC通信的接线|移植驱动程序|串口通信实战|STC32G单片机视频开发教程(冲哥)|第二十一集(上):串口与PC通信
    docker stats查询容器状态显示异常有模线
    STC51单片机学习笔记7——stc12c56 串口显示AD(单路ad+led指示灯)
  • 原文地址:https://blog.csdn.net/engchina/article/details/138006370
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号