Skip to content

Fixed issue with 3.5 Models hallucinating a function called "python", causing empty returns #683

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 49 additions & 17 deletions interpreter/llm/setup_openai_coding_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ def coding_llm(messages):

for chunk in response:

if interpreter.debug_mode:
print("Chunk from LLM", chunk)

if ('choices' not in chunk or len(chunk['choices']) == 0):
# This happens sometimes
continue
Expand All @@ -108,31 +111,60 @@ def coding_llm(messages):
# Accumulate deltas
accumulated_deltas = merge_deltas(accumulated_deltas, delta)

if interpreter.debug_mode:
print("Accumulated deltas", accumulated_deltas)

if "content" in delta and delta["content"]:
yield {"message": delta["content"]}

if ("function_call" in accumulated_deltas
and "arguments" in accumulated_deltas["function_call"]):

arguments = accumulated_deltas["function_call"]["arguments"]
arguments = parse_partial_json(arguments)

if arguments:

if (language is None
and "language" in arguments
and "code" in arguments # <- This ensures we're *finished* typing language, as opposed to partially done
and arguments["language"]):
language = arguments["language"]
if ("name" in accumulated_deltas["function_call"] and accumulated_deltas["function_call"]["name"] == "execute"):
arguments = accumulated_deltas["function_call"]["arguments"]
arguments = parse_partial_json(arguments)

if arguments:
if (language is None
and "language" in arguments
and "code" in arguments # <- This ensures we're *finished* typing language, as opposed to partially done
and arguments["language"]):
language = arguments["language"]
yield {"language": language}

if language is not None and "code" in arguments:
# Calculate the delta (new characters only)
code_delta = arguments["code"][len(code):]
# Update the code
code = arguments["code"]
# Yield the delta
if code_delta:
yield {"code": code_delta}
else:
if interpreter.debug_mode:
print("Arguments not a dict.")

# 3.5 REALLY likes to halucinate a function named `python` and you can't really fix that, it seems.
# We just need to deal with it.
elif ("name" in accumulated_deltas["function_call"] and accumulated_deltas["function_call"]["name"] == "python"):
if interpreter.debug_mode:
print("Got direct python call")
if (language is None):
language = "python"
yield {"language": language}
if language is not None and "code" in arguments:
# Calculate the delta (new characters only)
code_delta = arguments["code"][len(code):]

if language is not None:
# Pull the code string straight out of the "arguments" string
code_delta = accumulated_deltas["function_call"]["arguments"][len(code):]
# Update the code
code = arguments["code"]
code = accumulated_deltas["function_call"]["arguments"]
# Yield the delta
if code_delta:
yield {"code": code_delta}

yield {"code": code_delta}

else:
if interpreter.debug_mode:
print("GOT BAD FUNCTION CALL: ", accumulated_deltas["function_call"])


return coding_llm