openai-cookbook
openai-cookbook copied to clipboard
error in chat.py
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

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.
@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 It didn't work for me. still the same error.
My mistake. @styks1987 's solution just works. I had to put the key directly in chatbot.py instead of taking it from environment variables.
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.
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.
This issue was closed because it has been stalled for 10 days with no activity.