首页 > Python资料 博客日记
ollama-python-Python快速部署Llama 3等大型语言模型最简单方法
2024-06-29 22:00:04Python资料围观12次
ollama介绍
在本地启动并运行大型语言模型。运行Llama 3、Phi 3、Mistral、Gemma和其他型号。
Llama 3
Meta Llama 3 是 Meta Inc. 开发的一系列最先进的模型,提供8B和70B参数大小(预训练或指令调整)。
Llama 3 指令调整模型针对对话/聊天用例进行了微调和优化,并且在常见基准测试中优于许多可用的开源聊天模型。
安装
pip install ollama
高性价比GPU资源:https://www.ucloud.cn/site/active/gpu.html?ytag=gpu_wenzhang_tongyong_shemei
用法
import ollamaresponse = ollama.chat(model='llama2', messages=[ { 'role': 'user', 'content': 'Why is the sky blue?', },])print(response['message']['content'])
流式响应
可以通过设置stream=True、修改函数调用以返回 Python 生成器来启用响应流,其中每个部分都是流中的一个对象。
import ollama stream = ollama.chat( model='llama2', messages=[{'role': 'user', 'content': 'Why is the sky blue?'}], stream=True, ) for chunk in stream: print(chunk['message']['content'], end='', flush=True)
应用程序编程接口
Ollama Python 库的 API 是围绕Ollama REST API设计的
聊天
ollama.chat(model='llama2', messages=[{'role': 'user', 'content': 'Why is the sky blue?'}])
新增
ollama.generate(model='llama2', prompt='Why is the sky blue?')
列表
ollama.list()
展示
ollama.show('llama2')
创建
modelfile=''' FROM llama2 SYSTEM You are mario from super mario bros. ''' ollama.create(model='example', modelfile=modelfile)
复制
ollama.copy('llama2', 'user/llama2')
删除
ollama.delete('llama2') Pull ollama.pull('llama2') push ollama.push('user/llama2')
嵌入
ollama.embeddings(model='llama2', prompt='The sky is blue because of rayleigh scattering')
定制客户端
可以使用以下字段创建自定义客户端:
- host:要连接的 Ollama 主机
- timeout: 请求超时时间
from ollama import Client client = Client(host='http://localhost:11434') response = client.chat(model='llama2', messages=[ { 'role': 'user', 'content': 'Why is the sky blue?', }, ])
异步客户端
import asyncio from ollama import AsyncClient async def chat(): message = {'role': 'user', 'content': 'Why is the sky blue?'} response = await AsyncClient().chat(model='llama2', messages=[message]) asyncio.run(chat())
设置stream=True修改函数以返回 Python 异步生成器:
import asyncio from ollama import AsyncClient async def chat(): message = {'role': 'user', 'content': 'Why is the sky blue?'} async for part in await AsyncClient().chat(model='llama2', messages=[message], stream=True): print(part['message']['content'], end='', flush=True) asyncio.run(chat())
错误
如果请求返回错误状态或在流式传输时检测到错误,则会引发错误。
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)
标签:
相关文章
最新发布
- day33-Django3.2(二)
- Python 异常处理:try、except、else 和 finally 的使用指南
- 【Python】利用代理IP爬取当当网数据做数据分析
- python经典百题之static定义静态变量的方法
- 华为OD机试C卷-- 计算疫情扩散时间(Java & JS & Python & C)
- 已经在python中安装库(如numpy、requests等)但仍无法在pycharm中使用的问题
- 【Python datetime模块精讲】:时间旅行者的日志,精准操控日期与时间
- python -- NotOpenSSLWarning: urllib3 v2 only supports OpenSSL 1.1.1+
- 华为OD机试C卷-- 高效货运(Java & JS & Python & C)
- 【python】PyQt5信号与槽原理剖析与应用实战
点击排行
- 版本匹配指南:Numpy版本和Python版本的对应关系
- 相关性分析——Pearson相关系数+热力图(附data和Python完整代码)
- 在Java中使用XxlCrawler时防止被反爬的几种方式
- Python 可视化 web 神器:streamlit、Gradio、dash、nicegui;低代码 Python Web 框架:PyWebIO
- PyPy JIT编译器背后的奥秘(揭开PyPy高性能Python的编译优化技术)
- 版本匹配指南:PyTorch版本、torchvision 版本和Python版本的对应关系
- Python分析程序性能指南 - 3种内存和CPU诊断方法助你优化代码(掌握Python内存和CPU分析技巧)
- PyCharm:提升Python开发效率的强大IDE工具