mshumer / gpt-author

MIT License
2.45k stars 354 forks source link

KeyError: 'content' #38

Closed aithec77 closed 3 months ago

aithec77 commented 3 months ago

When trying to generate a book using api claude 3 in google colab on chapter 15 it gives an error. What to do?

Requirement already satisfied: EbookLib in /usr/local/lib/python3.10/dist-packages (0.18) Requirement already satisfied: lxml in /usr/local/lib/python3.10/dist-packages (from EbookLib) (4.9.4) Requirement already satisfied: six in /usr/local/lib/python3.10/dist-packages (from EbookLib) (1.16.0) Enter the desired writing style: exciting and mystical, with elements of science fiction and philosophy; - full of surprises, leading the reader through a series of complex resolutions and mysteries; - dramatic, with deeply developed characters facing difficult moral choices; - emotional, emphasizing human relationships and their importance in a world of technological wonders; - written with a high intellectual load, in order to stimulate the reader to reflect on the nature of human existence and the future of civilization. Enter a high-level description of the book: In the year 2182 on the A-class space station of the planet Omega 8, Alex faces an unexpected twist of fate when he finds an infant at his door. The Law of Interchangeability: In a world where aging is halted and consciousness can be transferred into androids, childbirth is strictly controlled by the Empire to keep the population stable. The Dilemma: Alex, who has never had children and has no right to have them, decides to enlist the help of a friend, the station chief, to decide what to do with a child he has found. Enter the number of chapters: 20 Generating plot outline... Plot outline generated. Generating chapter 1... Chapter 1 generated. Generating chapter 2... Chapter 2 generated. Generating chapter 3... Chapter 3 generated. Generating chapter 4... Chapter 4 generated. Generating chapter 5... Chapter 5 generated. Generating chapter 6... Chapter 6 generated. Generating chapter 7... Chapter 7 generated. Generating chapter 8... Chapter 8 generated. Generating chapter 9... Chapter 9 generated. Generating chapter 10... Chapter 10 generated. Generating chapter 11... Chapter 11 generated. Generating chapter 12... Chapter 12 generated. Generating chapter 13... Chapter 13 generated. Generating chapter 14... Chapter 14 generated. Generating chapter 15...

KeyError Traceback (most recent call last) in <cell line: 172>() 170 171 # Generate the book --> 172 plot_outline, book, chapters = generate_book(writing_style, book_description, num_chapters) 173 174 title = generate_title(plot_outline)

1 frames in generate_text(prompt, model, max_tokens, temperature) 31 } 32 response = requests.post("https://api.anthropic.com/v1/messages", headers=headers, json=data) ---> 33 response_text = response.json()['content'][0]['text'] 34 return response_text.strip() 35

KeyError: 'content'

chipotle commented 3 months ago

This is (probably) a rate limit error. The notebook as provided has absolutely no error reporting/handling, which makes it difficult to be sure, but it's the most likely culprit. I would suggest, if you want to play around this and don't want to give Anthropic lots of money to increase your rate limit,

If you want to delve into Python a bit more, I would also suggest adding some basic error handling to generate_text, and maybe retry on a 429 or 529 error (which are hitting your rate limit and Anthropic's API being overloaded, respectively). I used something like

if response.status_code != 200:
    print(f" ! got status code {response.status_code}")
    print(response.json())
else:
    response_text = response.json()['content'][0]['text']
    return response_text.strip()

…but I also broke the notebook down into a series of cells rather than just one big script, because I wanted to be able to investigate just what's being generated along the way to see things like "how good is the outline Claude-Author has generated from the prompt" and "how well is Claude-Author following its own generated outline as it creates the chapters".

aithec77 commented 3 months ago

Thank you very much for your reply. I reduced the number of chapters to 10 and now the books are generated without errors)