openai的python库如何自定义key和域名?chatgpt的接口,gpt4的接口

废话不多说,直接上代码,看怎么自定义key和域名

安装openai的python库

pip install openai  

项目github地址:github.com/xing61/xiaoy

  • 第2步:编写代码。注意配置的base_url是:https://flag.smarttrot.com/v1

运行以下代码

import os
from openai import OpenAI
import openai
import requests
import time
import json
import time

API_SECRET_KEY = “xxxxxx”;
BASE_URL = “https://flag.smarttrot.com/v1/”

# chat
def chat_completions3(query):
client = OpenAI(api_key=API_SECRET_KEY, base_url=BASE_URL)
resp = client.chat.completions.create(
model=”gpt-3.5-turbo”,
messages=[
{“role”: “system”, “content”: “You are a helpful assistant.”},
{“role”: “user”, “content”: query}
]
)
print(resp)
print(resp.choices[0].message.content)

正常运行打印如下

Hello there! How can I assist you today ?

© 版权声明

相关文章

暂无评论

暂无评论...