Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
knbknb opened this issue Dec 15, 2024 · 1 comment

Comments

@knbknb
Copy link

knbknb commented Dec 15, 2024

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
@NirDiamant
Copy link
Owner

NirDiamant commented Dec 15, 2024 via email

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

No branches or pull requests

2 participants