openai-cookbook icon indicating copy to clipboard operation
openai-cookbook copied to clipboard

error in chat.py

Open mnarasimhan02 opened this issue 1 year ago • 5 comments

I am trying to deploy an app from open ai , the app got deployed, however when i run the app, I get a streamlit error message

TypeError: string indices must be integers Traceback:

File "python3.10/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 565, in _run_script exec(code, module.dict) File "/Documents/GitHub/chatbotkb/chat.py", line 71, in response = query(messages) File "/Documents/GitHub/chatbotkb/chat.py", line 51, in query response = st.session_state['chat'].ask_assistant(question) File "/Documents/GitHub/chatbotkb/chatbot.py", line 58, in ask_assistant if 'searching for answers' in assistant_response['content'].lower():

Screen Shot 2023-03-31 at 3 39 20 PM

mnarasimhan02 avatar Mar 31 '23 19:03 mnarasimhan02

Based on the error message you provided, it looks like there is an issue with the way you are accessing a string index in your code. Specifically, the error message "TypeError: string indices must be integers" suggests that you are trying to access a string index using a non-integer value, such as a string or a float.

Looking at the stack trace, the error seems to be originating from line 58 of "chatbot.py" file, where you are trying to access a string index using the "content" key of the "assistant_response" dictionary. The error message suggests that the "content" value might be a string, but you are trying to access it using a non-integer index.

To fix this issue, you should check the type of the "content" value in the "assistant_response" dictionary and make sure that you are using the correct index type when accessing it. It's possible that the "content" value is not a string, or that you are trying to access it using an invalid index.

One way to debug this issue is to print out the value of the "assistant_response" dictionary before accessing the "content" key to see what type of value it contains. You can also use the "type()" function to check the type of the "content" value, like this: print(type(assistant_response['content'])) This should give you some insight into what might be causing the issue.

AbdelAzizMohamedMousa avatar Apr 01 '23 15:04 AbdelAzizMohamedMousa

@mnarasimhan02 - This happens when you don't have your API key specified correctly.

To test, in the chatbot.py file set openai.api_key = 'sk-XXXXXX' and see if it works. This fixed the issue for me.

styks1987 avatar Apr 17 '23 12:04 styks1987

@styks1987 It didn't work for me. still the same error.

torano111 avatar May 02 '23 11:05 torano111

My mistake. @styks1987 's solution just works. I had to put the key directly in chatbot.py instead of taking it from environment variables.

torano111 avatar May 06 '23 08:05 torano111

The chatbot.py file should probably be updated to include openai.api_key = 'sk-XXXXXX' as a template and let people trying to use the tutorial that they need to include their API key.

alekfestekjian avatar May 09 '23 16:05 alekfestekjian

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 10 days.

github-actions[bot] avatar Oct 11 '23 01:10 github-actions[bot]

This issue was closed because it has been stalled for 10 days with no activity.

github-actions[bot] avatar Oct 22 '23 01:10 github-actions[bot]