Claude/Sonnet Python API - больше токенов замораживает, меньше токенов усечныPython

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 Claude/Sonnet Python API - больше токенов замораживает, меньше токенов усечны

Сообщение Anonymous »

My prompt was to create a blog with 5 sections covering 5 different individuals in an industry niche.
When I had max_tokens= 300 (and then 1000), it ran quickly, but the result was not complete.
When I tried max_tokens=1500 (or 3000), then the program freezes, and doesn't come back after even about 5 minutes. Я спросил Клода, что делать, и он предоставил этот код для повторения, но в любое время, когда я более 1500 токенов, я получаю тайм -аут. < /P>
def get_claude_response(prompt, max_retries=3, initial_max_tokens=2000, timeout=60):
for attempt in range(max_retries):
try:
message = client.messages.create(
model="claude-3-sonnet-20240229",
max_tokens=initial_max_tokens * (attempt + 1), # Increase max_tokens on each retry
messages=[
{"role": "user", "content": prompt}
],
timeout=timeout # Set a timeout for the API call
)
return message.content[0].text
except anthropic.APITimeoutError:
print(f"Timeout occurred. Retrying with increased max_tokens. Attempt {attempt + 1}/{max_retries}")
time.sleep(2) # Wait for 2 seconds before retrying
except Exception as e:
print(f"An error occurred: {str(e)}")
return None

print("Max retries reached. Unable to get a complete response.")
return None

# Example usage
prompt = "details here..."

print("Prompt:")
print(prompt)

response = get_claude_response(prompt)

if response:
print(response)
else:
print("Failed to get a response from Claude.")

I asked Claude itself for a solution and it came up with a method to call and get back chunks. Although it worked, it was very slow and was 5 calls, which might be costing me more in the long run than one call with more tokens.

def call_claude_sonnet_in_chunks(prompt, max_chunks=10, tokens_per_chunk=500, timeout=55):
full_response = ""
messages = [{"role": "user",
"content": prompt}]

for i in range(max_chunks):
try:
print ("Call Claude:Sonnet i=" + str(i) + " tokens_per_chunk=" + str(tokens_per_chunk))
message = client.messages.create(
model="claude-3-sonnet-20240229",
max_tokens=tokens_per_chunk,
messages=messages,
timeout=timeout
)

chunk = message.content[0].text
full_response += chunk

# Check if the response is complete
if chunk.strip().endswith('.'):
break

# Prepare for the next chunk
messages.append({"role": "assistant", "content": chunk})
messages.append({"role": "user", "content": "Please continue your previous response."})

time.sleep(1) # Small delay between requests
except anthropic.APITimeoutError:
print(f"Timeout occurred on chunk {i + 1}. Moving to next chunk.")
except Exception as e:
print(f"An error occurred: {str(e)}")
break

return full_response.strip()


Подробнее здесь: https://stackoverflow.com/questions/789 ... -truncates
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение
  • Claude/Sonnet Python API - больше токенов замораживает, меньше токенов усечны
    Anonymous » » в форуме Python
    0 Ответы
    3 Просмотры
    Последнее сообщение Anonymous
  • AWS Bedrock Claude Sonnet 3.5 с изображением и системной подсказкой
    Anonymous » » в форуме Python
    0 Ответы
    7 Просмотры
    Последнее сообщение Anonymous
  • AWS Bedrock Claude Sonnet 3.5 с изображением и системной подсказкой
    Anonymous » » в форуме Python
    0 Ответы
    9 Просмотры
    Последнее сообщение Anonymous
  • Claude Sonnet может позвонить инструменту только один раз за раз
    Anonymous » » в форуме Python
    0 Ответы
    4 Просмотры
    Последнее сообщение Anonymous
  • Эффективное поддержание контекста чата с помощью GenAI API (GPT, Claude) без повторной отправки всех запросов
    Anonymous » » в форуме Python
    0 Ответы
    21 Просмотры
    Последнее сообщение Anonymous

Вернуться в «Python»