配置Actions后自定义OpenAI GPT无法访问API求助
问题描述
我尝试通过OpenAI GPT的Actions配置调用自定义API,已将一个FastAPI示例部署到线上环境,复制自动生成的OpenAPI spec并添加server配置后,GPT能识别接口方法,但实际调用时报错,比如「Error talking to xxx」或「Not Found」。另外,我能成功配置调用jsonplaceholder的接口,怀疑是不是参数处理问题?
排查方向与解决方案
1. 服务监听地址错误(核心问题)
你的FastAPI启动代码中,uvicorn绑定的是127.0.0.1:
uvicorn.run(app, host="127.0.0.1", port=int(port))
这个地址是本地回环地址,仅能在容器内部访问,外部请求无法到达。部署到线上平台时,需要绑定0.0.0.0,让服务监听所有网卡:
uvicorn.run(app, host="0.0.0.0", port=int(port))
2. 验证接口可访问性
修改代码后重新部署,先通过浏览器或curl手动测试接口:
- 访问根路径:
curl https://main-bvxea6i-74oriiawvvtoy.eu-5.platformsh.site/ - 访问message接口:
curl https://main-bvxea6i-74oriiawvvtoy.eu-5.platformsh.site/message - 访问calc接口:
curl https://main-bvxea6i-74oriiawvvtoy.eu-5.platformsh.site/calc/5?y=10
确保所有接口都能返回正确响应,再重新配置GPT Actions。
3. OpenAPI spec参数验证
你的OpenAPI spec中,calc接口的参数定义是正确的,路径参数x和查询参数y的格式符合OpenAPI 3.1规范,GPT能够正确解析。之前的报错大概率是因为服务无法被访问,而非参数问题。
原示例代码
import os import uvicorn from typing import Union from fastapi import FastAPI app = FastAPI() @app.get("/") def home(): return "Home sweet home" @app.get("/message") def message(): return {"msg": "The early bird catches the worm."} @app.get("/calc/{x}") def calc(x: int, y: Union[int, None] = None): y = y or 0 return {"x": x, "y": y, "result": int(x) * int(y)} if __name__ == "__main__": port = os.getenv("PORT") or 8080 uvicorn.run(app, host="127.0.0.1", port=int(port))
原OpenAPI Spec
{ "openapi": "3.1.0", "info": { "title": "FastAPI", "version": "0.1.0" }, "servers": [ { "url": "https://main-bvxea6i-74oriiawvvtoy.eu-5.platformsh.site/" } ], "paths": { "/": { "get": { "summary": "Home", "operationId": "home__get", "responses": { "200": { "description": "Successful Response", "content": { "application/json": { "schema": {} } } } } } }, "/message": { "get": { "summary": "Message", "operationId": "message_message_get", "responses": { "200": { "description": "Successful Response", "content": { "application/json": { "schema": {} } } } } } }, "/calc/{x}": { "get": { "summary": "Calc", "operationId": "calc_calc__x__get", "parameters": [ { "name": "x", "in": "path", "required": true, "schema": { "type": "integer", "title": "X" } }, { "name": "y", "in": "query", "required": false, "schema": { "anyOf": [ { "type": "integer" }, { "type": "null" } ], "title": "Y" } } ], "responses": { "200": { "description": "Successful Response", "content": { "application/json": { "schema": {} } } }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } } } } } } }, "components": { "schemas": { "HTTPValidationError": { "properties": { "detail": { "items": { "$ref": "#/components/schemas/ValidationError" }, "type": "array", "title": "Detail" } }, "type": "object", "title": "HTTPValidationError" }, "ValidationError": { "properties": { "loc": { "items": { "anyOf": [ { "type": "string" }, { "type": "integer" } ] }, "type": "array", "title": "Location" }, "msg": { "type": "string", "title": "Message" }, "type": { "type": "string", "title": "Error Type" } }, "type": "object", "required": [ "loc", "msg", "type" ], "title": "ValidationError" } } } }
内容的提问来源于stack exchange,提问作者myke
相关产品推荐
相关产品推荐

