In [1]:
Copied!
from semlib import Bare, Session
session = Session()
from semlib import Bare, Session
session = Session()
prompt is a basic wrapper around prompting an LLM and optionally getting structured output:
In [2]:
Copied!
presidents: list[str] = await session.prompt(
"Who were the 39th through 42nd presidents of the United States? Return the name only.",
return_type=Bare(list[str]),
)
presidents
presidents: list[str] = await session.prompt(
"Who were the 39th through 42nd presidents of the United States? Return the name only.",
return_type=Bare(list[str]),
)
presidents
Out[2]:
['Jimmy Carter', 'Ronald Reagan', 'George H. W. Bush', 'Bill Clinton']
sort sorts a list of items based on a ranking criterion:
In [3]:
Copied!
await session.sort(presidents, by="right-leaning")
await session.sort(presidents, by="right-leaning")
Out[3]:
['Jimmy Carter', 'Bill Clinton', 'George H. W. Bush', 'Ronald Reagan']
filter filters a list of items based on a criterion:
In [4]:
Copied!
await session.filter(presidents, by="former actor", negate=True)
await session.filter(presidents, by="former actor", negate=True)
Out[4]:
['Jimmy Carter', 'George H. W. Bush', 'Bill Clinton']
map transforms a list of items based on a prompt template:
In [5]:
Copied!
ages: list[int] = await session.map(
presidents,
template="How old was {} when he took office?",
return_type=Bare(int),
)
ages
ages: list[int] = await session.map(
presidents,
template="How old was {} when he took office?",
return_type=Bare(int),
)
ages
Out[5]:
[52, 69, 64, 46]
total_cost returns the total cost of all LLM calls made in the session:
In [6]:
Copied!
f"${session.total_cost():.3f}"
f"${session.total_cost():.3f}"
Out[6]:
'$0.007'