作者: Jakub Rusinowski · 最后更新: 2026年7月10日
把你的本地机器变成供其他应用调用的 AI 后端。
把你的本地机器变成供其他应用调用的 AI 后端。
Ollama 默认在 11434 端口运行一个服务器。
curl http://localhost:11434/api/generate -d '{
"model": "llama3.1",
"prompt": "Why is the sky blue?"
}'
许多应用需要 OpenAI 的 API 格式。Ollama 也提供了这一点! 端点: http://localhost:11434/v1/chat/completions
from openai import OpenAI
client = OpenAI(
base_url='http://localhost:11434/v1',
api_key='ollama', # required but unused
)
response = client.chat.completions.create(
model="llama3.1",
messages=[{"role": "user", "content": "Hello!"}]
)
print(response.choices[0].message.content)