调用openai实现聊天功能

前言

本文主要是【聊天机器人】——调用openai实现聊天功能的文章,如果有什么需要改进的地方还请大佬指出⛺️

作者简介:大家好,我是听风与他
☁️博客首页:CSDN主页听风与他
每日一句:狠狠沉淀,顶峰相见

目录

    • 前言
    • 1.推荐使用google云实验室
    • 2.下载依赖
    • 3.调用openai的key实现机器人功能
    • 文章末尾

1.推荐使用google云实验室

  • 网址: https://colab.research.google.com/

2.下载依赖

pip install openai

3.调用openai的key实现机器人功能

import openai

# 设置 API Key
openai.api_key = "openai的key"
openai.api_base = "中转api地址";

# 定义一个函数来处理用户的输入
def handle_input(user_input):
    # 创建 ChatGPT 补全请求
    completion = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[{"role": "user", "content": user_input}],
    )

    # 返回补全结果
    return completion.choices[0].message.content

# 开始循环处理用户的输入
while True:
    user_input = input("请输入问题:")
    response = handle_input(user_input)
    print(response)

文章末尾

在这里插入图片描述

你可能感兴趣的:(python,大模型,openai)