Welcome 微信登录

首页 / 人工智能 / Python编写的Ollama大模型测试程序示例

import ollama
import requests
def test_direct_connection():
    """测试直接连接"""
    try:
        # 直接测试端口连通性
        response = requests.get('http://localhost:11434/api/tags', timeout=5)
        print("✅ 端口连接成功:", response.status_code)
        return True
    except Exception as e:
        print("❌ 端口连接失败:", e)
        return False

def test_ollama_client():
    """测试Ollama客户端连接"""
    try:
        # 首先测试默认连接
        client = ollama.Client(host='http://localhost:11434')
        models = client.list()
        for model in models['models']:
            print(f"{model}")
        return True
    except Exception as e:
        print("❌ Ollama客户端连接失败:", str(e))
        return False

def test_with_explicit_host():
    """使用显式主机名测试"""
    try:
        # 显式指定主机和端口
        client = ollama.Client(host='http://localhost:11434')
        models = client.list()
        print("✅ 显式连接成功!")
        return True
    except Exception as e:
        print("❌ 显式连接失败:", str(e))
        return False

def test_model_operations():
    """测试基本模型操作"""
    try:
        client = ollama.Client(host='http://localhost:11434')
        # 测试简单推理
        response = client.chat(
          &n...
该文章为易网时代-编程资源站会员专属文章,请先登录后再进行查看。