Skip to content

Forum in maintenance, we will back soon 🙂

Connect Python with...
 
Notifications
Clear all

Connect Python with OpenAI API install error

23 Posts
4 Users
6 Reactions
86 Views
Hasan Aboul Hasan
(@admin)
Posts: 1276
Member Admin
 

@ionut-antal can you please share which script you are trying?

 
Posted : 01/08/2024 1:34 pm
(@ionut-antal)
Posts: 21
Eminent Member Customer
Topic starter
 

@ssadvisor if this is not the right code, then definitely I need some more help.

 
Posted : 01/09/2024 8:46 pm
(@ionut-antal)
Posts: 21
Eminent Member Customer
Topic starter
 

@admin

 

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)

 
Posted : 01/09/2024 8:47 pm
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
 

@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

 
Posted : 01/09/2024 10:05 pm
Hasan Aboul Hasan
(@admin)
Posts: 1276
Member Admin
 

@ionut-antal I updated the code, please pull again and test

 
Posted : 01/10/2024 8:47 am
(@ionut-antal)
Posts: 21
Eminent Member Customer
Topic starter
 

@admin I did test the new script, here is the result.

 
Posted : 01/10/2024 10:02 pm
Hasan Aboul Hasan
(@admin)
Posts: 1276
Member Admin
 

I updated it again; it must be working now; I tested it from my side.

 
Posted : 01/11/2024 6:32 am
SSAdvisor reacted
(@ionut-antal)
Posts: 21
Eminent Member Customer
Topic starter
 

@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. 

 
Posted : 01/14/2024 4:02 pm
Page 2 / 2
Share:
[the_ad_group id="312"]