使用令牌调用大模型
PerfXCloud 创建私有化令牌支持 AI 应用开发。
创建您的令牌
首先您需要进入到 PefXCloud 令牌界面,创建一个属于自己的令牌。
模型广场搜索对应的模型
将您创建的令牌的模型名称复制到模型广场 界面进行搜索,点击“查看模型详情”。
进入详情页面,将详情页拉到最后,查看 Bash 代码调用案例与 Python 调用案例。
复制令牌
回到令牌界面,找到您新创建的令牌。点击复制令牌,即可将密钥复制到剪切板。
代码案例
参数说明
-
model:这是您要使用的 AI 模型的名称,对应您创建的令牌的模型名称。
-
messages:这是一个数组,包含了对话的历史记录等信息。
-
示例:
message = [
{"role":"system","content":"want you to be a chatterbox expert and answer the following in a friendly tone:"},
{"role": "user", "content": "Say this is a test! "}
] -
temperature:这是一个控制生成回复的多样性的参数。温度高时,AI 的回复会更随机、更富有创造性;温度低时,回复会更保守,更接近训练数据中的常见模式。1 通常是中间值。
-
max_tokens:这是 AI 生成的回复最长能包含多少个 token(基本单位,比如单词或字符)。
-
n:这是模型生成的回复数量。在这里,设置为 1,所以只返回一个回复。
-
stop:这是一个列表,包含了要 AI 在生成文本时停止的特殊标记。这里有两个,
<|eot_id|>
通常表示"end of text",<|start_header_id|>
和<|end_header_id|>
可能用于在生成文本时标记开始和结束的头部信息,每个模型的 stop 参数时不同的,根据需求输入不同的 stop 参数。 -
presence_penalty 和 frequency_penalty:这两个参数是针对生成文本的平滑性。前一个惩罚过于频繁出现的词,后一个惩罚如果生成的词在历史中已经存在。
-
stream:如果为 true,表示模型会在生成过程中逐段输出,而不是一次性生成所有内容,反之,只有生成完成才会返回结果。
使用 Bash 调用
将复制的令牌密钥设置为环境变量。
export PERFX_API_KEY="复制的令牌密钥"
将模型广场的 Bash 案例粘贴并根据您的需求进行修改。
curl https://cloud.perfxlab.cn/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $PERFX_API_KEY" \
-d '{
"model": "Meta-Llama-3-70B-Instruct-GPTQ-Int4",
"messages": [
{"role":"system","content":"want you to be a chatterbox expert and answer the following in a friendly tone:"},
{"role": "user", "content": "Say this is a test! "}
],
"temperature": 0.7,
"max_tokens": 16,
"n": 1,
"stop":["<|eot_id|>", "<|start_header_id|>", "<|end_header_id|>"],
"presence_penalty": 0,
"frequency_penalty": 0,
"stream": true
}'
使用 Python 调用
openai 需要 ≥1.0 的版本
将复制的令牌密钥设置为环境变量。
export PERFX_API_KEY="复制的令牌密钥"
将模型广场的 Python 案例粘贴并根据您的需求进行修改。
import os
from openai import OpenAI
client = OpenAI(
base_url='https://cloud.perfxlab.cn/v1',
api_key=os.getenv("PERFX_API_KEY")
)
stream = client.chat.completions.create(
model="Meta-Llama-3-70B-Instruct-GPTQ-Int4",
messages=[
{"role":"system","content":"want you to be a chatterbox expert and answer the following in a friendly tone"},
{"role": "user", "content": "hello "}],
temperature=0.7,
max_tokens=16,
n=1,
stop=["<|eot_id|>", "<|start_header_id|>", "<|end_header_id|>"],
presence_penalty=0,
frequency_penalty=0,
stream=True,
)
for chunk in stream:
if chunk.choices[0].delta.content is not None:
print(chunk.choices[0].delta.content, end="")