OllamaFunctions 学习笔记

devtools/2024/10/22 5:02:23/

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/devtools/6129.html

相关文章

基于springboot实现英语知识应用网站系统项目【项目源码+论文说明】

基于springboot实现英语知识应用网站系统演示 摘要 随着信息技术在管理上越来越深入而广泛的应用,管理信息系统的实施在技术上已逐步成熟。本文介绍了英语知识应用网站的开发全过程。通过分析英语知识应用网站管理的不足,创建了一个计算机管理英语知识应…

社区论坛小圈子小程序源码系统:自定义小程序管理社区圈子软件圈子系统系统开发 - 做社区圈子

制作阶段 1.确定需求:首先,要明确小程序的功能需求。例如,用户注册、登录、发表动态、浏览动态、评论、点赞等。同时,要确定页面的布局和设计风格。 2.设计界面:根据需求,进行界面设计。包括首页、个人中…

urllib爬虫案例(实战)

#urllib的爬虫案例-通过最原始的爬虫方式 爬虫之前如果抓包工具Fiddler证书安装失败,采用以下方法 1、打开cmd 2、进入fillder的目录 如下: 里面的路径改成你自己的安装路径 cd /d "D:\Program Files (x86)\Fiddler2" 然后再执行下…

Django admin后台添加自定义菜单和功能页面

django admin是根据注册的模型来动态生成菜单,从这个思路出发,如果想添加自定义菜单,那就创建一个空模型并且注册。步骤如下: 1、创建空模型: class ResetSVNAuthFileModel(models.Model):"""仅用来显…

第十五届蓝桥杯题解-数字接龙

题意:经过所有格子,并且不能进行交叉,走的下一个格子必须是当前格子值1%k,输出路径最小的那一条(有8个方向,一会粘图) 思路:按照8个方向设置偏移量进行dfs,第一个到达终…

春藤实业启动SAP S/4HANA Cloud Public Edition项目,与工博科技携手数字化转型之路

3月11日,广东省春藤实业有限公司(以下简称“春藤实业”)SAP S/4HANA Cloud Public Edition(以下简称“SAP ERP公有云”)项目正式启动。春藤实业董事长陈董、联络协调项目经理慕总、内部推行项目经理陈总以及工博董事长…

Hive基础2

一、数据字段类型 基本数据类型 数值相关类型 整数 tinyint smallint int bigint 小数 float double decimal 精度最高 日期类型 date 日期 timestamps 日期时间 字符串类型 string varchar char 布尔类型 BOOLEAN 表示真假 只能存储0或1 复杂类型 array 数组类型…

Docker - 容器互联

原文地址,使用效果更佳! Docker - 容器互联 | CoderMast编程桅杆Docker - 容器互联 在上一个章节中我们学习了 Docker 容器的端口映射,可以将 Docker 容器和本地以及网络中的端口进行连接起来。 但端口映射并不是唯一把 Docker 连接到另一个…