Ollama代理

更多
2026-01-11 18:02 - 2026-01-11 18:10 #1005 by service
新帖
# 1. 确保Ollama在运行(默认端口11434)
Code:
ollama serve
# 或者检查Ollama是否已在运行
Code:
curl http://localhost:11434/api/tags

# 2. 运行我们的代理程序
Code:
python ollama_joomla_publisher.py

# 3. 配置客户端使用代理端口
# 将客户端的API地址从 localhost:11434 改为 localhost:11435如果你没有安装Ollama,需要先安装Windows安装Ollama:powershell# 1. 下载并安装Ollama
# 访问: ollama.com/download/windows

# 2. 安装后启动Ollama服务
# Ollama会作为Windows服务自动启动

# 3. 拉取模型(例如llama3.2)
Code:
ollama pull llama3.2

# 4. 运行我们的代理程序
Code:
python ollama_joomla_publisher.py

# 5. 测试连接
Code:
curl http://localhost:11435/api/tags

如何配置不同客户端使用代理:
1. Ollama官方Web UI
修改连接地址为:
Code:
http://localhost:11435

2. Open WebUI
在设置中修改API地址:yaml# 在docker-compose.yml或环境变量中
Code:
OLLAMA_API_BASE_URL: http://localhost:11435

3. ChatBox客户端
在设置中修改:
   API Base URL: http://localhost:11435

4. Python代码
python# 原来的代码:
Code:
import requests response = requests.post('http://localhost:11434/api/generate', ...)


# 改为:
Code:
response = requests.post('http://localhost:11435/api/generate', ...)


一个测试脚本,验证一切是否正常:
Code:
python# test_ollama_proxy.py import requests import json def test_ollama_direct(): """测试直接连接Ollama""" try: response = requests.get('http://localhost:11434/api/tags', timeout=5) if response.status_code == 200: print("✅ Ollama本体运行正常 (端口11434)") return True else: print(f"❌ Ollama返回错误: {response.status_code}") return False except Exception as e: print(f"❌ 无法连接到Ollama (11434): {e}") return False def test_proxy(): """测试代理服务器""" try: response = requests.get('http://localhost:11435/api/tags', timeout=5) if response.status_code == 200: print("✅ 代理服务器运行正常 (端口11435)") return True else: print(f"❌ 代理服务器返回错误: {response.status_code}") return False except Exception as e: print(f"❌ 无法连接到代理服务器 (11435): {e}") return False def test_chat_through_proxy(): """通过代理发送聊天请求""" try: data = { "model": "llama3.2", "prompt": "你好,请说'测试成功'", "stream": False } response = requests.post( 'http://localhost:11435/api/generate', json=data, timeout=30 ) if response.status_code == 200: result = response.json() print(f"✅ 通过代理聊天成功: {result.get('response', '')[:50]}...") return True else: print(f"❌ 聊天请求失败: {response.status_code}") return False except Exception as e: print(f"❌ 聊天测试失败: {e}") return False def main(): print("🧪 Ollama代理测试") print("=" * 40) # 测试1:检查Ollama是否运行 print("\n1. 检查Ollama本体...") ollama_ok = test_ollama_direct() if not ollama_ok: print("\n⚠️ Ollama未运行或未安装") print("请先启动Ollama:") print(" 1. 打开Ollama应用") print(" 2. 或运行: ollama serve") print(" 3. 或安装Ollama: https://ollama.com") return # 测试2:检查代理服务器 print("\n2. 检查代理服务器...") proxy_ok = test_proxy() if not proxy_ok: print("\n⚠️ 代理服务器未运行") print("请先运行代理程序:") print(" python ollama_joomla_publisher.py") return # 测试3:通过代理聊天 print("\n3. 测试通过代理聊天...") test_chat_through_proxy() print("\n" + "=" * 40) print("📋 总结:") print(f" Ollama本体: {'✅ 正常' if ollama_ok else '❌ 异常'}") print(f" 代理服务器: {'✅ 正常' if proxy_ok else '❌ 异常'}") if ollama_ok and proxy_ok: print("\n🎉 所有测试通过!") print("\n📝 使用说明:") print(" 1. 确保 ollama_joomla_publisher.py 在运行") print(" 2. 将你的客户端API地址改为: http://localhost:11435") print(" 3. 开始对话,内容会自动发布到Joomla") if __name__ == "__main__": main()


快速诊断步骤:
  1. 首先检查Ollama是否安装:powershell# 检查Ollama是否安装
    ollama --version

    # 如果没有安装,下载安装
    # ollama.com/download/windows
  2. 如果已安装,检查Ollama是否运行:powershell# 检查11434端口
    Code:
    curl http://localhost:11434/api/tags

    # 如果返回错误,启动Ollama
    ollama serve
    # 或者在Windows搜索"Ollama",打开应用
  3. 运行我们的代理程序:(powershell )   
    Code:
    python ollama_joomla_publisher.py
  4. 配置客户端使用代理
    • 将API地址从 
      Code:
      localhost:11434
       改为 
      Code:
      localhost:11435
  5. 测试
    • 在客户端发送一条消息
    • 检查代理程序的控制台,应该能看到对话记录
Last edit: 2026-01-11 18:10 by service.

登录注册一个帐号 参加讨论