import os
from openai import OpenAI
client = OpenAI(
# This is the default and can be omitted
api_key = "sk-u1oNdFZblFE09tVx2c42981a97De42C*****",
base_url = "<https://api.tu-zi.com/v1>",
)
chat_completion = client.chat.completions.create(
messages=[
{
"role": "user",
"content": "背个望庐山瀑布",
}
],
model="gpt-4",
)
print(chat_completion)
gpt-4o-fast
,openai-gpt-4o
,gpt-4o-all
)import base64
import requests
# OpenAI API Key
api_key = "sk-BdFjUwDwNyPjcv78*****"
# Function to encode the image
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
# Path to your image
image_path = r"C:\\Users\\wa***\\Downloads\\兔子圆形.png" # 替换为您本地图片的路径
# Getting the base64 string
base64_image = encode_image(image_path)
# 设置请求头
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
# 设置请求的payload
payload = {
"model": "gpt-4o-fast", # 使用的模型
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "What’s in this image?"
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
}
]
}
],
"max_tokens": 300
}
# 发送POST请求到自定义的API地址
response = requests.post("<https://api.tu-zi.com/v1/chat/completions>", headers=headers, json=payload)
# 解析响应
response_data = response.json()
# 打印文本内容
print("Text Response:", response_data)
tts-1
import os
from pathlib import Path
from openai import OpenAI
# 初始化OpenAI客户端
client = OpenAI(
api_key="sk-Z6OJzK67cZvLUXvt31D82bFa1******", # 替换为你的API密钥
base_url="<https://api.tu-zi.com/v1>",
)
# 输入的文本内容
text_input = """
### 一、申请内容
审计报告***位、项目负责人等记入科研诚信异常名录,取消其一定年限内申请科研资助的资格,并依法追究其他责任。
"""
# 将文本按限制的长度分割
max_length = 1000 # 每个请求最大字符数,稍小于4096字符限制
text_parts = [text_input[i:i+max_length] for i in range(0, len(text_input), max_length)]
# 设置保存音频文件的基础路径
base_speech_file_path = Path("C:/Users/***1/Downloads/speech_part")
# 循环生成音频文件
for idx, part in enumerate(text_parts):
speech_file_path = base_speech_file_path.with_name(f"speech_part_{idx+1}.mp3")
# 请求文字转语音服务
response = client.audio.speech.create(
model="tts-1", # 指定TTS模型
voice="alloy", # 指定使用的声音
input=part, # 输入的文本部分
)
# 将生成的音频文件保存到指定路径
with open(speech_file_path, 'wb') as audio_file:
audio_file.write(response.content) # 直接写入二进制内容
print(f"Audio file saved at: {speech_file_path}")
需要用到逆向模型gpt-4-gizmo-*
,其中*号部分用官网GPTS链接后缀部分代替,如
其它代码部分与之前一致
import openai
# 设置API密钥
openai.api_key = "sk-Z6OJzK67cZvL****"
# 如果你有自定义的 base_url(比如使用代理),可以设置:
openai.api_base = "<https://api.tu-zi.com/v1>"
# 创建聊天补全,使用流模式
response = openai.ChatCompletion.create(
model="gpt-4-gizmo-g-dse9iXvor-dong-yu-hui-xiao-zuo-wen-zhu-shou",
messages=[
{
"role": "user",
"content": "你是谁?",
}
],
stream=True # 启用流模式
)
# 逐步处理并输出响应
for chunk in response:
if 'choices' in chunk:
content = chunk['choices'][0]['delta'].get('content', '')
print(content, end='') # 逐步输出,不换行
测试效果
<aside> 💡 更多内容可参考https://platform.openai.com/docs/overview官方文档,修改下文档中的网址和密钥即可
</aside>