本文最后更新于 676 天前,其中的信息可能已经有所发展或是发生改变。
之前写了一个tg机器人,但是一直没有继续去写,所以他只会echo。最近觉得手机上使用GPT有些麻烦,于是决定让机器人接入GPT。经过了解发现OpenAI的官方API缺点如下:
- 只能用信用卡付款。
- 并发很低,一分钟只能调用几次。
- 假如服务器在国内就无法连接。
于是去寻找第三方API替代,最终找到了AI Proxy。(官网地址:AIProxy)看了下定价应该是比官方稍贵一点:

gpt4尚未测试过实际消耗,如果是gpt-3.5-turbo,实际消耗比标的Max Per Request还要低得多。
注册登录后充值积分(充值时输入邀请码:IAMCZY,可以赠送额外3%积分)。随后在控制台中找到API Key,修改一下我们之前的机器人代码就好啦
import json
import requests
from flask import Flask, request
from openai import OpenAI
app = Flask(__name__)
BOTTOKEN = '<TOKEN>' # TelegramBotToken xxxxx:xxxxxxxxxxx
API_URL = f'https://api.telegram.org/bot{BOTTOKEN}'
APIKEY = '<api_key>' # api_key
BASEURL = '<base_url>' # base_url,like https://api.aiproxy.io/v1
PORT = 8443 # webhook port
MODEL = 'gpt-3.5-turbo'
# the model you want to use
# gpt-4-0125-preview,
# gpt-4-turbo-preview,
# gpt-4-1106-preview,
# gpt-4-vision-preview,
# gpt-4,
# gpt-4-0314,
# gpt-4-0613,
# gpt-4-32k,
# gpt-4-32k-0314,
# gpt-4-32k-0613,
# gpt-3.5-turbo,
# gpt-3.5-turbo-16k,
# gpt-3.5-turbo-0301,
# gpt-3.5-turbo-0613,
# gpt-3.5-turbo-1106,
# gpt-3.5-turbo-0125,
# gpt-3.5-turbo-16k-0613,
def sendMessage(chat_id, text):
requests.get(f'{API_URL}/sendMessage', params={
'chat_id': chat_id,
'text': text
})
@app.route(/webhook/event, methods=['POST']) # receive messages
def event():
data = request.json
text = I can only understand pure text messages.
userid = str(data['message']['chat']['id'])
if 'text' in data['message']:
text = data['message']['text']
client = OpenAI(
api_key=APIKEY,
base_url=BASEURL
)
chat_completion = client.chat.completions.create(
model=MODEL,
messages=[
{role: system, content: }, # System prompt
{role: user, content: text}
]
)
sendMessage(userid, chat_completion.choices[0].message.content)
else:
sendMessage(userid, text)
return json.dumps(data)
if __name__ == '__main__':
app.run(port=PORT)

Tql,加油
Super convenient since it integrates with GCash. No more waiting for long bank transfers. Love spintimegcash
Really loving the vibe of this site. It feels secure and the promotions are very generous for new players. jl588
Finally a site that integrates GCash perfectly. No more waiting hours for my funds to reflect. Just pure gaming! gcashokplay