-1

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.

torano
  • 97
  • 7
  • Yes I did put the actual key string instead of "OPENAI_API_KEY" – torano May 01 '23 at 10:52
  • Can we please get a [mcve]? – Robert May 01 '23 at 14:43
  • 1
    https://github.com/openai/openai-cookbook/issues/307 – Haoliang May 02 '23 at 03:07
  • In the link, it is said that putting api key in chatbot.py fixes the problem but it doesn' work for me. The same result. – torano May 02 '23 at 11:40
  • @Robert I just made .env file with my key (MY_ACTUAL_KEY=sk-XXXX) and inserted the code "import os openai.api_key = os.getenv("MY_ACTUAL_KEY")" in line 9 in chatbot.py, and run the command "streamlit run apps/chatbot-kickstarter/chat.py" in console. – torano May 02 '23 at 11:48

2 Answers2

0

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

torano
  • 97
  • 7
-1

Please try this, if work accept the answer, otherwise i delete it

Try remove ['content'] from assistant_response

The error says "string indices must be integers"

This mean that you are trying to do something like this:

out = "a string object"["content"]

And it obviously wont work

Probably assistant_response is already a string or is the wrong object for what you're doing

PS: A correct use of string indices is something like this:

out = "a string object"[0]
print(out)
>>> "a"

Tutorial