-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
LLM can't be trusted to parse it's own json #148
Comments
This issue #70 is not accurate but it's a duplicate of this issue now. |
several python libs solve/attempt to solve this, in order of my personal opinion of them: probably more -- however, not sure if any have a typescript equivalent |
if it's openai, we can use structured output mode: https://platform.openai.com/docs/guides/structured-outputs |
kind of a hacky workaround for non-openai models: https://docs.litellm.ai/docs/completion/json_mode -- it's called json mode, but i think you can do any kind of structured output. Just replace the OPENAI_API_URL with localhost:4000 and should be compatible |
This could help with the issue:
|
@St4rgarden I wonder if simply explaining it better in instructions would solve it like
|
yep. hi @Elyx0 :) |
Yeah I had a similar question about the current approach for Using export async function generateObject({
runtime,
context,
modelClass,
}: {
runtime: IAgentRuntime;
context: string;
modelClass: string;
}): Promise<any> {
if (!context) {
elizaLogger.error("generateObject context is empty");
return null;
}
let retryDelay = 1000;
while (true) {
try {
const response = await generateText({
runtime,
context,
modelClass,
});
const parsedResponse = parseJSONObjectFromText(response);
if (parsedResponse) {
return parsedResponse;
}
} catch (error) {
elizaLogger.error("Error in generateObject:", error);
}
await new Promise((resolve) => setTimeout(resolve, retryDelay));
retryDelay *= 2;
}
} My proposal is to replace it with the /**
Generate JSON with any schema for a given prompt using a language model.
This function does not stream the output. If you want to stream the output, use `streamObject` instead.
@returns
A result object that contains the generated object, the finish reason, the token usage, and additional information.
*/
declare function generateObject(options: Omit<CallSettings, 'stopSequences'> & Prompt & {
output: 'no-schema';
model: LanguageModel;
mode?: 'json';
experimental_telemetry?: TelemetrySettings;
experimental_providerMetadata?: ProviderMetadata;
_internal?: {
generateId?: () => string;
currentDate?: () => Date;
};
}): Promise<GenerateObjectResult<JSONValue>>; Switching to this method would improve reliability and reduce custom parsing logic. I'd be interested to hear your thoughts! |
Describe the bug
We trust the LLM to parse it's own JSON resulting in what a separate issue referred to as an infinite loop (which technically will resolve itself if left alone to smash on the OpenAI endpoint for long enough)
Notice above that the action: value
NONE
is not a string. Now take a look at the correctly parsed JSON immediately following this:Here the LLM has correctly formatted
NONE
as'NONE'
a correct string.To Reproduce
Just run eliza with a cheap llm model long enough and you will definitely encounter this one.
Expected behavior
The message returned from the LLM should then be formatted into JSON in the program.
The text was updated successfully, but these errors were encountered: