StructuredOutputParser seems verbose - heres another implementation #1523
Replies: 2 comments 2 replies
-
Could pretty easily allow defining custom formatting that enforces the write name for the type so that it is easier to create advanced type formats (but also easier to diverge from the zod schema) by just accepting an optional format string strictly types to at least have const identifier = 'OutputJSON' as const
export type CustomFormatString = `${any}type ${typeof identifier} = {${any}}${any}`; It has worked for far larger types like Templatetype Output ={
input: Input;
character: {
firstName: string;
lastName: string;
age: number;
gender: string;
ethnicity: string;
personalityTraits: {
openness: number;
conscientiousness: number;
extraversion: number;
agreeableness: number;
neuroticism: number;
};
personalityDescriptions: string[]; // Array of descriptions about their personality
hobbies: string[];
values: string[];
lifeGoals: string[];
fears: string[];
strengths: string[];
weaknesses: string[];
backstory: string;
lifeStoryDescriptions: string[]; // Array of descriptions about their life story
relationships: {
family: string[];
friends: string[];
enemies: string[];
};
currentLifeSituation: string;
};
}; Output{
"input": "The most powerful wizard that ever lived who has a magical aura",
"character": {
"firstName": "Merlin",
"lastName": "",
"age": 943,
"gender": "Male",
"ethnicity": "",
"personalityTraits": {
"openness": 0.8,
"conscientiousness": 1,
"extraversion": 0.4,
"agreeableness": 0.5,
"neuroticism": 0.3
},
"personalityDescriptions": [
"Merlin is incredibly wise and knowledgeable, with a deep understanding of magic and the mystical realm. He is also highly disciplined and hardworking, constantly pushing himself to achieve new heights in his powers.",
"Despite his great power, Merlin is generally reserved and introspective, preferring to spend much of his time alone in meditation or deep contemplation of the mysteries of the universe."
],
"hobbies": [
"Studying ancient texts to unlock new spells and understand the secrets of the universe.",
"Meditating on the nature of existence and consciousness.",
"Occasionally traveling through time to study different eras and learn from their cultures."
],
"values": [
"The pursuit of wisdom and knowledge above all else.",
"Maintaining balance in the universe through careful use of his powers.",
"Teaching others about magic and helping those who seek his guidance."
],
"lifeGoals": [
"To continue learning and growing in his magical abilities, eventually unlocking the full potential of his powers.",
"To maintain balance in the universe and keep dark forces at bay.",
"To share his knowledge with others through teaching and mentoring young wizards."
],
"fears": [
"Losing control of his powers and causing harm to the world through unintended consequences.",
"Becoming corrupted by power and turning to the dark side of magic.",
"Being isolated from others for too long and losing touch with reality."
],
"strengths": [
"Unparalleled mastery of magic, with a deep understanding of even the most obscure spells and rituals.",
"A profound sense of wisdom and insight, allowing him to see beyond the superficial surface of reality.",
"A deep connection to the natural world and the mystical realm, granting him great power over the forces of nature."
],
"weaknesses": [
"Tendency to become so absorbed in his studies and meditation that he neglects his physical health or social life.",
"Occasional bouts of intense self-doubt or anxiety, particularly when faced with daunting challenges or enemies.",
"Limited ability to relate to others who do not share his deep understanding of magic and mysticism."
],
"backstory": "Merlin was born into a family of powerful wizards, each specializing in a different aspect of magic. From a young age, it was clear that Merlin was destined for greatness, manifesting astounding magical abilities even as a small child. He studied under some of the greatest wizards in history, eventually surpassing them all with his exceptional powers. Over time, Merlin began to take on the role of teacher himself, mentoring young wizards and sharing his knowledge with those who sought it out. He has witnessed many great events over the centuries, from wars between kingdoms to earth-shattering cosmic events. Throughout it all, he has remained dedicated to his pursuit of knowledge and wisdom, never faltering in his quest to unlock new secrets about the universe.",
"lifeStoryDescriptions": [
"Throughout his long life, Merlin has been instrumental in many pivotal moments throughout history, from quietly advising kings and emperors to directly intervening in battles between powerful wizards.",
"Merlin has also spent much of his life pursuing personal growth and enlightenment, venturing deep into the mystical realm to explore the furthest reaches of the universe.",
"While Merlin is often seen as remote and aloof by those who do not know him well, those who encounter him on a deeper level come to understand the profound compassion and empathy that underlie his actions."
],
"relationships": {
"family": [],
"friends": [
"King Arthur, whom he mentored throughout his youth and later advised during many important battles.",
"Morgan le Fay, a powerful sorceress with whom he has engaged in many epic magical battles over the centuries.",
],
"enemies": [
"Mordred, a dark wizard who seeks to overthrow the kingdom and plunge it into chaos.",
"Vortigern, a powerful warlock who has long sought to capture and steal Merlin's magic for his own uses."
]
},
"currentLifeSituation": "Merlin spends most of his days secluded in his tower deep within an enchanted forest, meditating on the nature of reality and continuing to unlock new secrets about the mystical realm. However, he occasionally emerges from his hermitage in order to advise important leaders or intervene in crises affecting the balance of power in the universe."
}
} |
Beta Was this translation helpful? Give feedback.
-
hey @bradennapier, I need some help, whenever I am executing the code with some input string to query my database I am getting this error, it would be helpful if you guide me if I am doing something wrong here, the below is the error and the code that I am executing...I suspect after this particular line the error appears :
here is my code
|
Beta Was this translation helpful? Give feedback.
-
I have had perfect results thus far with a far less verbose method which doesn't require all the extra cognitive additions and formatting / token costs.
It works by using the typescript compiler to take a zod schema and turn it into a string with jsdoc if
describe
is used. It has even worked flawlessly with unions, intersections, etc (for example something like{ success: true } | { success: false; reason: string }
. Just thought I'd shareZod Schema / Result
Generated Prompt Example
OutputParser
Implementation
Beta Was this translation helpful? Give feedback.
All reactions