Skip to content

Ollama API

算力舱中默认部署了 Ollama,对于微服中的应用可以通过直接使用算力舱暴露的 Ollama API 来使用。

  • ollama 地址为
  • ollama 兼容 OpenAI API 地址为

如果您本地也安装了微服客户端,您也可以使用上面的连接给您本地的应用调用。比如可以直接使用本地的 curl 访问。

sh
curl https://ollama-ai.${微服名称}.heiyu.space/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{"model": "qwen3:4b", "messages": [{"role": "user", "content": "Hello!"}]}'

算力舱中的 ollama 也将端口暴露在局域网中了,您可以通过 http://算力舱ip:11434 来访问 ollama 服务. 算力舱的局域网ip可以在设备列表中查看

python 示例

python
import base64
import requests
import json

# 获取图片并进行 base64 编码
image_url = "https://i-blog.csdnimg.cn/direct/7184675ec6ad495e8b7469c17edd56d9.png"
try:
    response = requests.get(image_url)
    response.raise_for_status()  # 检查请求是否成功
    image_data = response.content
    encoded_image = base64.b64encode(image_data).decode('utf-8')
except requests.exceptions.RequestException as e:
    print(f"Error downloading image: {e}")
    exit(1)

# 构建请求的 JSON 数据
request_data = {
  "model": "qwen3-vl:2b-instruct",
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": "提取文字为 Markdown 格式,要求格式正确。"
        },
        {
          "type": "image_url",
          "image_url": {
            "url": f"data:image/jpeg;base64,{encoded_image}"
          }
        }
      ]
    }
  ]
}

ollama_api_url = "https://ollama-ai.${微服名称}.heiyu.space/v1/chat/completions"
headers = {
    "Content-Type": "application/json"
}

try:
    response = requests.post(ollama_api_url, headers=headers, data=json.dumps(request_data))
    response.raise_for_status()  # 检查请求是否成功

    # 打印响应
    print(response.json())
except requests.exceptions.RequestException as e:
    print(f"Error sending request to Ollama API: {e}")