What is wrong with my code.
I have tried variations, but still keep getting the error of ‘OpenAI’ object has no attribute ‘ChatCompletion’
import os
from openai import OpenAI
def read_file_content(file_path):
with open(file_path, 'r') as file:
return file.read().strip()
def read_prompts(file_path):
with open(file_path, 'r') as file:
return [line.strip() for line in file if line.strip()]
def ask_openai(client, prompt, context, model):
try:
response = client.ChatCompletion.create(
model=model,
messages=[
{"role": "system", "content": context},
{"role": "user", "content": prompt}
]
)
if response.choices:
# Accessing the text attribute of the first choice
return response.choices[0].message.content.strip()
else:
return "No response generated."
except Exception as e:
return f"Error: {e}"
def main():
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
model = 'f'
context_file_path = '/'
prompts_file_path = ''
context = read_file_content(context_file_path)
prompts = read_prompts(prompts_file_path)
for prompt in prompts:
answer = ask_openai(client, prompt, context, model)
print(f"Prompt: {prompt}\nAnswer: {answer}\n")
if __name__ == "__main__":
main()