Skip to content

examples(vector_databases): migrate PolarDB, AnalyticDB, Hologres, redisjson to openai>=1.0 API#2721

Open
kiwigitops wants to merge 4 commits into
openai:mainfrom
kiwigitops:examples/migrate-vector-db-notebooks-openai-v1
Open

examples(vector_databases): migrate PolarDB, AnalyticDB, Hologres, redisjson to openai>=1.0 API#2721
kiwigitops wants to merge 4 commits into
openai:mainfrom
kiwigitops:examples/migrate-vector-db-notebooks-openai-v1

Conversation

@kiwigitops
Copy link
Copy Markdown

Summary

Four more vector-database notebooks in examples/vector_databases/ still call the pre-v1 OpenAI Python SDK (openai.Embedding.create(...), openai.api_key = ..., dict-style response["data"][0]["embedding"]). The pre-v1 module-level API was removed in openai>=1.0.0 (November 2023), so these notebooks fail immediately on the current SDK with:

AttributeError: module 'openai' has no attribute 'Embedding'

This PR migrates all four to the v1+ OpenAI() client API. No model or parameter changes — pure SDK-surface fixes.

Notebooks touched

  • examples/vector_databases/PolarDB/Getting_started_with_PolarDB_and_OpenAI.ipynb
  • examples/vector_databases/analyticdb/Getting_started_with_AnalyticDB_and_OpenAI.ipynb
  • examples/vector_databases/hologres/Getting_started_with_Hologres_and_OpenAI.ipynb
  • examples/vector_databases/redis/redisjson/redisjson.ipynb

Per-notebook changes

For PolarDB / AnalyticDB / Hologres (same shape):

-import openai
+from openai import OpenAI
+client = OpenAI()

 def query_xxx(...):
-    embedded_query = openai.Embedding.create(
-        input=query,
-        model="text-embedding-3-small",
-    )["data"][0]["embedding"]
+    embedded_query = client.embeddings.create(
+        input=query,
+        model="text-embedding-3-small",
+    ).data[0].embedding

For redis/redisjson — client is already used for the Redis connection, so the OpenAI client is named openai_client to avoid the collision:

-import openai
-...
-openai.api_key = os.getenv("OPENAI_API_KEY")
+from openai import OpenAI
+...
+openai_client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

 def get_vector(text, model="text-embedding-3-small"):
     text = text.replace("\n", " ")
-    return openai.Embedding.create(input = [text], model = model)['data'][0]['embedding']
+    return openai_client.embeddings.create(input=[text], model=model).data[0].embedding

Test plan

  • All four notebooks still validate as JSON
  • No remaining openai.Embedding.create, openai.api_key, ["data"][0]["embedding"], or other pre-v1 patterns in any code cell
  • Same models, same parameters, same overall flow — purely a SDK-surface migration

Related

Follow-up to the same migration pattern from:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant