Live data from Hacker News

Show HN: Magentic – Use LLMs as simple Python functions

github.com

61–70 of 70 posts

Re: Show HN: Magentic – Use LLMs as simple Python functions

#61
post #59

I am amazed that `...` is a valid syntax in Python, not a pseudo grammar. This library is impressive, I appreciate it and I will apply it to my project.

What's the difference between '...' and the more common 'pass'?

In this case, functionally, nothing. Some other commenters have suggested it does something interesting by implying "AI will provide the logic," whereas "pass" doesn't necessarily do that.

Re: Show HN: Magentic – Use LLMs as simple Python functions

#62
post #57

Earlier quoted context omitted.

I found this was the most compact way to represent what I wanted to define, and makes it easy to keep the type hints for parameters. If you look inside `@prompt` it's creating a `PromptFunction` instance which I think would be a similar API to what you would end up with without using decorators https://github.com/jackmpcollins/magentic/blob/afdb22513385b...

I never got on board of decorators in python, but you sold me on it.

Another really good use is performance metrics -- decorators to track execution time, for instance, with the ability to specify things like function groups, logic concepts, etc. It makes it trivial to add this sort of observability to your code.

Re: Show HN: Magentic – Use LLMs as simple Python functions

#63
post #59

I am amazed that `...` is a valid syntax in Python, not a pseudo grammar. This library is impressive, I appreciate it and I will apply it to my project.

What's the difference between '...' and the more common 'pass'?

In code, using ... implies that the code is yet to be written. pass means it's explicitly a noop.

Re: Show HN: Magentic – Use LLMs as simple Python functions

#64
post #59

I am amazed that `...` is a valid syntax in Python, not a pseudo grammar. This library is impressive, I appreciate it and I will apply it to my project.

What's the difference between '...' and the more common 'pass'?

I find students correctly infer what to do with "..." whereas they were afraid to touch "pass".

E.g, if I gave them this:

    def foo(x):
      ...  #add your implementation here
    
    def bar(x):
      pass #add your implementation here
I'd get back this:

    def foo(x):
      return x+1
      
    def bar(x):
      return x+1
      pass

Re: Show HN: Magentic – Use LLMs as simple Python functions

#65
post #23

Write some tests for those functions. It will be worth it. No, I am not kidding, especially for AI we need tests, but we should report accuracy instead of a hard fail/pass.

No problem, boss.

    @prompt("Find out if {programs} are correct.")
    def do_they_work(programs: list) -> bool:
        ...

I just pushed it to production. Dashboard is all green. See you when I get back from vacation!

Re: Show HN: Magentic – Use LLMs as simple Python functions

#67

Earlier quoted context omitted.

It is just a noop, but here it looks very appropriate/readable because it reads as saying "AI will fill this in".

It's a misuse of the Python Ellipsis, though PEP has no opinion on it. The Ellipsis is "Special value used mostly in conjunction with extended slicing syntax for user-defined container data types." In other words, it happens to work and look neat, but pass is the correct way to do it.

Ellipses is actually used in quite a few places. See the answers and comments on this stackoverflow post[0]. The usage most similar to what I have in the magentic examples is with the `@overload` decorator in the typing module[1].

With that said, you are free to put any code in the function body including `pass` or just a docstring or even `raise NotImplementedError` - it will not be executed. Using Ellipses satisfies VSCode/pyright type checking and seemed neatest to me for the examples and docs. I have some additional notes on this in the README[2].

[0] https://stackoverflow.com/q/772124/9995080

[1] https://docs.python.org/3/library/typing.html#typing.overloa...

[2] https://github.com/jackmpcollins/magentic#type-checking

Re: Show HN: Magentic – Use LLMs as simple Python functions

#68

This looks really useful. Langchain is not my idea of a fun time. Love the examples too. Low-effort humor is the best: > create_superhero("Garden Man") > # Superhero(name='Garden Man', age=30, power='Control over plants', enemies=['Pollution Man', 'Concrete Woman'])

FWIW, at my last company we had a section in the developer guide encouraging using humor in tests - not only did it make them more fun to write, but it engaged the readership better.

I’ve been integrating humor into our unit tests for a bit now and have gotten feedback from a few engineers who really seem to appreciate it.

Re: Show HN: Magentic – Use LLMs as simple Python functions

#69
post #40

We need a new language/DSL. Python is a lost cause for strings as first-class.

How so? What disadvantages does having strings as a first class Type have?

I expressed myself too succinctly and without context, sorry.

I meant we need a new DSL better suited for prompt engg, and a UI that better supports longer strings. Actualy this UI can be something compatible with Python.

But overall a reimagination of the dev experience is what I am getting at (like Jupyter for LLMs).

Dm me [redacted] on X for more.

Re: Show HN: Magentic – Use LLMs as simple Python functions

#70

Does this do System vs. Assistant vs. User prompting?

Right now we just pass a single user prompt to the chat model. Setting the system prompt could also be done in the `@prompt` decorator. I've added a github issue to track https://github.com/jackmpcollins/magentic/issues/31

Update: I've added the ability to add chat messages using a new decorator `@chatprompt` in v0.7.0. See https://github.com/jackmpcollins/magentic/releases/tag/v0.7....
Post reply on HN