一、准备
本项目使用 www.perfxcloud.cn 大模型平台,网站上有详细的说明文档。
快速开始 | PerfXClouddocs.perfxlab.cn/docs/tutorial-basics/get-started/
注意:PerfXCloud的API接口兼容 OpenAI 的ChatGPT,可以直接使用 OpenAI SDK或者其他中间件来访问 PerfXCloud。这使得开发者从ChatGPT或者其他平台切换到PerfXCloud变得非常非常容易。
1.如果还没有PerfXCloud的账号,可以登录网站创建一个。
2.PerfXCloud目前支持:
Qwen1.5-110B-Chat-GPTQ-Int4, Qwen1.5-72B-Chat-GPTQ-Int4, Qwen2-72B-Instruct-GPTQ-Int4, Meta-Llama-3-70B-Instruct-GPTQ-Int4, Qwen-14B-Chat-Int4, Meta-Llama-3-8B-Instruct,Llama3-Chinese_v2, chatglm3-6b,llama-3-8b, Qwen1.5-7B,Qwen2-7B,gpt2, SOLAR-10_7B-Instruct,llama-2-7b, PHI_1-5,opt-1b3, phi-2,internLM, Mistral-7B-Instruct, MindChat-Qwen-7B-v2,StarCoder-1B (还在持续更新中)
大模型广场
二、ChatBot-CMD
本项目是一个简单的对话系统,利用 PerfXCloud 的 API 进行交互。用户可以通过命令行与 AI 聊天,对话历史会被存储以便进行上下文管理。代码实现了 API 调用、流式响应处理以及对话历史管理。
三、程序部署
安装必要的库
在开始项目之前,需要安装 openai 库。你可以使用以下命令来安装:
pip install openai
导入库
在 Python 脚本中,需要导入必要的库和模块。以下是导入库的 Python 代码:
from openai import OpenAI
初始化 PerfXCloud 客户端
首先,需要初始化 PerfXCloud 客户端,需要提供 URL 和 API 密钥。以下是初始化客户端的 Python 代码:
client = OpenAI(
base_url='https://cloud.perfxlab.cn/v1',
api_key='PerfXCloud 获取key'
)
主函数及对话管理
在主函数中,首先定义一个空列表来存储对话历史。然后进入一个无限循环,用于持续接收用户输入并与 AI 进行对话。以下是完整的 Python 代码及解释:
def main(): # 开始对话,并存储对话历史,以便进行上下文管理
conversation_history = []
while True:
# 获取用户输入
user_input = input("您: ")
# 将用户输入添加到对话历史
conversation_history.append({"role": "user", "content": user_input})
# 调用API进行对话
stream = client.chat.completions.create(
model="Qwen1.5-110B-Chat-GPTQ-Int4",
messages=conversation_history,
temperature=1,
max_tokens=512,
n=1,
presence_penalty=0,
frequency_penalty=0,
stream=True,
)
print("AI: ", end="")
assistants_response = ""
for chunk in stream:
if chunk.choices[0].delta.content is not None:
assistants_response += chunk.choices[0].delta.content
print(chunk.choices[0].delta.content, end="")
print()
# 将机器人回复添加到对话历史
conversation_history.append({"role": "assistant", "content": assistants_response})
# 检查是否结束对话
if "再见" in user_input or "退出" in user_input:
break
if __name__ == "__main__":
main()
Python 代码解释
1.初始化对话历史:
conversation_history = []
用一个列表来存储对话的历史记录,以便在后续的对话中提供上下文。
2.获取用户输入:
user_input = input("您: ")
使用 input()函数获取用户的输入。
3.更新对话历史:
conversation_history.append({"role": "user", "content": user_input})
将用户输入添加到对话历史中。
4.调用 PerfXCloud API:
stream = client.chat.completions.create(
model="Qwen1.5-110B-Chat-GPTQ-Int4",
messages=conversation_history,
temperature=1,
max_tokens=512,
n=1,
presence_penalty=0,
frequency_penalty=0,
stream=True,
)
使用 PerfXCloud 的 API 进行对话,参数包括模型名称、对话历史、生成文本的温度、最大 tokens 数等。
5.处理流式响应:
assistants_response = ""
for chunk in stream:
if chunk.choices[0].delta.content is not None:
assistants_response += chunk.choices[0].delta.content
print(chunk.choices[0].delta.content, end="")
处理流式响应,将每一部分的响应内容逐步打印并拼接成完整的回复。
6.更新对话历史:
conversation_history.append({"role": "assistant", "content": assistants_response})
将 AI 的回复添加到对话历史中。
7.检查是否结束对话
if "再见" in user_input or "退出" in user_input:
break
根据用户输入内容判断是否结束对话。
6.完整代码
ChatBot-CMD 完整代码:
from openai import OpenAI
client = OpenAI(
base_url='https://cloud.perfxlab.cn/v1',
api_key='PerfXCloud 获取key'
)
def main(): # 开始对话,并存储对话历史,以便进行上下文管理
conversation_history = []
while True:
# 获取用户输入
user_input = input("您: ")
# 将用户输入添加到对话历史
conversation_history.append({"role": "user", "content": user_input})
# 调用API进行对话
stream = client.chat.completions.create(
model="Qwen1.5-110B-Chat-GPTQ-Int4",
messages=conversation_history,
temperature=1,
max_tokens=512,
n=1,
presence_penalty=0,
frequency_penalty=0,
stream=True,
)
print("AI: ", end="")
assistants_response = ""
for chunk in stream:
if chunk.choices[0].delta.content is not None:
assistants_response += chunk.choices[0].delta.content
print(chunk.choices[0].delta.content, end="")
print()
# 将机器人回复添加到对话历史
conversation_history.append({"role": "assistant", "content": assistants_response})
# 检查是否结束对话
if "再见" in user_input or "退出" in user_input:
break
if __name__ == "__main__":
main()
四、运行项目
确保所有代码都正确写入一个 Python 脚本文件(例如 chatbot.py
),然后通过以下 Bash 命令运行脚本:
python chatbot.py
运行后可以在终端与大模型进行对话:
本项目代码地址
PerfXCloud_APP/cmd_chatbotgitee.com/PerfXCloud-APP/cmd_chatbot编辑
项目演示视频
https://www.bilibili.com/video/BV12s421g7ENwww.bilibili.com/video/BV12s421g7EN