https://www.boundaryml.com logo
🟡 Ensuring Unique ID Ranks in BAML Document Reran...
# questions
j
Hi BAML community! Newish BAML user here. I am trying to a BAML script to rerank documents according to their relevance to a given query. The BAML class looks like this (where id are unique IDs of the N input documents):
Copy code
class Ranking {
  rank int
  id int
}
class RankedDocuments {
  ranked_documents Ranking[]
}
How should I best go about enforcing that each of the N unique IDs is ranked exactly once? I could imagine trying to do this a) in BAML, or b) in (Python) code interacting with the BAML client to dynamically adjust prompt/output models, or c) in code interacting with the parsed BAML response to retry the LLM with an informative error message explaining why the produced ranking is invalid. What is the "BAML way" to do this? I think that I can hardly be the first person with a usecase like this, so pointers to similar examples would be great, too, as I could not find anything.
g
This feels like something Vaibhav would answer better than me, but, here's what I would do: I don't trust the model to line up id's very well, especially if the ids are large numbers, or if there are many of them. So I wouldn't use the ids at all, I would return a list, and let the list index be an implicit id for the scope of the function. But it's possible that the model will skip an input, or even get the positions of two inputs swapped. We can spend some output tokens to enssure that the rankings output is faithful to the doc input by using the doc in the output:
Copy code
class RankedDoc {
  doc_prefix string @description("First several words of the input doc")
  rank int
}

function Rank(inp: string[]) -> RankedDoc[] {
  client SomeClient
  prompt #"
    Produce a new ranking for each of the input docs in {{ inp }}.
    For each output ranking, include the first few words from the
    input document.

    The order of the output ranks should be the same as the order
    of the input docs. The input is a batch. Do not reorder the 
    documents by rank.

    {{ ctx.output_format }}
  "#
}
BTW welcome to the community!! 👋
i
I second this. This can be a huge use case if solved. I have also worked with similar problems where I ask models to return Ids and then I post-process the way I need, but never felt confident that it's a good solution. LLM can surely mess this up. If anyone has solved this, please let me know!
p
Hi @iakashvekariya , check out this blog post by Pedram Navid, head of DevRel at Dagster. He solved it using DSPy, but should be possible to write a similar function signature in BAML, too: https://pedramnavid.com/blog/dspy-part-one/
Ignore the click-baity title, the article is well written and it has some good insights
Key takeaways: - assigning an id and a score, and returning a list of (id, score) pairs seems to work well on his use case - chain of thought prompting may be necessary because the model needs to be able to reason over its own thought process to assign a more meaningful score, so that it can return a rank list - in BAML, it makes sense to add a "reasoning" field in the QueryResult model because it helps debug the model's reasoning as to how it came about the ranking list that it produced (DSPy doesn't need this because the reasoning field is implicitly included in the chain of thought module, so the Pydantic model doesn't need to define it explicitly)
It's quite simple to do chain of thought prompting in BAML, see their docs: https://docs.boundaryml.com/examples/prompt-engineering/chain-of-thought
i
Thanks a lot @prrao. Will go through this!
j
Thanks for this! I am thinking to handle around N=20-30 documents in the re-ranker, so not a crazy amount, and we used Python's instructor for similar use-cases in the past, including chain-of-thought prompting as well as a Pydantic validator to check that all document ids are indeed ranked, follow by an LLM-based retry if not. Don't know if you have anything to add, @Vaibhav (BAML 🐑) (just tagging you because I think @greg (BAML) mentioned you specifically, sorry if that is not the case).
v
only small nit i would recommend to make this better:
Copy code
function Rank(inp: string[]) -> RankedDoc[] {
  client SomeClient
  prompt #"
    Produce a new ranking for each of the input docs.
    For each output ranking, include the first few words from the
    input document.

    The order of the output ranks should be the same as the order
    of the input docs. The input is a batch. Do not reorder the 
    documents by rank.

    {{ ctx.output_format }}

    {{ _.role('user') }}
    {% for doc in inp %}
    {{ loop.index0 }}: {{ doc }}
    {% endfor %}
  "#
}
that way the LLM doesn't have to invent numbers
and it knows the index
Copy code
class RankedDoc {
  doc_index int
  doc_prefix string @description("First several words of the input doc")
  rank int
}
and then you get a double verification (
doc_index
and
doc_prefix
) before you do rank.
I would also 100% do CoT for this problem
or use a thinking model