教程 9:搭建本地 API 服务器

作者: Jakub Rusinowski · 最后更新: 2026年7月10日

把你的本地机器变成供其他应用调用的 AI 后端。

本指南内容

把你的本地机器变成供其他应用调用的 AI 后端。

Ollama API

Ollama 默认在 11434 端口运行一个服务器。

curl http://localhost:11434/api/generate -d '{
  "model": "llama3.1",
  "prompt": "Why is the sky blue?"
}'

OpenAI 兼容端点

许多应用需要 OpenAI 的 API 格式。Ollama 也提供了这一点! 端点: http://localhost:11434/v1/chat/completions

Python 示例

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)

更进一步

← 所有指南 | 检查 GPU 兼容性