Search code examples
pythonopenai-api

TypeError when using openai-api


Using the code below and openai version 0.28.0 i get an error which i can't resolve:

File "", line 11, in TypeError: string indices must be integers, not 'str'

Which indice is it complaining about. Seems I'm a little blind today...

import requests
from bs4 import BeautifulSoup
from docx import Document
import openai

# Set your OpenAI API key
openai.api_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"


# URL of the website you want to scrape
website_url = "https://somesite.com"


# Send a GET request to the website
response = requests.get(website_url)

# Parse the HTML content of the website using BeautifulSoup
soup = BeautifulSoup(response.content, "html.parser")

# Extract text blocks larger than 100 characters
text_blocks = []
for paragraph in soup.find_all("p"):
    text = paragraph.get_text().strip()
    if len(text) >= 100:
        text_blocks.append(text)

# Translate text blocks from English to German using OpenAI's Chat API
translated_text_blocks = []
for text_block in text_blocks:
    chat_input = f"Translate the following English text to German: '{text_block}'"
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",  # Use the language model
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": chat_input},
        ],
    )

    # Extract translated text from the API response
    translated_text = response.choices[0].message["content"]["body"]
    translated_text_blocks.append(translated_text)

# Create a new Word document
document = Document()

# Add translated text blocks to the Word document
for translated_text in translated_text_blocks:
    document.add_paragraph(translated_text)

# Save the Word document
document.save("translated_content.docx")

The full console output is:

>>> # Send a GET request to the website
>>> 
>>> response = requests.get(website_url)
>>> # Parse the HTML content of the website using BeautifulSoup
>>> 
>>> soup = BeautifulSoup(response.content, "html.parser")      
>>> # Extract text blocks larger than 100 characters
>>> 
>>> text_blocks = []
>>> for paragraph in soup.find_all("p"):
...     text = paragraph.get_text().strip()
...     if len(text) >= 100:
...         text_blocks.append(text)
... # Translate text blocks from English to German using OpenAI's Chat API
...
>>> translated_text_blocks = []
>>> for text_block in text_blocks:
...     chat_input = f"Translate the following English text to German: '{text_block}'"
...     response = openai.ChatCompletion.create(
...         model="gpt-3.5-turbo",  # Use the language model
...         messages=[
...             {"role": "system", "content": "You are a helpful assistant."},
...             {"role": "user", "content": chat_input},
...         ],
...     )
...     # Extract translated text from the API response
...     translated_text = response.choices[0].message["content"]["body"]
...     translated_text_blocks.append(translated_text)
... # Create a new Word document
...
Traceback (most recent call last):
  File "<stdin>", line 11, in <module>
TypeError: string indices must be integers, not 'str'
>>> document = Document()
>>> # Add translated text blocks to the Word document
>>>
>>> for translated_text in translated_text_blocks:
...     document.add_paragraph(translated_text)
... # Save the Word document
...
>>> document.save("translated_content.docx")
>>> print("Translated text blocks have been saved to 'translated_content.docx'.")
Translated text blocks have been saved to 'translated_content.docx'.

Solution

  • Your problem is caused by this line of code:

    translated_text = response.choices[0].message["content"]["body"]

    response.choices[0].message["content"] is already your response from openai api in str type and so you are getting this error because you are trying to get item from str by key what is wrong.

    So just replace this line on this line:

    translated_text = response.choices[0].message["content"]