mirror of
https://github.com/lancedb/lancedb.git
synced 2026-08-22 14:08:21 +00:00
7adcffc2b4
PyO3 exposed `LsmWriteSpec` with its default `builtins` module, causing mkdocstrings to resolve the public `lancedb.LsmWriteSpec` re-export as `builtins.LsmWriteSpec` and fail the documentation build. Declare the native extension module and pin the public re-export with a regression test. This also applies the repository's current Ruff formatter to seven previously unformatted Python scripts.
61 lines
1.9 KiB
Python
61 lines
1.9 KiB
Python
# SPDX-License-Identifier: Apache-2.0
|
|
# SPDX-FileCopyrightText: Copyright The LanceDB Authors
|
|
"""A zero-dependency mock OpenAI embeddings API endpoint for testing purposes."""
|
|
|
|
import argparse
|
|
import json
|
|
import http.server
|
|
|
|
|
|
class MockOpenAIRequestHandler(http.server.BaseHTTPRequestHandler):
|
|
def do_POST(self):
|
|
content_length = int(self.headers["Content-Length"])
|
|
post_data = self.rfile.read(content_length)
|
|
post_data = json.loads(post_data.decode("utf-8"))
|
|
# See: https://platform.openai.com/docs/api-reference/embeddings/create
|
|
|
|
if isinstance(post_data["input"], str):
|
|
num_inputs = 1
|
|
else:
|
|
num_inputs = len(post_data["input"])
|
|
|
|
model = post_data.get("model", "text-embedding-ada-002")
|
|
|
|
data = []
|
|
for i in range(num_inputs):
|
|
data.append(
|
|
{
|
|
"object": "embedding",
|
|
"embedding": [0.1] * 1536,
|
|
"index": i,
|
|
}
|
|
)
|
|
|
|
response = {
|
|
"object": "list",
|
|
"data": data,
|
|
"model": model,
|
|
"usage": {
|
|
"prompt_tokens": 0,
|
|
"total_tokens": 0,
|
|
},
|
|
}
|
|
|
|
self.send_response(200)
|
|
self.send_header("Content-type", "application/json")
|
|
self.end_headers()
|
|
self.wfile.write(json.dumps(response).encode("utf-8"))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser(description="Mock OpenAI embeddings API endpoint")
|
|
parser.add_argument("--port", type=int, default=8000, help="Port to listen on")
|
|
args = parser.parse_args()
|
|
port = args.port
|
|
|
|
print(f"server started on port {port}. Press Ctrl-C to stop.")
|
|
print(f"To use, set OPENAI_BASE_URL=http://localhost:{port} in your environment.")
|
|
|
|
with http.server.HTTPServer(("0.0.0.0", port), MockOpenAIRequestHandler) as server:
|
|
server.serve_forever()
|