Before You Build on ContextGem#
Each point below is something that ContextGem already does, and that is easy to build again, or to get wrong, at first sight. Each links to the page that describes it.
References are the document’s own objects. A paragraph in
reference_paragraphsof an extracted item is the same object as indocument.paragraphs, and each paragraph and sentence has aunique_id. Put your own data, e.g. the ID of the record that a paragraph comes from, in the paragraph’scustom_data, and read it from the references. Do not match references by their text, which two paragraphs can share. See 📍 Locating Paragraphs and Sentences.A document holds copies of the aspects and concepts you give it. They are copied when they are assigned or added, so that you can reuse them for other documents: read the extracted items from
document.aspectsanddocument.concepts, e.g. withget_concept_by_name(), or from the list that the extraction method returns, not from the objects you created. SeeDocument.An answer that cannot be used is asked for again. An answer of an LLM or a decision model that cannot be parsed or validated, or that the model or its provider declined to give, and an empty answer in a chat, is requested again, up to
max_retries_invalid_datatimes (3 by default), and then the fallback models are tried. A refusal that none of them answers raisesModelRefusalError, which you can catch apart from the other failures. The exception is a decision model that refuses to answer on some of the paragraphs of an aspect while it answers on the others: these paragraphs go to its fallback models, and what none of them decides is left out of the aspect. See 🔁 How Failed Requests Are Retried and 🚫 Refusals.Errors are exceptions of their own.
ContextGemErroris the base of the errors of a run, e.g.ModelExtractionErrorandModelAPIError, while a mistake in your code, e.g. an invalid argument, is aValueErroror aTypeError. Withraise_exception_on_extraction_error=False, a failed extraction logs a warning instead. See Exceptions and Warnings.The costs of a model without prices. With
auto_pricing=True, a model thatgenai-priceshas no prices for gets anUnknownPricesWarning, and its costs stay at zero. A warning filter on that category makes it raise where it is created, e.g. for a budget. See 🛑 Stopping a Model Whose Prices Are Unknown.Tests without API calls. Replace
litellm.acompletionandlitellm.adecisions, which ContextGem looks up at each request, with a stand-in in your tests. See Testing Without API Calls.Which version the documentation describes. This documentation describes the latest release. The docstrings of the installed package describe the installed version, and the changelog gives the changes between versions.