Search code examples
pythonstreamlitopenai-apigpt-3chatgpt-api

problem with running OpenAI Cookbook's chatbot


I'm having trouble running the chatbot app in the OpenAI Cookbook repository.

What I tried

I installed the necessary packages with 'pip install -r requirements.txt'. I made .env file with my OpenAI API Key, and inserted the code below in chatbot.py line 9.

import os
openai.api_key = os.getenv("OPENAI_API_KEY")

The setup above is by my guess, because the doc is totally unclear about how to set up.

I run the app in local by the command "streamlit run apps/chatbot-kickstarter/chat.py." It didn't work properly. The app run but when I entered text and pressed 'submit' button in the app, I got an error:

Uncaught app exception
Traceback (most recent call last):
  File "C:\Users\XXX\AppData\Local\Programs\Python\Python310\lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 565, in _run_script
exec(code, module.__dict__)
  File "F:\PythonProjects\openai-cookbook\apps\chatbot-kickstarter\chat.py", line 71, in <module>
response = query(messages)
  File "F:\PythonProjects\openai-cookbook\apps\chatbot-kickstarter\chat.py", line 51, in query
response = st.session_state['chat'].ask_assistant(question)
  File "F:\PythonProjects\openai-cookbook\apps/chatbot-kickstarter\chatbot.py", line 61, in ask_assistant
if 'searching for answers' in assistant_response['content'].lower():
TypeError: string indices must be integers

I use Python 3.10.6.

I would appreciate any help or guidance to resolve these issues.


Solution

  • Putting the key directly in chatbot.py just worked. It shouldn't be taken from environment variables.