网站首页轮播图片,小学生课程同步做网站软件,wordpress 文章最长,wordpress能做图片站Ollama Python 库 Ollama Python 库提供了将 Python 3.8 项目与 Ollama 集成的最简单方法。 先决条件 应该安装并运行 Ollama拉取一个模型以与库一起使用#xff1a;例如ollama pull modelollama pull llama3.2 有关可用模型的更多信息#xff0c;请参阅 Ollama.com。… Ollama Python 库 Ollama Python 库提供了将 Python 3.8 项目与 Ollama 集成的最简单方法。 先决条件 应该安装并运行 Ollama拉取一个模型以与库一起使用例如ollama pull modelollama pull llama3.2 有关可用模型的更多信息请参阅 Ollama.com。 安装 pip install ollama 用法 from ollama import chat
from ollama import ChatResponseresponse: ChatResponse chat(modelllama3.2, messages[{role: user,content: Why is the sky blue?,},
])
print(response[message][content])
# or access fields directly from the response object
print(response.message.content) 有关响应类型的更多信息请参阅 _types.py。 流式处理响应 可以通过设置 来启用响应流。streamTrue from ollama import chatstream chat(modelllama3.2,messages[{role: user, content: Why is the sky blue?}],streamTrue,
)for chunk in stream:print(chunk[message][content], end, flushTrue) 自定义客户端 可以通过实例化 或从 创建自定义客户端。ClientAsyncClientollama 所有额外的关键字参数都传递到 httpx 中。客户端。 from ollama import Client
client Client(hosthttp://localhost:11434,headers{x-some-header: some-value}
)
response client.chat(modelllama3.2, messages[{role: user,content: Why is the sky blue?,},
]) 异步客户端 该类用于发出异步请求。它可以配置与类相同的字段。AsyncClientClient import asyncio
from ollama import AsyncClientasync def chat():message {role: user, content: Why is the sky blue?}response await AsyncClient().chat(modelllama3.2, messages[message])asyncio.run(chat()) 设置 modify 函数以返回 Python 异步生成器streamTrue import asyncio
from ollama import AsyncClientasync def chat():message {role: user, content: Why is the sky blue?}async for part in await AsyncClient().chat(modelllama3.2, messages[message], streamTrue):print(part[message][content], end, flushTrue)asyncio.run(chat()) 应用程序接口 Ollama Python 库的 API 是围绕 Ollama REST API 设计的 聊天 ollama.chat(modelllama3.2, messages[{role: user, content: Why is the sky blue?}]) 生成 ollama.generate(modelllama3.2, promptWhy is the sky blue?) 列表 ollama.list() 显示 ollama.show(llama3.2) 创造 ollama.create(modelexample, from_llama3.2, systemYou are Mario from Super Mario Bros.) 复制 ollama.copy(llama3.2, user/llama3.2) 删除 ollama.delete(llama3.2) 拉 ollama.pull(llama3.2) 推 ollama.push(user/llama3.2) 嵌入 ollama.embed(modelllama3.2, inputThe sky is blue because of rayleigh scattering) 嵌入批处理 ollama.embed(modelllama3.2, input[The sky is blue because of rayleigh scattering, Grass is green because of chlorophyll]) 附言 ollama.ps() 错误 如果请求返回错误状态或在流式传输时检测到错误则会引发错误。 model does-not-yet-existtry:ollama.chat(model)
except ollama.ResponseError as e:print(Error:, e.error)if e.status_code 404:ollama.pull(model)