Skip to content

Commit

Permalink
Fix: openai.BadRequestError: Error code: 400 - {'error': {'message': …
Browse files Browse the repository at this point in the history
…"Invalid 'content': string too long. Expected a string with maximum length 256000, but got a string with length 268401 instead.", 'type': 'invalid_request_error', 'param': 'content', 'code': 'string_above_max_length'}}
  • Loading branch information
hiroshinishio committed Jul 8, 2024
1 parent 54d7d13 commit 3188426
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
2 changes: 2 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def get_env_var(name: str) -> str:
# OpenAI Credentials from environment variables
OPENAI_API_KEY: str = get_env_var(name="OPENAI_API_KEY")
OPENAI_FINAL_STATUSES: list[str] = ["cancelled", "completed", "expired", "failed"]
OPENAI_MAX_ARRAY_LENGTH = 32 # https://community.openai.com/t/assistant-threads-create-400-messages-array-too-long/754574/1
OPENAI_MAX_STRING_LENGTH = 256000 # https://community.openai.com/t/assistant-threads-create-400-messages-array-too-long/754574/5
OPENAI_MAX_TOKENS = 4096
OPENAI_MODEL_ID = "gpt-4o"
OPENAI_ORG_ID: str = get_env_var(name="OPENAI_ORG_ID")
Expand Down
28 changes: 13 additions & 15 deletions services/openai/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
from openai.types.beta.threads import Run, Message, TextContentBlock
from openai.types.beta.threads.run_submit_tool_outputs_params import ToolOutput


# Local imports
from config import OPENAI_FINAL_STATUSES, OPENAI_MODEL_ID, TIMEOUT_IN_SECONDS
from config import OPENAI_FINAL_STATUSES, OPENAI_MAX_STRING_LENGTH, OPENAI_MODEL_ID, TIMEOUT_IN_SECONDS
from services.openai.functions import (
GET_REMOTE_FILE_CONTENT,
functions,
Expand All @@ -29,12 +28,12 @@
commit_multiple_changes_to_remote_branch,
update_comment,
)

from utils.file_manager import (
clean_specific_lines,
correct_hunk_headers,
split_diffs,
)
from utils.handle_exceptions import handle_exceptions


def create_assistant() -> tuple[Assistant, str]:
Expand Down Expand Up @@ -231,22 +230,21 @@ def run_assistant(
return token_input, token_output


@handle_exceptions(raise_on_error=True)
def submit_message(
client: OpenAI, assistant: Assistant, thread: Thread, user_message: str
) -> tuple[Run, str]:
"""https://cookbook.openai.com/examples/assistants_api_overview_python"""
client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content=user_message,
timeout=TIMEOUT_IN_SECONDS,
)
input_data = json.dumps(
{
"content": str(user_message),
"role": "'user",
}
)
# Ensure the message string length is <= 256,000 characters. See https://community.openai.com/t/assistant-threads-create-400-messages-array-too-long/754574/5
for i in range(0, len(user_message), OPENAI_MAX_STRING_LENGTH):
chunk = user_message[i: i + OPENAI_MAX_STRING_LENGTH]
client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content=chunk,
timeout=TIMEOUT_IN_SECONDS,
)
input_data = json.dumps({"role": "'user", "content": str(user_message)})
return (
client.beta.threads.runs.create(
thread_id=thread.id, assistant_id=assistant.id, timeout=TIMEOUT_IN_SECONDS
Expand Down

0 comments on commit 3188426

Please sign in to comment.