r/learnprogramming • u/nderstand2grow • Feb 21 '24
Code Review "The Unix Philosophy" says create small functions that do one thing well. Is this code Unix-y?
Genuine question: What design pattern works best here: Let's say I want to write small functions that do one thing very well. In this application, I want to make API calls to an LLM and extract the returned JSON.
args = {LLM parameters, e.g., prompt}
1. foo(args): calls the LLM API with args
2. goo (foo(args)): uses backoff (retry) and makes sure the output is JSON
3. hoo(goo(foo(args))): extracts and desers the JSONAt any point, things could go wrong. I could use a monadic approach and turn each of these functions into a monad:
1. foo: args -> Maybe(API_res)
2. goo: Maybe(API_res) -> Maybe(JSON)
3. hoo: Maybe(JSON) -> Maybe(dict)But before I knew about monads, I thought: wouldn't be cool if when a function goes wrong and needs to be called again, it had access to its "parent" function which called it? Like: Currently args is only passed to foo. What if, depending on how things went wrong, goo needed to see args?
One approach is to make foo pass its args as well:
foo: args -> args, outputThen goo would take that and do something with it. But what if now hoo also needed to know about args to make sure the extracted JSON conforms to the JSON schema mentioned in args? Now we'd have to do:
foo: args -> args, output
goo: args, output -> args, output, JSON
goo: args, output, JSON -> dict (deserialized JSON)I think this is not "elegant". Is there any better solution?
•
u/AutoModerator Feb 21 '24
On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge.
If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options:
as a way to voice your protest.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
6
u/[deleted] Feb 21 '24
I don't know why you don't just have foo calling goo. Call goo in a loop within foo until it returns a JSON result or meets a failure criteria. Then foo returns that JSON (on success), and the main routine sends that to hoo.
Monads are good too.
I just think the point to split functions should be determined by some larger goals of what parts should be orthogonal, what interfaces are nice to work with, where is there a performance bottleneck, etc.
5
u/high_throughput Feb 21 '24
The monad here is Maybe. The functions are not themselves monads.
Sounds like tight coupling tbh
If you're into monads and want all functions involved to have access to some piece of data without passing it explicitly, there's what Haskell calls Reader. You can stack it via monad transformers, e.g.
ReaderT ArgsType Maybe ResultTypefor a function type that can both access some args and also only optionally return a value.1
u/[deleted] Feb 21 '24
My suggestion is to query the language model imposing a grammar, for example using guidance or gbnf on llama.cpp, the accuracy of the results are much better then letting the llm generating a json freely. https://github.com/ggerganov/llama.cpp/blob/master/grammars/json.gbnf