OllamaFunctions 学习笔记

embedded/2024/10/22 4:46:15/

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/embedded/4491.html

相关文章

【机器学习】机器学习学习笔记 - 数据预处理 - 01

machine learning 监督学习: 是指在有标记的样本(labeled samples)上建立机器学习的模型无监督学习: 是指在没有标记的样本上建立机器学习的模型semi-supervised learning: 是指在部分标记样本上建立机器学习的模型强化学习: 是指在与环境交互的过程中&…

Liunx入门学习 之 基础操作指令讲解(小白必看)

股票的规律找到了,不是涨就是跌 一、Linux下基本指令 1.ls 指令 2.pwd 命令 3.cd 指令 4.touch 指令 5.mkdir 指令 6.rmdir指令 && rm 指令 7.man 指令 8.cp 指令 9.mv指令 10.cat 11.more 指令 12.less 指令 13.head 指令 14.tail 指令 15…

音视频、网络带宽等常用概念详解

1.aac音频参数解释 AAC帧大小为1024个sample:指AAC编码一般以1024个采样为一个音频帧。 采样率48khz:指1秒(即1000毫秒)采集48000次。 计算1秒有多少帧:48000 / 1024 46.875 a(用a表示计算结果&#xff0…

09篇 docker命令详解

1. docker run -d --name my_container -p 8080:80 my_image 这条命令会创建并启动一个后台运行的容器,其中-d代表以守护进程方式运行,--name用于指定容器名称,-p用于端口映射,将宿主机的8080端口映射到容器的80端口&#xff0c…

有回显命令执行-命令拼接+关键字绕过

[题目信息]: 题目名称题目难度有回显命令执行-命令拼接(训练题)2 [题目考点]: 远程命令执行漏洞,用户通过浏览器提交执行命令,由于服务器端没有针对执行函数做过滤,导致在没有指定绝对路径的…

【机器学习】探究Q-Learning通过学习最优策略来解决AI序列决策问题

꒰˃͈꒵˂͈꒱ write in front ꒰˃͈꒵˂͈꒱ ʕ̯•͡˔•̯᷅ʔ大家好,我是xiaoxie.希望你看完之后,有不足之处请多多谅解,让我们一起共同进步૮₍❀ᴗ͈ . ᴗ͈ აxiaoxieʕ̯•͡˔•̯᷅ʔ—CSDN博客 本文由xiaoxieʕ̯•͡˔•̯᷅ʔ 原创 CSDN …

短视频素材哪里去找?推荐几个视频素材免费下载的网站

高质量的资源,会让你的视频创作更加生动和具有吸引力。下面我为你介绍世界各地的优质无水印视频素材网站,以拓宽你的创作视野,帮助你在这个视觉革命的时代中脱颖而出。 1. 蛙学府(中国) 提供各种类别的优质高清视频素…

idm线程越多越好吗 idm线程数多少合适 IDM百度云下载 IDM下载器如何修改线程数

IDM(Internet Download Manager)是一款流行的网络下载器,它支持多线程下载,这意味着它可以同时建立多个连接来下载文件的不同部分,从而提高下载速度。我们在使用IDM的时候总是有很多疑问,今天我们学习IDM线…