Advertisement
nicuf

Python OpenAI API error: module 'openai' has no attribute 'Completion'. Did you mean: 'completions'?

Feb 23rd, 2024
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. Python OpenAI API error: module 'openai' has no attribute 'Completion'. Did you mean: 'completions'?
  2.  
  3. https://stackoverflow.com/questions/78046390/python-openai-api-error-module-openai-has-no-attribute-completion-did-you
  4.  
  5. I try to make a bot for my website in Python. But I have an error.
  6.  
  7. The Error:
  8.  
  9. You: hello
  10. Traceback (most recent call last):
  11. File "D:\module2.py", line 20, in <module>
  12. response = chat_with_gpt(user_input)
  13. ^^^^^^^^^^^^^^^^^^^^^^^^^
  14. File "D:\module2.py", line 6, in chat_with_gpt
  15. response = openai.Completion.create(
  16. ^^^^^^^^^^^^^^^^^
  17. AttributeError: module 'openai' has no attribute 'Completion'. Did you mean: 'completions'?
  18.  
  19. This is the Python code:
  20.  
  21. I tried many combinations, and I also tried with text-davinci-003, but nothing worked.
  22.  
  23. import openai
  24.  
  25. openai.api_key = "API-KEY"
  26.  
  27. def chat_with_gpt(prompt):
  28. response = openai.Completion.create(
  29. # engine="text-davinci-003", # Adjust the engine as necessary
  30. engine="gpt-3.5-turbo", # Adjust the engine as necessary
  31. prompt=prompt,
  32. max_tokens=150
  33. )
  34. return response.choices[0].text.strip()
  35.  
  36. if __name__ == "__main__":
  37. while True:
  38. user_input = input("You: ")
  39. user_input = user_input.lower()
  40. if user_input in ["quit", "exit", "bye"]:
  41. break
  42. response = chat_with_gpt(user_input)
  43. print("Chatbot:", response)
  44.  
  45. pythonpython-3.xbotsopenai-apigpt-3
  46.  
  47. Share
  48. Edit
  49. Delete
  50. Flag
  51. edited 1 hour ago
  52. Rok Benko's user avatar
  53. Rok Benko
  54. 18.7k33 gold badges3030 silver badges5454 bronze badges
  55. asked 1 hour ago
  56. Anao_ O's user avatar
  57. Anao_ O
  58. 3344 bronze badges
  59.  
  60. 1
  61.  
  62. Why do you think that openai should have an attribute called Completion? –
  63. mkrieger1
  64. 1 hour ago
  65. And did you make sure that it shouldn't have been completions as suggested by the error message ? –
  66. Thierry Lathuille
  67. 1 hour ago
  68. I made my code, buy the code here: stackoverflow.com/questions/76040193/… –
  69. Anao_ O
  70. 1 hour ago
  71. I change it with: response = openai.completions.create( and I get the error: ` File "C:\Users\necul\AppData\Local\Programs\Python\Python312\Lib\site-packages\openai_utils_utils.py", line 298, in wrapper raise TypeError(msg) TypeError: Missing required arguments; Expected either ('model' and 'prompt') or ('model', 'prompt' and 'stream') arguments to be given >>> ` –
  72. Anao_ O
  73. 1 hour ago
  74.  
  75. @Anao_O See my answer. –
  76. Rok Benko
  77. 1 hour ago
  78.  
  79. Add a comment
  80. 1 Answer
  81. Sorted by:
  82. 2
  83.  
  84. There are a few problems in your code:
  85.  
  86. using the wrong method name (i.e., Completion)
  87. using the deprecated parameter (i.e., engine)
  88. using the incompatible model with the Completions API
  89.  
  90. The following code should work:
  91.  
  92. response = openai.completions.create( # Changed
  93. model="gpt-3.5-turbo-instruct", # Changed
  94. prompt=prompt,
  95. max_tokens=150
  96. )
  97.  
  98. Share
  99. Edit
  100. Follow
  101. Flag
  102. answered 1 hour ago
  103. Rok Benko's user avatar
  104. Rok Benko
  105. 18.7k33 gold badges3030 silver badges5454 bronze badges
  106.  
  107. yes, thanks, better. But also, I get this error: ` File "C:\Users\necul\AppData\Local\Programs\Python\Python312\Lib\site-packages\openai_base_client.py", line 877, in _request raise self._make_status_error_from_response(err.response) from None openai.RateLimitError: Error code: 429 - {'error': {'message': 'You exceeded your current quota, please check your plan and billing details. For more information on this error, read the docs: platform.openai.com/docs/guides/error-codes/api-errors.', 'type': 'insufficient_quota', 'param': None, 'code': 'insufficient_quota'}}` –
  108. Anao_ O
  109. 1 hour ago
  110.  
  111. See OpenAI API error 429: "You exceeded your current quota, please check your plan and billing details". –
  112. Rok Benko
  113. 1 hour ago
  114. 1
  115. thanks. Indeed. Were very good you solutions. –
  116. Anao_ O
  117. 1 hour ago
  118.  
  119. Add a comment
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement