Getting an error while creating a Natural language to SQL code converter
import openai
# Set your OpenAI GPT-3.5 Turbo API key
openai.api_key = 'your-api-key'
def convert_to_sql(natural_language_text):
prompt = f"Convert the following natural language text to SQL: {natural_language_text}"
response = openai.chat.completions.create(
engine="text-davinci-003", # You can also use "text-davinci-003" for the Davinci model
prompt=prompt,
max_tokens=100 # You can adjust the maximum number of tokens based on your requirements
)
return response.choices[0].text.strip()
# Example usage
natural_language_text = "Retrieve all employees who joined the company after 2020."
sql_query = convert_to_sql(natural_language_text)
print(f"Natural Language Text: {natural_language_text}")
print(f"SQL Query: {sql_query}")
Any input/suggestions will be really helpful.
1 Like
text-davinci-003
used the legacy completions
endpoint not chat/completions
, but has been deprecated.
You can use gpt-3.5-turbo-instruct
as a replacement on the completions
endpoint,
https://platform.openai.com/docs/api-reference/completions
Or use one of the current models.
1 Like
Thanks but this is still not working -
import openai
# Set your OpenAI GPT-3.5 Turbo API key
openai.api_key = 'your-api-key'
def convert_to_sql(natural_language_text):
prompt = f"Convert the following natural language text to SQL: {natural_language_text}"
response = openai.chat.completions.create(
engine="gpt-3.5-turbo-instruct", # You can also use "text-davinci-003" for the Davinci model
prompt=prompt,
max_tokens=100 # You can adjust the maximum number of tokens based on your requirements
)
return response.choices[0].text.strip()
# Example usage
natural_language_text = "Retrieve all employees who joined the company after 2020."
sql_query = convert_to_sql(natural_language_text)
print(f"Natural Language Text: {natural_language_text}")
print(f"SQL Query: {sql_query}")
Error -
1 Like
API Reference - OpenAI API
You need to use the new Chat Completions Endpoint, as shown below.
from openai import OpenAI
client = OpenAI()
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
]
)
print(completion.choices[0].message)
You may also need to run pip install --upgrade openai
1 Like