Skip to content

Notebook "prompt-templates-variables-jinja2" : still uses OpenAI v0 legacy API #3

Open
@knbknb

Description

@knbknb

I like the elegance of your Notebooks, using Jinja Templates and Langchain from the very beginning.

However, I noticed that the get_get_completion() function in all_prompt_engineering_techniques/prompt-templates-variables-jinja2.ipynb still uses the OpenAI v0 legacy API. The requirements.txt file installs/demands openai==1.51.1.

So when I run the the class PromptTemplate: ... code cell in that Jupyter Notebook I get an error.

APIRemovedInV1:  
You tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0 - see the README at https://github.com/openai/openai-python for the API.  
You can run `openai migrate` to automatically upgrade your codebase to use the 1.0.0 interface.  
Alternatively, you can pin your installation to the old version, e.g. `pip install openai==0.28`

I replaced the current code cell defining get_get_completion() function (not reproduced here) with the following code. So I know how to get along. However I don't know how many Notebooks in this repo will be affected, And I don't know if you prefer to fix everything yourself with the `openai migrate' mechanism. That's wh< I'm not sending a pull request.

### my replacement of the  `get_get_completion()` function - new openAI v1 interface
import os
from openai import OpenAI 
from jinja2 import Template
from dotenv import load_dotenv

load_dotenv()

def get_completion(prompt, model="gpt-4o-mini"):
    ''' Get a completion from the OpenAI API 
    Args:
        prompt (str): The prompt to send to the API
        model (str): The model to use for the completion
    Returns:
        str: The completion text
    '''
    messages = [{"role": "user", "content": prompt}]
    client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
    response = client.chat.completions.create(
        model=model,
        messages=messages,
        temperature=0,
    )
    return response.choices[0].message.content

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions