用Python调用chatGpt接口

  1. 安装openai
    pip install openai

     

  2. 编写python代码(chat.py)
    复制代码
     1 import openai
     2 openai.api_key = '个人的APIKEY'
     3 while True:
     4   mesgs = []
     5   user_input = input('我: ')
     6   if user_input =='end': 
     7     break 
     8   mesgs.append({'role': 'user', 'content':user_input })
     9   response = openai.ChatCompletion.create(model='gpt-3.5-turbo',messages=mesgs)
    10   ai_response = response['choices'][0]['message']['content']
    11   print(f'人工智能:{ ai_response}')
    复制代码

     

  3. 运行chat.py
    python chat.py

     

  4. 和AI对话开始
    我: hello
    人工智能:Hello! How can I assist you today?
    我: 你好
    人工智能:你好!有什么我能帮你的吗?
    我: こんにちは
    人工智能:こんにちは、私はAIです。何かお手伝いできることはありますか?
    我:

     

posted @   我为P狂  阅读(345)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类
点击右上角即可分享
微信分享提示