Skip to content

examples(elasticsearch): migrate notebook to openai>=1.0 client API#2719

Open
kiwigitops wants to merge 1 commit into
openai:mainfrom
kiwigitops:examples/elasticsearch-migrate-openai-v1
Open

examples(elasticsearch): migrate notebook to openai>=1.0 client API#2719
kiwigitops wants to merge 1 commit into
openai:mainfrom
kiwigitops:examples/elasticsearch-migrate-openai-v1

Conversation

@kiwigitops
Copy link
Copy Markdown

Summary

examples/vector_databases/elasticsearch/elasticsearch-retrieval-augmented-generation.ipynb still uses the pre-v1 OpenAI Python SDK (openai.api_key = ..., openai.Embedding.create(...), openai.ChatCompletion.create(...), dict-style response["data"][0]["embedding"]). The pre-v1 API was removed in openai>=1.0.0 (Nov 2023), so the notebook now fails with AttributeError: module 'openai' has no attribute 'Embedding' on the current SDK.

This PR migrates the affected cells. The Elasticsearch client variable is left alone — the new OpenAI client is named openai_client to avoid collision.

Changes

-import openai
+from openai import OpenAI
-openai.api_key = OPENAI_API_KEY
+openai_client = OpenAI(api_key=OPENAI_API_KEY)
...
-question_embedding = openai.Embedding.create(input=question, model=EMBEDDING_MODEL)
+question_embedding = openai_client.embeddings.create(input=question, model=EMBEDDING_MODEL)
-"query_vector": question_embedding["data"][0]["embedding"],
+"query_vector": question_embedding.data[0].embedding,
-summary = openai.ChatCompletion.create(
+summary = openai_client.chat.completions.create(
   model="gpt-3.5-turbo", messages=[...]
 )

The downstream usage summary.choices/choice.message.content already works on the v1+ response shape, so only the create call needed swapping.

Test plan

  • Notebook JSON validates
  • No remaining openai.ChatCompletion, openai.Embedding, openai.api_key, or dict-style ["data"][0]["embedding"] access in code cells
  • Same model, same parameters — purely SDK-surface migration

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