Forum in maintenance, we will back soon 🙂
Connect Python with OpenAI API install error
@ssadvisor if this is not the right code, then definitely I need some more help.
Â
import openai
openai.api_key = "sk-XXX"
selected_model = "gpt-3.5-turbo"
# Initialize the list of messages
messages = [{"role": "system", "content": "You are a helpful assistant."}]
# Start a chat
def continue_chat(user_message):
# Add the user's message to the list
messages.append({"role": "user", "content": user_message})
# Get the chat response
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo", # or gpt-4 if available
messages=messages, # the list of all messages
)
# Add the AI's response to the list
messages.append(
{"role": "assistant", "content": response.choices[0].message["content"]}
)
return response.choices[0].message["content"]
# Chat with the bot
while True:
user_message = input("You: ")
if user_message.lower() == "quit":
break
ai_response = continue_chat(user_message)
print("AI: ", ai_response)
@ionut-antal feel free to schedule a one on one with me.
Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack
I updated it again; it must be working now; I tested it from my side.
@admin Many thanks for your patience and support. One of the problems I had was that (some how automatically)Â I was saving the file script I was working on , somewhere else then where the Python interpreter is. Now I have to choice manually where to save the file script, but, at least I now how.Â
Another think is that that , to communicate with OpenAI , I don't ask in the Code but in the Terminal.
All in all it is working.Â