用户使用指引
1 通过API-KEY访问大模型
1.1 准备依赖
1.1.1 获取API-KEY
(1)登录HepAI官网使用统一认证或注册本地用户登录。
(2) 点击右上角我的
-API-KEYS
-创建API-KEY
,创建成功后复制。

1.1.2 设置环境变量
vi ~/.bashrc
# 将下一行内容添加到文件末尾:
export HEPAI_API_KEY=<your api-key>
-
输入
:wq
保存,并执行:source ~/.bashrc
刷新环境变量。 -
检查:命令行输入
echo $HEPAI_API_KEY
,输出你的API-KEY
表示设置成功。
1.1.3 安装HepAI库
需要安装hepai>=1.1.13
pip install hepai --upgrade
1.2 流式和非流式请求大模型
import os
from typing import Union, Any
from hepai import HepAI, ChatCompletion, Stream, ChatCompletionChunk
client = HepAI(api_key=os.getenv("OPENAI_API_KEY"))
res: Union[ChatCompletion, Stream] = client.chat.completions.create(
model="hepai/gpt-4o",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello! who are you"}
],
stream=True, # 设置是否流式输出
)
# 返回completion可能是Stream(流式)或ChatCompletion(非流式)或其他类型
if isinstance(res, Stream):
full_response = "" # 保存完整输出
for i, msg in enumerate(res):
x = msg.choices[0].delta.content if isinstance(msg, ChatCompletionChunk) else msg
if x: # x可能为None时略过
full_response += x
print(x, end="", flush=True)
print()
elif isinstance(res, ChatCompletion):
full_response = res.choices[0].message
print(full_response) # 非流式输出
else:
print(res) # 其他类型输出
2 访问远程模型
将模型部署在远程服务器上,使用get_remote_model
方法获得远程模型实例,像本地模型一样调用。
from hepai import HepAI
# 创建HepAI客户端
client = HepAI(api_key=os.getenv("HEPAI_API_KEY"))
# 获取一个远程模型对象
remote_model: HRemoteModel = client.get_remote_model(model_name=model_name)
# 请求远程模型的__call__方法
rst = remote_model(1, 2, extra="extra")
assert isinstance(rst, str), f"rst: type: {type(rst)}, {rst}"
print(f"Request str via remote model PASSED, type: {type(rst)}, value: {rst}")
# 请求远程模型的get_int方法
rst = remote_model.get_int(1, 2)
assert isinstance(rst, int), f"rst: type: {type(rst)}, {rst}"
print(f"Request int via remote model PASSED, type: {type(rst)}, value: {rst}")
# 请求远程模型的get_float方法
rst = remote_model.get_float(1., 2)
assert isinstance(rst, float), f"rst: type: {type(rst)}, {rst}"
print(f"Request float via remote model PASSED, type: {type(rst)}, value: {rst}")
# 请求远程模型的get_list方法
rst = remote_model.get_list([1, 2, 3], [4, 5, 6])
assert isinstance(rst, list), f"rst: type: {type(rst)}, {rst}"
print(f"Request list via remote model PASSED, type: {type(rst)}, value: {rst}")
# 请求远程模型的get_dict方法
rst = remote_model.get_dict({"a": 1, "b": 2}, {"x": 3, "y": 4})
assert isinstance(rst, dict), f"rst: type: {type(rst)}, {rst}"
print(f"Request dict via remote model PASSED, type: {type(rst)}, value: {rst}")