Skip to content

Instantly share code, notes, and snippets.

@bbelderbos
Created February 11, 2025 17:19
Show Gist options
  • Save bbelderbos/d4cc17951e0d3d7e55909c4042a5cd59 to your computer and use it in GitHub Desktop.
Save bbelderbos/d4cc17951e0d3d7e55909c4042a5cd59 to your computer and use it in GitHub Desktop.
# /// script
# dependencies = [
# "marvin",
# ]
# ///
import os
import sys
import argparse
from pydantic import BaseModel, Field
import marvin
if "MARVIN_OPENAI_API_KEY" not in os.environ:
sys.exit("Set MARVIN_OPENAI_API_KEY in env first")
class Book(BaseModel):
title: str = Field(..., description="Title of the book")
author: str = Field(..., description="Author")
year: int | None = Field(None, description="Year of publication")
@marvin.fn
def recommend_similar_books(query_book: Book) -> list[Book]:
"""
Given a Book object, return a list of similar books.
The recommendations should consider similar genres.
"""
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Recommend similar books based on a given book."
)
parser.add_argument("--title", required=True, help="Title of the book")
parser.add_argument("--author", required=True, help="Book author")
args = parser.parse_args()
query_book = Book(
title=args.title,
author=args.author,
)
recommendations = recommend_similar_books(query_book)
for book in recommendations:
print(book.title, book.author, book.year)
"""
example run:
uv run book_recommend.py --title "the dark forest" --author 'Cixin Liu'
The Three-Body Problem Cixin Liu 2008
Death's End Cixin Liu 2010
Foundation Isaac Asimov 1951
Snow Crash Neal Stephenson 1992
Dune Frank Herbert 1965
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment