You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

OpenAI Python API与Firestore模拟器本地开发兼容性问题求助

Firestore模拟器本地开发与Azure OpenAI调用冲突导致脚本挂起问题

部署到Cloud Run的Python脚本可正常运行,但本地使用Firestore模拟器时出现异常:执行Firestore写入操作后,调用oai_client.chat.completions.create会导致脚本挂起,无法通过^C终止;关闭脚本终端后,模拟器抛出连接重置警告。若注释掉Firestore写入部分,脚本可正常执行完毕。更换端口(如8002或万位随机端口)也无法解决该问题。

模拟器启动命令

$ gcloud beta emulators firestore start --project test --host-port 'localhost:8001'

环境信息

  • Google Cloud SDK 452.0.0
  • cloud-firestore-emulator 1.18.2
  • Python openai包版本1.3.3(曾尝试0.27.8、1.1.2、1.2.4)
  • 操作系统:Mac OS Sonoma 14.0

脚本代码

import os
from openai import AzureOpenAI
import google.auth
from google.cloud import firestore
import mock

PORT = "8001"

os.environ["FIRESTORE_EMULATOR_HOST"] = f"localhost:{PORT}"
os.environ["FIRESTORE_EMULATOR_HOST_PATH"] = f"localhost:{PORT}/firestore"
os.environ["FIRESTORE_HOST"] = f"http://localhost:{PORT}/firestore"
os.environ["FIRESTORE_DATASET"] = "streamlit_firestore"
os.environ["FIRESTORE_PROJECT_ID"] = "test"
credentials = mock.Mock(spec=google.auth.credentials.Credentials)
fs_client = firestore.Client(project="test", credentials=credentials)

key = open("secrets/azure-openai-key1.txt").read().strip()
oai_client = AzureOpenAI(
    azure_endpoint="https://azeastus2ai001.openai.azure.com/",
    api_version="2023-07-01-preview",
    api_key=key,
)

# ARBITRARY WRITE TO FIRESTORE
coll = fs_client.collection("fake@uhoh.com")
ref = coll.document()
ref.set({'base': 'jumping'})

# THIS HANGS
response = oai_client.chat.completions.create(
    model="gpt-35-turbo-16k-1",
    messages=[
        {'role': 'system', 'content': 'You are a helpful assistant.'},
        {'role': 'user', 'content': 'hello'}
    ],
    temperature=0.5
)

message_full = response.choices[0].message
print(message_full.content)

模拟器警告信息

[firestore] Nov 20, 2023 6:22:04 PM io.netty.channel.DefaultChannelPipeline onUnhandledInboundException
[firestore] WARNING: An exceptionCaught() event was fired, and it reached at the tail of the pipeline. It usually means the last handler in the pipeline did not handle the exception.
[firestore] java.net.SocketException: Connection reset
[firestore]     at java.base/sun.nio.ch.SocketChannelImpl.throwConnectionReset(SocketChannelImpl.java:394)
[firestore]     at java.base/sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:426)
[firestore]     at io.netty.buffer.PooledByteBuf.setBytes(PooledByteBuf.java:256)
[firestore]     at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1132)
[firestore]     at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:357)
[firestore]     at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:151)
[firestore]     at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:788)
[firestore]     at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:724)
[firestore]     at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:650)
[firestore]     at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:562)
[firestore]     at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997)
[firestore]     at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
[firestore]     at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
[firestore]     at java.base/java.lang.Thread.run(Thread.java:833)

内容的提问来源于stack exchange,提问作者Nathan Lloyd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 13:24:51