蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

核对本地兼容接口、模型ID和错误响应

PYTHONWindows、CPython3.12.14与独立CPU Ollama0.40.15;合成无训练GGUF,仅验证局部协议。provider_probe.py
provider_probe.py54 行
"""Probe an explicitly supplied owned IPv4 loopback OpenAI-compatible service."""
import argparse,json
from http.client import HTTPConnection
from urllib.parse import urlsplit
def endpoint(value):
 u=urlsplit(value)
 if u.scheme!="http" or u.hostname!="127.0.0.1" or not u.port or u.username or u.password or u.query or u.fragment:
  raise ValueError("only explicit HTTP 127.0.0.1:port without credentials/query is allowed")
 if u.path.rstrip("/")!="/v1":raise ValueError("this owned probe requires the /v1 prefix")
 return u.port
def exercise(base,model):
 port=endpoint(base);rows=[]
 def call(path,body=None,bearer=False):
  connection=HTTPConnection("127.0.0.1",port,timeout=30)
  headers={"Content-Type":"application/json"}
  if bearer:headers["Authorization"]="Bearer MBSKY_FAKE_NOT_A_CREDENTIAL"
  try:
   connection.request("GET" if body is None else "POST",path,None if body is None else json.dumps(body),headers)
   response=connection.getresponse();raw=response.read()
   try:payload=json.loads(raw)
   except json.JSONDecodeError:payload={"raw_text":raw.decode("utf8")}
   return response.status,payload
  finally:connection.close()
 def check(name,inputs,operation,expected,actual):
  assert expected==actual,(name,expected,actual)
  rows.append(dict(name=name,input=inputs,operation=operation,expected=expected,actual=actual))
 status,data=call("/v1/models")
 check("model_id","GET /v1/models","inspect native model IDs, not GUI display labels",
  [200,True],[status,model in {x["id"] for x in data.get("data",[])}])
 body={"model":model,"messages":[{"role":"user","content":"ping"}],"stream":False,"temperature":0,"max_tokens":2}
 status,data=call("/v1/chat/completions",body)
 output=data.get("choices",[{}])[0].get("message",{}).get("content")
 check("chat_without_key",body,"make an actual native compatible chat request",[200," pong pong"],[status,output])
 status,data=call("/v1/chat/completions",body,bearer=True)
 output=data.get("choices",[{}])[0].get("message",{}).get("content")
 check("fake_bearer_not_auth",dict(header="synthetic dummy"),"observe the same local service accepting an arbitrary dummy bearer",
  [200," pong pong"],[status,output])
 status,data=call("/v1/chat/completions",{**body,"model":"mbsky-definitely-missing:owned"})
 check("unknown_model","uncreated owned model ID","observe native unknown-model failure",
  [404,True],[status,bool(data.get("error"))])
 status,data=call("/models")
 check("wrong_prefix","GET /models without /v1","inspect the actual native path failure",404,status)
 invalid=["https://example.invalid/v1","http://127.0.0.1:1234/v1?key=fake","http://127.0.0.1:1234","http://fake@127.0.0.1:1234/v1"]
 refused=0
 for value in invalid:
  try:endpoint(value)
  except ValueError:refused+=1
 check("owned_url_guard",invalid,"reject four URLs before any request; this whitelist is not Hermes endpoint logic",4,refused)
 return dict(cases=rows,scope="Native local Ollama OpenAI-compatible endpoints plus owned URL validation only; Hermes, CC Switch, LM Studio and all cloud authentication unexecuted",
  existing_credentials_read=False,source_scripts_executed=False,external_requests=0,model_quality_reviewed=False)
if __name__=="__main__":
 p=argparse.ArgumentParser(description=__doc__);p.add_argument("--base-url",required=True);p.add_argument("--model",default="mbsky-provider:checked")
 a=p.parse_args();print(json.dumps(exercise(a.base_url,a.model),ensure_ascii=False))
本例需在 Windows、CPython3.12.14与独立CPU Ollama0.40.15;合成无训练GGUF,仅验证局部协议。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows、CPython3.12.14与独立CPU Ollama0.40.15;合成无训练GGUF,仅验证局部协议。

入口文件:projects/hermes-provider/provider_probe.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 provider_probe.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows、CPython3.12.14与独立CPU Ollama0.40.15;合成无训练GGUF,仅验证局部协议。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Hermes Agent

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a199.zip

下载课程关联工程包:Hermes Agent

课程关联工程包(登记关联20章) · 含当前章节或实例 · mbskycom202612a184.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习