Python ChatGPT API AI连续流畅回答,效果跟官网一样

话不多说,python先上代码:(需要把第二行的api key改成自己的) ​import openaiopenai.api_key = "Your_API_Key"messages = [{"role&

话不多说,python先上代码:(需要把第二行的api key改成自己的)

​
import openai

openai.api_key = "Your_API_Key"
messages = [
    {"role": "system", "content": "You are a helpful and kind AI Assistant."}
]
while True:
   answer = ''
   prompt = input("我: ")
   messages.append(
            {'role': 'user', 'content': prompt})
   response = openai.ChatCompletion.create(
      model='gpt-3.5-turbo',
      messages=messages,
      max_tokens=4000,
      temperature=0,
      stream=True,
   )
   print('ChatGPT: ', end='')
   all_text = ''
   for event in response: 
      print(answer, end='', flush=True)   
      event_text = event['choices'][0]['delta']
      answer = event_text.get('content', '')
      all_text += answer
   message

发布者:admin,转转请注明出处:http://www.yc00.com/web/1733274588a3657853.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信