Skip to content

feat(learn): modernize gen-qa-openai notebook to OpenAI SDK v1.x#545

Closed
jhamon wants to merge 6 commits intomasterfrom
jhamon/modernize-gen-qa-openai
Closed

feat(learn): modernize gen-qa-openai notebook to OpenAI SDK v1.x#545
jhamon wants to merge 6 commits intomasterfrom
jhamon/modernize-gen-qa-openai

Conversation

@jhamon
Copy link
Collaborator

@jhamon jhamon commented Feb 3, 2026

Problem

The learn/generation/openai/gen-qa-openai.ipynb notebook uses deprecated OpenAI SDK v0.27 with the legacy Completion API and the retired text-davinci-003 model. This prevents users from running the notebook and demonstrates outdated patterns.

Solution

Modernized the notebook to use OpenAI SDK v1.66.3 with the current Chat Completions API and gpt-5-mini model. This brings the notebook up to date with current best practices and ensures it will continue working with OpenAI's supported model lineup.

Key Changes

  1. Dependencies: Updated openai==0.27.7openai==1.66.3 and pinecone-client[grpc]==2.2.1pinecone==8.0.0

  2. API Initialization: Migrated from global configuration to client-based pattern:

    # Before
    import openai
    openai.api_key = os.getenv("OPENAI_API_KEY")
    
    # After
    from openai import OpenAI
    client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
  3. Chat Completions API: Replaced Completion API with Chat API using gpt-5-mini:

    # Before
    res = openai.Completion.create(
        engine='text-davinci-003',
        prompt=query,
        temperature=0,
        max_tokens=400
    )
    res['choices'][0]['text'].strip()
    
    # After
    res = client.chat.completions.create(
        model='gpt-5-mini',
        messages=[{"role": "user", "content": query}],
        temperature=0,
        max_tokens=400
    )
    res.choices[0].message.content.strip()
  4. Embeddings API: Updated to new client method and response access patterns:

    # Before
    res = openai.Embedding.create(input=[text], engine='text-embedding-ada-002')
    embeds = [record['embedding'] for record in res['data']]
    
    # After
    res = client.embeddings.create(input=[text], model='text-embedding-ada-002')
    embeds = [record.embedding for record in res.data]

Model Selection

Selected gpt-5-mini because:

  • Part of OpenAI's current GPT-5 flagship series (as of Feb 2026)
  • Cost-efficient and well-suited for focused RAG tasks
  • Not being deprecated, ensuring notebook longevity
  • More capable than retiring GPT-4.1/GPT-4o models

Breaking Changes

None for end users - the notebook functionality remains the same. API keys and usage patterns are maintained.

Testing

Verified through code pattern analysis:

  • ✅ All legacy API patterns removed (openai.Completion, openai.Embedding)
  • ✅ Modern client-based patterns present throughout
  • ✅ Correct model (gpt-5-mini) and response access patterns
  • ✅ All 5 embedding API calls updated
  • ✅ Both complete() and retrieve() functions modernized

Full notebook execution requires valid OpenAI and Pinecone API keys.

Related

Related to other SDK v8 modernization efforts across the examples repository.

Made with Cursor


Note

Low Risk
Low risk since changes are limited to tutorial notebooks, but execution could break if the pinned OpenAI/Pinecone SDK versions or chosen models differ from a user’s environment.

Overview
Modernizes the OpenAI RAG learning notebooks to current SDKs. Updates pinned deps to OpenAI SDK v1 and Pinecone v8, and switches OpenAI usage from global openai.* calls to the client-based OpenAI(...) pattern.

Moves generation to chat-based models and updates response handling. gen-qa-openai.ipynb replaces text-davinci-003 completions with client.chat.completions.create(model="gpt-5-mini"), while openai-ml-qa/01-making-queries.ipynb uses chat completions with gpt-3.5-turbo; both notebooks update embeddings calls to client.embeddings.create(..., model=...) and migrate all response indexing from dict-style (res['data'], res['choices']) to attribute access (res.data, res.choices).

Written by Cursor Bugbot for commit a6bbb78. This will update automatically on new commits. Configure here.

jhamon and others added 6 commits February 2, 2026 19:38
- Update dependencies: openai==1.86.0, pinecone~=8.0
- Replace openai.Embedding.create() with openai_client.embeddings.create()
- Replace openai.Completion.create() with openai_client.chat.completions.create()
- Update to gpt-3.5-turbo chat model from text-davinci-003
- Remove deprecated openai.Engine.list() authentication check
- Initialize OpenAI client using new v1.x pattern
- Update response access from dict to attribute notation
- Apply ruff-style code formatting

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
- Move all imports to first code cell following notebook guidelines
- Sort imports properly (stdlib first, then third-party)
- Remove unnecessary f-string prefix

Co-authored-by: Cursor <cursoragent@cursor.com>
The check-pinning script doesn't handle backslash line continuations properly,
treating the backslash as a package name. Put the pip install on one line instead.

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Migrate the retrieval-augmented generation notebook from OpenAI SDK v0.27 to v1.66.3:

- Update dependencies: openai==1.66.3, pinecone==8.0.0
- Replace global openai config with OpenAI client instantiation
- Migrate from Completion API to Chat Completions API using gpt-5-mini model
- Update Embeddings API calls to use client.embeddings.create()
- Modernize response access patterns (dictionary → attribute access)
- Update complete() and retrieve() functions with new API patterns

All API calls now use the modern client-based pattern and current model lineup.

Co-authored-by: Cursor <cursoragent@cursor.com>
@jhamon jhamon added the enhancement New feature or request label Feb 3, 2026
@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@jhamon
Copy link
Collaborator Author

jhamon commented Feb 3, 2026

Closing in favor of #546 which contains only the gen-qa-openai.ipynb changes without unrelated commits from the previous branch.

@jhamon jhamon closed this Feb 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant