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)

相关推荐
在水一缸2 分钟前
深度解析 Grok 4.5:当推理模型遇上大规模工程实践
人工智能·深度学习·大模型·工程实践·推理模型·混合专家架构·grok 4.5
云智慧AIOps社区7 分钟前
2026 国产化 ITSM 替代指南:横向测评 ServiceNow、轻帆云、Jira等五款主流IT服务管理平台
运维·人工智能·运维开发·it服务管理·itsm平台
学习日记5259 分钟前
【提示词工程系统教程 05】上下文工程:静态指令、动态检索与RAG架构
人工智能·prompt
腻害兔9 分钟前
【若依项目-产品经理视角】RuoYi-Vue-Pro 源码拆解:ERP 企业资源模块,一个轻量级进销存的完整实现?
前端·javascript·vue.js·人工智能·前端框架·产品经理·ai编程
小羊Yveesss10 分钟前
模板建站哪个平台好?模板数量之外还要比较编辑与SEO能力
大数据·人工智能·小程序
程序员-李俞12 分钟前
向量引擎接入 SQL 问答沙箱前:只读权限、Base URL 和费用封顶怎么验收
人工智能·大模型·接口测试·api中转·ai api
梦想的初衷~13 分钟前
植被遥感反演与数据同化算法体系教程:从PROSAIL前向模拟到作物估产
人工智能·python·机器学习·作物模型·遥感数据同化·prosail·植被参数反演
LadenKiller16 分钟前
近期AI协作写量化规则,要按阶段安排任务
人工智能·python
网易云信20 分钟前
制造业、零售与物流的“神经末梢”:为什么企业级IM是这些行业的数字基建?
人工智能·agent
千瓜25 分钟前
用户洞察:负鼠走红?解读新世代“动物人格”
大数据·人工智能·数据分析·生活·新媒体