1 Star 0 Fork 216

东方甲乙木 / python-learn

forked from mktime / python-learn 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
gpt35.py 1.39 KB
一键复制 编辑 原始数据 按行查看 历史
mktime 提交于 2023-04-08 13:51 . 添加GPT使用例子
import openai
import sys
import os
#api_key = os.getenv('OPENAI_API_KEY')
global_msg = [
{"role": "system", "content": "你是一位有用的助手"}
]
def get_reply(input_msg):
msg = {"role": "user", "content": input_msg}
global_msg.append(msg)
response = openai.ChatCompletion.create(
model = "gpt-3.5-turbo",
top_p = 1,
frequency_penalty = 0,
presence_penalty = 0,
temperature = 1,
max_tokens = 3000,
messages = global_msg
)
#print(response)
reply = ''
choices = response['choices']
for r in choices:
reply += r['message']['content']
return reply
def print_banner():
banner = '''
____ ____ _____
/ ___| _ \_ _|
| | _| |_) || |
| |_| | __/ | |
\____|_| |_|
'''
print(banner)
def start_robot():
print_banner()
print('>>', end='', flush=True)
while True:
try:
msg = sys.stdin.readline().strip()
except KeyboardInterrupt:
print('\n再见!')
break
if not msg:
print('>>', end='', flush=True)
continue
reply = get_reply(msg)
reply += '\n>>'
print('>>' + reply, end='', flush=True)
if __name__ == '__main__':
start_robot()
Python
1
https://gitee.com/softwyy/python-learn.git
git@gitee.com:softwyy/python-learn.git
softwyy
python-learn
python-learn
master

搜索帮助