当前位置: 首页 > news >正文

​《Ollama Python 库​》

Ollama Python 库

Ollama Python 库提供了将 Python 3.8+ 项目与 Ollama 集成的最简单方法。

先决条件

  • 应该安装并运行 Ollama
  • 拉取一个模型以与库一起使用:例如ollama pull <model>ollama pull llama3.2
    • 有关可用模型的更多信息,请参阅 Ollama.com。

安装

pip install ollama

用法

from ollama import chat
from ollama import ChatResponseresponse: ChatResponse = chat(model='llama3.2', messages=[{'role': 'user','content': 'Why is the sky blue?',},
])
print(response['message']['content'])
# or access fields directly from the response object
print(response.message.content)

有关响应类型的更多信息,请参阅 _types.py。

流式处理响应

可以通过设置 来启用响应流。stream=True

from ollama import chatstream = chat(model='llama3.2',messages=[{'role': 'user', 'content': 'Why is the sky blue?'}],stream=True,
)for chunk in stream:print(chunk['message']['content'], end='', flush=True)

自定义客户端

可以通过实例化 或从 创建自定义客户端。ClientAsyncClientollama

所有额外的关键字参数都传递到 httpx 中。客户端。

from ollama import Client
client = Client(host='http://localhost:11434',headers={'x-some-header': 'some-value'}
)
response = client.chat(model='llama3.2', messages=[{'role': 'user','content': 'Why is the sky blue?',},
])

异步客户端

该类用于发出异步请求。它可以配置与类相同的字段。AsyncClientClient

import asyncio
from ollama import AsyncClientasync def chat():message = {'role': 'user', 'content': 'Why is the sky blue?'}response = await AsyncClient().chat(model='llama3.2', messages=[message])asyncio.run(chat())

设置 modify 函数以返回 Python 异步生成器:stream=True

import asyncio
from ollama import AsyncClientasync def chat():message = {'role': 'user', 'content': 'Why is the sky blue?'}async for part in await AsyncClient().chat(model='llama3.2', messages=[message], stream=True):print(part['message']['content'], end='', flush=True)asyncio.run(chat())

应用程序接口

Ollama Python 库的 API 是围绕 Ollama REST API 设计的

聊天

ollama.chat(model='llama3.2', messages=[{'role': 'user', 'content': 'Why is the sky blue?'}])

生成

ollama.generate(model='llama3.2', prompt='Why is the sky blue?')

列表

ollama.list()

显示

ollama.show('llama3.2')

创造

ollama.create(model='example', from_='llama3.2', system="You are Mario from Super Mario Bros.")

复制

ollama.copy('llama3.2', 'user/llama3.2')

删除

ollama.delete('llama3.2')

ollama.pull('llama3.2')

ollama.push('user/llama3.2')

嵌入

ollama.embed(model='llama3.2', input='The sky is blue because of rayleigh scattering')

嵌入(批处理)

ollama.embed(model='llama3.2', input=['The sky is blue because of rayleigh scattering', 'Grass is green because of chlorophyll'])

附言

ollama.ps()

错误

如果请求返回错误状态或在流式传输时检测到错误,则会引发错误。

model = 'does-not-yet-exist'try:ollama.chat(model)
except ollama.ResponseError as e:print('Error:', e.error)if e.status_code == 404:ollama.pull(model)
http://www.lryc.cn/news/530205.html

相关文章:

  • Java的Integer缓存池
  • Ubuntu16.04编译安装Cartographer 1.0版本
  • Qt调用FFmpeg库实时播放UDP组播视频流
  • C# 类与对象详解
  • 【Elasticsearch 基础入门】Centos7下Elasticsearch 7.x安装与配置(单机)
  • 大模型本地部署使用方法(Ollama脚手架工具、FisherAI浏览器大模型插件、AnythingLLM大模型集成应用平台)
  • 【华为OD-E卷 - 报数游戏 100分(python、java、c++、js、c)】
  • 深入理解Spring框架:从基础到实践
  • 一觉醒来全球编码能力下降100000倍,新手小白的我决定科普C语言——函数
  • CentOS 上安装 Go (Golang)
  • 软件模拟I2C案例前提须知——EEPROM芯片之M24C02
  • GIS教程:全国数码商城系统
  • BroadCom-RDMA博通网卡如何进行驱动安装和设置使得对应网口具有RDMA功能以适配RDMA相机
  • 分布式微服务系统架构第90集:现代化金融核心系统
  • 进阶数据结构——双向循环链表
  • 记录一次,PyQT的报错,多线程Udp失效,使用工具如netstat来检查端口使用情况。
  • 安装anaconda3 后 电脑如何单独运行python,python还需要独立安装吗?
  • 电子电气架构 --- 汽车电子拓扑架构的演进过程
  • ASP.NET Core 中使用依赖注入 (DI) 容器获取并执行自定义服务
  • leetcode——验证二叉搜索树(java)
  • 搜索引擎快速收录:关键词布局的艺术
  • VLN视觉语言导航基础
  • 4 Hadoop 面试真题
  • java练习(2)
  • vscode命令面板输入 CMake:build不执行提示输入
  • Java中对消息序列化和反序列化并且加入到Spring消息容器中
  • FFmpeg源码:av_base64_decode函数分析
  • 【后端面试总结】mysql的group by怎么用
  • 计算机视觉和图像处理
  • 一文读懂Python之random模块(31)