"""Compare actual generation and embedding models, including a Q8_0 fixture."""
import argparse
import json
from pathlib import Path
from sampling_api import request,success
def compare(port,store,text,q8,embedding):
rows={r["name"]:r for r in success(port,"/api/tags")["models"]}
def details(model):
return success(port,"/api/show",dict(model=model))
def chat(model,**kwargs):
return request(port,"/api/chat",dict(model=model,stream=False,
messages=[dict(role="user",content="ping")],options=dict(num_ctx=128,num_predict=2,temperature=0),**kwargs))
observed=[[(details(model)["details"]["quantization_level"]),rows[model]["size"],chat(model)[1]["message"]["content"]]
for model in (text,q8)]
vectors=success(port,"/api/embed",dict(model=embedding,input=["ping","pong"]))["embeddings"]
cannot_chat=chat(embedding)
cannot_embed=request(port,"/api/embed",dict(model=text,input="ping"))
cannot_think=chat(text,think=True)
# The supplied bytes are a tiny valid, pre-authored PNG.
image=(store/"pixel.b64").read_text(encoding="ascii").strip()
cannot_see=request(port,"/api/chat",dict(model=text,stream=False,
messages=[{"role":"user","content":"ping","images":[image]}]))
return dict(quantization=observed,capabilities={model:details(model).get("capabilities",[]) for model in (text,q8,embedding)},
vectors=[len(row) for row in vectors],
wrong_endpoints=[[cannot_chat[0],"does not support chat" in cannot_chat[1]["error"]],
[cannot_embed[0],"support embeddings" in cannot_embed[1]["error"]]],
unsupported=[[cannot_think[0],"does not support thinking" in cannot_think[1]["error"]],
[cannot_see[0],"image input is not supported" in cannot_see[1]["error"]]])
if __name__=="__main__":
parser=argparse.ArgumentParser(description=__doc__)
parser.add_argument("--port",type=int,default=11434)
parser.add_argument("--input-directory",type=Path,default=Path(__file__).parent)
parser.add_argument("--text",default="mbsky-sample:text")
parser.add_argument("--q8",default="mbsky-sample:q8")
parser.add_argument("--embedding",default="mbsky-sample:embed")
args=parser.parse_args()
print(json.dumps(compare(args.port,args.input_directory,args.text,args.q8,args.embedding)))
本例需在 Windows、CPython 3.12、Ollama 0.40.15。终端脚本使用 pywinpty。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows、CPython 3.12、Ollama 0.40.15。终端脚本使用 pywinpty。
入口文件:projects/ollama-sampling/model_selection.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
model_selection.py,或下载右侧课程工程包。 - 确认本地已准备 Windows、CPython 3.12、Ollama 0.40.15。终端脚本使用 pywinpty。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联29章) · 含当前章节或实例 · mbskycom202612a217.zip
课程关联工程包(登记关联2章) · 含当前章节或实例 · mbskycom202612a231.zip