ollama-python终极指南:从零构建智能对话应用
ollama-python终极指南:从零构建智能对话应用
【免费下载链接】ollama-python 项目地址: https://gitcode.com/GitHub_Trending/ol/ollama-python
Ollama Python库是Python 3.8+项目与Ollama集成的最简单方式,让开发者能够轻松构建智能对话应用。本文将为你提供一份完整的ollama-python使用指南,从安装到高级功能,助你快速上手。
准备工作:开启ollama-python之旅 🚀
在开始使用ollama-python之前,需要完成两项准备工作:
- 安装并运行Ollama
- 拉取一个模型,例如:
ollama pull gemma3。你可以在Ollama.com找到更多可用模型。
快速安装:三步搞定ollama-python ⚡
安装ollama-python非常简单,只需在终端中运行以下命令:
pip install ollama
入门示例:构建你的第一个智能对话 🤖
下面是一个简单的示例,展示如何使用ollama-python进行对话:
from ollama import chat
from ollama import ChatResponse
response: ChatResponse = chat(model='gemma3', messages=[
{
'role': 'user',
'content': 'Why is the sky blue?',
},
])
print(response['message']['content'])
# 或者直接访问响应对象的字段
print(response.message.content)
你可以在ollama/_types.py中查看更多关于响应类型的信息。
高级功能:流式响应与异步操作 ⚡
流式响应
通过设置stream=True,可以启用响应流式传输:
from ollama import chat
stream = chat(
model='gemma3',
messages=[{'role': 'user', 'content': 'Why is the sky blue?'}],
stream=True,
)
for chunk in stream:
print(chunk['message']['content'], end='', flush=True)
异步客户端
AsyncClient类用于进行异步请求,配置方式与Client类相同:
import asyncio
from ollama import AsyncClient
async def chat():
message = {'role': 'user', 'content': 'Why is the sky blue?'}
response = await AsyncClient().chat(model='gemma3', 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='gemma3', messages=[message], stream=True):
print(part['message']['content'], end='', flush=True)
asyncio.run(chat())
云端模型:释放更大算力 ☁️
通过offloading到Ollama的云端,可以运行更大的模型,同时保持本地工作流。
支持的模型
目前支持的云端模型包括:deepseek-v3.1:671b-cloud、gpt-oss:20b-cloud、gpt-oss:120b-cloud、kimi-k2:1t-cloud、qwen3-coder:480b-cloud、kimi-k2-thinking。更多信息请查看Ollama Models - Cloud。
通过本地Ollama运行云端模型
- 登录(一次性操作):
ollama signin
- 拉取云端模型:
ollama pull gpt-oss:120b-cloud
- 发送请求:
from ollama import Client
client = Client()
messages = [
{
'role': 'user',
'content': 'Why is the sky blue?',
},
]
for part in client.chat('gpt-oss:120b-cloud', messages=messages, stream=True):
print(part.message.content, end='', flush=True)
自定义客户端:灵活配置你的请求 🔧
可以通过实例化Client或AsyncClient来创建自定义客户端。所有额外的关键字参数都会传递给httpx.Client。
from ollama import Client
client = Client(
host='http://localhost:11434',
headers={'x-some-header': 'some-value'}
)
response = client.chat(model='gemma3', messages=[
{
'role': 'user',
'content': 'Why is the sky blue?',
},
])
完整API:探索更多可能性 📚
Ollama Python库的API围绕Ollama REST API设计,提供了丰富的功能:
对话
ollama.chat(model='gemma3', messages=[{'role': 'user', 'content': 'Why is the sky blue?'}])
生成
ollama.generate(model='gemma3', prompt='Why is the sky blue?')
列出模型
ollama.list()
显示模型信息
ollama.show('gemma3')
创建模型
ollama.create(model='example', from_='gemma3', system="You are Mario from Super Mario Bros.")
复制模型
ollama.copy('gemma3', 'user/gemma3')
删除模型
ollama.delete('gemma3')
拉取模型
ollama.pull('gemma3')
推送模型
ollama.push('user/gemma3')
嵌入
ollama.embed(model='gemma3', input='The sky is blue because of rayleigh scattering')
批量嵌入
ollama.embed(model='gemma3', 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)
开始你的ollama-python之旅 🌟
现在你已经了解了ollama-python的基本使用方法和高级功能,是时候开始构建你自己的智能对话应用了。无论你是想创建聊天机器人、智能助手,还是其他AI驱动的应用,ollama-python都能为你提供强大的支持。
要开始使用,只需克隆仓库:
git clone https://gitcode.com/GitHub_Trending/ol/ollama-python
然后按照本文的指南,一步步探索ollama-python的无限可能!
【免费下载链接】ollama-python 项目地址: https://gitcode.com/GitHub_Trending/ol/ollama-python
更多推荐



所有评论(0)