ollama-python-Python快速部署Llama 3等大型语言模型最简单方法

ollama介绍

在本地启动并运行大型语言模型。运行Llama 3、Phi 3、Mistral、Gemma和其他型号。

Llama 3

Meta Llama 3 是 Meta Inc. 开发的一系列最先进的模型,提供8B70B参数大小(预训练或指令调整)。

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)

相关推荐
MindUp2 分钟前
企业私有化文件管理系统选型实录:从部署架构到AI能力的技术调研笔记
人工智能·笔记·架构
长江后浪博客4 分钟前
无人机AI识虫:用“空中巡检 + GIS地图 + AI识别”解决大规模种植虫害问题
人工智能·无人机·智慧农业·植保无人机·无人机ai识虫·空中巡检·gis地图
ITresearchGuest10 分钟前
AI 是怎么操作浏览器的——browser use 实现原理
人工智能
悟天特斯11 分钟前
智慧楼宇楼宇自控系统:从孤立控制到协同优化的BAS架构演进
人工智能·物联网·架构
法狗狗技术团队16 分钟前
万息投标标书审查功能介绍:AI如何辅助检查投标文件?
人工智能·招标·投标·标书·标书检查·标书审查·标书检查工具
wangchunyu11421 分钟前
Cursor介绍和安装说明
人工智能
LadiesAndGentlemen30 分钟前
环球GeoAI-遥感VLM|2026-08-31|More with Less:通用 VLM 不换架构,也能在遥感基准上打平专用模型
人工智能·神经网络·目标检测·自然语言处理·aigc
杨杨杨大侠34 分钟前
一次大模型 API 请求是怎么跑起来的:从 Harness 到 GPU、并发与 KV Cache
aigc·openai·ai编程
小道士写程序35 分钟前
自然语言处理NLP - 开篇 什么是 NLP
人工智能·自然语言处理
zzzll111136 分钟前
RAGFlow:开源 RAG 引擎的架构解析与实战指南
人工智能