赞
踩
Python3.8+以上环境
安装依赖包 pip install openai
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2024-05-08 8:56
# @Author : Jack
# @File : chat_direct
"""
chat_direct
"""
import openai
from openai import OpenAI
openai.api_key = '***********************'
client = OpenAI(
api_key=openai.api_key,
base_url='https://*******/v1'
)
def chatGpt(text, stream=False):
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": text}
],
stream=stream
)
if stream:
for chunk in completion:
print(chunk.choices[0].delta.content)
else:
print(completion.choices[0].message.content)
if __name__ == '__main__':
chatGpt("你好啊,你是谁,自我介绍一下? ")
chatGpt("不要回答,不要回答!", stream=True)
base_url='https://*******/v1'
换成直连地址。
openai.api_key =
换成你的aip_key
N学无止界
回复 ChatGPT转接地址
即可.Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。