依赖环境
Python3.8+以上环境
安装依赖包 pip install openai
使用方法-Python
#!/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 OpenAIopenai.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