Spaces:
Configuration error
Configuration error
# -*- coding: utf-8 -*- | |
from openai import OpenAI | |
from utils.datasetSaver import save_dict_to_json_file | |
from zhipuai import ZhipuAI | |
client = OpenAI(api_key="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") | |
# def generate_assistant_response(conversation_history,user_input): | |
# # 将用户输入添加到对话历史中 | |
# conversation_history.append({"role": "user", "content": user_input}) | |
# | |
# # 使用GPT-3.5-turbo模型生成回复 | |
# response = client.chat.completions.create( | |
# model="gpt-3.5-turbo", # 指定要使用的模型 | |
# messages=conversation_history, # 包含对话历史的消息列表 | |
# temperature=0.1, # 控制生成文本的创造性,值越高则更随机 | |
# max_tokens=4096, # 生成文本的最大长度限制 | |
# top_p=0.1 # 用于控制采样概率分布的参数,值越低则更保守 | |
# ) | |
# | |
# # 获取助手的回复并将其添加到对话历史中 | |
# assistant_response = response.choices[0].message.content | |
# conversation_history.append({"role": "assistant", "content": assistant_response}) | |
# | |
# return conversation_history,assistant_response | |
user_using = 'admin' | |
class ChatGPT: | |
def __init__(self, model, init_system, save_message=False,user=user_using): | |
self.init_system = init_system | |
self.messages = [init_system] | |
self.model = model | |
self.save_message = save_message | |
self.user = user | |
def get_response(self, user_input=''): | |
if user_input!='': | |
self.messages.append({"role": "user", "content": user_input}) | |
elif user_input == 'newgame': | |
self.messages = [self.init_system] | |
response = client.chat.completions.create( | |
model=self.model, | |
messages=self.messages, | |
temperature=0.1, | |
max_tokens=4096, | |
top_p=0.1 | |
) | |
assistant_response = response.choices[0].message.content | |
self.messages.append({"role": "assistant", "content": assistant_response}) | |
return assistant_response | |
def clean_history(self): | |
""" 清空历史信息 | |
""" | |
self.messages.clear() | |
self.messages = [self.init_system] | |
def save_history(self): | |
# 用于确定当前在操作的用户 | |
file_path = r"C:\Users\Administrator\Desktop\PycharmProject\novalConvertProject\data\userData\usingUser.txt" | |
with open(file_path, 'r') as file: | |
first_line = file.readline() | |
self.user = first_line | |
save_dict_to_json_file(user=self.user,input_dict=self.messages) | |
# zhiPuAPI= '146a6031170a313fd5f397341f303035.NDb5a64yOhFALiv6' | |
zhiPuAPI = 'ac71b83f1e1998656a513fda37060b06.YUHmPGrauGngkKWz' | |
class zhiPuGlm: | |
def __init__(self, model,init_system,api_key=zhiPuAPI, user=user_using): | |
self.client = ZhipuAI(api_key=api_key) | |
self.model = model | |
self.messages = [init_system] | |
self.init_system = init_system | |
self.user = user | |
def get_response(self, user_input='开始游戏'): | |
self.messages.append({"role": "user", "content": user_input}) | |
# if user_input != '开始游戏': | |
# self.messages.append({"role": "user", "content": user_input}) | |
if user_input == 'newgame': | |
self.messages = [self.init_system] | |
response = self.client.chat.completions.create( | |
model=self.model, | |
messages=self.messages, | |
temperature=0.1, | |
max_tokens=8192, | |
top_p=0.1 | |
) | |
assistant_response = response.choices[0].message.content | |
self.messages.append({"role": "assistant", "content": assistant_response}) | |
return assistant_response | |
def clean_history(self): | |
self.messages.clear() | |
self.messages = [self.init_system] | |
def save_history(self): | |
#用于确定当前在操作的用户 | |
file_path = r"C:\Users\Administrator\Desktop\PycharmProject\novalConvertProject\data\userData\usingUser.txt" | |
with open(file_path, 'r') as file: | |
first_line = file.readline() | |
self.user = first_line | |
save_dict_to_json_file(user=self.user, input_dict=self.messages) | |