Ollama Python Library 使用指南
简介
Ollama Python库提供了一种最简单的方式将Python 3.8+项目与Ollama集成。这个库支持同步和异步操作,可以轻松实现与Ollama模型的交互。
前置条件
ollama pull <model>
拉取所需模型(例如:ollama pull llama3.2
)安装
pip install ollama
基本用法
简单对话
from ollama import chat
from ollama import ChatResponse
response: ChatResponse = chat(model='llama3.2', messages=[
{
'role': 'user',
'content': 'Why is the sky blue?',
},
])
print(response['message']['content'])
# 或直接访问响应对象的字段
print(response.message.content)
流式响应
from ollama import chat
stream = 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)
自定义客户端
同步客户端
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?',
},
])
异步客户端
import asyncio
from ollama import AsyncClient
async def chat():
message = {'role': 'user', 'content': 'Why is the sky blue?'}
response = await AsyncClient().chat(model='llama3.2', messages=[message])
asyncio.run(chat())
API功能
主要功能
chat()
: 进行对话generate()
: 生成文本list()
: 列出模型show()
: 显示模型信息create()
: 创建模型copy()
: 复制模型delete()
: 删除模型pull()
: 拉取模型push()
: 推送模型embed()
: 生成嵌入向量ps()
: 查看进程状态错误处理
try:
ollama.chat('does-not-exist-model')
except ollama.ResponseError as e:
print('Error:', e.error)
if e.status_code == 404:
ollama.pull(model)
注意事项
stream=True
启用作者:老大白菜