OllamaFunctions 学习笔记

ops/2024/10/20 20:18:30/

OllamaFunctions 学习笔记

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

0. 引言

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

1. 使用方法

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

from langchain_experimental.llms.ollama_functions import OllamaFunctionsllm = OllamaFunctions(base_url="http://xxx.xxx.xxx.xxx:11434", model="gpt-4", temperature=0.0)

然后,您可以绑定使用 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"},
)

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

from langchain_core.messages import HumanMessagemodel.invoke("what is the weather in Boston?")

输出,

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

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)

输出,

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

完结!

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


http://www.ppmy.cn/ops/5560.html

相关文章

腾讯云免费ssl证书申请与宝塔手动部署

1.在我的证书 - SSL 证书 - 控制台 (tencent.com)页面点击“申请免费证书” 2.在申请页面填写域名、邮箱,对于其中“验证方式”,如果服务器是部署在腾讯云的话,可以选“自动DNS” 3.等待审核通过之后,在我的证书 - SSL 证书 - 控…

Flutter 的 showDialog 和 showCupertinoDialog 有什么区别?

我将我的 App 里用的 Flutter 升级到了 3.19,没想到,以前我用 showDialog 和 AlertDialog 组合创建的二次确认框,变得无敌难看了,大幅度增加了整个框的圆角和里面默认按钮的圆角。不得已,我必须修改一下,以…

大数据测试:构建Hadoop和Spark分布式HA运行环境

随着大数据技术的不断发展,Hadoop和Spark已成为处理大规模数据的热门框架。在生产环境中,高可用性(HA)是至关重要的,以确保数据处理和分析任务不受中断。本文将详细介绍如何构建 Hadoop和Spark分布式HA运行环境&#x…

Flutter-----异步编程:Future和Stream

异步编程:使用 Future 和 async-await | Dart 什么是异步操作/异步操作的作用? Dart 代码运行在单个执行“线程”中。如果 Dart 代码在执行时阻塞,例如:处理一个需要长时间运行的计算操作或等待 I/O 完成。此时整个程序会被“冻…

Golang插件系统实现

插件可以在解耦的基础上灵活扩展应用功能,本文介绍了如何基于Golang标准库实现插件功能,帮助我们构建更灵活可扩展的应用。原文: Plugins with Go 什么是插件 简单来说,插件就是可以被其他软件加载的软件,通常用于扩展应用程序的功…

一文学会 ts 构建工具 —— tsup

文章目录 能打包什么?安装用法自定义配置文件条件配置在 package.json 中配置多入口打包生成类型声明文件sourcemap生成格式自定义输出文件代码分割产物目标环境支持 es5编译的环境变量对开发命令行工具友好监听模式 watch提供成功构建的钩子 onSuccess压缩产物 min…

linux离线安装mysql

一、下载mysql 地址:MySQL 这里选择64为还是32为要根据操作系统来 uname -m 二、上传解压配置mysql 使用root账户登录linux服务器,在opt文件下创建mysql文件夹 cd /opt sudo mkdir mysql 使用Xftp上传mysql压缩包到此文件夹下(自行决定路径) cd mysql/…

.NET 设计模式—备忘录模式(Memento Pattern)

简介 备忘录模式,又称之为快照模式(Snapshop Pattern),是一种行为型设计模式,,它允许在不破坏对象封装性的前提下,捕获并保存一个对象的内部状态,以便在需要时恢复该对象到原先的状态。备忘录模式可以为我们…