I am getting following exceptions when trying to execute a Python script in Visual Studio Code (VSC). I suspect this is a simple env config issue but am new to Python and can't see it.
Import "openai" could not be resolved by Pylance Import "gradio" could not be resolved by Pylance
I am using Mac Catalina 10.15.7. VSC Version: 1.75.1. I have installed Python, openai and gradio:
--version Python 3.9.12 (base)
--version openai 0.27.7pip install gradio
This is the script:
import openai
import gradio as gr
print("Debugging AI script")
openai.api_key = "..."
print("API Key: " + openai.api_key)
messages = [
{
"role": "system",
"content":"You are a helpful andd kind AI Assitant"
},
]
def chatbot(input):
if input:
messages.append({
"role":"user",
"content": input
})
chat = openai.ChatCompletion.create(model="gpt-3.5-turbo",
messages = messages)
reply = chat.choices[0].message.content
messages.append({
"role":"assistant",
"content": reply
})
return reply
inputs = gr.inputs.Textbox(lines=7,
label="Chat with Mega Brain")
outputs = gr.outputs.Textbox(label="Speak Sir")
gr.Interface(fn=chatbot, inputs=inputs,
outputs=outputs, title="AI Mega-Brain Mega-Chat",
description="Ask the Brain anything",
theme="compact").launch(share=True)
Ive tried a number of solutions, included these posted here, to no avail.
Any help appreciated. thanks
You need to make sure that interpreter used by VS Code matches one you are using in the terminal. In the environment you installed python in type: which python
(or which python3
, depending on your setup.
Then follow these instructions and select the path that matches output of the command above.
Lastly, I highly recommend setting up per-project environments (see: Mamba, Conda, or Poetry all accomplishing the similar goals in slightly different ways)